Skip to content

Commit ef22f2d

Browse files
committed
Rust: Do not let type info flow into a let statement identifier when the let statement is annotated
1 parent 2ed54d5 commit ef22f2d

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ private Struct getRangeType(RangeExpr re) {
250250
result instanceof RangeToInclusiveStruct
251251
}
252252

253+
/** A `let` statement with an annotation without any uses of the `_` type syntax. */
254+
predicate fullyAnnotatedLetStmt(LetStmt let) {
255+
exists(let.getTypeRepr()) and
256+
not exists(InferTypeRepr t | t.getParentNode*() = let.getTypeRepr())
257+
}
258+
253259
/**
254260
* Holds if the type tree of `n1` at `prefix1` should be equal to the type tree
255261
* of `n2` at `prefix2` and type information should propagate in both directions
@@ -266,6 +272,7 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
266272
)
267273
or
268274
exists(LetStmt let |
275+
not fullyAnnotatedLetStmt(let) and
269276
let.getPat() = n1 and
270277
let.getInitializer() = n2
271278
)
@@ -352,15 +359,35 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
352359
)
353360
}
354361

362+
/**
363+
* Similar to `typeEquality` but we only want type information to flow along the
364+
* equality from left to right. That is, the type of `n2` will be inferred from
365+
* the type of `n1`, but not the other way around.
366+
*/
367+
private predicate directionalTypeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
368+
prefix1.isEmpty() and
369+
prefix2.isEmpty() and
370+
exists(LetStmt let |
371+
fullyAnnotatedLetStmt(let) and
372+
let.getPat() = n1 and
373+
let.getInitializer() = n2
374+
)
375+
}
376+
355377
pragma[nomagic]
356378
private Type inferTypeEquality(AstNode n, TypePath path) {
357379
exists(TypePath prefix1, AstNode n2, TypePath prefix2, TypePath suffix |
358380
result = inferType(n2, prefix2.appendInverse(suffix)) and
359381
path = prefix1.append(suffix)
360382
|
383+
// use type equality from right to left
361384
typeEquality(n, prefix1, n2, prefix2)
362385
or
386+
// use type equality from left to right
363387
typeEquality(n2, prefix2, n, prefix1)
388+
or
389+
// use type equality from left to right
390+
directionalTypeEquality(n2, prefix2, n, prefix1)
364391
)
365392
}
366393

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,21 +3438,17 @@ inferType
34383438
| main.rs:2114:15:2114:15 | x | | {EXTERNAL LOCATION} | bool |
34393439
| main.rs:2114:32:2116:9 | { ... } | | {EXTERNAL LOCATION} | bool |
34403440
| main.rs:2115:13:2115:13 | x | | {EXTERNAL LOCATION} | bool |
3441-
| main.rs:2120:13:2120:13 | x | | {EXTERNAL LOCATION} | i32 |
34423441
| main.rs:2120:13:2120:13 | x | | {EXTERNAL LOCATION} | i64 |
34433442
| main.rs:2120:22:2120:23 | 73 | | {EXTERNAL LOCATION} | i32 |
34443443
| main.rs:2120:22:2120:23 | 73 | | {EXTERNAL LOCATION} | i64 |
3445-
| main.rs:2121:9:2121:9 | x | | {EXTERNAL LOCATION} | i32 |
34463444
| main.rs:2121:9:2121:9 | x | | {EXTERNAL LOCATION} | i64 |
34473445
| main.rs:2121:9:2121:22 | x.my_add(...) | | {EXTERNAL LOCATION} | i64 |
34483446
| main.rs:2121:18:2121:21 | 5i64 | | {EXTERNAL LOCATION} | i64 |
3449-
| main.rs:2122:9:2122:9 | x | | {EXTERNAL LOCATION} | i32 |
34503447
| main.rs:2122:9:2122:9 | x | | {EXTERNAL LOCATION} | i64 |
34513448
| main.rs:2122:9:2122:23 | x.my_add(...) | | {EXTERNAL LOCATION} | i64 |
34523449
| main.rs:2122:18:2122:22 | &5i64 | | file://:0:0:0:0 | & |
34533450
| main.rs:2122:18:2122:22 | &5i64 | &T | {EXTERNAL LOCATION} | i64 |
34543451
| main.rs:2122:19:2122:22 | 5i64 | | {EXTERNAL LOCATION} | i64 |
3455-
| main.rs:2123:9:2123:9 | x | | {EXTERNAL LOCATION} | i32 |
34563452
| main.rs:2123:9:2123:9 | x | | {EXTERNAL LOCATION} | i64 |
34573453
| main.rs:2123:9:2123:22 | x.my_add(...) | | {EXTERNAL LOCATION} | i64 |
34583454
| main.rs:2123:18:2123:21 | true | | {EXTERNAL LOCATION} | bool |
@@ -3558,7 +3554,6 @@ inferType
35583554
| main.rs:2171:18:2171:22 | vals2 | | file://:0:0:0:0 | [] |
35593555
| main.rs:2171:18:2171:22 | vals2 | [T;...] | {EXTERNAL LOCATION} | u16 |
35603556
| main.rs:2173:13:2173:17 | vals3 | | file://:0:0:0:0 | [] |
3561-
| main.rs:2173:13:2173:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
35623557
| main.rs:2173:13:2173:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
35633558
| main.rs:2173:26:2173:26 | 3 | | {EXTERNAL LOCATION} | i32 |
35643559
| main.rs:2173:31:2173:39 | [...] | | file://:0:0:0:0 | [] |
@@ -3570,13 +3565,10 @@ inferType
35703565
| main.rs:2173:35:2173:35 | 2 | | {EXTERNAL LOCATION} | u32 |
35713566
| main.rs:2173:38:2173:38 | 3 | | {EXTERNAL LOCATION} | i32 |
35723567
| main.rs:2173:38:2173:38 | 3 | | {EXTERNAL LOCATION} | u32 |
3573-
| main.rs:2174:13:2174:13 | u | | {EXTERNAL LOCATION} | i32 |
35743568
| main.rs:2174:13:2174:13 | u | | {EXTERNAL LOCATION} | u32 |
35753569
| main.rs:2174:18:2174:22 | vals3 | | file://:0:0:0:0 | [] |
3576-
| main.rs:2174:18:2174:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
35773570
| main.rs:2174:18:2174:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
35783571
| main.rs:2176:13:2176:17 | vals4 | | file://:0:0:0:0 | [] |
3579-
| main.rs:2176:13:2176:17 | vals4 | [T;...] | {EXTERNAL LOCATION} | i32 |
35803572
| main.rs:2176:13:2176:17 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
35813573
| main.rs:2176:26:2176:26 | 3 | | {EXTERNAL LOCATION} | i32 |
35823574
| main.rs:2176:31:2176:36 | [1; 3] | | file://:0:0:0:0 | [] |
@@ -3585,10 +3577,8 @@ inferType
35853577
| main.rs:2176:32:2176:32 | 1 | | {EXTERNAL LOCATION} | i32 |
35863578
| main.rs:2176:32:2176:32 | 1 | | {EXTERNAL LOCATION} | u64 |
35873579
| main.rs:2176:35:2176:35 | 3 | | {EXTERNAL LOCATION} | i32 |
3588-
| main.rs:2177:13:2177:13 | u | | {EXTERNAL LOCATION} | i32 |
35893580
| main.rs:2177:13:2177:13 | u | | {EXTERNAL LOCATION} | u64 |
35903581
| main.rs:2177:18:2177:22 | vals4 | | file://:0:0:0:0 | [] |
3591-
| main.rs:2177:18:2177:22 | vals4 | [T;...] | {EXTERNAL LOCATION} | i32 |
35923582
| main.rs:2177:18:2177:22 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
35933583
| main.rs:2179:17:2179:24 | strings1 | | file://:0:0:0:0 | [] |
35943584
| main.rs:2179:17:2179:24 | strings1 | [T;...] | file://:0:0:0:0 | & |
@@ -3950,16 +3940,13 @@ inferType
39503940
| main.rs:2255:30:2255:33 | map1 | V.A | {EXTERNAL LOCATION} | Global |
39513941
| main.rs:2255:30:2255:33 | map1 | V.T | file://:0:0:0:0 | & |
39523942
| main.rs:2255:30:2255:33 | map1 | V.T.&T | {EXTERNAL LOCATION} | str |
3953-
| main.rs:2259:17:2259:17 | a | | {EXTERNAL LOCATION} | i32 |
39543943
| main.rs:2259:17:2259:17 | a | | {EXTERNAL LOCATION} | i64 |
39553944
| main.rs:2259:26:2259:26 | 0 | | {EXTERNAL LOCATION} | i32 |
39563945
| main.rs:2259:26:2259:26 | 0 | | {EXTERNAL LOCATION} | i64 |
3957-
| main.rs:2261:23:2261:23 | a | | {EXTERNAL LOCATION} | i32 |
39583946
| main.rs:2261:23:2261:23 | a | | {EXTERNAL LOCATION} | i64 |
39593947
| main.rs:2261:23:2261:28 | ... < ... | | {EXTERNAL LOCATION} | bool |
39603948
| main.rs:2261:27:2261:28 | 10 | | {EXTERNAL LOCATION} | i32 |
39613949
| main.rs:2261:27:2261:28 | 10 | | {EXTERNAL LOCATION} | i64 |
3962-
| main.rs:2263:13:2263:13 | a | | {EXTERNAL LOCATION} | i32 |
39633950
| main.rs:2263:13:2263:13 | a | | {EXTERNAL LOCATION} | i64 |
39643951
| main.rs:2263:13:2263:18 | ... += ... | | file://:0:0:0:0 | () |
39653952
| main.rs:2263:18:2263:18 | 1 | | {EXTERNAL LOCATION} | i32 |
@@ -4953,8 +4940,6 @@ inferType
49534940
| pattern_matching.rs:504:22:504:71 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
49544941
| pattern_matching.rs:510:9:510:13 | slice | | file://:0:0:0:0 | & |
49554942
| pattern_matching.rs:510:9:510:13 | slice | &T | file://:0:0:0:0 | [] |
4956-
| pattern_matching.rs:510:9:510:13 | slice | &T | file://:0:0:0:0 | [] |
4957-
| pattern_matching.rs:510:9:510:13 | slice | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
49584943
| pattern_matching.rs:510:9:510:13 | slice | &T.[T] | {EXTERNAL LOCATION} | i32 |
49594944
| pattern_matching.rs:510:25:510:40 | &... | | file://:0:0:0:0 | & |
49604945
| pattern_matching.rs:510:25:510:40 | &... | &T | file://:0:0:0:0 | [] |
@@ -4972,55 +4957,39 @@ inferType
49724957
| pattern_matching.rs:510:39:510:39 | 5 | | {EXTERNAL LOCATION} | i32 |
49734958
| pattern_matching.rs:513:11:513:15 | slice | | file://:0:0:0:0 | & |
49744959
| pattern_matching.rs:513:11:513:15 | slice | &T | file://:0:0:0:0 | [] |
4975-
| pattern_matching.rs:513:11:513:15 | slice | &T | file://:0:0:0:0 | [] |
4976-
| pattern_matching.rs:513:11:513:15 | slice | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
49774960
| pattern_matching.rs:513:11:513:15 | slice | &T.[T] | {EXTERNAL LOCATION} | i32 |
49784961
| pattern_matching.rs:514:9:514:10 | SlicePat | | file://:0:0:0:0 | & |
49794962
| pattern_matching.rs:514:9:514:10 | SlicePat | &T | file://:0:0:0:0 | [] |
4980-
| pattern_matching.rs:514:9:514:10 | SlicePat | &T | file://:0:0:0:0 | [] |
4981-
| pattern_matching.rs:514:9:514:10 | SlicePat | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
49824963
| pattern_matching.rs:514:9:514:10 | SlicePat | &T.[T] | {EXTERNAL LOCATION} | i32 |
49834964
| pattern_matching.rs:515:17:515:27 | empty_slice | | file://:0:0:0:0 | & |
49844965
| pattern_matching.rs:515:17:515:27 | empty_slice | &T | file://:0:0:0:0 | [] |
4985-
| pattern_matching.rs:515:17:515:27 | empty_slice | &T | file://:0:0:0:0 | [] |
4986-
| pattern_matching.rs:515:17:515:27 | empty_slice | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
49874966
| pattern_matching.rs:515:17:515:27 | empty_slice | &T.[T] | {EXTERNAL LOCATION} | i32 |
49884967
| pattern_matching.rs:515:31:515:35 | slice | | file://:0:0:0:0 | & |
49894968
| pattern_matching.rs:515:31:515:35 | slice | &T | file://:0:0:0:0 | [] |
4990-
| pattern_matching.rs:515:31:515:35 | slice | &T | file://:0:0:0:0 | [] |
4991-
| pattern_matching.rs:515:31:515:35 | slice | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
49924969
| pattern_matching.rs:515:31:515:35 | slice | &T.[T] | {EXTERNAL LOCATION} | i32 |
49934970
| pattern_matching.rs:516:22:516:40 | "Empty slice: {:?}\\n" | | file://:0:0:0:0 | & |
49944971
| pattern_matching.rs:516:22:516:40 | "Empty slice: {:?}\\n" | &T | {EXTERNAL LOCATION} | str |
49954972
| pattern_matching.rs:516:22:516:53 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
49964973
| pattern_matching.rs:516:22:516:53 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
49974974
| pattern_matching.rs:516:43:516:53 | empty_slice | | file://:0:0:0:0 | & |
49984975
| pattern_matching.rs:516:43:516:53 | empty_slice | &T | file://:0:0:0:0 | [] |
4999-
| pattern_matching.rs:516:43:516:53 | empty_slice | &T | file://:0:0:0:0 | [] |
5000-
| pattern_matching.rs:516:43:516:53 | empty_slice | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
50014976
| pattern_matching.rs:516:43:516:53 | empty_slice | &T.[T] | {EXTERNAL LOCATION} | i32 |
50024977
| pattern_matching.rs:518:9:518:11 | SlicePat | | file://:0:0:0:0 | & |
50034978
| pattern_matching.rs:518:9:518:11 | SlicePat | &T | file://:0:0:0:0 | [] |
5004-
| pattern_matching.rs:518:9:518:11 | SlicePat | &T | file://:0:0:0:0 | [] |
5005-
| pattern_matching.rs:518:9:518:11 | SlicePat | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
50064979
| pattern_matching.rs:518:9:518:11 | SlicePat | &T.[T] | {EXTERNAL LOCATION} | i32 |
50074980
| pattern_matching.rs:520:22:520:41 | "Single element: {}\\n" | | file://:0:0:0:0 | & |
50084981
| pattern_matching.rs:520:22:520:41 | "Single element: {}\\n" | &T | {EXTERNAL LOCATION} | str |
50094982
| pattern_matching.rs:520:22:520:54 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
50104983
| pattern_matching.rs:520:22:520:54 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
50114984
| pattern_matching.rs:522:9:522:23 | SlicePat | | file://:0:0:0:0 | & |
50124985
| pattern_matching.rs:522:9:522:23 | SlicePat | &T | file://:0:0:0:0 | [] |
5013-
| pattern_matching.rs:522:9:522:23 | SlicePat | &T | file://:0:0:0:0 | [] |
5014-
| pattern_matching.rs:522:9:522:23 | SlicePat | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
50154986
| pattern_matching.rs:522:9:522:23 | SlicePat | &T.[T] | {EXTERNAL LOCATION} | i32 |
50164987
| pattern_matching.rs:525:22:525:43 | "Two elements: {}, {}\\n" | | file://:0:0:0:0 | & |
50174988
| pattern_matching.rs:525:22:525:43 | "Two elements: {}, {}\\n" | &T | {EXTERNAL LOCATION} | str |
50184989
| pattern_matching.rs:525:22:525:70 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
50194990
| pattern_matching.rs:525:22:525:70 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
50204991
| pattern_matching.rs:527:9:527:34 | SlicePat | | file://:0:0:0:0 | & |
50214992
| pattern_matching.rs:527:9:527:34 | SlicePat | &T | file://:0:0:0:0 | [] |
5022-
| pattern_matching.rs:527:9:527:34 | SlicePat | &T | file://:0:0:0:0 | [] |
5023-
| pattern_matching.rs:527:9:527:34 | SlicePat | &T.[T;...] | {EXTERNAL LOCATION} | i32 |
50244993
| pattern_matching.rs:527:9:527:34 | SlicePat | &T.[T] | {EXTERNAL LOCATION} | i32 |
50254994
| pattern_matching.rs:532:17:532:53 | "First: {}, last: {}, middle l... | | file://:0:0:0:0 | & |
50264995
| pattern_matching.rs:532:17:532:53 | "First: {}, last: {}, middle l... | &T | {EXTERNAL LOCATION} | str |
@@ -5434,3 +5403,4 @@ inferType
54345403
| pattern_matching.rs:809:9:809:9 | _ | | {EXTERNAL LOCATION} | i32 |
54355404
| pattern_matching.rs:814:5:814:7 | f(...) | | {EXTERNAL LOCATION} | Option |
54365405
testFailures
5406+
| main.rs:2263:21:2263:53 | //... | Missing result: target=add_assign |

0 commit comments

Comments
 (0)