Skip to content

Commit 82b1e77

Browse files
committed
Rust: Add more type inference tests
1 parent 2ad8e2b commit 82b1e77

File tree

2 files changed

+404
-5
lines changed

2 files changed

+404
-5
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,52 @@ pub mod path_buf {
26962696
}
26972697
}
26982698

2699+
mod if_expr {
2700+
pub trait MyTrait<T: Sized> {
2701+
fn m(&self) -> T;
2702+
}
2703+
2704+
#[derive(Default)]
2705+
struct S<T>(T);
2706+
2707+
impl MyTrait<i32> for S<i32> {
2708+
fn m(&self) -> i32 {
2709+
self.0 // $ fieldof=S
2710+
}
2711+
}
2712+
2713+
impl MyTrait<i32> for S<S<i32>> {
2714+
fn m(&self) -> i32 {
2715+
self.0 .0 // $ fieldof=S
2716+
}
2717+
}
2718+
2719+
impl<T: Copy> S<T> {
2720+
fn m2(&self) -> S<S<T>> {
2721+
S(S(self.0)) // $ fieldof=S
2722+
}
2723+
}
2724+
2725+
pub fn f(b: bool) -> Box<dyn MyTrait<i32>> {
2726+
let x = if b {
2727+
let y = Default::default(); // $ target=default
2728+
y // $ type=y:T.i32
2729+
} else {
2730+
S(2)
2731+
};
2732+
2733+
// This code exhibits an explosion in type inference when type information is propagated
2734+
// from an `if` expression to its branches.
2735+
let x = S(1);
2736+
if b {
2737+
let x = x.m2(); // $ target=m2
2738+
Box::new(x) // $ target=new
2739+
} else {
2740+
Box::new(x) // $ target=new
2741+
}
2742+
}
2743+
}
2744+
26992745
mod blanket_impl;
27002746
mod closure;
27012747
mod dereference;
@@ -2733,4 +2779,5 @@ fn main() {
27332779
pattern_matching::test_all_patterns(); // $ target=test_all_patterns
27342780
pattern_matching_experimental::box_patterns(); // $ target=box_patterns
27352781
dyn_type::test(); // $ target=test
2782+
if_expr::f(true); // $ target=f
27362783
}

0 commit comments

Comments
 (0)