Skip to content

Commit 0ff4dbc

Browse files
committed
Rust: Fix type inference inconsistency
1 parent 1981668 commit 0ff4dbc

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,19 @@ private module CertainTypeInference {
324324
or
325325
// A `let` statement with a type annotation is a coercion site and hence
326326
// is not a certain type equality.
327-
exists(LetStmt let | not let.hasTypeRepr() |
328-
let.getPat() = n1 and
327+
exists(LetStmt let |
328+
not let.hasTypeRepr() and
329+
// Due to "binding modes" the type of the pattern is not necessarily the
330+
// same as the type of the initializer. The pattern being an identifier
331+
// pattern is sufficient to ensure that this is not the case.
332+
let.getPat().(IdentPat) = n1 and
329333
let.getInitializer() = n2
330334
)
331335
or
332336
exists(LetExpr let |
333-
let.getPat() = n1 and
337+
// Similarly as for let statements, we need to rule out binding modes
338+
// changing the type.
339+
let.getPat().(IdentPat) = n1 and
334340
let.getScrutinee() = n2
335341
)
336342
or
@@ -486,6 +492,11 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
486492
or
487493
n1 = n2.(MatchExpr).getAnArm().getExpr()
488494
or
495+
exists(LetExpr let |
496+
n1 = let.getScrutinee() and
497+
n2 = let.getPat()
498+
)
499+
or
489500
exists(MatchExpr me |
490501
n1 = me.getScrutinee() and
491502
n2 = me.getAnArm().getPat()

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5884,7 +5884,6 @@ inferType
58845884
| pattern_matching.rs:482:22:482:60 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
58855885
| pattern_matching.rs:482:50:482:60 | single_elem | | {EXTERNAL LOCATION} | i32 |
58865886
| pattern_matching.rs:487:9:487:18 | ref_tuple1 | | file://:0:0:0:0 | & |
5887-
| pattern_matching.rs:487:9:487:18 | ref_tuple1 | | file://:0:0:0:0 | (T_2) |
58885887
| pattern_matching.rs:487:9:487:18 | ref_tuple1 | &T | file://:0:0:0:0 | (T_2) |
58895888
| pattern_matching.rs:487:9:487:18 | ref_tuple1 | &T.0(2) | {EXTERNAL LOCATION} | i32 |
58905889
| pattern_matching.rs:487:9:487:18 | ref_tuple1 | &T.1(2) | {EXTERNAL LOCATION} | i32 |
@@ -5897,13 +5896,8 @@ inferType
58975896
| pattern_matching.rs:487:36:487:41 | TupleExpr | 1(2) | {EXTERNAL LOCATION} | i32 |
58985897
| pattern_matching.rs:487:37:487:37 | 1 | | {EXTERNAL LOCATION} | i32 |
58995898
| pattern_matching.rs:487:40:487:40 | 2 | | {EXTERNAL LOCATION} | i32 |
5900-
| pattern_matching.rs:488:12:488:17 | TuplePat | | file://:0:0:0:0 | & |
59015899
| pattern_matching.rs:488:12:488:17 | TuplePat | | file://:0:0:0:0 | (T_2) |
5902-
| pattern_matching.rs:488:12:488:17 | TuplePat | &T | file://:0:0:0:0 | (T_2) |
5903-
| pattern_matching.rs:488:12:488:17 | TuplePat | &T.0(2) | {EXTERNAL LOCATION} | i32 |
5904-
| pattern_matching.rs:488:12:488:17 | TuplePat | &T.1(2) | {EXTERNAL LOCATION} | i32 |
59055900
| pattern_matching.rs:488:21:488:30 | ref_tuple1 | | file://:0:0:0:0 | & |
5906-
| pattern_matching.rs:488:21:488:30 | ref_tuple1 | | file://:0:0:0:0 | (T_2) |
59075901
| pattern_matching.rs:488:21:488:30 | ref_tuple1 | &T | file://:0:0:0:0 | (T_2) |
59085902
| pattern_matching.rs:488:21:488:30 | ref_tuple1 | &T.0(2) | {EXTERNAL LOCATION} | i32 |
59095903
| pattern_matching.rs:488:21:488:30 | ref_tuple1 | &T.1(2) | {EXTERNAL LOCATION} | i32 |
@@ -5916,7 +5910,6 @@ inferType
59165910
| pattern_matching.rs:490:18:490:27 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
59175911
| pattern_matching.rs:490:18:490:27 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
59185912
| pattern_matching.rs:494:9:494:18 | ref_tuple2 | | file://:0:0:0:0 | & |
5919-
| pattern_matching.rs:494:9:494:18 | ref_tuple2 | | file://:0:0:0:0 | (T_2) |
59205913
| pattern_matching.rs:494:9:494:18 | ref_tuple2 | &T | file://:0:0:0:0 | (T_2) |
59215914
| pattern_matching.rs:494:9:494:18 | ref_tuple2 | &T.0(2) | {EXTERNAL LOCATION} | i32 |
59225915
| pattern_matching.rs:494:9:494:18 | ref_tuple2 | &T.1(2) | {EXTERNAL LOCATION} | i32 |
@@ -5929,13 +5922,8 @@ inferType
59295922
| pattern_matching.rs:494:36:494:41 | TupleExpr | 1(2) | {EXTERNAL LOCATION} | i32 |
59305923
| pattern_matching.rs:494:37:494:37 | 1 | | {EXTERNAL LOCATION} | i32 |
59315924
| pattern_matching.rs:494:40:494:40 | 2 | | {EXTERNAL LOCATION} | i32 |
5932-
| pattern_matching.rs:495:9:495:14 | TuplePat | | file://:0:0:0:0 | & |
59335925
| pattern_matching.rs:495:9:495:14 | TuplePat | | file://:0:0:0:0 | (T_2) |
5934-
| pattern_matching.rs:495:9:495:14 | TuplePat | &T | file://:0:0:0:0 | (T_2) |
5935-
| pattern_matching.rs:495:9:495:14 | TuplePat | &T.0(2) | {EXTERNAL LOCATION} | i32 |
5936-
| pattern_matching.rs:495:9:495:14 | TuplePat | &T.1(2) | {EXTERNAL LOCATION} | i32 |
59375926
| pattern_matching.rs:495:18:495:27 | ref_tuple2 | | file://:0:0:0:0 | & |
5938-
| pattern_matching.rs:495:18:495:27 | ref_tuple2 | | file://:0:0:0:0 | (T_2) |
59395927
| pattern_matching.rs:495:18:495:27 | ref_tuple2 | &T | file://:0:0:0:0 | (T_2) |
59405928
| pattern_matching.rs:495:18:495:27 | ref_tuple2 | &T.0(2) | {EXTERNAL LOCATION} | i32 |
59415929
| pattern_matching.rs:495:18:495:27 | ref_tuple2 | &T.1(2) | {EXTERNAL LOCATION} | i32 |

0 commit comments

Comments
 (0)