Skip to content

Commit d83ebf0

Browse files
committed
Rust: More conservative resolution of <Foo as Bar<...>> paths
1 parent 838f3b9 commit d83ebf0

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

rust/ql/lib/codeql/rust/internal/typeinference/AssociatedType.qll

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ AssocType getTraitAssocType(Trait trait) { result.getTrait() = trait.getSupertra
2424

2525
/** Holds if `path` is of the form `<type as trait>::name` */
2626
pragma[nomagic]
27-
predicate pathTypeAsTraitAssoc(Path path, TypeRepr typeRepr, Path traitPath, string name) {
27+
predicate pathTypeAsTraitAssoc(
28+
Path path, TypeRepr typeRepr, PathTypeRepr traitRepr, Trait trait, string name
29+
) {
2830
exists(PathSegment segment |
2931
segment = path.getQualifier().getSegment() and
3032
typeRepr = segment.getTypeRepr() and
31-
traitPath = segment.getTraitTypeRepr().getPath() and
33+
traitRepr = segment.getTraitTypeRepr() and
34+
trait = resolvePath(traitRepr.getPath()) and
3235
name = path.getText()
3336
)
3437
}
@@ -43,10 +46,10 @@ predicate tpAssociatedType(TypeParam tp, AssocType assoc, Path path) {
4346
resolvePath(path.getQualifier()) = tp and
4447
resolvePath(path) = assoc
4548
or
46-
exists(PathTypeRepr typeRepr, Path traitPath, string name |
47-
pathTypeAsTraitAssoc(path, typeRepr, traitPath, name) and
49+
exists(PathTypeRepr typeRepr, TraitItemNode trait, string name |
50+
pathTypeAsTraitAssoc(path, typeRepr, _, trait, name) and
4851
tp = resolvePath(typeRepr.getPath()) and
49-
assoc = resolvePath(traitPath).(TraitItemNode).getAssocItem(name)
52+
assoc = trait.getAssocItem(name)
5053
)
5154
}
5255

rust/ql/lib/codeql/rust/internal/typeinference/TypeMention.qll

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -701,17 +701,16 @@ class PreTypeMention = PreTypeMention::TypeMention;
701701
* concrete type given by `tm`.
702702
*/
703703
private predicate pathConcreteTypeAssocType(
704-
Path path, PreTypeMention tm, Trait trait, TypeAlias alias
704+
Path path, PreTypeMention tm, Trait trait, PreTypeMention tmTrait, TypeAlias alias
705705
) {
706706
exists(Path qualifier |
707707
qualifier = path.getQualifier() and
708708
not resolvePath(tm.(PathTypeRepr).getPath()) instanceof TypeParam
709709
|
710710
// path of the form `<Type as Trait>::AssocType`
711711
// ^^^ tm ^^^^^^^^^ name
712-
exists(string name, Path traitPath |
713-
pathTypeAsTraitAssoc(path, tm, traitPath, name) and
714-
trait = resolvePath(traitPath) and
712+
exists(string name |
713+
pathTypeAsTraitAssoc(path, tm, tmTrait, trait, name) and
715714
getTraitAssocType(trait, name) = alias
716715
)
717716
or
@@ -721,14 +720,15 @@ private predicate pathConcreteTypeAssocType(
721720
alias = resolvePath(path) and
722721
qualifier = impl.getASelfPath() and
723722
tm = impl.(Impl).getSelfTy() and
724-
trait.(TraitItemNode).getAnAssocItem() = alias
723+
trait.(TraitItemNode).getAnAssocItem() = alias and
724+
tmTrait = impl.(ImplItemNode).getTraitPath()
725725
)
726726
)
727727
}
728728

729729
private module PathSatisfiesConstraintInput implements SatisfiesConstraintInputSig<PreTypeMention> {
730730
predicate relevantConstraint(PreTypeMention tm, Type constraint) {
731-
pathConcreteTypeAssocType(_, tm, constraint.(TraitType).getTrait(), _)
731+
pathConcreteTypeAssocType(_, tm, constraint.(TraitType).getTrait(), _, _)
732732
}
733733
}
734734

@@ -740,10 +740,21 @@ private module PathSatisfiesConstraint =
740740
* on a concrete type.
741741
*/
742742
private Type getPathConcreteAssocTypeAt(Path path, TypePath typePath) {
743-
exists(PreTypeMention tm, TraitItemNode t, TypeAlias alias, TypePath path0 |
744-
pathConcreteTypeAssocType(path, tm, t, alias) and
745-
PathSatisfiesConstraint::satisfiesConstraintType(tm, TTrait(t), path0, result) and
743+
exists(
744+
PreTypeMention tm, ImplItemNode impl, TraitItemNode t, PreTypeMention trait, TypeAlias alias,
745+
TypePath path0
746+
|
747+
pathConcreteTypeAssocType(path, tm, t, trait, alias) and
748+
PathSatisfiesConstraint::satisfiesConstraintTypeThrough(tm, impl, TTrait(t), path0, result) and
746749
path0.isCons(TAssociatedTypeTypeParameter(t, alias), typePath)
750+
|
751+
trait.getTypeAt(TypePath::nil()) != TTrait(t)
752+
or
753+
not exists(TypePath path1, Type t1 |
754+
t1 = impl.getTraitPath().(PreTypeMention).getTypeAt(path1) and
755+
not t1 instanceof TypeParameter and
756+
t1 != trait.getTypeAt(path1)
757+
)
747758
)
748759
}
749760

rust/ql/test/library-tests/type-inference/associated_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ mod concrete_type_as_generic_access_associated_type {
236236

237237
pub fn test() {
238238
let s = S;
239-
let _a = s.convert(true); // $ target=S::convert type=_a:i32 SPURIOUS: bool
240-
let _b = s.convert(42); // $ target=S::convert type=_b:bool SPURIOUS: i32
239+
let _a = s.convert(true); // $ target=S::convert $ MISSING: type=_a:i32
240+
let _b = s.convert(42); // $ target=S::convert $ MISSING: type=_b:bool
241241
}
242242
}
243243

rust/ql/test/library-tests/type-inference/regressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ mod regression2 {
7171
fn foo() {
7272
let s1 = S1;
7373
let s2 = S2;
74-
let x = s1 - &s2; // $ target=S1SubRefS2 type=x:S2 $ SPURIOUS: type=x:S1
74+
let x = s1 - &s2; // $ target=S1SubRefS2 type=x:S2
7575
}
7676
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ inferCertainType
105105
| associated_types.rs:229:23:229:27 | SelfParam | | {EXTERNAL LOCATION} | & |
106106
| associated_types.rs:229:23:229:27 | SelfParam | TRef | associated_types.rs:10:1:11:9 | S |
107107
| associated_types.rs:229:30:229:30 | t | | associated_types.rs:229:20:229:20 | T |
108-
| associated_types.rs:232:9:234:9 | { ... } | | {EXTERNAL LOCATION} | bool |
109-
| associated_types.rs:232:9:234:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
110108
| associated_types.rs:233:24:233:24 | t | | associated_types.rs:229:20:229:20 | T |
111109
| associated_types.rs:237:19:241:5 | { ... } | | {EXTERNAL LOCATION} | () |
112110
| associated_types.rs:239:28:239:31 | true | | {EXTERNAL LOCATION} | bool |
@@ -4956,7 +4954,6 @@ inferCertainType
49564954
| regressions.rs:66:16:66:19 | SelfParam | | regressions.rs:39:5:40:14 | S1 |
49574955
| regressions.rs:66:22:66:26 | other | | {EXTERNAL LOCATION} | & |
49584956
| regressions.rs:66:22:66:26 | other | TRef | regressions.rs:41:5:42:14 | S2 |
4959-
| regressions.rs:66:61:68:9 | { ... } | | regressions.rs:39:5:40:14 | S1 |
49604957
| regressions.rs:66:61:68:9 | { ... } | | regressions.rs:41:5:42:14 | S2 |
49614958
| regressions.rs:67:22:67:25 | self | | regressions.rs:39:5:40:14 | S1 |
49624959
| regressions.rs:67:29:67:33 | other | | {EXTERNAL LOCATION} | & |
@@ -5130,25 +5127,13 @@ inferType
51305127
| associated_types.rs:229:23:229:27 | SelfParam | | {EXTERNAL LOCATION} | & |
51315128
| associated_types.rs:229:23:229:27 | SelfParam | TRef | associated_types.rs:10:1:11:9 | S |
51325129
| associated_types.rs:229:30:229:30 | t | | associated_types.rs:229:20:229:20 | T |
5133-
| associated_types.rs:232:9:234:9 | { ... } | | {EXTERNAL LOCATION} | bool |
5134-
| associated_types.rs:232:9:234:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
5135-
| associated_types.rs:233:13:233:25 | ...::through(...) | | {EXTERNAL LOCATION} | bool |
5136-
| associated_types.rs:233:13:233:25 | ...::through(...) | | {EXTERNAL LOCATION} | i32 |
51375130
| associated_types.rs:233:24:233:24 | t | | associated_types.rs:229:20:229:20 | T |
51385131
| associated_types.rs:237:19:241:5 | { ... } | | {EXTERNAL LOCATION} | () |
51395132
| associated_types.rs:238:13:238:13 | s | | associated_types.rs:10:1:11:9 | S |
51405133
| associated_types.rs:238:17:238:17 | S | | associated_types.rs:10:1:11:9 | S |
5141-
| associated_types.rs:239:13:239:14 | _a | | {EXTERNAL LOCATION} | bool |
5142-
| associated_types.rs:239:13:239:14 | _a | | {EXTERNAL LOCATION} | i32 |
51435134
| associated_types.rs:239:18:239:18 | s | | associated_types.rs:10:1:11:9 | S |
5144-
| associated_types.rs:239:18:239:32 | s.convert(...) | | {EXTERNAL LOCATION} | bool |
5145-
| associated_types.rs:239:18:239:32 | s.convert(...) | | {EXTERNAL LOCATION} | i32 |
51465135
| associated_types.rs:239:28:239:31 | true | | {EXTERNAL LOCATION} | bool |
5147-
| associated_types.rs:240:13:240:14 | _b | | {EXTERNAL LOCATION} | bool |
5148-
| associated_types.rs:240:13:240:14 | _b | | {EXTERNAL LOCATION} | i32 |
51495136
| associated_types.rs:240:18:240:18 | s | | associated_types.rs:10:1:11:9 | S |
5150-
| associated_types.rs:240:18:240:30 | s.convert(...) | | {EXTERNAL LOCATION} | bool |
5151-
| associated_types.rs:240:18:240:30 | s.convert(...) | | {EXTERNAL LOCATION} | i32 |
51525137
| associated_types.rs:240:28:240:29 | 42 | | {EXTERNAL LOCATION} | i32 |
51535138
| associated_types.rs:248:30:248:34 | thing | | associated_types.rs:248:19:248:27 | T |
51545139
| associated_types.rs:248:65:250:5 | { ... } | | associated_types.rs:248:19:248:27 | T::Output[GetSet] |
@@ -14816,9 +14801,7 @@ inferType
1481614801
| regressions.rs:66:16:66:19 | SelfParam | | regressions.rs:39:5:40:14 | S1 |
1481714802
| regressions.rs:66:22:66:26 | other | | {EXTERNAL LOCATION} | & |
1481814803
| regressions.rs:66:22:66:26 | other | TRef | regressions.rs:41:5:42:14 | S2 |
14819-
| regressions.rs:66:61:68:9 | { ... } | | regressions.rs:39:5:40:14 | S1 |
1482014804
| regressions.rs:66:61:68:9 | { ... } | | regressions.rs:41:5:42:14 | S2 |
14821-
| regressions.rs:67:13:67:34 | ...::sub(...) | | regressions.rs:39:5:40:14 | S1 |
1482214805
| regressions.rs:67:13:67:34 | ...::sub(...) | | regressions.rs:41:5:42:14 | S2 |
1482314806
| regressions.rs:67:22:67:25 | self | | regressions.rs:39:5:40:14 | S1 |
1482414807
| regressions.rs:67:28:67:33 | * ... | | regressions.rs:41:5:42:14 | S2 |
@@ -14829,10 +14812,8 @@ inferType
1482914812
| regressions.rs:72:18:72:19 | S1 | | regressions.rs:39:5:40:14 | S1 |
1483014813
| regressions.rs:73:13:73:14 | s2 | | regressions.rs:41:5:42:14 | S2 |
1483114814
| regressions.rs:73:18:73:19 | S2 | | regressions.rs:41:5:42:14 | S2 |
14832-
| regressions.rs:74:13:74:13 | x | | regressions.rs:39:5:40:14 | S1 |
1483314815
| regressions.rs:74:13:74:13 | x | | regressions.rs:41:5:42:14 | S2 |
1483414816
| regressions.rs:74:17:74:18 | s1 | | regressions.rs:39:5:40:14 | S1 |
14835-
| regressions.rs:74:17:74:24 | ... - ... | | regressions.rs:39:5:40:14 | S1 |
1483614817
| regressions.rs:74:17:74:24 | ... - ... | | regressions.rs:41:5:42:14 | S2 |
1483714818
| regressions.rs:74:22:74:24 | &s2 | | {EXTERNAL LOCATION} | & |
1483814819
| regressions.rs:74:22:74:24 | &s2 | TRef | regressions.rs:41:5:42:14 | S2 |

0 commit comments

Comments
 (0)