Skip to content

Commit 14a362d

Browse files
authored
Merge pull request #20029 from github/aibaars/more-pattern-tests
Rust: add more type inference tests for patterns and a simple one for a closure call
2 parents 344535b + 519905e commit 14a362d

File tree

4 files changed

+2239
-436
lines changed

4 files changed

+2239
-436
lines changed

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

Lines changed: 29 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
#![feature(box_patterns)]
12
mod field_access {
23
#[derive(Debug)]
34
struct S;
4-
55
#[derive(Debug)]
66
struct MyThing {
77
a: S,
@@ -2351,139 +2351,39 @@ mod tuples {
23512351
}
23522352
}
23532353

2354-
pub mod pattern_matching {
2355-
struct MyRecordStruct<T1, T2> {
2356-
value1: T1,
2357-
value2: T2,
2358-
}
2359-
2360-
struct MyTupleStruct<T1, T2>(T1, T2);
2361-
2362-
enum MyEnum<T1, T2> {
2363-
Variant1 { value1: T1, value2: T2 },
2364-
Variant2(T2, T1),
2365-
}
2354+
pub mod pattern_matching;
2355+
pub mod pattern_matching_experimental {
2356+
pub fn box_patterns() {
2357+
let boxed_value = Box::new(100i32); // $ method=new
23662358

2367-
pub fn f() -> Option<()> {
2368-
let value = Some(42);
2369-
if let Some(mesg) = value {
2370-
let mesg = mesg; // $ type=mesg:i32
2371-
println!("{mesg}");
2372-
}
2373-
match value {
2374-
Some(mesg) => {
2375-
let mesg = mesg; // $ type=mesg:i32
2376-
println!("{mesg}");
2359+
// BoxPat - Box patterns (requires feature flag)
2360+
match boxed_value {
2361+
box 100 => {
2362+
println!("Boxed 100");
23772363
}
2378-
None => (),
2379-
};
2380-
let mesg = value.unwrap(); // $ method=unwrap
2381-
let mesg = mesg; // $ type=mesg:i32
2382-
println!("{mesg}");
2383-
let mesg = value?; // $ type=mesg:i32
2384-
println!("{mesg}");
2385-
2386-
let value2 = &Some(42);
2387-
if let &Some(mesg) = value2 {
2388-
let mesg = mesg; // $ type=mesg:i32
2389-
println!("{mesg}");
2390-
}
2391-
2392-
let value3 = 42;
2393-
if let ref mesg = value3 {
2394-
let mesg = mesg; // $ type=mesg:&T.i32
2395-
println!("{mesg}");
2396-
}
2397-
2398-
let value4 = Some(42);
2399-
if let Some(ref mesg) = value4 {
2400-
let mesg = mesg; // $ type=mesg:&T.i32
2401-
println!("{mesg}");
2402-
}
2403-
2404-
let ref value5 = 42;
2405-
let x = value5; // $ type=x:&T.i32
2406-
2407-
let my_record_struct = MyRecordStruct {
2408-
value1: 42,
2409-
value2: false,
2410-
};
2411-
if let MyRecordStruct { value1, value2 } = my_record_struct {
2412-
let x = value1; // $ type=x:i32
2413-
let y = value2; // $ type=y:bool
2414-
();
2415-
}
2416-
2417-
let my_tuple_struct = MyTupleStruct(42, false);
2418-
if let MyTupleStruct(value1, value2) = my_tuple_struct {
2419-
let x = value1; // $ type=x:i32
2420-
let y = value2; // $ type=y:bool
2421-
();
2422-
}
2423-
2424-
let my_enum1 = MyEnum::Variant1 {
2425-
value1: 42,
2426-
value2: false,
2427-
};
2428-
match my_enum1 {
2429-
MyEnum::Variant1 { value1, value2 } => {
2430-
let x = value1; // $ type=x:i32
2431-
let y = value2; // $ type=y:bool
2432-
();
2433-
}
2434-
MyEnum::Variant2(value1, value2) => {
2435-
let x = value1; // $ type=x:bool
2436-
let y = value2; // $ type=y:i32
2437-
();
2364+
box x => {
2365+
let unboxed = x; // $ MISSING: type=unboxed:i32
2366+
println!("Boxed value: {}", unboxed);
24382367
}
24392368
}
24402369

2441-
let my_nested_enum = MyEnum::Variant2(
2442-
false,
2443-
MyRecordStruct {
2444-
value1: 42,
2445-
value2: "string",
2446-
},
2447-
);
2448-
2449-
match my_nested_enum {
2450-
MyEnum::Variant2(
2451-
value1,
2452-
MyRecordStruct {
2453-
value1: x,
2454-
value2: y,
2455-
},
2456-
) => {
2457-
let a = value1; // $ type=a:bool
2458-
let b = x; // $ type=b:i32
2459-
let c = y; // $ type=c:&T.str
2460-
();
2370+
// Nested box pattern
2371+
let nested_box = Box::new(Box::new(42i32)); // $ method=new
2372+
match nested_box {
2373+
box box x => {
2374+
let nested_unboxed = x; // $ MISSING: type=nested_unboxed:i32
2375+
println!("Nested boxed: {}", nested_unboxed);
24612376
}
2462-
_ => (),
24632377
}
2378+
}
2379+
}
24642380

2465-
let opt1 = Some(Default::default()); // $ type=opt1:T.i32 method=default
2466-
#[rustfmt::skip]
2467-
let _ = if let Some::<i32>(x) = opt1
2468-
{
2469-
x; // $ type=x:i32
2470-
};
2471-
2472-
let opt2 = Some(Default::default()); // $ type=opt2:T.i32 method=default
2473-
#[rustfmt::skip]
2474-
let _ = if let Option::Some::<i32>(x) = opt2
2475-
{
2476-
x; // $ type=x:i32
2477-
};
2478-
2479-
let opt3 = Some(Default::default()); // $ type=opt3:T.i32 method=default
2480-
#[rustfmt::skip]
2481-
let _ = if let Option::<i32>::Some(x) = opt3
2482-
{
2483-
x; // $ type=x:i32
2484-
};
2485-
2486-
None
2381+
mod closures {
2382+
pub fn f() {
2383+
Some(1).map(|x| {
2384+
let x = x; // $ MISSING: type=x:i32
2385+
println!("{x}");
2386+
}); // $ method=map
24872387
}
24882388
}
24892389

@@ -2515,5 +2415,7 @@ fn main() {
25152415
method_determined_by_argument_type::f(); // $ method=f
25162416
tuples::f(); // $ method=f
25172417
dereference::test(); // $ method=test
2518-
pattern_matching::f(); // $ method=f
2418+
pattern_matching::test_all_patterns(); // $ method=test_all_patterns
2419+
pattern_matching_experimental::box_patterns(); // $ method=box_patterns
2420+
closures::f() // $ method=f
25192421
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
qltest_use_nightly: true

0 commit comments

Comments
 (0)