Skip to content

Commit c00cf2f

Browse files
committed
Rust: Factor out getTypeMentionForTypeParameter
1 parent e2a524b commit c00cf2f

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

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

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ class PathTypeMention extends TypeMention, Path {
6161

6262
TypeItemNode getResolved() { result = resolved }
6363

64+
/**
65+
* Gets a type alias with the name `name` of the trait that this path resolves
66+
* to, if any.
67+
*/
6468
pragma[nomagic]
6569
private TypeAlias getResolvedTraitAlias(string name) {
66-
exists(TraitItemNode trait |
67-
trait = resolved and
68-
result = trait.getAnAssocItem() and
69-
name = result.getName().getText()
70-
)
70+
result = resolved.(TraitItemNode).getAnAssocItem() and
71+
name = result.getName().getText()
7172
}
7273

7374
pragma[nomagic]
@@ -140,40 +141,18 @@ class PathTypeMention extends TypeMention, Path {
140141
)
141142
}
142143

143-
override Type resolveTypeAt(TypePath typePath) {
144-
result = this.aliasResolveTypeAt(typePath)
145-
or
146-
typePath.isEmpty() and
147-
(
148-
result = TStruct(resolved)
149-
or
150-
result = TEnum(resolved)
151-
or
152-
exists(TraitItemNode trait | trait = resolved |
153-
// If this is a `Self` path, then it resolves to the implicit `Self`
154-
// type parameter, otherwise it is a trait bound.
155-
if this = trait.getASelfPath()
156-
then result = TSelfTypeParameter(trait)
157-
else result = TTrait(trait)
158-
)
159-
or
160-
result = TTypeParamTypeParameter(resolved)
161-
or
162-
result = TAssociatedTypeTypeParameter(resolved)
163-
)
164-
or
144+
/** Gets the type mention in this path for the type parameter `tp`, if any. */
145+
pragma[nomagic]
146+
private TypeMention getTypeMentionForTypeParameter(TypeParameter tp) {
165147
not exists(resolved.(TypeAlias).getTypeRepr()) and
166-
exists(TypeParameter tp, TypeMention arg, TypePath suffix |
167-
result = arg.resolveTypeAt(suffix) and
168-
typePath = TypePath::cons(tp, suffix)
169-
|
148+
(
170149
exists(int i |
171-
arg = this.getPositionalTypeArgument(pragma[only_bind_into](i)) and
150+
result = this.getPositionalTypeArgument(pragma[only_bind_into](i)) and
172151
tp = this.resolveType().getTypeParameter(pragma[only_bind_into](i))
173152
)
174153
or
175154
exists(TypeAlias alias |
176-
arg = this.getAnAssocTypeArgument(alias) and
155+
result = this.getAnAssocTypeArgument(alias) and
177156
tp = TAssociatedTypeTypeParameter(alias)
178157
)
179158
or
@@ -195,14 +174,42 @@ class PathTypeMention extends TypeMention, Path {
195174
param.getTrait() = resolved and
196175
name = param.getTypeAlias().getName().getText() and
197176
alias = impl.getASuccessor(pragma[only_bind_into](name)) and
198-
arg = alias.getTypeRepr() and
177+
result = alias.getTypeRepr() and
199178
tp =
200179
TAssociatedTypeTypeParameter(resolved
201180
.(TraitItemNode)
202181
.getAssocItem(pragma[only_bind_into](name)))
203182
)
204183
)
205184
}
185+
186+
override Type resolveTypeAt(TypePath typePath) {
187+
result = this.aliasResolveTypeAt(typePath)
188+
or
189+
typePath.isEmpty() and
190+
(
191+
result = TStruct(resolved)
192+
or
193+
result = TEnum(resolved)
194+
or
195+
exists(TraitItemNode trait | trait = resolved |
196+
// If this is a `Self` path, then it resolves to the implicit `Self`
197+
// type parameter, otherwise it is a trait bound.
198+
if this = trait.getASelfPath()
199+
then result = TSelfTypeParameter(trait)
200+
else result = TTrait(trait)
201+
)
202+
or
203+
result = TTypeParamTypeParameter(resolved)
204+
or
205+
result = TAssociatedTypeTypeParameter(resolved)
206+
)
207+
or
208+
exists(TypeParameter tp, TypePath suffix |
209+
result = this.getTypeMentionForTypeParameter(tp).resolveTypeAt(suffix) and
210+
typePath = TypePath::cons(tp, suffix)
211+
)
212+
}
206213
}
207214

208215
class PathTypeReprMention extends TypeMention, PathTypeRepr {

0 commit comments

Comments
 (0)