Skip to content

Commit d274c4a

Browse files
committed
dbg3
1 parent 312944c commit d274c4a

File tree

4 files changed

+93
-89
lines changed

4 files changed

+93
-89
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ abstract class ItemNode extends Locatable {
165165
exists(ItemNode node |
166166
this = node.(ImplItemNode).resolveSelfTy() and
167167
result = node.getASuccessorRec(name) and
168-
result instanceof AssocItemNode
168+
result instanceof AssocItemNode and
169+
not result instanceof TypeAlias
169170
)
170171
or
171172
// trait items with default implementations made available in an implementation

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

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,46 @@ private import TypeInference
77

88
/** An AST node that may mention a type. */
99
abstract class TypeMention extends AstNode {
10-
/** Gets the `i`th type argument mention, if any. */
11-
abstract TypeMention getTypeArgument(int i);
10+
/** Gets the type at `path` that this mention resolves to, if any. */
11+
abstract Type resolveTypeAt(TypePath path);
1212

13-
/** Gets the type that this node resolves to, if any. */
14-
abstract Type resolveType();
13+
final Type resolveType() { result = this.resolveTypeAt(TypePath::nil()) }
14+
}
1515

16-
// /** Gets the sub mention at `path`. */
17-
// pragma[nomagic]
18-
// private TypeMention getMentionAt(TypePath path) {
19-
// path.isEmpty() and
20-
// result = this
21-
// or
22-
// exists(int i, TypeParameter tp, TypeMention arg, TypePath suffix |
23-
// arg = this.getTypeArgument(pragma[only_bind_into](i)) and
24-
// result = arg.getMentionAt(suffix) and
25-
// path = TypePath::cons(tp, suffix) and
26-
// tp = this.resolveType().getTypeParameter(pragma[only_bind_into](i))
27-
// )
28-
// }
29-
/** Gets the type that the sub mention at `path` resolves to, if any. */
30-
Type resolveTypeAt(TypePath path) {
16+
class ArrayTypeReprMention extends TypeMention instanceof ArrayTypeRepr {
17+
override Type resolveTypeAt(TypePath path) {
3118
path.isEmpty() and
32-
result = this.resolveType()
19+
result = TArrayType()
3320
or
34-
exists(int i, TypeParameter tp, TypeMention arg, TypePath suffix |
35-
arg = this.getTypeArgument(pragma[only_bind_into](i)) and
36-
result = arg.resolveTypeAt(suffix) and
37-
path = TypePath::cons(tp, suffix) and
38-
tp = this.resolveType().getTypeParameter(pragma[only_bind_into](i))
21+
exists(TypePath suffix |
22+
result = super.getElementTypeRepr().(TypeMention).resolveTypeAt(suffix) and
23+
path = TypePath::cons(TArrayTypeParameter(), suffix)
3924
)
4025
}
41-
// result = this.getMentionAt(path).resolveType() }
42-
}
43-
44-
class ArrayTypeReprMention extends TypeMention instanceof ArrayTypeRepr {
45-
override TypeMention getTypeArgument(int i) { result = super.getElementTypeRepr() and i = 0 }
46-
47-
override Type resolveType() { result = TArrayType() }
4826
}
4927

5028
class RefTypeReprMention extends TypeMention instanceof RefTypeRepr {
51-
override TypeMention getTypeArgument(int i) { result = super.getTypeRepr() and i = 0 }
52-
53-
override Type resolveType() { result = TRefType() }
29+
override Type resolveTypeAt(TypePath path) {
30+
path.isEmpty() and
31+
result = TRefType()
32+
or
33+
exists(TypePath suffix |
34+
result = super.getTypeRepr().(TypeMention).resolveTypeAt(suffix) and
35+
path = TypePath::cons(TRefTypeParameter(), suffix)
36+
)
37+
}
5438
}
5539

5640
class SliceTypeReprMention extends TypeMention instanceof SliceTypeRepr {
57-
override TypeMention getTypeArgument(int i) { result = super.getTypeRepr() and i = 0 }
58-
59-
override Type resolveType() { result = TSliceType() }
41+
override Type resolveTypeAt(TypePath path) {
42+
path.isEmpty() and
43+
result = TSliceType()
44+
or
45+
exists(TypePath suffix |
46+
result = super.getTypeRepr().(TypeMention).resolveTypeAt(suffix) and
47+
path = TypePath::cons(TSliceTypeParameter(), suffix)
48+
)
49+
}
6050
}
6151

6252
class PathTypeMention extends TypeMention, Path {
@@ -89,15 +79,9 @@ class PathTypeMention extends TypeMention, Path {
8979
)
9080
}
9181

92-
override TypeMention getTypeArgument(int i) {
82+
private TypeMention getTypeArgument0(int i) {
9383
result = this.getSegment().getGenericArgList().getTypeArg(i)
9484
or
95-
// If a type argument is not given in the path, then we use the default for
96-
// the type parameter if one exists for the type.
97-
not exists(this.getSegment().getGenericArgList().getTypeArg(i)) and
98-
result = this.resolveType().getTypeParameterDefault(i) and
99-
this = any(PathTypeRepr ptp).getPath().getQualifier*()
100-
or
10185
// `Self` paths inside `impl` blocks have implicit type arguments that are
10286
// the type parameters of the `impl` block. For example, in
10387
//
@@ -114,6 +98,16 @@ class PathTypeMention extends TypeMention, Path {
11498
this = node.getASelfPath() and
11599
result = node.(ImplItemNode).getSelfPath().getSegment().getGenericArgList().getTypeArg(i)
116100
)
101+
}
102+
103+
private TypeMention getTypeArgument(int i) {
104+
result = this.getTypeArgument0(i)
105+
or
106+
// If a type argument is not given in the path, then we use the default for
107+
// the type parameter if one exists for the type.
108+
not exists(this.getTypeArgument0(i)) and
109+
result = this.resolveType().getTypeParameterDefault(i) and
110+
this = any(PathTypeRepr ptp).getPath().getQualifier*()
117111
or
118112
// If `path` is the trait of an `impl` block then any associated types
119113
// defined in the `impl` block are type arguments to the trait.
@@ -154,18 +148,18 @@ class PathTypeMention extends TypeMention, Path {
154148
or
155149
exists(TypeParameter tp, TypeMention arg, TypePath prefix, TypePath suffix, int i |
156150
tp = rhs.resolveTypeAt(prefix) and
157-
tp = pathGetTypeParameter(alias, i) and
158-
arg = this.getSegment().getGenericArgList().getTypeArg(i) and
151+
tp = pathGetTypeParameter(alias, pragma[only_bind_into](i)) and
152+
arg = this.getSegment().getGenericArgList().getTypeArg(pragma[only_bind_into](i)) and
159153
result = arg.resolveTypeAt(suffix) and
160154
typePath = prefix.append(suffix)
161155
)
162156
)
163157
}
164158

165-
override Type resolveType() {
166-
result = this.aliasResolveTypeAt(TypePath::nil())
159+
override Type resolveTypeAt(TypePath typePath) {
160+
result = this.aliasResolveTypeAt(typePath)
167161
or
168-
not exists(resolved.(TypeAlias).getTypeRepr()) and
162+
typePath.isEmpty() and
169163
(
170164
result = TStruct(resolved)
171165
or
@@ -181,19 +175,19 @@ class PathTypeMention extends TypeMention, Path {
181175
or
182176
result = TTypeParamTypeParameter(resolved)
183177
or
184-
exists(TypeAlias alias | alias = resolved |
178+
exists(TypeAlias alias |
179+
alias = resolved and
185180
result.(AssociatedTypeTypeParameter).getTypeAlias() = alias
186-
or
187-
result = alias.getTypeRepr().(TypeMention).resolveType()
188181
)
189182
)
190-
}
191-
192-
override Type resolveTypeAt(TypePath typePath) {
193-
result = this.aliasResolveTypeAt(typePath)
194183
or
195184
not exists(resolved.(TypeAlias).getTypeRepr()) and
196-
result = super.resolveTypeAt(typePath)
185+
exists(int i, TypeParameter tp, TypeMention arg, TypePath suffix |
186+
arg = this.getTypeArgument(pragma[only_bind_into](i)) and
187+
result = arg.resolveTypeAt(suffix) and
188+
typePath = TypePath::cons(tp, suffix) and
189+
tp = this.resolveType().getTypeParameter(pragma[only_bind_into](i))
190+
)
197191
}
198192
}
199193

@@ -202,17 +196,14 @@ class PathTypeReprMention extends TypeMention, PathTypeRepr {
202196

203197
PathTypeReprMention() { path = this.getPath() }
204198

205-
override TypeMention getTypeArgument(int i) { result = path.getTypeArgument(i) }
206-
207-
override Type resolveType() { result = path.resolveType() }
208-
209199
override Type resolveTypeAt(TypePath typePath) { result = path.resolveTypeAt(typePath) }
210200
}
211201

212202
class ImplTraitTypeReprMention extends TypeMention instanceof ImplTraitTypeRepr {
213-
override TypeMention getTypeArgument(int i) { none() }
214-
215-
override ImplTraitType resolveType() { result.getImplTraitTypeRepr() = this }
203+
override Type resolveTypeAt(TypePath typePath) {
204+
typePath.isEmpty() and
205+
result.(ImplTraitType).getImplTraitTypeRepr() = this
206+
}
216207
}
217208

218209
private TypeParameter pathGetTypeParameter(TypeAlias alias, int i) {
@@ -222,30 +213,44 @@ private TypeParameter pathGetTypeParameter(TypeAlias alias, int i) {
222213
// Used to represent implicit `Self` type arguments in traits and `impl` blocks,
223214
// see `PathMention` for details.
224215
class TypeParamMention extends TypeMention instanceof TypeParam {
225-
override TypeMention getTypeArgument(int i) { none() }
226-
227-
override Type resolveType() { result = TTypeParamTypeParameter(this) }
216+
override Type resolveTypeAt(TypePath typePath) {
217+
typePath.isEmpty() and
218+
result = TTypeParamTypeParameter(this)
219+
}
228220
}
229221

222+
// TODO: Remove?
230223
// Used to represent implicit type arguments for associated types in traits.
231224
class TypeAliasMention extends TypeMention instanceof TypeAlias {
232225
private Type t;
233226

234227
TypeAliasMention() { t = TAssociatedTypeTypeParameter(this) }
235228

236-
override TypeMention getTypeArgument(int i) { none() }
237-
238-
override Type resolveType() { result = t }
229+
override Type resolveTypeAt(TypePath typePath) {
230+
typePath.isEmpty() and
231+
result = t
232+
}
233+
// override TypeMention getTypeArgument(int i) { none() }
234+
// override Type resolveType() { result = t }
239235
}
240236

241237
class TraitMention extends TypeMention instanceof TraitItemNode {
242-
override TypeMention getTypeArgument(int i) {
243-
result = super.getTypeParam(i)
238+
override Type resolveTypeAt(TypePath typePath) {
239+
typePath.isEmpty() and
240+
result = TTrait(this)
244241
or
245-
traitAliasIndex(this, i, result)
242+
exists(TypeAlias alias |
243+
traitAliasIndex(this, _, alias) and
244+
typePath = TypePath::singleton(result) and
245+
result = TAssociatedTypeTypeParameter(alias)
246+
)
247+
or
248+
exists(TypeParam tp |
249+
tp = super.getTypeParam(_) and
250+
typePath = TypePath::singleton(result) and
251+
result = TTypeParamTypeParameter(tp)
252+
)
246253
}
247-
248-
override Type resolveType() { result = TTrait(this) }
249254
}
250255

251256
// NOTE: Since the implicit type parameter for the self type parameter never
@@ -259,7 +264,8 @@ class SelfTypeParameterMention extends TypeMention instanceof Name {
259264

260265
Trait getTrait() { result = trait }
261266

262-
override Type resolveType() { result = TSelfTypeParameter(trait) }
263-
264-
override TypeMention getTypeArgument(int i) { none() }
267+
override Type resolveTypeAt(TypePath typePath) {
268+
typePath.isEmpty() and
269+
result = TSelfTypeParameter(trait)
270+
}
265271
}

rust/ql/test/extractor-tests/generated/Path/CONSISTENCY/TypeInferenceConsistency.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
multipleCallTargets
22
| dereference.rs:61:15:61:24 | e1.deref() |
3-
| main.rs:2076:13:2076:31 | ...::from(...) |
4-
| main.rs:2077:13:2077:31 | ...::from(...) |
5-
| main.rs:2078:13:2078:31 | ...::from(...) |
6-
| main.rs:2084:13:2084:31 | ...::from(...) |
7-
| main.rs:2085:13:2085:31 | ...::from(...) |
83
| main.rs:2086:13:2086:31 | ...::from(...) |
9-
| main.rs:2122:21:2122:43 | ...::from(...) |
4+
| main.rs:2087:13:2087:31 | ...::from(...) |
5+
| main.rs:2088:13:2088:31 | ...::from(...) |
6+
| main.rs:2094:13:2094:31 | ...::from(...) |
7+
| main.rs:2095:13:2095:31 | ...::from(...) |
8+
| main.rs:2096:13:2096:31 | ...::from(...) |
9+
| main.rs:2132:21:2132:43 | ...::from(...) |

0 commit comments

Comments
 (0)