Improve error message for lifetime error with dyn Trait
#67378
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to improve the errors in cases like #54779.
Because
SomeType<dyn OtherTrait>
andimpl SomeTrait for dyn OtherTrait
are 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 Trait
does.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
+ 'static
or 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