Improve error message for lifetime error with dyn Trait#67378
Improve error message for lifetime error with dyn Trait#67378ohadravid wants to merge 1 commit intorust-lang:masterfrom
dyn Trait#67378Conversation
| "you can add an explicit constraint to the implementation so that it applies \ | ||
| to types with less than `'static` lifetime", | ||
| ), | ||
| snippet.replace(dyn_trait_name, &format!("{} + '_", &dyn_trait_name)), |
There was a problem hiding this comment.
I don't have time to do an in-depth review right now, but we should try to come up with a different way of doing this. Fiddling with the textual representation of the code is a recipe for pain later on. It produces output that isn't always correct (like the problem with the missing parens and some incorrect suggestions I noticed below), and is brittle and affected by whitespace. I'll take another look later to see if I can have more actionable feedback on what to do instead.
|
☔ The latest upstream changes (presumably #67495) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Ping from Triage: Any updates @ohadravid or @estebank? |
|
@joelpalmer I'll close this and try something more minimal in a different PR |
This is an attempt to improve the errors in cases like #54779.
Because
SomeType<dyn OtherTrait>andimpl SomeTrait for dyn OtherTraitare implicitlydyn OtherTrait+ 'static, it can be quite confusing as to why things don't work. (I originally stumbled upon this because of this question - playground link).This PR tries to handle those two cases, by:
dyn Trait + '_, likeimpl Traitdoes.There seems to be a lot of places to handle these errors, and I can't really say I fully understand the sub/sup/origin region stuff, but I got it to mostly work and wanted to get some feedback on what should I do next (and if! I'm not sure if this is the best idea/approach).
Also there are almost no existing examples in the test suite which seems to cover this usecase.
Most noticeably, I think that if you already specify
+ 'staticor some other lifetime, the suggestion will be both useless and incorrect, and if you haveBox<&dyn Trait>you'll getBox<&dyn Trait + '_>which isn't really correct (should be&(dyn Trait + '_)).r? @estebank