Rust: Rename ContextType -> UnknownType

This commit is contained in:
Tom Hvitved
2025-11-14 10:16:21 +01:00
parent 79bdf897d7
commit 4c7f9c0144
5 changed files with 37 additions and 29 deletions

View File

@@ -51,7 +51,7 @@ newtype TType =
TSliceType() or
TNeverType() or
TPtrType() or
TContextType() or
TUnknownType() or
TTupleTypeParameter(int arity, int i) { exists(TTuple(arity)) and i in [0 .. arity - 1] } or
TTypeParamTypeParameter(TypeParam t) or
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getAnAssocItem() = t } or
@@ -374,21 +374,27 @@ class PtrType extends Type, TPtrType {
/**
* A special pseudo type used to indicate that the actual type may have to be
* inferred from a context.
* inferred by propagating type information back into call arguments.
*
* For example, a call like `Default::default()` is assigned this type, which
* means that the actual type is to be inferred from the context in which the call
* occurs.
* For example, in
*
* Context types are not restricted to root types, for example in a call like
* ```rust
* let x = Default::default();
* foo(x);
* ```
*
* `Default::default()` is assigned this type, which allows us to infer the actual
* type from the type of `foo`'s first parameter.
*
* Unknown types are not restricted to root types, for example in a call like
* `Vec::new()` we assign this type at the type path corresponding to the type
* parameter of `Vec`.
*
* Context types are used to restrict when type information is allowed to flow
* Unknown types are used to restrict when type information is allowed to flow
* into call arguments (including method call receivers), in order to avoid
* combinatorial explosions.
*/
class ContextType extends Type, TContextType {
class UnknownType extends Type, TUnknownType {
override TypeParameter getPositionalTypeParameter(int i) { none() }
override string toString() { result = "(context typed)" }

View File

@@ -859,7 +859,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
pragma[nomagic]
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
// Handle constructions that use `Self{...}` syntax
// Handle constructions that use `Self {...}` syntax
exists(TypeMention tm, TypePath path0 |
tm = this.getStructPath() and
result = tm.resolveTypeAt(path0) and
@@ -872,7 +872,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
* be inferred from the context.
*/
pragma[nomagic]
predicate isContextTypedAt(DeclarationPosition pos, TypePath path) {
predicate hasUnknownTypeAt(DeclarationPosition pos, TypePath path) {
exists(Declaration d, TypeParameter tp |
d = this.getTarget() and
pos.isStructPos() and
@@ -939,8 +939,8 @@ private Type inferStructExprType0(AstNode n, boolean isReturn, TypePath path) {
|
result = StructExprMatching::inferAccessType(a, apos, path)
or
a.isContextTypedAt(apos, path) and
result = TContextType()
a.hasUnknownTypeAt(apos, path) and
result = TUnknownType()
)
}
@@ -1022,7 +1022,7 @@ private module ContextTyping {
* at `pos` and `path` may have to be inferred from the context.
*/
bindingset[this, i, target]
predicate isContextTypedAt(
predicate hasUnknownTypeAt(
ImplOrTraitItemNode i, Function target, FunctionPosition pos, TypePath path
) {
exists(TypeParameter tp |
@@ -1045,10 +1045,12 @@ private module ContextTyping {
}
pragma[nomagic]
private predicate isContextTyped(AstNode n, TypePath path) { inferType(n, path) = TContextType() }
private predicate hasUnknownTypeAt(AstNode n, TypePath path) {
inferType(n, path) = TUnknownType()
}
pragma[nomagic]
private predicate isContextTyped(AstNode n) { isContextTyped(n, _) }
private predicate hasUnknownType(AstNode n) { hasUnknownTypeAt(n, _) }
signature Type inferCallTypeSig(AstNode n, boolean isReturn, TypePath path);
@@ -1062,7 +1064,7 @@ private module ContextTyping {
pragma[nomagic]
private Type inferCallTypeFromContextCand(AstNode n, TypePath path, TypePath prefix) {
result = inferCallType(n, false, path) and
isContextTyped(n) and
hasUnknownType(n) and
prefix = path
or
exists(TypePath mid |
@@ -1077,7 +1079,7 @@ private module ContextTyping {
or
exists(TypePath prefix |
result = inferCallTypeFromContextCand(n, path, prefix) and
isContextTyped(n, prefix)
hasUnknownTypeAt(n, prefix)
)
}
}
@@ -1744,7 +1746,7 @@ private module MethodResolution {
Type getTypeAt(TypePath path) {
result = mc_.getACandidateReceiverTypeAtSubstituteLookupTraits(derefChain, borrow, path) and
not result = TNeverType() and
not result = TContextType()
not result = TUnknownType()
}
pragma[nomagic]
@@ -2160,9 +2162,9 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
* from the context.
*/
pragma[nomagic]
predicate isContextTypedAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
exists(ImplOrTraitItemNode i |
this.isContextTypedAt(i, this.getTarget(i, derefChainBorrow), pos, path)
this.hasUnknownTypeAt(i, this.getTarget(i, derefChainBorrow), pos, path)
)
}
}
@@ -2180,8 +2182,8 @@ private Type inferMethodCallType0(
(
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
or
a.isContextTypedAt(derefChainBorrow, apos, path0) and
result = TContextType()
a.hasUnknownTypeAt(derefChainBorrow, apos, path0) and
result = TUnknownType()
)
|
if
@@ -2669,9 +2671,9 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
* from the context.
*/
pragma[nomagic]
predicate isContextTypedAt(FunctionPosition pos, TypePath path) {
predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
exists(ImplOrTraitItemNode i |
this.isContextTypedAt(i,
this.hasUnknownTypeAt(i,
[
this.resolveCallTargetViaPathResolution().(NonMethodFunction),
this.resolveCallTargetViaTypeInference(i),
@@ -2705,8 +2707,8 @@ private Type inferNonMethodCallType0(AstNode n, boolean isReturn, TypePath path)
|
result = NonMethodCallMatching::inferAccessType(a, apos, path)
or
a.isContextTypedAt(apos, path) and
result = TContextType()
a.hasUnknownTypeAt(apos, path) and
result = TUnknownType()
)
}

View File

@@ -93,7 +93,7 @@ module SatisfiesBlanketConstraint<
Type getTypeAt(TypePath path) {
result = at.getTypeAt(blanketPath.appendInverse(path)) and
not result = TNeverType() and
not result = TContextType()
not result = TUnknownType()
}
string toString() { result = at.toString() + " [blanket at " + blanketPath.toString() + "]" }

View File

@@ -230,7 +230,7 @@ module ArgIsInstantiationOf<
Type getTypeAt(TypePath path) {
result = substituteLookupTraits(super.getTypeAt(path)) and
not result = TNeverType() and
not result = TContextType()
not result = TUnknownType()
}
}

View File

@@ -6,7 +6,7 @@ import TypeInference
query predicate inferType(AstNode n, TypePath path, Type t) {
t = TypeInference::inferType(n, path) and
t != TContextType() and
t != TUnknownType() and
n.fromSource() and
not n.isFromMacroExpansion() and
not n instanceof IdentPat and // avoid overlap in the output with the underlying `Name` node