Skip to content

Commit 27f7f74

Browse files
committed
Rust: Check whole blanket constraints, not just the root trait type
1 parent be329c8 commit 27f7f74

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,18 +1202,21 @@ final class TypeParamItemNode extends NamedItemNode, TypeItemNode instanceof Typ
12021202
}
12031203

12041204
pragma[nomagic]
1205-
Path getABoundPath() { result = this.getTypeBoundAt(_, _).getTypeRepr().(PathTypeRepr).getPath() }
1206-
1207-
pragma[nomagic]
1208-
ItemNode resolveBound(int index) {
1205+
Path getBoundPath(int index) {
12091206
result =
12101207
rank[index + 1](int i, int j |
12111208
|
1212-
resolvePath(this.getTypeBoundAt(i, j).getTypeRepr().(PathTypeRepr).getPath()) order by i, j
1209+
this.getTypeBoundAt(i, j).getTypeRepr().(PathTypeRepr).getPath() order by i, j
12131210
)
12141211
}
12151212

1216-
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
1213+
pragma[nomagic]
1214+
Path getABoundPath() { result = this.getBoundPath(_) }
1215+
1216+
pragma[nomagic]
1217+
ItemNode resolveBound(int index) { result = resolvePath(this.getBoundPath(index)) }
1218+
1219+
ItemNode resolveABound() { result = this.resolveBound(_) }
12171220

12181221
pragma[nomagic]
12191222
ItemNode resolveAdditionalBound(ItemNode constrainingItem) {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ private import TypeInference
1515
* Holds if `traitBound` is the first non-trivial trait bound of `tp`.
1616
*/
1717
pragma[nomagic]
18-
private predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Trait traitBound) {
18+
private predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Path traitBound) {
1919
traitBound =
20-
min(Trait trait, int i |
21-
trait = tp.resolveBound(i) and
20+
min(Trait trait, Path path, int i |
21+
path = tp.getBoundPath(i) and
22+
trait = resolvePath(path) and
2223
// Exclude traits that are known to not narrow things down very much.
2324
not trait.getName().getText() =
2425
[
@@ -27,7 +28,7 @@ private predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Trait trait
2728
"Send", "Sync", "Unpin", "UnwindSafe", "RefUnwindSafe"
2829
]
2930
|
30-
trait order by i
31+
path order by i
3132
)
3233
}
3334

@@ -103,11 +104,11 @@ module SatisfiesBlanketConstraint<
103104
}
104105

105106
private module SatisfiesBlanketConstraintInput implements
106-
SatisfiesTypeInputSig<ArgumentTypeAndBlanketOffset>
107+
SatisfiesConstraintInputSig<ArgumentTypeAndBlanketOffset, TypeMention>
107108
{
108109
pragma[nomagic]
109110
additional predicate relevantConstraint(
110-
ArgumentTypeAndBlanketOffset ato, ImplItemNode impl, Trait traitBound
111+
ArgumentTypeAndBlanketOffset ato, ImplItemNode impl, Path traitBound
111112
) {
112113
exists(ArgumentType at, TypePath blanketPath, TypeParam blanketTypeParam |
113114
ato = MkArgumentTypeAndBlanketOffset(at, blanketPath) and
@@ -117,24 +118,24 @@ module SatisfiesBlanketConstraint<
117118
}
118119

119120
pragma[nomagic]
120-
predicate relevantConstraint(ArgumentTypeAndBlanketOffset ato, Type constraint) {
121-
relevantConstraint(ato, _, constraint.(TraitType).getTrait())
121+
predicate relevantConstraint(ArgumentTypeAndBlanketOffset ato, TypeMention constraint) {
122+
relevantConstraint(ato, _, constraint)
122123
}
123124
}
124125

125126
private module SatisfiesBlanketConstraint =
126-
SatisfiesType<ArgumentTypeAndBlanketOffset, SatisfiesBlanketConstraintInput>;
127+
SatisfiesConstraint<ArgumentTypeAndBlanketOffset, TypeMention, SatisfiesBlanketConstraintInput>;
127128

128129
/**
129130
* Holds if the argument type `at` satisfies the first non-trivial blanket
130131
* constraint of `impl`, or if there are no non-trivial constraints of `impl`.
131132
*/
132133
pragma[nomagic]
133134
predicate satisfiesBlanketConstraint(ArgumentType at, ImplItemNode impl) {
134-
exists(ArgumentTypeAndBlanketOffset ato, Trait traitBound |
135+
exists(ArgumentTypeAndBlanketOffset ato, Path traitBound |
135136
ato = MkArgumentTypeAndBlanketOffset(at, _) and
136137
SatisfiesBlanketConstraintInput::relevantConstraint(ato, impl, traitBound) and
137-
SatisfiesBlanketConstraint::satisfiesConstraint(ato, TTrait(traitBound), _, _)
138+
SatisfiesBlanketConstraint::satisfiesConstraint(ato, traitBound, _, _)
138139
)
139140
or
140141
exists(TypeParam blanketTypeParam |
@@ -149,10 +150,10 @@ module SatisfiesBlanketConstraint<
149150
*/
150151
pragma[nomagic]
151152
predicate dissatisfiesBlanketConstraint(ArgumentType at, ImplItemNode impl) {
152-
exists(ArgumentTypeAndBlanketOffset ato, Trait traitBound |
153+
exists(ArgumentTypeAndBlanketOffset ato, Path traitBound |
153154
ato = MkArgumentTypeAndBlanketOffset(at, _) and
154155
SatisfiesBlanketConstraintInput::relevantConstraint(ato, impl, traitBound) and
155-
SatisfiesBlanketConstraint::dissatisfiesConstraint(ato, TTrait(traitBound))
156+
SatisfiesBlanketConstraint::dissatisfiesConstraint(ato, traitBound)
156157
)
157158
}
158159
}

0 commit comments

Comments
 (0)