Merge pull request #21420 from hvitved/rust/type-inference-qualified-trait-arg-path

Rust: More conservative resolution of `<Foo as Bar<...>>` paths
This commit is contained in:
Tom Hvitved
2026-03-06 15:14:01 +01:00
committed by GitHub
6 changed files with 117 additions and 33 deletions

View File

@@ -24,11 +24,14 @@ AssocType getTraitAssocType(Trait trait) { result.getTrait() = trait.getSupertra
/** Holds if `path` is of the form `<type as trait>::name` */
pragma[nomagic]
predicate pathTypeAsTraitAssoc(Path path, TypeRepr typeRepr, Path traitPath, string name) {
predicate pathTypeAsTraitAssoc(
Path path, TypeRepr typeRepr, PathTypeRepr traitRepr, Trait trait, string name
) {
exists(PathSegment segment |
segment = path.getQualifier().getSegment() and
typeRepr = segment.getTypeRepr() and
traitPath = segment.getTraitTypeRepr().getPath() and
traitRepr = segment.getTraitTypeRepr() and
trait = resolvePath(traitRepr.getPath()) and
name = path.getText()
)
}
@@ -43,10 +46,10 @@ predicate tpAssociatedType(TypeParam tp, AssocType assoc, Path path) {
resolvePath(path.getQualifier()) = tp and
resolvePath(path) = assoc
or
exists(PathTypeRepr typeRepr, Path traitPath, string name |
pathTypeAsTraitAssoc(path, typeRepr, traitPath, name) and
exists(PathTypeRepr typeRepr, TraitItemNode trait, string name |
pathTypeAsTraitAssoc(path, typeRepr, _, trait, name) and
tp = resolvePath(typeRepr.getPath()) and
assoc = resolvePath(traitPath).(TraitItemNode).getAssocItem(name)
assoc = trait.getAssocItem(name)
)
}

View File

@@ -701,7 +701,7 @@ class PreTypeMention = PreTypeMention::TypeMention;
* concrete type given by `tm`.
*/
private predicate pathConcreteTypeAssocType(
Path path, PreTypeMention tm, Trait trait, TypeAlias alias
Path path, PreTypeMention tm, TraitItemNode trait, PreTypeMention tmTrait, TypeAlias alias
) {
exists(Path qualifier |
qualifier = path.getQualifier() and
@@ -709,9 +709,8 @@ private predicate pathConcreteTypeAssocType(
|
// path of the form `<Type as Trait>::AssocType`
// ^^^ tm ^^^^^^^^^ name
exists(string name, Path traitPath |
pathTypeAsTraitAssoc(path, tm, traitPath, name) and
trait = resolvePath(traitPath) and
exists(string name |
pathTypeAsTraitAssoc(path, tm, tmTrait, trait, name) and
getTraitAssocType(trait, name) = alias
)
or
@@ -721,14 +720,15 @@ private predicate pathConcreteTypeAssocType(
alias = resolvePath(path) and
qualifier = impl.getASelfPath() and
tm = impl.(Impl).getSelfTy() and
trait.(TraitItemNode).getAnAssocItem() = alias
trait.getAnAssocItem() = alias and
tmTrait = impl.getTraitPath()
)
)
}
private module PathSatisfiesConstraintInput implements SatisfiesConstraintInputSig<PreTypeMention> {
predicate relevantConstraint(PreTypeMention tm, Type constraint) {
pathConcreteTypeAssocType(_, tm, constraint.(TraitType).getTrait(), _)
pathConcreteTypeAssocType(_, tm, constraint.(TraitType).getTrait(), _, _)
}
}
@@ -740,10 +740,22 @@ private module PathSatisfiesConstraint =
* on a concrete type.
*/
private Type getPathConcreteAssocTypeAt(Path path, TypePath typePath) {
exists(PreTypeMention tm, TraitItemNode t, TypeAlias alias, TypePath path0 |
pathConcreteTypeAssocType(path, tm, t, alias) and
PathSatisfiesConstraint::satisfiesConstraintType(tm, TTrait(t), path0, result) and
path0.isCons(TAssociatedTypeTypeParameter(t, alias), typePath)
exists(
PreTypeMention tm, ImplItemNode impl, TraitItemNode trait, TraitType t, PreTypeMention tmTrait,
TypeAlias alias, TypePath path0
|
pathConcreteTypeAssocType(path, tm, trait, tmTrait, alias) and
t = TTrait(trait) and
PathSatisfiesConstraint::satisfiesConstraintTypeThrough(tm, impl, t, path0, result) and
path0.isCons(TAssociatedTypeTypeParameter(trait, alias), typePath)
|
tmTrait.getTypeAt(TypePath::nil()) != t
or
not exists(TypePath path1, Type t1 |
t1 = impl.getTraitPath().(PreTypeMention).getTypeAt(path1) and
not t1 instanceof TypeParameter and
t1 != tmTrait.getTypeAt(path1)
)
)
}