-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Improve output of impl Trait
errors
#68110
Comments
It's not clear that this is unambiguously the right solution for the user in this case. First, the user may not want to pay the overhead of dynamic dispatch, and second the trait may not be object safe. Another solution would be to use an Equally important, I think it would be to tell the user what the problem is (that
Only types implement traits, and
|
We can certainly qualify the suggestion more strongly and mention other alternatives, but it seems to me that dynamic dispatch is indeed the path of least resistance for what a new developer is trying to accomplish. Using dynamic dispatch is never wrong just may be suboptimal.
This is something that we can check as a precondition for structured suggestions, and if unfulfilled it is also a good time to teach about it.
I completely agree. I'm playing around with some different approaches. This is what I've got so far (wording very much WIP):
|
Sounds great. Make sure to also mention
Wait, all of these cases are using |
Long message incoming
👍
I'd like to keep the verbosity a bit lower than enumerating all of them, but if we give the error a new error code we could list them in the index.
I'm trying to look at this from the point of view of someone trying things out, and "leading the horse to water". The discussion around The test file I'm working on in this unpublished branch is: struct Struct;
struct Other;
trait Trait {}
impl Trait for Struct {}
impl Trait for Other {}
// Commented out because they cause earlier errors that stop the world, but I will cater to them later.
//fn foo() -> Foo<dyn Trait> { Struct } //~ ERROR E0277
#[allow(bare_trait_objects)]
fn fuz() -> (usize, Trait) { (42, Struct) } //~ ERROR E0277
fn bar() -> (usize, dyn Trait) { (42, Struct) } //~ ERROR E0277
//#[allow(bare_trait_objects)]
//fn baz() -> (Iterator<Item = u32>, usize) { Struct } //~ ERROR E0277
//fn bat() -> (dyn Iterator<Item = u32>, usize) { Struct } //~ ERROR E0277
#[allow(bare_trait_objects)]
fn bap() -> Trait { Struct } //~ ERROR E0277
fn ban() -> dyn Trait { Struct } //~ ERROR E0277
fn bak() -> dyn Trait { unimplemented!() } //~ ERROR E0277
fn bal() -> dyn Trait { //~ ERROR E0277
if true {
return Struct;
}
Other
}
fn main() {} and this is the current full output for it
but my ideal end state is
For the case where the user is already using
but I'd like us to have a structured suggestion as well
Some extra clarification might be warranted: I filed this ticket while looking at the |
This was just a note on my part, no need to mention them.
I would prefer having different tickets for each sub-problem though so that solving each subproblem is more actionable.
This suggests that this is invalid: fn foo() -> impl Ord {
if 0 == 1 { return 0 }
else if 0 == 2 { return 1 }
else { return 2 }
}
I'd split this into two links, one for impl Trait, and one for trait objects (which should be put after the trait objects suggestion). Finally, after the trait object suggestion, let's also suggest an enum. |
Given the following code:
we currently emit:
The first case should suggest boxing everything instead of using
impl Trait
:The second case should point at the source of the
(): Trait
bound:The text was updated successfully, but these errors were encountered: