-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Further tweak lifetime errors involving dyn Trait
and impl Trait
in return position
#72804
Conversation
Public feedback given a small number of people in the community in replies to https://twitter.com/ekuber/status/1266565884379725824 |
error[E0758]: cannot infer an appropriate lifetime | ||
--> $DIR/issue-16922.rs:4:14 | ||
| | ||
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> { | ||
| -- data with this lifetime... | ||
| -- this data with an anonymous lifetime `'_`... | ||
LL | Box::new(value) as Box<dyn Any> | ||
| ---------^^^^^- | ||
| | | | ||
| | ...and is captured here | ||
| ...is required to be `'static` by this... | ||
| ^^^^^ ...is captured here requiring it to live as long as `'static` | ||
| | ||
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 3:1 | ||
help: to permit non-static references in a trait object value, you can add an explicit bound for an anonymous lifetime `'_` | ||
| | ||
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> { | ||
| ^^^^ |
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.
The suggestion here isn't entirely correct. We should be suggesting fn foo<'a, T: Any + 'a>(value: &'a T) -> Box<dyn Any + 'a>
instead.
When adding documentation for the new error code, I noticed that we don't address the implicit |
d961861
to
27cad49
Compare
src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr
Outdated
Show resolved
Hide resolved
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) } | ||
| ---- ^ ...is captured here requiring it to live as long as `'static` | ||
| | | ||
| this data with an anonymous lifetime `'_`... |
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.
When we have sentences like this, can we make sure that the first fragment is always above the second? I think users will probably read left-to-right over top-to-bottom, but it wouldn't hurt to make it unambiguous.
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.
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 see: it's not so much of a problem in general, then. Would it be difficult to alter the behaviour with regards to which labels are pushed down? If we can improve the situation even for more uncommon cases, it seems worth doing, unless it's significant work.
LL | fn foo<'a>(x: &i32) -> impl Copy + 'a { x } | ||
| ---- ^ lifetime `'a` required | ||
| | | ||
| help: add explicit lifetime `'a` to the type of `x`: `&'a 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 think something as simple as:
| help: add explicit lifetime `'a` to the type of `x`: `&'a i32` | |
| help: add an explicit lifetime: `&'a i32` |
would do; it's easier to read.
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.
The explicit lifetime already exists, so saying an
would imply the creation of a new lifetime (that's how it'd read to me). Removing the lifetime name and the description of x
's type removes some jargon, but I think there's a case to be made about this being a good moment to teach that jargon.
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.
That sounds reasonable. What do you think about altering slightly to help: add the explicit lifetime […]
? That reads slightly better to me, but is a little longer.
src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr
Outdated
Show resolved
Hide resolved
As always, these diagnostics improvements look great! I just have a few nit-picky comments :) |
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.
Initial comments based on reading the diffs -- I do think this PR is better than status quo, so feel free to push back on me some :)
| | ||
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the method body at 6:5 | ||
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for an anonymous lifetime `'_` |
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 feel like it'd be good if we could take this opportunity to explain lifetime elision a bit better. How hard would it be to say
help: to declare that you capture borrowed data from self
, you can add an explicit '_
bound
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.
LL | Box::new(move || { *x }) | ||
| ^^^^^^^^^^^^^^ ...is captured here requiring it to live as long as `'static` | ||
| | ||
help: consider changing the trait object's explicit `'static` bound to an anonymous lifetime `'_` |
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.
another example. It feels like it's not just "an anonymous lifetime", it's the lifetime from the argument x
.
help: consider changing the trait object's explicit 'static
bound to '_
, to declare that you capture data from the argument x
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.
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.
Nice!
Please check the new wording. |
This comment has been minimized.
This comment has been minimized.
20f60d1
to
b9be6e1
Compare
This comment has been minimized.
This comment has been minimized.
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.
r=me post rebase
| | ||
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 17:1 | ||
help: to declare that the trait object captures data from argument `v`, you can add an explicit `'_` lifetime bound |
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.
this is really good
fb79b6d
to
56b1ca4
Compare
@bors r=nikomatsakis |
📌 Commit 56b1ca4f60df0ead090759b60d14200f731a08a1 has been approved by |
☔ The latest upstream changes (presumably #73147) made this pull request unmergeable. Please resolve the merge conflicts. |
📌 Commit bfe1434 has been approved by |
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…, r=nikomatsakis Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position * Suggest substituting `'static` lifetime in impl/dyn `Trait + 'static` instead of `Trait + 'static + '_` * When `'static` is explicit, also suggest constraining argument with it * Reduce verbosity of suggestion message and mention lifetime in label * Tweak output for overlapping required/captured spans * Give these errors an error code Follow up to rust-lang#72543. r? @nikomatsakis
…arth Rollup of 17 pull requests Successful merges: - rust-lang#70551 (Make all uses of ty::Error delay a span bug) - rust-lang#71338 (Expand "recursive opaque type" diagnostic) - rust-lang#71976 (Improve diagnostics for `let x += 1`) - rust-lang#72279 (add raw_ref macros) - rust-lang#72628 (Add tests for 'impl Default for [T; N]') - rust-lang#72804 (Further tweak lifetime errors involving `dyn Trait` and `impl Trait` in return position) - rust-lang#72814 (remove visit_terminator_kind from MIR visitor) - rust-lang#72836 (Complete the std::time documentation to warn about the inconsistencies between OS) - rust-lang#72968 (Only highlight doc search results via mouseover if mouse has moved) - rust-lang#73034 (Export `#[inline]` fns with extern indicators) - rust-lang#73315 (Clean up some weird command strings) - rust-lang#73320 (Make new type param suggestion more targetted) - rust-lang#73361 (Tweak "non-primitive cast" error) - rust-lang#73425 (Mention functions pointers in the documentation) - rust-lang#73428 (Fix typo in librustc_ast docs) - rust-lang#73447 (Improve document for `Result::as_deref(_mut)` methods) - rust-lang#73476 (Added tooltip for should_panic code examples) Failed merges: r? @ghost
…, r=nikomatsakis Account for multiple impl/dyn Trait in return type when suggesting `'_` Make `impl` and `dyn` Trait lifetime suggestions a bit more resilient. Follow up to rust-lang#72804. r? @nikomatsakis
'static
lifetime in impl/dynTrait + 'static
instead ofTrait + 'static + '_
'static
is explicit, also suggest constraining argument with itFollow up to #72543.
r? @nikomatsakis