Skip to content

Commit 7d536a3

Browse files
committed
Rust: When resolving methods on reference also consider the reference itself
1 parent 8451796 commit 7d536a3

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,6 @@ final class MethodCall extends Call {
11411141
(
11421142
path0.isCons(TRefTypeParameter(), path)
11431143
or
1144-
not path0.isCons(TRefTypeParameter(), _) and
1145-
not (path0.isEmpty() and result = TRefType()) and
11461144
path = path0
11471145
)
11481146
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn explicit_polymorphic_dereference() {
5858
fn explicit_ref_dereference() {
5959
// Explicit dereference with type parameter
6060
let e1 = &'a';
61-
let _f1 = e1.deref(); // $ MISSING: method=deref type=_f1:&T.char
61+
let _f1 = e1.deref(); // $ method=deref MISSING: type=_f1:&T.char
6262

6363
// Explicit dereference with type parameter
6464
let e2 = &'a';

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,14 +1157,14 @@ mod method_call_type_conversion {
11571157
// https://doc.rust-lang.org/std/string/struct.String.html#deref.
11581158
let u = x9.parse::<u32>(); // $ method=parse type=u:T.u32
11591159

1160-
let my_thing = &MyInt { a: 37 };
1160+
let my_thing = &MyInt { a: 37 }; // $ SPURIOUS: type=my_thing:&T.&T.MyInt
11611161
// implicit borrow of a `&`
1162-
let a = my_thing.method_on_borrow(); // $ MISSING: method=MyInt::method_on_borrow
1162+
let a = my_thing.method_on_borrow(); // $ method=MyInt::method_on_borrow
11631163
println!("{:?}", a);
11641164

11651165
// no implicit borrow
11661166
let my_thing = &MyInt { a: 38 };
1167-
let a = my_thing.method_not_on_borrow(); // $ MISSING: method=MyInt::method_not_on_borrow
1167+
let a = my_thing.method_not_on_borrow(); // $ method=MyInt::method_not_on_borrow
11681168
println!("{:?}", a);
11691169
}
11701170
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ inferType
126126
| dereference.rs:55:17:55:18 | c3 | T | {EXTERNAL LOCATION} | i64 |
127127
| dereference.rs:60:9:60:10 | e1 | | file://:0:0:0:0 | & |
128128
| dereference.rs:60:9:60:10 | e1 | &T | {EXTERNAL LOCATION} | char |
129+
| dereference.rs:60:9:60:10 | e1 | &T | file://:0:0:0:0 | & |
129130
| dereference.rs:60:14:60:17 | &'a' | | file://:0:0:0:0 | & |
130131
| dereference.rs:60:14:60:17 | &'a' | &T | {EXTERNAL LOCATION} | char |
132+
| dereference.rs:60:14:60:17 | &'a' | &T | file://:0:0:0:0 | & |
131133
| dereference.rs:60:15:60:17 | 'a' | | {EXTERNAL LOCATION} | char |
134+
| dereference.rs:60:15:60:17 | 'a' | | file://:0:0:0:0 | & |
135+
| dereference.rs:61:9:61:11 | _f1 | | file://:0:0:0:0 | & |
132136
| dereference.rs:61:15:61:16 | e1 | | file://:0:0:0:0 | & |
133137
| dereference.rs:61:15:61:16 | e1 | &T | {EXTERNAL LOCATION} | char |
138+
| dereference.rs:61:15:61:16 | e1 | &T | file://:0:0:0:0 | & |
139+
| dereference.rs:61:15:61:24 | e1.deref() | | file://:0:0:0:0 | & |
134140
| dereference.rs:64:9:64:10 | e2 | | file://:0:0:0:0 | & |
135141
| dereference.rs:64:9:64:10 | e2 | &T | {EXTERNAL LOCATION} | char |
136142
| dereference.rs:64:9:64:10 | e2 | &T | file://:0:0:0:0 | & |
@@ -1733,25 +1739,39 @@ inferType
17331739
| main.rs:1158:17:1158:33 | x9.parse() | | {EXTERNAL LOCATION} | Result |
17341740
| main.rs:1158:17:1158:33 | x9.parse() | T | {EXTERNAL LOCATION} | u32 |
17351741
| main.rs:1160:13:1160:20 | my_thing | | file://:0:0:0:0 | & |
1742+
| main.rs:1160:13:1160:20 | my_thing | &T | file://:0:0:0:0 | & |
17361743
| main.rs:1160:13:1160:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1744+
| main.rs:1160:13:1160:20 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
17371745
| main.rs:1160:24:1160:39 | &... | | file://:0:0:0:0 | & |
1746+
| main.rs:1160:24:1160:39 | &... | &T | file://:0:0:0:0 | & |
17381747
| main.rs:1160:24:1160:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
1748+
| main.rs:1160:24:1160:39 | &... | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1749+
| main.rs:1160:25:1160:39 | MyInt {...} | | file://:0:0:0:0 | & |
17391750
| main.rs:1160:25:1160:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
1751+
| main.rs:1160:25:1160:39 | MyInt {...} | &T | main.rs:1082:5:1085:5 | MyInt |
17401752
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i32 |
17411753
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i64 |
1754+
| main.rs:1162:13:1162:13 | a | | {EXTERNAL LOCATION} | i64 |
17421755
| main.rs:1162:17:1162:24 | my_thing | | file://:0:0:0:0 | & |
1756+
| main.rs:1162:17:1162:24 | my_thing | &T | file://:0:0:0:0 | & |
17431757
| main.rs:1162:17:1162:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1758+
| main.rs:1162:17:1162:24 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1759+
| main.rs:1162:17:1162:43 | my_thing.method_on_borrow() | | {EXTERNAL LOCATION} | i64 |
17441760
| main.rs:1163:18:1163:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1761+
| main.rs:1163:26:1163:26 | a | | {EXTERNAL LOCATION} | i64 |
17451762
| main.rs:1166:13:1166:20 | my_thing | | file://:0:0:0:0 | & |
17461763
| main.rs:1166:13:1166:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
17471764
| main.rs:1166:24:1166:39 | &... | | file://:0:0:0:0 | & |
17481765
| main.rs:1166:24:1166:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
17491766
| main.rs:1166:25:1166:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
17501767
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i32 |
17511768
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i64 |
1769+
| main.rs:1167:13:1167:13 | a | | {EXTERNAL LOCATION} | i64 |
17521770
| main.rs:1167:17:1167:24 | my_thing | | file://:0:0:0:0 | & |
17531771
| main.rs:1167:17:1167:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1772+
| main.rs:1167:17:1167:47 | my_thing.method_not_on_borrow() | | {EXTERNAL LOCATION} | i64 |
17541773
| main.rs:1168:18:1168:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1774+
| main.rs:1168:26:1168:26 | a | | {EXTERNAL LOCATION} | i64 |
17551775
| main.rs:1175:16:1175:20 | SelfParam | | file://:0:0:0:0 | & |
17561776
| main.rs:1175:16:1175:20 | SelfParam | &T | main.rs:1173:5:1181:5 | Self [trait MyTrait] |
17571777
| main.rs:1178:16:1178:20 | SelfParam | | file://:0:0:0:0 | & |

0 commit comments

Comments
 (0)