Skip to content

Commit a20fed8

Browse files
committed
Rust: Add type inference tests for impl trait types
1 parent 8238746 commit a20fed8

File tree

2 files changed

+1185
-1122
lines changed

2 files changed

+1185
-1122
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,10 @@ mod async_ {
18731873
}
18741874

18751875
mod impl_trait {
1876+
#[derive(Copy, Clone)]
18761877
struct S1;
18771878
struct S2;
1879+
struct S3<T3>(T3);
18781880

18791881
trait Trait1 {
18801882
fn f1(&self) {} // Trait1f1
@@ -1906,6 +1908,13 @@ mod impl_trait {
19061908
}
19071909
}
19081910

1911+
impl<T: Clone> MyTrait<T> for S3<T> {
1912+
fn get_a(&self) -> T {
1913+
let S3(t) = self;
1914+
t.clone()
1915+
}
1916+
}
1917+
19091918
fn get_a_my_trait() -> impl MyTrait<S2> {
19101919
S1
19111920
}
@@ -1914,6 +1923,14 @@ mod impl_trait {
19141923
t.get_a() // $ target=MyTrait::get_a
19151924
}
19161925

1926+
fn get_a_my_trait2<T: Clone>(x: T) -> impl MyTrait<T> {
1927+
S3(x)
1928+
}
1929+
1930+
fn get_a_my_trait3<T: Clone>(x: T) -> Option<impl MyTrait<T>> {
1931+
Some(S3(x))
1932+
}
1933+
19171934
fn uses_my_trait2<A>(t: impl MyTrait<A>) -> A {
19181935
t.get_a() // $ target=MyTrait::get_a
19191936
}
@@ -1927,6 +1944,9 @@ mod impl_trait {
19271944
let a = get_a_my_trait(); // $ target=get_a_my_trait
19281945
let c = uses_my_trait2(a); // $ type=c:S2 target=uses_my_trait2
19291946
let d = uses_my_trait2(S1); // $ type=d:S2 target=uses_my_trait2
1947+
let e = get_a_my_trait2(S1).get_a(); // $ target=get_a_my_trait2 target=MyTrait::get_a MISSING: type=e:S1
1948+
// For this function the `impl` type does not appear in the root of the return type
1949+
let f = get_a_my_trait3(S1).unwrap().get_a(); // $ target=get_a_my_trait3 target=unwrap MISSING: target=MyTrait::get_a type=f:S1
19301950
}
19311951
}
19321952

@@ -2385,7 +2405,7 @@ mod tuples {
23852405

23862406
let pair = [1, 1].into(); // $ type=pair:(T_2) type=pair:0(2).i32 type=pair:1(2).i32 MISSING: target=into
23872407
match pair {
2388-
(0,0) => print!("unexpected"),
2408+
(0, 0) => print!("unexpected"),
23892409
_ => print!("expected"),
23902410
}
23912411
let x = pair.0; // $ type=x:i32

0 commit comments

Comments
 (0)