Skip to content

Commit de8e535

Browse files
committed
Rust: Move predicates up to right before first usage
1 parent 1b683f6 commit de8e535

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

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

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,54 @@ private TypeMention getTypeAnnotation(AstNode n) {
250250
)
251251
}
252252

253+
/**
254+
* Gets the type of the implicitly typed `self` parameter, taking into account
255+
* whether the parameter is passed by value or by reference.
256+
*/
257+
bindingset[self, suffix, t]
258+
pragma[inline_late]
259+
private Type getRefAdjustShorthandSelfType(SelfParam self, TypePath suffix, Type t, TypePath path) {
260+
not self.hasTypeRepr() and
261+
(
262+
if self.isRef()
263+
then
264+
// `fn f(&self, ...)`
265+
path.isEmpty() and
266+
result = TRefType()
267+
or
268+
path = TypePath::cons(TRefTypeParameter(), suffix) and
269+
result = t
270+
else (
271+
// `fn f(self, ...)`
272+
path = suffix and
273+
result = t
274+
)
275+
)
276+
}
277+
278+
pragma[nomagic]
279+
private Type resolveImplSelfType(Impl i, TypePath path) {
280+
result = i.getSelfTy().(TypeMention).resolveTypeAt(path)
281+
}
282+
283+
/**
284+
* Gets the type at `path` of the parameter `self` which uses the [shorthand
285+
* syntax][1] which is sugar for an explicit annotation.
286+
*
287+
* [1]: https://doc.rust-lang.org/stable/reference/items/associated-items.html#r-associated.fn.method.self-pat-shorthands
288+
*/
289+
pragma[nomagic]
290+
private Type inferShorthandSelfType(SelfParam self, TypePath path) {
291+
exists(ImplOrTraitItemNode i, TypePath suffix, Type t |
292+
self = i.getAnAssocItem().(Function).getParamList().getSelfParam() and
293+
result = getRefAdjustShorthandSelfType(self, suffix, t, path)
294+
|
295+
t = resolveImplSelfType(i, suffix)
296+
or
297+
t = TSelfTypeParameter(i) and suffix.isEmpty()
298+
)
299+
}
300+
253301
/** Gets the type of `n`, which has an explicit type annotation. */
254302
pragma[nomagic]
255303
private Type inferAnnotatedType(AstNode n, TypePath path) {
@@ -603,54 +651,6 @@ private Type inferTypeEquality(AstNode n, TypePath path) {
603651
)
604652
}
605653

606-
/**
607-
* Gets the type of the implicitly typed `self` parameter, taking into account
608-
* whether the parameter is passed by value or by reference.
609-
*/
610-
bindingset[self, suffix, t]
611-
pragma[inline_late]
612-
private Type getRefAdjustShorthandSelfType(SelfParam self, TypePath suffix, Type t, TypePath path) {
613-
not self.hasTypeRepr() and
614-
(
615-
if self.isRef()
616-
then
617-
// `fn f(&self, ...)`
618-
path.isEmpty() and
619-
result = TRefType()
620-
or
621-
path = TypePath::cons(TRefTypeParameter(), suffix) and
622-
result = t
623-
else (
624-
// `fn f(self, ...)`
625-
path = suffix and
626-
result = t
627-
)
628-
)
629-
}
630-
631-
pragma[nomagic]
632-
private Type resolveImplSelfType(Impl i, TypePath path) {
633-
result = i.getSelfTy().(TypeMention).resolveTypeAt(path)
634-
}
635-
636-
/**
637-
* Gets the type at `path` of the parameter `self` which uses the [shorthand
638-
* syntax][1] which is sugar for an explicit annotation.
639-
*
640-
* [1]: https://doc.rust-lang.org/stable/reference/items/associated-items.html#r-associated.fn.method.self-pat-shorthands
641-
*/
642-
pragma[nomagic]
643-
private Type inferShorthandSelfType(SelfParam self, TypePath path) {
644-
exists(ImplOrTraitItemNode i, TypePath suffix, Type t |
645-
self = i.getAnAssocItem().(Function).getParamList().getSelfParam() and
646-
result = getRefAdjustShorthandSelfType(self, suffix, t, path)
647-
|
648-
t = resolveImplSelfType(i, suffix)
649-
or
650-
t = TSelfTypeParameter(i) and suffix.isEmpty()
651-
)
652-
}
653-
654654
/**
655655
* A matching configuration for resolving types of struct expressions
656656
* like `Foo { bar = baz }`.

0 commit comments

Comments
 (0)