-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Type inference for impl trait types with type parameters #20119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a20fed8
9a0c587
bb56b0d
39f602c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:2238:13:2238:31 | ...::from(...) | | ||
| main.rs:2239:13:2239:31 | ...::from(...) | | ||
| main.rs:2240:13:2240:31 | ...::from(...) | | ||
| main.rs:2246:13:2246:31 | ...::from(...) | | ||
| main.rs:2247:13:2247:31 | ...::from(...) | | ||
| main.rs:2248:13:2248:31 | ...::from(...) | |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1873,8 +1873,10 @@ mod async_ { | |
} | ||
|
||
mod impl_trait { | ||
#[derive(Copy, Clone)] | ||
struct S1; | ||
struct S2; | ||
struct S3<T3>(T3); | ||
|
||
trait Trait1 { | ||
fn f1(&self) {} // Trait1f1 | ||
|
@@ -1906,6 +1908,13 @@ mod impl_trait { | |
} | ||
} | ||
|
||
impl<T: Clone> MyTrait<T> for S3<T> { | ||
fn get_a(&self) -> T { | ||
let S3(t) = self; | ||
t.clone() | ||
} | ||
} | ||
|
||
fn get_a_my_trait() -> impl MyTrait<S2> { | ||
S1 | ||
} | ||
|
@@ -1914,6 +1923,18 @@ mod impl_trait { | |
t.get_a() // $ target=MyTrait::get_a | ||
} | ||
|
||
fn get_a_my_trait2<T: Clone>(x: T) -> impl MyTrait<T> { | ||
S3(x) | ||
} | ||
|
||
fn get_a_my_trait3<T: Clone>(x: T) -> Option<impl MyTrait<T>> { | ||
Some(S3(x)) | ||
} | ||
|
||
fn get_a_my_trait4<T: Clone>(x: T) -> (impl MyTrait<T>, impl MyTrait<T>) { | ||
(S3(x.clone()), S3(x)) // $ target=clone | ||
} | ||
|
||
fn uses_my_trait2<A>(t: impl MyTrait<A>) -> A { | ||
t.get_a() // $ target=MyTrait::get_a | ||
} | ||
|
@@ -1927,6 +1948,10 @@ mod impl_trait { | |
let a = get_a_my_trait(); // $ target=get_a_my_trait | ||
let c = uses_my_trait2(a); // $ type=c:S2 target=uses_my_trait2 | ||
let d = uses_my_trait2(S1); // $ type=d:S2 target=uses_my_trait2 | ||
let e = get_a_my_trait2(S1).get_a(); // $ target=get_a_my_trait2 target=MyTrait::get_a type=e:S1 | ||
// For this function the `impl` type does not appear in the root of the return type | ||
let f = get_a_my_trait3(S1).unwrap().get_a(); // $ target=get_a_my_trait3 target=unwrap target=MyTrait::get_a type=f:S1 | ||
let g = get_a_my_trait4(S1).0.get_a(); // $ target=get_a_my_trait4 target=MyTrait::get_a type=g:S1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm interested in what happens when we have nested
I haven't tested anything but it feels like there's a danger the type parameter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good question that I haven't thought of. With how it works right now they would become type parameters of both the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a follow-up PR is fine, if you're keeping track. :) |
||
} | ||
} | ||
|
||
|
@@ -2385,7 +2410,7 @@ mod tuples { | |
|
||
let pair = [1, 1].into(); // $ type=pair:(T_2) type=pair:0(2).i32 type=pair:1(2).i32 MISSING: target=into | ||
match pair { | ||
(0,0) => print!("unexpected"), | ||
(0, 0) => print!("unexpected"), | ||
_ => print!("expected"), | ||
} | ||
let x = pair.0; // $ type=x:i32 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like us to come up with a better name for this predicate - in particular the pattern
get...Pos
makes my brain expect this to return a position / index, which it doesn't. On the other hand "return position" is established phrasing so we don't want to lose that. Perhaps just re-order it togetReturnPosFunction
???There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
getReturnPosOfFunction
? We could also split this into two predicatesgetFunction
andisReturnPos
. Both of those seem clear, but we don't otherwise have a need for the generalgetFunction
that also works forimpl
in arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a readability point of view I quite like the two predicates solution. Assuming it doesn't make the code much more messy elsewhere.