Skip to content

Commit 8ebebf0

Browse files
committed
Rust: Add type inference test with associated type that collides with type parameter
1 parent ac6715f commit 8ebebf0

File tree

2 files changed

+2626
-2578
lines changed

2 files changed

+2626
-2578
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,23 @@ mod type_aliases {
10421042

10431043
type S7<T7> = Result<S6<T7>, S1>;
10441044

1045+
struct GenS<GenT>(GenT);
1046+
1047+
trait TraitWithAssocType {
1048+
type Output;
1049+
fn get_input(self) -> Self::Output;
1050+
}
1051+
1052+
impl<Output> TraitWithAssocType for GenS<Output> {
1053+
// This is not a recursive type, the `Output` on the right-hand side
1054+
// refers to the type parameter of the impl block just above.
1055+
type Output = Result<Output, Output>;
1056+
1057+
fn get_input(self) -> Self::Output {
1058+
Ok(self.0) // $ fieldof=GenS type=Ok(...):Result type=Ok(...):T.Output type=Ok(...):E.Output
1059+
}
1060+
}
1061+
10451062
pub fn f() {
10461063
// Type can be inferred from the constructor
10471064
let p1: MyPair = PairOption::PairBoth(S1, S2);
@@ -1062,6 +1079,8 @@ mod type_aliases {
10621079
g(PairOption::PairSnd(PairOption::PairSnd(S3))); // $ target=g
10631080

10641081
let x: S7<S2>; // $ type=x:Result $ type=x:E.S1 $ type=x:T.S4 $ type=x:T.T41.S2 $ type=x:T.T42.S5 $ type=x:T.T42.T5.S2
1082+
1083+
let y = GenS(true).get_input(); // $ type=y:Result type=y:T.bool type=y:E.bool target=get_input
10651084
}
10661085
}
10671086

@@ -2006,7 +2025,11 @@ mod method_determined_by_argument_type {
20062025

20072026
// MyAdd<bool>::my_add
20082027
fn my_add(self, value: bool) -> Self {
2009-
if value { 1 } else { 0 }
2028+
if value {
2029+
1
2030+
} else {
2031+
0
2032+
}
20102033
}
20112034
}
20122035

@@ -2057,7 +2080,11 @@ mod method_determined_by_argument_type {
20572080
impl MyFrom<bool> for i64 {
20582081
// MyFrom<bool>::my_from
20592082
fn my_from(value: bool) -> Self {
2060-
if value { 1 } else { 0 }
2083+
if value {
2084+
1
2085+
} else {
2086+
0
2087+
}
20612088
}
20622089
}
20632090

@@ -2407,7 +2434,7 @@ mod closures {
24072434
Some(1).map(|x| {
24082435
let x = x; // $ MISSING: type=x:i32
24092436
println!("{x}");
2410-
}); // $ target=map
2437+
}); // $ target=map
24112438

24122439
let table = Table::new(); // $ target=new type=table:Table
24132440
let result = table.count_with(|row| // $ type=result:i64

0 commit comments

Comments
 (0)