Skip to content

Commit 6ec844f

Browse files
committed
Rust: Only resolve deref methods on references to avoid blowup
1 parent 6b2c125 commit 6ec844f

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,15 @@ final class MethodCall extends Call {
11571157
(
11581158
path0.isCons(TRefTypeParameter(), path)
11591159
or
1160+
(
1161+
not path0.isCons(TRefTypeParameter(), _) and
1162+
not (path0.isEmpty() and result = TRefType())
1163+
or
1164+
// Ideally we should find all methods on reference types, but as
1165+
// that currently causes a blowup we limit this to the `deref`
1166+
// method in order to make dereferencing work.
1167+
this.getMethodName() = "deref"
1168+
) and
11601169
path = path0
11611170
)
11621171
|

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 }; // $ SPURIOUS: type=my_thing:&T.&T.MyInt
1160+
let my_thing = &MyInt { a: 37 };
11611161
// implicit borrow of a `&`
1162-
let a = my_thing.method_on_borrow(); // $ method=MyInt::method_on_borrow
1162+
let a = my_thing.method_on_borrow(); // $ MISSING: 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(); // $ method=MyInt::method_not_on_borrow
1167+
let a = my_thing.method_not_on_borrow(); // $ MISSING: method=MyInt::method_not_on_borrow
11681168
println!("{:?}", a);
11691169
}
11701170
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,39 +1675,25 @@ inferType
16751675
| main.rs:1158:17:1158:33 | x9.parse() | | {EXTERNAL LOCATION} | Result |
16761676
| main.rs:1158:17:1158:33 | x9.parse() | T | {EXTERNAL LOCATION} | u32 |
16771677
| main.rs:1160:13:1160:20 | my_thing | | file://:0:0:0:0 | & |
1678-
| main.rs:1160:13:1160:20 | my_thing | &T | file://:0:0:0:0 | & |
16791678
| main.rs:1160:13:1160:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1680-
| main.rs:1160:13:1160:20 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
16811679
| main.rs:1160:24:1160:39 | &... | | file://:0:0:0:0 | & |
1682-
| main.rs:1160:24:1160:39 | &... | &T | file://:0:0:0:0 | & |
16831680
| main.rs:1160:24:1160:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
1684-
| main.rs:1160:24:1160:39 | &... | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1685-
| main.rs:1160:25:1160:39 | MyInt {...} | | file://:0:0:0:0 | & |
16861681
| main.rs:1160:25:1160:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
1687-
| main.rs:1160:25:1160:39 | MyInt {...} | &T | main.rs:1082:5:1085:5 | MyInt |
16881682
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i32 |
16891683
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i64 |
1690-
| main.rs:1162:13:1162:13 | a | | {EXTERNAL LOCATION} | i64 |
16911684
| main.rs:1162:17:1162:24 | my_thing | | file://:0:0:0:0 | & |
1692-
| main.rs:1162:17:1162:24 | my_thing | &T | file://:0:0:0:0 | & |
16931685
| main.rs:1162:17:1162:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1694-
| main.rs:1162:17:1162:24 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1695-
| main.rs:1162:17:1162:43 | my_thing.method_on_borrow() | | {EXTERNAL LOCATION} | i64 |
16961686
| main.rs:1163:18:1163:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1697-
| main.rs:1163:26:1163:26 | a | | {EXTERNAL LOCATION} | i64 |
16981687
| main.rs:1166:13:1166:20 | my_thing | | file://:0:0:0:0 | & |
16991688
| main.rs:1166:13:1166:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
17001689
| main.rs:1166:24:1166:39 | &... | | file://:0:0:0:0 | & |
17011690
| main.rs:1166:24:1166:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
17021691
| main.rs:1166:25:1166:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
17031692
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i32 |
17041693
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i64 |
1705-
| main.rs:1167:13:1167:13 | a | | {EXTERNAL LOCATION} | i64 |
17061694
| main.rs:1167:17:1167:24 | my_thing | | file://:0:0:0:0 | & |
17071695
| main.rs:1167:17:1167:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1708-
| main.rs:1167:17:1167:47 | my_thing.method_not_on_borrow() | | {EXTERNAL LOCATION} | i64 |
17091696
| main.rs:1168:18:1168:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1710-
| main.rs:1168:26:1168:26 | a | | {EXTERNAL LOCATION} | i64 |
17111697
| main.rs:1175:16:1175:20 | SelfParam | | file://:0:0:0:0 | & |
17121698
| main.rs:1175:16:1175:20 | SelfParam | &T | main.rs:1173:5:1181:5 | Self [trait MyTrait] |
17131699
| main.rs:1178:16:1178:20 | SelfParam | | file://:0:0:0:0 | & |

0 commit comments

Comments
 (0)