Skip to content

Commit df3c851

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

File tree

2 files changed

+378
-5
lines changed

2 files changed

+378
-5
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,44 @@ 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+
struct S<T>(T);
2705+
2706+
impl MyTrait<i32> for S<i32> {
2707+
fn m(&self) -> i32 {
2708+
self.0 // $ fieldof=S
2709+
}
2710+
}
2711+
2712+
impl MyTrait<i32> for S<S<i32>> {
2713+
fn m(&self) -> i32 {
2714+
self.0 .0 // $ fieldof=S
2715+
}
2716+
}
2717+
2718+
impl<T: Copy> S<T> {
2719+
fn m2(&self) -> S<S<T>> {
2720+
S(S(self.0)) // $ fieldof=S
2721+
}
2722+
}
2723+
2724+
pub fn f(b: bool) -> Box<dyn MyTrait<i32>> {
2725+
// This code exhibits an explosion in type inference when type information is propagated
2726+
// from an `if` expression to its branches.
2727+
let x = S(1);
2728+
if b {
2729+
let x = x.m2(); // $ target=m2
2730+
Box::new(x) // $ target=new
2731+
} else {
2732+
Box::new(x) // $ target=new
2733+
}
2734+
}
2735+
}
2736+
26992737
mod blanket_impl;
27002738
mod closure;
27012739
mod dereference;
@@ -2733,4 +2771,5 @@ fn main() {
27332771
pattern_matching::test_all_patterns(); // $ target=test_all_patterns
27342772
pattern_matching_experimental::box_patterns(); // $ target=box_patterns
27352773
dyn_type::test(); // $ target=test
2774+
if_expr::f(true); // $ target=f
27362775
}

0 commit comments

Comments
 (0)