Skip to content

Commit 1b2f160

Browse files
committed
Rust: Add type inference tests for associated types
1 parent b1ee795 commit 1b2f160

File tree

4 files changed

+3387
-3174
lines changed

4 files changed

+3387
-3174
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
multipleCallTargets
22
| dereference.rs:61:15:61:24 | e1.deref() |
3-
| main.rs:2213:13:2213:31 | ...::from(...) |
4-
| main.rs:2214:13:2214:31 | ...::from(...) |
5-
| main.rs:2215:13:2215:31 | ...::from(...) |
6-
| main.rs:2221:13:2221:31 | ...::from(...) |
7-
| main.rs:2222:13:2222:31 | ...::from(...) |
8-
| main.rs:2223:13:2223:31 | ...::from(...) |
3+
| main.rs:2253:13:2253:31 | ...::from(...) |
4+
| main.rs:2254:13:2254:31 | ...::from(...) |
5+
| main.rs:2255:13:2255:31 | ...::from(...) |
6+
| main.rs:2261:13:2261:31 | ...::from(...) |
7+
| main.rs:2262:13:2262:31 | ...::from(...) |
8+
| main.rs:2263:13:2263:31 | ...::from(...) |

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ trait GenericGet<A> {
1212
fn get(&self) -> A;
1313
}
1414

15+
trait AssocTrait<GP> {
16+
type AP;
17+
// AssocTrait::get
18+
fn get(&self) -> (GP, Self::AP);
19+
}
20+
1521
#[derive(Clone, Debug)]
1622
struct MyStruct {
1723
value: i32,
@@ -36,6 +42,17 @@ impl<A: Clone + Debug> GenericGet<A> for GenStruct<A> {
3642
}
3743
}
3844

45+
impl<GGP> AssocTrait<GGP> for GenStruct<GGP>
46+
where
47+
GGP: Clone + Debug,
48+
{
49+
type AP = bool;
50+
// GenStruct<GGP>::get
51+
fn get(&self) -> (GGP, bool) {
52+
(self.value.clone(), true) // $ fieldof=GenStruct target=clone
53+
}
54+
}
55+
3956
fn get_a<A, G: GenericGet<A> + ?Sized>(a: &G) -> A {
4057
a.get() // $ target=GenericGet::get
4158
}
@@ -58,10 +75,34 @@ fn test_poly_dyn_trait() {
5875
let _result = (*obj).get(); // $ target=deref target=GenericGet::get type=_result:bool
5976
}
6077

78+
fn assoc_dyn_get<A, B>(a: &dyn AssocTrait<A, AP = B>) -> (A, B) {
79+
a.get() // $ target=AssocTrait::get
80+
}
81+
82+
fn assoc_get<A, B, T: AssocTrait<A, AP = B> + ?Sized>(a: &T) -> (A, B) {
83+
a.get() // $ target=AssocTrait::get
84+
}
85+
86+
fn test_assoc_type(obj: &dyn AssocTrait<i64, AP = bool>) {
87+
let (
88+
_gp, // $ type=_gp:i64
89+
_ap, // $ MISSING: type=_ap:bool
90+
) = (*obj).get(); // $ target=deref target=AssocTrait::get
91+
let (
92+
_gp, // $ type=_gp:i64
93+
_ap, // $ MISSING: type=_ap:bool
94+
) = assoc_dyn_get(obj); // $ target=assoc_dyn_get
95+
let (
96+
_gp, // $ type=_gp:i64
97+
_ap, // $ MISSING: type=_ap:bool
98+
) = assoc_get(obj); // $ target=assoc_get
99+
}
100+
61101
pub fn test() {
62102
test_basic_dyn_trait(&MyStruct { value: 42 }); // $ target=test_basic_dyn_trait
63103
test_generic_dyn_trait(&GenStruct {
64104
value: "".to_string(),
65105
}); // $ target=test_generic_dyn_trait
66106
test_poly_dyn_trait(); // $ target=test_poly_dyn_trait
107+
test_assoc_type(&GenStruct { value: 100 }); // $ target=test_assoc_type
67108
}

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ mod function_trait_bounds {
653653
}
654654
}
655655

656-
mod trait_associated_type {
656+
mod associated_type_in_trait {
657657
#[derive(Debug)]
658658
struct Wrapper<A> {
659659
field: A,
@@ -803,6 +803,46 @@ mod trait_associated_type {
803803
}
804804
}
805805

806+
mod associated_type_in_supertrait {
807+
trait Supertrait {
808+
type Content;
809+
fn insert(content: Self::Content);
810+
}
811+
812+
trait Subtrait: Supertrait {
813+
// Subtrait::get_content
814+
fn get_content(&self) -> Self::Content;
815+
}
816+
817+
struct MyType<T>(T);
818+
819+
impl<T> Supertrait for MyType<T> {
820+
type Content = T;
821+
fn insert(_content: Self::Content) {
822+
println!("Inserting content: ");
823+
}
824+
}
825+
826+
impl<T: Clone> Subtrait for MyType<T> {
827+
// MyType::get_content
828+
fn get_content(&self) -> Self::Content {
829+
(*self).0.clone() // $ fieldof=MyType target=clone target=deref
830+
}
831+
}
832+
833+
fn get_content<T: Subtrait>(item: &T) -> T::Content {
834+
item.get_content() // $ target=Subtrait::get_content
835+
}
836+
837+
fn test() {
838+
let item1 = MyType(42i64);
839+
let _content1 = item1.get_content(); // $ target=MyType::get_content MISSING: type=_content1:i64
840+
841+
let item2 = MyType(true);
842+
let _content2 = get_content(&item2); // $ target=get_content MISSING: type=_content2:bool
843+
}
844+
}
845+
806846
mod generic_enum {
807847
#[derive(Debug)]
808848
enum MyEnum<A> {
@@ -2469,7 +2509,7 @@ fn main() {
24692509
method_non_parametric_impl::f(); // $ target=f
24702510
method_non_parametric_trait_impl::f(); // $ target=f
24712511
function_trait_bounds::f(); // $ target=f
2472-
trait_associated_type::f(); // $ target=f
2512+
associated_type_in_trait::f(); // $ target=f
24732513
generic_enum::f(); // $ target=f
24742514
method_supertraits::f(); // $ target=f
24752515
function_trait_bounds_2::f(); // $ target=f

0 commit comments

Comments
 (0)