Skip to content

Commit 0c82b6d

Browse files
committed
Rust: Add more type inference tests
1 parent 1d7d45e commit 0c82b6d

File tree

2 files changed

+149
-67
lines changed

2 files changed

+149
-67
lines changed

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

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,76 @@ mod tuples {
23462346
}
23472347
}
23482348

2349+
pub mod pattern_matching {
2350+
struct MyRecordStruct<T1, T2> {
2351+
value1: T1,
2352+
value2: T2,
2353+
}
2354+
2355+
struct MyTupleStruct<T1, T2>(T1, T2);
2356+
2357+
enum MyEnum<T1, T2> {
2358+
Variant1 { value1: T1, value2: T2 },
2359+
Variant2(T2, T1),
2360+
}
2361+
2362+
pub fn f() -> Option<()> {
2363+
let value = Some(42);
2364+
if let Some(mesg) = value {
2365+
let mesg = mesg; // $ MISSING: type=mesg:i32
2366+
println!("{mesg}");
2367+
}
2368+
match value {
2369+
Some(mesg) => {
2370+
let mesg = mesg; // $ MISSING: type=mesg:i32
2371+
println!("{mesg}");
2372+
}
2373+
None => (),
2374+
};
2375+
let mesg = value.unwrap(); // $ method=unwrap
2376+
let mesg = mesg; // $ type=mesg:i32
2377+
println!("{mesg}");
2378+
let mesg = value?; // $ type=mesg:i32
2379+
println!("{mesg}");
2380+
2381+
let my_record_struct = MyRecordStruct {
2382+
value1: 42,
2383+
value2: false,
2384+
};
2385+
if let MyRecordStruct { value1, value2 } = my_record_struct {
2386+
let x = value1; // $ MISSING: type=x:i32
2387+
let y = value2; // $ MISSING: type=y:bool
2388+
();
2389+
}
2390+
2391+
let my_tuple_struct = MyTupleStruct(42, false);
2392+
if let MyTupleStruct(value1, value2) = my_tuple_struct {
2393+
let x = value1; // $ MISSING: type=x:i32
2394+
let y = value2; // $ MISSING: type=y:bool
2395+
();
2396+
}
2397+
2398+
let my_enum1 = MyEnum::Variant1 {
2399+
value1: 42,
2400+
value2: false,
2401+
};
2402+
match my_enum1 {
2403+
MyEnum::Variant1 { value1, value2 } => {
2404+
let x = value1; // $ MISSING: type=x:i32
2405+
let y = value2; // $ MISSING: type=y:bool
2406+
();
2407+
}
2408+
MyEnum::Variant2(value1, value2) => {
2409+
let x = value1; // $ MISSING: type=x:bool
2410+
let y = value2; // $ MISSING: type=y:i32
2411+
();
2412+
}
2413+
}
2414+
2415+
None
2416+
}
2417+
}
2418+
23492419
fn main() {
23502420
field_access::f(); // $ method=f
23512421
method_impl::f(); // $ method=f
@@ -2374,27 +2444,5 @@ fn main() {
23742444
method_determined_by_argument_type::f(); // $ method=f
23752445
tuples::f(); // $ method=f
23762446
dereference::test(); // $ method=test
2377-
}
2378-
2379-
pub mod unwrap {
2380-
pub fn test_unwrapping() -> Option<()> {
2381-
let value = Some(42);
2382-
if let Some(mesg) = value {
2383-
let mesg = mesg; // $ MISSING: type=mesg:i32
2384-
println!("{mesg}");
2385-
}
2386-
match value {
2387-
Some(mesg) => {
2388-
let mesg = mesg; // $ MISSING: type=mesg:i32
2389-
println!("{mesg}");
2390-
}
2391-
None => (),
2392-
};
2393-
let mesg = value.unwrap(); // $ method=unwrap
2394-
let mesg = mesg; // $ type=mesg:i32
2395-
println!("{mesg}");
2396-
let mesg = value?; // $ type=mesg:i32
2397-
println!("{mesg}");
2398-
None
2399-
}
2447+
pattern_matching::f(); // $ method=f
24002448
}

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

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4009,48 +4009,82 @@ inferType
40094009
| main.rs:2326:14:2326:18 | S1 {...} | | main.rs:2322:5:2322:16 | S1 |
40104010
| main.rs:2326:21:2326:25 | S1 {...} | | main.rs:2322:5:2322:16 | S1 |
40114011
| main.rs:2328:16:2328:19 | SelfParam | | main.rs:2322:5:2322:16 | S1 |
4012-
| main.rs:2351:5:2351:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
4013-
| main.rs:2352:5:2352:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
4014-
| main.rs:2352:20:2352:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4015-
| main.rs:2352:41:2352:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4016-
| main.rs:2368:5:2368:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
4017-
| main.rs:2380:44:2399:5 | { ... } | | {EXTERNAL LOCATION} | Option |
4018-
| main.rs:2381:13:2381:17 | value | | {EXTERNAL LOCATION} | Option |
4019-
| main.rs:2381:13:2381:17 | value | T | {EXTERNAL LOCATION} | i32 |
4020-
| main.rs:2381:21:2381:28 | Some(...) | | {EXTERNAL LOCATION} | Option |
4021-
| main.rs:2381:21:2381:28 | Some(...) | T | {EXTERNAL LOCATION} | i32 |
4022-
| main.rs:2381:26:2381:27 | 42 | | {EXTERNAL LOCATION} | i32 |
4023-
| main.rs:2382:29:2382:33 | value | | {EXTERNAL LOCATION} | Option |
4024-
| main.rs:2382:29:2382:33 | value | T | {EXTERNAL LOCATION} | i32 |
4025-
| main.rs:2384:22:2384:29 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4026-
| main.rs:2384:22:2384:29 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4027-
| main.rs:2384:22:2384:29 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4028-
| main.rs:2384:22:2384:29 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4029-
| main.rs:2386:15:2386:19 | value | | {EXTERNAL LOCATION} | Option |
4030-
| main.rs:2386:15:2386:19 | value | T | {EXTERNAL LOCATION} | i32 |
4031-
| main.rs:2389:26:2389:33 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4032-
| main.rs:2389:26:2389:33 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4033-
| main.rs:2389:26:2389:33 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4034-
| main.rs:2389:26:2389:33 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4035-
| main.rs:2393:13:2393:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4036-
| main.rs:2393:20:2393:24 | value | | {EXTERNAL LOCATION} | Option |
4037-
| main.rs:2393:20:2393:24 | value | T | {EXTERNAL LOCATION} | i32 |
4038-
| main.rs:2393:20:2393:33 | value.unwrap() | | {EXTERNAL LOCATION} | i32 |
4039-
| main.rs:2394:13:2394:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4040-
| main.rs:2394:20:2394:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4041-
| main.rs:2395:18:2395:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4042-
| main.rs:2395:18:2395:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4043-
| main.rs:2395:18:2395:25 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4044-
| main.rs:2395:18:2395:25 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4045-
| main.rs:2395:20:2395:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4046-
| main.rs:2396:13:2396:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4047-
| main.rs:2396:20:2396:24 | value | | {EXTERNAL LOCATION} | Option |
4048-
| main.rs:2396:20:2396:24 | value | T | {EXTERNAL LOCATION} | i32 |
4049-
| main.rs:2396:20:2396:25 | TryExpr | | {EXTERNAL LOCATION} | i32 |
4050-
| main.rs:2397:18:2397:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4051-
| main.rs:2397:18:2397:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4052-
| main.rs:2397:18:2397:25 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4053-
| main.rs:2397:18:2397:25 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4054-
| main.rs:2397:20:2397:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4055-
| main.rs:2398:9:2398:12 | None | | {EXTERNAL LOCATION} | Option |
4012+
| main.rs:2362:30:2416:5 | { ... } | | {EXTERNAL LOCATION} | Option |
4013+
| main.rs:2363:13:2363:17 | value | | {EXTERNAL LOCATION} | Option |
4014+
| main.rs:2363:13:2363:17 | value | T | {EXTERNAL LOCATION} | i32 |
4015+
| main.rs:2363:21:2363:28 | Some(...) | | {EXTERNAL LOCATION} | Option |
4016+
| main.rs:2363:21:2363:28 | Some(...) | T | {EXTERNAL LOCATION} | i32 |
4017+
| main.rs:2363:26:2363:27 | 42 | | {EXTERNAL LOCATION} | i32 |
4018+
| main.rs:2364:29:2364:33 | value | | {EXTERNAL LOCATION} | Option |
4019+
| main.rs:2364:29:2364:33 | value | T | {EXTERNAL LOCATION} | i32 |
4020+
| main.rs:2366:22:2366:29 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4021+
| main.rs:2366:22:2366:29 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4022+
| main.rs:2366:22:2366:29 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4023+
| main.rs:2366:22:2366:29 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4024+
| main.rs:2368:15:2368:19 | value | | {EXTERNAL LOCATION} | Option |
4025+
| main.rs:2368:15:2368:19 | value | T | {EXTERNAL LOCATION} | i32 |
4026+
| main.rs:2371:26:2371:33 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4027+
| main.rs:2371:26:2371:33 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4028+
| main.rs:2371:26:2371:33 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4029+
| main.rs:2371:26:2371:33 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4030+
| main.rs:2375:13:2375:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4031+
| main.rs:2375:20:2375:24 | value | | {EXTERNAL LOCATION} | Option |
4032+
| main.rs:2375:20:2375:24 | value | T | {EXTERNAL LOCATION} | i32 |
4033+
| main.rs:2375:20:2375:33 | value.unwrap() | | {EXTERNAL LOCATION} | i32 |
4034+
| main.rs:2376:13:2376:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4035+
| main.rs:2376:20:2376:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4036+
| main.rs:2377:18:2377:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4037+
| main.rs:2377:18:2377:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4038+
| main.rs:2377:18:2377:25 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4039+
| main.rs:2377:18:2377:25 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4040+
| main.rs:2377:20:2377:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4041+
| main.rs:2378:13:2378:16 | mesg | | {EXTERNAL LOCATION} | i32 |
4042+
| main.rs:2378:20:2378:24 | value | | {EXTERNAL LOCATION} | Option |
4043+
| main.rs:2378:20:2378:24 | value | T | {EXTERNAL LOCATION} | i32 |
4044+
| main.rs:2378:20:2378:25 | TryExpr | | {EXTERNAL LOCATION} | i32 |
4045+
| main.rs:2379:18:2379:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
4046+
| main.rs:2379:18:2379:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
4047+
| main.rs:2379:18:2379:25 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
4048+
| main.rs:2379:18:2379:25 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
4049+
| main.rs:2379:20:2379:23 | mesg | | {EXTERNAL LOCATION} | i32 |
4050+
| main.rs:2381:13:2381:28 | my_record_struct | | main.rs:2350:5:2353:5 | MyRecordStruct |
4051+
| main.rs:2381:13:2381:28 | my_record_struct | T1 | {EXTERNAL LOCATION} | i32 |
4052+
| main.rs:2381:13:2381:28 | my_record_struct | T2 | {EXTERNAL LOCATION} | bool |
4053+
| main.rs:2381:32:2384:9 | MyRecordStruct {...} | | main.rs:2350:5:2353:5 | MyRecordStruct |
4054+
| main.rs:2381:32:2384:9 | MyRecordStruct {...} | T1 | {EXTERNAL LOCATION} | i32 |
4055+
| main.rs:2381:32:2384:9 | MyRecordStruct {...} | T2 | {EXTERNAL LOCATION} | bool |
4056+
| main.rs:2382:21:2382:22 | 42 | | {EXTERNAL LOCATION} | i32 |
4057+
| main.rs:2383:21:2383:25 | false | | {EXTERNAL LOCATION} | bool |
4058+
| main.rs:2385:52:2385:67 | my_record_struct | | main.rs:2350:5:2353:5 | MyRecordStruct |
4059+
| main.rs:2385:52:2385:67 | my_record_struct | T1 | {EXTERNAL LOCATION} | i32 |
4060+
| main.rs:2385:52:2385:67 | my_record_struct | T2 | {EXTERNAL LOCATION} | bool |
4061+
| main.rs:2391:13:2391:27 | my_tuple_struct | | main.rs:2355:5:2355:41 | MyTupleStruct |
4062+
| main.rs:2391:13:2391:27 | my_tuple_struct | T1 | {EXTERNAL LOCATION} | i32 |
4063+
| main.rs:2391:13:2391:27 | my_tuple_struct | T2 | {EXTERNAL LOCATION} | bool |
4064+
| main.rs:2391:31:2391:54 | MyTupleStruct(...) | | main.rs:2355:5:2355:41 | MyTupleStruct |
4065+
| main.rs:2391:31:2391:54 | MyTupleStruct(...) | T1 | {EXTERNAL LOCATION} | i32 |
4066+
| main.rs:2391:31:2391:54 | MyTupleStruct(...) | T2 | {EXTERNAL LOCATION} | bool |
4067+
| main.rs:2391:45:2391:46 | 42 | | {EXTERNAL LOCATION} | i32 |
4068+
| main.rs:2391:49:2391:53 | false | | {EXTERNAL LOCATION} | bool |
4069+
| main.rs:2392:48:2392:62 | my_tuple_struct | | main.rs:2355:5:2355:41 | MyTupleStruct |
4070+
| main.rs:2392:48:2392:62 | my_tuple_struct | T1 | {EXTERNAL LOCATION} | i32 |
4071+
| main.rs:2392:48:2392:62 | my_tuple_struct | T2 | {EXTERNAL LOCATION} | bool |
4072+
| main.rs:2398:13:2398:20 | my_enum1 | | main.rs:2357:5:2360:5 | MyEnum |
4073+
| main.rs:2398:13:2398:20 | my_enum1 | T1 | {EXTERNAL LOCATION} | i32 |
4074+
| main.rs:2398:13:2398:20 | my_enum1 | T2 | {EXTERNAL LOCATION} | bool |
4075+
| main.rs:2398:24:2401:9 | ...::Variant1 {...} | | main.rs:2357:5:2360:5 | MyEnum |
4076+
| main.rs:2398:24:2401:9 | ...::Variant1 {...} | T1 | {EXTERNAL LOCATION} | i32 |
4077+
| main.rs:2398:24:2401:9 | ...::Variant1 {...} | T2 | {EXTERNAL LOCATION} | bool |
4078+
| main.rs:2399:21:2399:22 | 42 | | {EXTERNAL LOCATION} | i32 |
4079+
| main.rs:2400:21:2400:25 | false | | {EXTERNAL LOCATION} | bool |
4080+
| main.rs:2402:15:2402:22 | my_enum1 | | main.rs:2357:5:2360:5 | MyEnum |
4081+
| main.rs:2402:15:2402:22 | my_enum1 | T1 | {EXTERNAL LOCATION} | i32 |
4082+
| main.rs:2402:15:2402:22 | my_enum1 | T2 | {EXTERNAL LOCATION} | bool |
4083+
| main.rs:2415:9:2415:12 | None | | {EXTERNAL LOCATION} | Option |
4084+
| main.rs:2421:5:2421:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
4085+
| main.rs:2422:5:2422:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
4086+
| main.rs:2422:20:2422:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4087+
| main.rs:2422:41:2422:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4088+
| main.rs:2438:5:2438:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
4089+
| main.rs:2447:5:2447:25 | ...::f(...) | | {EXTERNAL LOCATION} | Option |
40564090
testFailures

0 commit comments

Comments
 (0)