Skip to content

Commit 1b683f6

Browse files
committed
Rust: Infer certain type for self shorthand
1 parent f720102 commit 1b683f6

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ private TypeMention getTypeAnnotation(AstNode n) {
254254
pragma[nomagic]
255255
private Type inferAnnotatedType(AstNode n, TypePath path) {
256256
result = getTypeAnnotation(n).resolveTypeAt(path)
257+
or
258+
// The shorthand self syntax (i.e., a self parameter without a type
259+
// annotation) is sugar for a self parameter with an annotation.
260+
result = inferShorthandSelfType(n, path)
257261
}
258262

259263
/** Module for inferring certain type information. */
@@ -369,10 +373,7 @@ module CertainTypeInference {
369373
*/
370374
pragma[nomagic]
371375
Type inferCertainType(AstNode n, TypePath path) {
372-
exists(TypeMention tm |
373-
tm = getTypeAnnotation(n) and
374-
result = tm.resolveTypeAt(path)
375-
)
376+
result = inferAnnotatedType(n, path)
376377
or
377378
result = inferCertainCallExprType(n, path)
378379
or
@@ -608,7 +609,7 @@ private Type inferTypeEquality(AstNode n, TypePath path) {
608609
*/
609610
bindingset[self, suffix, t]
610611
pragma[inline_late]
611-
private Type getRefAdjustImplicitSelfType(SelfParam self, TypePath suffix, Type t, TypePath path) {
612+
private Type getRefAdjustShorthandSelfType(SelfParam self, TypePath suffix, Type t, TypePath path) {
612613
not self.hasTypeRepr() and
613614
(
614615
if self.isRef()
@@ -632,13 +633,17 @@ private Type resolveImplSelfType(Impl i, TypePath path) {
632633
result = i.getSelfTy().(TypeMention).resolveTypeAt(path)
633634
}
634635

635-
/** Gets the type at `path` of the implicitly typed `self` parameter. */
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+
*/
636642
pragma[nomagic]
637-
private Type inferImplicitSelfType(SelfParam self, TypePath path) {
638-
exists(ImplOrTraitItemNode i, Function f, TypePath suffix, Type t |
639-
f = i.getAnAssocItem() and
640-
self = f.getParamList().getSelfParam() and
641-
result = getRefAdjustImplicitSelfType(self, suffix, t, path)
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)
642647
|
643648
t = resolveImplSelfType(i, suffix)
644649
or
@@ -926,11 +931,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
926931
or
927932
exists(SelfParam self |
928933
self = pragma[only_bind_into](this.getParamList().getSelfParam()) and
929-
dpos.isSelf()
930-
|
934+
dpos.isSelf() and
931935
result = inferAnnotatedType(self, path) // `self` parameter with type annotation
932-
or
933-
result = inferImplicitSelfType(self, path) // `self` parameter without type annotation
934936
)
935937
or
936938
// For associated functions, we may also need to match type arguments against
@@ -2420,14 +2422,10 @@ private module Cached {
24202422
else any()
24212423
) and
24222424
(
2423-
result = inferAnnotatedType(n, path)
2424-
or
24252425
result = inferAssignmentOperationType(n, path)
24262426
or
24272427
result = inferTypeEquality(n, path)
24282428
or
2429-
result = inferImplicitSelfType(n, path)
2430-
or
24312429
result = inferStructExprType(n, path)
24322430
or
24332431
result = inferPathExprType(n, path)
@@ -2491,9 +2489,9 @@ private module Debug {
24912489
Input2::conditionSatisfiesConstraint(abs, condition, constraint)
24922490
}
24932491

2494-
predicate debugInferImplicitSelfType(SelfParam self, TypePath path, Type t) {
2492+
predicate debugInferShorthandSelfType(SelfParam self, TypePath path, Type t) {
24952493
self = getRelevantLocatable() and
2496-
t = inferImplicitSelfType(self, path)
2494+
t = inferShorthandSelfType(self, path)
24972495
}
24982496

24992497
predicate debugInferCallExprBaseType(AstNode n, TypePath path, Type t) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod method_impl {
7373

7474
impl Foo {
7575
pub fn m1(self) -> Self {
76-
self // $ type=self:Foo
76+
self // $ certainType=self:Foo
7777
}
7878

7979
pub fn m2(self) -> Foo {
@@ -108,7 +108,7 @@ mod trait_impl {
108108
impl MyTrait<bool> for MyThing {
109109
// MyThing::trait_method
110110
fn trait_method(self) -> bool {
111-
self.field // $ type=self:MyThing fieldof=MyThing
111+
self.field // $ certainType=self:MyThing fieldof=MyThing
112112
}
113113
}
114114

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,7 +2467,6 @@ inferType
24672467
| main.rs:1328:33:1328:36 | SelfParam | | main.rs:1326:5:1329:5 | Self [trait ATrait] |
24682468
| main.rs:1334:29:1334:33 | SelfParam | | file://:0:0:0:0 | & |
24692469
| main.rs:1334:29:1334:33 | SelfParam | &T | file://:0:0:0:0 | & |
2470-
| main.rs:1334:29:1334:33 | SelfParam | &T | main.rs:1307:5:1310:5 | MyInt |
24712470
| main.rs:1334:29:1334:33 | SelfParam | &T.&T | main.rs:1307:5:1310:5 | MyInt |
24722471
| main.rs:1334:43:1336:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
24732472
| main.rs:1335:13:1335:22 | (...) | | main.rs:1307:5:1310:5 | MyInt |
@@ -2481,7 +2480,6 @@ inferType
24812480
| main.rs:1335:16:1335:20 | * ... | &T | main.rs:1307:5:1310:5 | MyInt |
24822481
| main.rs:1335:17:1335:20 | self | | file://:0:0:0:0 | & |
24832482
| main.rs:1335:17:1335:20 | self | &T | file://:0:0:0:0 | & |
2484-
| main.rs:1335:17:1335:20 | self | &T | main.rs:1307:5:1310:5 | MyInt |
24852483
| main.rs:1335:17:1335:20 | self | &T.&T | main.rs:1307:5:1310:5 | MyInt |
24862484
| main.rs:1339:33:1339:36 | SelfParam | | file://:0:0:0:0 | & |
24872485
| main.rs:1339:33:1339:36 | SelfParam | &T | main.rs:1307:5:1310:5 | MyInt |
@@ -3798,7 +3796,6 @@ inferType
37983796
| main.rs:2030:31:2032:9 | { ... } | | main.rs:2002:5:2002:14 | S2 |
37993797
| main.rs:2031:13:2031:14 | S2 | | main.rs:2002:5:2002:14 | S2 |
38003798
| main.rs:2036:18:2036:22 | SelfParam | | file://:0:0:0:0 | & |
3801-
| main.rs:2036:18:2036:22 | SelfParam | | main.rs:2003:5:2003:22 | S3 |
38023799
| main.rs:2036:18:2036:22 | SelfParam | &T | main.rs:2003:5:2003:22 | S3 |
38033800
| main.rs:2036:18:2036:22 | SelfParam | &T.T3 | main.rs:2035:10:2035:17 | T |
38043801
| main.rs:2036:30:2039:9 | { ... } | | main.rs:2035:10:2035:17 | T |
@@ -3807,7 +3804,6 @@ inferType
38073804
| main.rs:2037:17:2037:21 | S3(...) | &T | main.rs:2003:5:2003:22 | S3 |
38083805
| main.rs:2037:17:2037:21 | S3(...) | &T.T3 | main.rs:2035:10:2035:17 | T |
38093806
| main.rs:2037:25:2037:28 | self | | file://:0:0:0:0 | & |
3810-
| main.rs:2037:25:2037:28 | self | | main.rs:2003:5:2003:22 | S3 |
38113807
| main.rs:2037:25:2037:28 | self | &T | main.rs:2003:5:2003:22 | S3 |
38123808
| main.rs:2037:25:2037:28 | self | &T.T3 | main.rs:2035:10:2035:17 | T |
38133809
| main.rs:2038:13:2038:21 | t.clone() | | main.rs:2035:10:2035:17 | T |

0 commit comments

Comments
 (0)