Rust: Add type inference tests for associated types

This commit is contained in:
Simon Friis Vindum
2025-07-24 15:44:14 +02:00
parent b1ee795225
commit 1b2f160b55
4 changed files with 3387 additions and 3174 deletions

View File

@@ -1,8 +1,8 @@
multipleCallTargets
| dereference.rs:61:15:61:24 | e1.deref() |
| main.rs:2213:13:2213:31 | ...::from(...) |
| main.rs:2214:13:2214:31 | ...::from(...) |
| main.rs:2215:13:2215:31 | ...::from(...) |
| main.rs:2221:13:2221:31 | ...::from(...) |
| main.rs:2222:13:2222:31 | ...::from(...) |
| main.rs:2223:13:2223:31 | ...::from(...) |
| main.rs:2253:13:2253:31 | ...::from(...) |
| main.rs:2254:13:2254:31 | ...::from(...) |
| main.rs:2255:13:2255:31 | ...::from(...) |
| main.rs:2261:13:2261:31 | ...::from(...) |
| main.rs:2262:13:2262:31 | ...::from(...) |
| main.rs:2263:13:2263:31 | ...::from(...) |

View File

@@ -12,6 +12,12 @@ trait GenericGet<A> {
fn get(&self) -> A;
}
trait AssocTrait<GP> {
type AP;
// AssocTrait::get
fn get(&self) -> (GP, Self::AP);
}
#[derive(Clone, Debug)]
struct MyStruct {
value: i32,
@@ -36,6 +42,17 @@ impl<A: Clone + Debug> GenericGet<A> for GenStruct<A> {
}
}
impl<GGP> AssocTrait<GGP> for GenStruct<GGP>
where
GGP: Clone + Debug,
{
type AP = bool;
// GenStruct<GGP>::get
fn get(&self) -> (GGP, bool) {
(self.value.clone(), true) // $ fieldof=GenStruct target=clone
}
}
fn get_a<A, G: GenericGet<A> + ?Sized>(a: &G) -> A {
a.get() // $ target=GenericGet::get
}
@@ -58,10 +75,34 @@ fn test_poly_dyn_trait() {
let _result = (*obj).get(); // $ target=deref target=GenericGet::get type=_result:bool
}
fn assoc_dyn_get<A, B>(a: &dyn AssocTrait<A, AP = B>) -> (A, B) {
a.get() // $ target=AssocTrait::get
}
fn assoc_get<A, B, T: AssocTrait<A, AP = B> + ?Sized>(a: &T) -> (A, B) {
a.get() // $ target=AssocTrait::get
}
fn test_assoc_type(obj: &dyn AssocTrait<i64, AP = bool>) {
let (
_gp, // $ type=_gp:i64
_ap, // $ MISSING: type=_ap:bool
) = (*obj).get(); // $ target=deref target=AssocTrait::get
let (
_gp, // $ type=_gp:i64
_ap, // $ MISSING: type=_ap:bool
) = assoc_dyn_get(obj); // $ target=assoc_dyn_get
let (
_gp, // $ type=_gp:i64
_ap, // $ MISSING: type=_ap:bool
) = assoc_get(obj); // $ target=assoc_get
}
pub fn test() {
test_basic_dyn_trait(&MyStruct { value: 42 }); // $ target=test_basic_dyn_trait
test_generic_dyn_trait(&GenStruct {
value: "".to_string(),
}); // $ target=test_generic_dyn_trait
test_poly_dyn_trait(); // $ target=test_poly_dyn_trait
test_assoc_type(&GenStruct { value: 100 }); // $ target=test_assoc_type
}

View File

@@ -653,7 +653,7 @@ mod function_trait_bounds {
}
}
mod trait_associated_type {
mod associated_type_in_trait {
#[derive(Debug)]
struct Wrapper<A> {
field: A,
@@ -803,6 +803,46 @@ mod trait_associated_type {
}
}
mod associated_type_in_supertrait {
trait Supertrait {
type Content;
fn insert(content: Self::Content);
}
trait Subtrait: Supertrait {
// Subtrait::get_content
fn get_content(&self) -> Self::Content;
}
struct MyType<T>(T);
impl<T> Supertrait for MyType<T> {
type Content = T;
fn insert(_content: Self::Content) {
println!("Inserting content: ");
}
}
impl<T: Clone> Subtrait for MyType<T> {
// MyType::get_content
fn get_content(&self) -> Self::Content {
(*self).0.clone() // $ fieldof=MyType target=clone target=deref
}
}
fn get_content<T: Subtrait>(item: &T) -> T::Content {
item.get_content() // $ target=Subtrait::get_content
}
fn test() {
let item1 = MyType(42i64);
let _content1 = item1.get_content(); // $ target=MyType::get_content MISSING: type=_content1:i64
let item2 = MyType(true);
let _content2 = get_content(&item2); // $ target=get_content MISSING: type=_content2:bool
}
}
mod generic_enum {
#[derive(Debug)]
enum MyEnum<A> {
@@ -2469,7 +2509,7 @@ fn main() {
method_non_parametric_impl::f(); // $ target=f
method_non_parametric_trait_impl::f(); // $ target=f
function_trait_bounds::f(); // $ target=f
trait_associated_type::f(); // $ target=f
associated_type_in_trait::f(); // $ target=f
generic_enum::f(); // $ target=f
method_supertraits::f(); // $ target=f
function_trait_bounds_2::f(); // $ target=f