You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
E0412 is correct but E0782 is emitted (in trait definitions) #116434
error[E0782]: trait objects must include the `dyn` keyword
--> src/main.rs:4:17
|
4 | fn foo() -> Clone;
| ^^^^^
|
help: add `dyn` keyword before this trait
|
4 | fn foo() -> dyn Clone;
| +++
For more information about this error, try `rustc --explain E0782`.
Desired output
Obviously this isn't helpful, as Clone isn't object safe. The correct error is E0412:
error[E0412]: cannot find type`Clone`in this scope
--> src/main.rs:4:17
|
4 | fn foo() -> Clone;
| ^^^
|
help: you might have meant to use the associated type|
4 | fn foo() -> Self::Clone;
| ++++++
For more information about this error, try `rustc --explain E0412`.
Rationale and extra context
E0412 is correctly emitted if the associated type name is changed to not collide with any traits currently in scope. So:
traitFoo{typeBar;fnfoo() -> Bar}
Errors as expected.
I've also seen this confuse new users "in the wild" on Rustcord - that's what originally inspired me to file this issue.
It looks like #119752 (in the queue) changes this to E0038 without a suggestion (for edition 2021):
error[E0038]: the trait `Clone` cannot be made into an object
--> /Users/e/Documents/GitHub/rust/tests/ui/suggestions/issue-116434.rs:5:17
|
LL | fn foo() -> Clone;
| ^^^^^ `Clone` cannot be made into an object
|
= note: the trait cannot be made into an object because it requires `Self: Sized`
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
A suggestion to -> impl Clone is also valid (and with the above pull appears for edition < 2021), but perhaps your -> Self::Clone makes more sense in context, if not exactly... great.
Do you have any links or can share how this is tripping up other users?
Do you have any links or can share how this is tripping up other users?
It's been a while, but what I saw on the Discord went something like this:
traitDbHandle{/* irrelevant, but not object safe */}// Some lines away... traitDbInterface{// (presumably assuming the namespaces don't overlap) typeDbHandle;fnhandle() -> DbHandle;}
The user then gets the "please add dyn" message, which they follow.
The compiler then tells them "sorry you can't dyn that," and they wind up in the Discord very confused about the mixed signals.
The problematic dyn suggestion is also gone from nightly: playground
The type (trait) DbHandle is in scope, so I don't think E0412 (cannot find type in this scope) is correct.
If it provides the correct suggestion though, it would definitely be an improvement to include that suggestion in the output. I don't see a clear way to get there from the E0038 code path though. :(
Activity
trevyn commentedon Jan 20, 2024
@rustbot claim
trevyn commentedon Jan 20, 2024
It looks like #119752 (in the queue) changes this to E0038 without a suggestion (for edition 2021):
A suggestion to
-> impl Cloneis also valid (and with the above pull appears for edition < 2021), but perhaps your-> Self::Clonemakes more sense in context, if not exactly... great.Do you have any links or can share how this is tripping up other users?
trevyn commentedon Jan 20, 2024
Ah, apparently it's possible to produce multiple suggestions. I'll poke at it and see what comes out.
Colonial-Dev commentedon Jan 20, 2024
It's been a while, but what I saw on the Discord went something like this:
The user then gets the "please add
dyn" message, which they follow.The compiler then tells them "sorry you can't
dynthat," and they wind up in the Discord very confused about the mixed signals.trevyn commentedon Jan 20, 2024
The problematic
dynsuggestion is also gone from nightly: playgroundThe type (trait) DbHandle is in scope, so I don't think E0412 (cannot find type in this scope) is correct.
If it provides the correct suggestion though, it would definitely be an improvement to include that suggestion in the output. I don't see a clear way to get there from the E0038 code path though. :(
trevyn commentedon Jan 21, 2024
Making some progress on this, currently blocked on #120164
trevyn commentedon Jan 21, 2024
@rustbot label -D-invalid-suggestion
trevyn commentedon Jan 25, 2024
blocked on #120275
Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-er…
Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-er…
trevyn commentedon Feb 2, 2024
I have a PR ready to go for this that adds the
Self::suggestion on E0038, waiting for bors to merge #120530Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-er…
Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-er…
Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-er…
Unrolled build for rust-lang#120530
Rollup merge of rust-lang#121111 - trevyn:associated-type-suggestion,…
Unrolled build for rust-lang#121111