Skip to content

Commit 68a37f9

Browse files
committed
Rust: Add something similar as a type inference test case.
1 parent 33ea822 commit 68a37f9

File tree

2 files changed

+96
-36
lines changed

2 files changed

+96
-36
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,41 @@ mod tuples {
22562256
}
22572257
}
22582258

2259+
mod closures {
2260+
struct Row {
2261+
data: i64,
2262+
}
2263+
2264+
impl Row {
2265+
fn get(&self) -> i64 {
2266+
self.data // $ fieldof=Row
2267+
}
2268+
}
2269+
2270+
struct Table {
2271+
rows: Vec<Row>,
2272+
}
2273+
2274+
impl Table {
2275+
fn new() -> Self {
2276+
Table { rows: Vec::new() } // $ method=new
2277+
}
2278+
2279+
fn count_with(&self, property: impl Fn(Row) -> bool) -> i64 {
2280+
0 // (not implemented)
2281+
}
2282+
}
2283+
2284+
pub fn f() {
2285+
let table = Table::new(); // $ method=new type=table:Table
2286+
let result = table.count_with(|row| // $ type=result:i64
2287+
{
2288+
let v = row.get(); // $ MISSING: method=get type=v:i64
2289+
v > 0 // $ MISSING: method=gt
2290+
}); // $ method=count_with
2291+
}
2292+
}
2293+
22592294
fn main() {
22602295
field_access::f(); // $ method=f
22612296
method_impl::f(); // $ method=f
@@ -2283,6 +2318,7 @@ fn main() {
22832318
macros::f(); // $ method=f
22842319
method_determined_by_argument_type::f(); // $ method=f
22852320
tuples::f(); // $ method=f
2321+
closures::f(); // $ method=f
22862322
dereference::test(); // $ method=test
22872323
}
22882324

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

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,40 +3639,64 @@ inferType
36393639
| main.rs:2236:14:2236:18 | S1 {...} | | main.rs:2232:5:2232:16 | S1 |
36403640
| main.rs:2236:21:2236:25 | S1 {...} | | main.rs:2232:5:2232:16 | S1 |
36413641
| main.rs:2238:16:2238:19 | SelfParam | | main.rs:2232:5:2232:16 | S1 |
3642-
| main.rs:2261:5:2261:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
3643-
| main.rs:2262:5:2262:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
3644-
| main.rs:2262:20:2262:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3645-
| main.rs:2262:41:2262:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3646-
| main.rs:2278:5:2278:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
3647-
| main.rs:2290:44:2309:5 | { ... } | | {EXTERNAL LOCATION} | Option |
3648-
| main.rs:2291:13:2291:17 | value | | {EXTERNAL LOCATION} | Option |
3649-
| main.rs:2291:13:2291:17 | value | T | {EXTERNAL LOCATION} | i32 |
3650-
| main.rs:2291:21:2291:28 | Some(...) | | {EXTERNAL LOCATION} | Option |
3651-
| main.rs:2291:21:2291:28 | Some(...) | T | {EXTERNAL LOCATION} | i32 |
3652-
| main.rs:2291:26:2291:27 | 42 | | {EXTERNAL LOCATION} | i32 |
3653-
| main.rs:2292:29:2292:33 | value | | {EXTERNAL LOCATION} | Option |
3654-
| main.rs:2292:29:2292:33 | value | T | {EXTERNAL LOCATION} | i32 |
3655-
| main.rs:2294:22:2294:29 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3656-
| main.rs:2294:22:2294:29 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3657-
| main.rs:2296:15:2296:19 | value | | {EXTERNAL LOCATION} | Option |
3658-
| main.rs:2296:15:2296:19 | value | T | {EXTERNAL LOCATION} | i32 |
3659-
| main.rs:2299:26:2299:33 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3660-
| main.rs:2299:26:2299:33 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3661-
| main.rs:2303:13:2303:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3662-
| main.rs:2303:20:2303:24 | value | | {EXTERNAL LOCATION} | Option |
3663-
| main.rs:2303:20:2303:24 | value | T | {EXTERNAL LOCATION} | i32 |
3664-
| main.rs:2303:20:2303:33 | value.unwrap() | | {EXTERNAL LOCATION} | i32 |
3665-
| main.rs:2304:13:2304:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3666-
| main.rs:2304:20:2304:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3667-
| main.rs:2305:18:2305:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3668-
| main.rs:2305:18:2305:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3669-
| main.rs:2305:20:2305:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3670-
| main.rs:2306:13:2306:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3671-
| main.rs:2306:20:2306:24 | value | | {EXTERNAL LOCATION} | Option |
3672-
| main.rs:2306:20:2306:24 | value | T | {EXTERNAL LOCATION} | i32 |
3673-
| main.rs:2306:20:2306:25 | TryExpr | | {EXTERNAL LOCATION} | i32 |
3674-
| main.rs:2307:18:2307:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3675-
| main.rs:2307:18:2307:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3676-
| main.rs:2307:20:2307:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3677-
| main.rs:2308:9:2308:12 | None | | {EXTERNAL LOCATION} | Option |
3642+
| main.rs:2265:16:2265:20 | SelfParam | | file://:0:0:0:0 | & |
3643+
| main.rs:2265:16:2265:20 | SelfParam | &T | main.rs:2260:5:2262:5 | Row |
3644+
| main.rs:2265:30:2267:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3645+
| main.rs:2266:13:2266:16 | self | | file://:0:0:0:0 | & |
3646+
| main.rs:2266:13:2266:16 | self | &T | main.rs:2260:5:2262:5 | Row |
3647+
| main.rs:2266:13:2266:21 | self.data | | {EXTERNAL LOCATION} | i64 |
3648+
| main.rs:2275:26:2277:9 | { ... } | | main.rs:2270:5:2272:5 | Table |
3649+
| main.rs:2276:13:2276:38 | Table {...} | | main.rs:2270:5:2272:5 | Table |
3650+
| main.rs:2276:27:2276:36 | ...::new(...) | | {EXTERNAL LOCATION} | Vec |
3651+
| main.rs:2276:27:2276:36 | ...::new(...) | A | {EXTERNAL LOCATION} | Global |
3652+
| main.rs:2276:27:2276:36 | ...::new(...) | T | main.rs:2260:5:2262:5 | Row |
3653+
| main.rs:2279:23:2279:27 | SelfParam | | file://:0:0:0:0 | & |
3654+
| main.rs:2279:23:2279:27 | SelfParam | &T | main.rs:2270:5:2272:5 | Table |
3655+
| main.rs:2279:30:2279:37 | property | | main.rs:2279:40:2279:59 | ImplTraitTypeRepr |
3656+
| main.rs:2279:69:2281:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
3657+
| main.rs:2279:69:2281:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3658+
| main.rs:2280:13:2280:13 | 0 | | {EXTERNAL LOCATION} | i32 |
3659+
| main.rs:2280:13:2280:13 | 0 | | {EXTERNAL LOCATION} | i64 |
3660+
| main.rs:2285:13:2285:17 | table | | main.rs:2270:5:2272:5 | Table |
3661+
| main.rs:2285:21:2285:32 | ...::new(...) | | main.rs:2270:5:2272:5 | Table |
3662+
| main.rs:2286:13:2286:18 | result | | {EXTERNAL LOCATION} | i64 |
3663+
| main.rs:2286:22:2286:26 | table | | main.rs:2270:5:2272:5 | Table |
3664+
| main.rs:2286:22:2290:14 | table.count_with(...) | | {EXTERNAL LOCATION} | i64 |
3665+
| main.rs:2289:21:2289:21 | 0 | | {EXTERNAL LOCATION} | i32 |
3666+
| main.rs:2296:5:2296:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
3667+
| main.rs:2297:5:2297:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
3668+
| main.rs:2297:20:2297:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3669+
| main.rs:2297:41:2297:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3670+
| main.rs:2313:5:2313:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
3671+
| main.rs:2326:44:2345:5 | { ... } | | {EXTERNAL LOCATION} | Option |
3672+
| main.rs:2327:13:2327:17 | value | | {EXTERNAL LOCATION} | Option |
3673+
| main.rs:2327:13:2327:17 | value | T | {EXTERNAL LOCATION} | i32 |
3674+
| main.rs:2327:21:2327:28 | Some(...) | | {EXTERNAL LOCATION} | Option |
3675+
| main.rs:2327:21:2327:28 | Some(...) | T | {EXTERNAL LOCATION} | i32 |
3676+
| main.rs:2327:26:2327:27 | 42 | | {EXTERNAL LOCATION} | i32 |
3677+
| main.rs:2328:29:2328:33 | value | | {EXTERNAL LOCATION} | Option |
3678+
| main.rs:2328:29:2328:33 | value | T | {EXTERNAL LOCATION} | i32 |
3679+
| main.rs:2330:22:2330:29 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3680+
| main.rs:2330:22:2330:29 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3681+
| main.rs:2332:15:2332:19 | value | | {EXTERNAL LOCATION} | Option |
3682+
| main.rs:2332:15:2332:19 | value | T | {EXTERNAL LOCATION} | i32 |
3683+
| main.rs:2335:26:2335:33 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3684+
| main.rs:2335:26:2335:33 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3685+
| main.rs:2339:13:2339:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3686+
| main.rs:2339:20:2339:24 | value | | {EXTERNAL LOCATION} | Option |
3687+
| main.rs:2339:20:2339:24 | value | T | {EXTERNAL LOCATION} | i32 |
3688+
| main.rs:2339:20:2339:33 | value.unwrap() | | {EXTERNAL LOCATION} | i32 |
3689+
| main.rs:2340:13:2340:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3690+
| main.rs:2340:20:2340:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3691+
| main.rs:2341:18:2341:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3692+
| main.rs:2341:18:2341:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3693+
| main.rs:2341:20:2341:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3694+
| main.rs:2342:13:2342:16 | mesg | | {EXTERNAL LOCATION} | i32 |
3695+
| main.rs:2342:20:2342:24 | value | | {EXTERNAL LOCATION} | Option |
3696+
| main.rs:2342:20:2342:24 | value | T | {EXTERNAL LOCATION} | i32 |
3697+
| main.rs:2342:20:2342:25 | TryExpr | | {EXTERNAL LOCATION} | i32 |
3698+
| main.rs:2343:18:2343:25 | "{mesg}\\n" | | file://:0:0:0:0 | & |
3699+
| main.rs:2343:18:2343:25 | "{mesg}\\n" | &T | {EXTERNAL LOCATION} | str |
3700+
| main.rs:2343:20:2343:23 | mesg | | {EXTERNAL LOCATION} | i32 |
3701+
| main.rs:2344:9:2344:12 | None | | {EXTERNAL LOCATION} | Option |
36783702
testFailures

0 commit comments

Comments
 (0)