-
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
Suggest using anonymous lifetime in impl Trait
return
#58919
Conversation
This is a blatant hack, but wanted to have a PR out so we can talk about the desired output. |
Hmm that output is interesting. I wonder, as a matter of coding style and/or readability, if people would prefer to see the following suggested: fn elided<'a>(x: &'a i32) -> impl Copy + 'a { x } (but I don't know, I think I often err on the side of explicitness when people would prefer elision...) |
Oh; I guess that test case, in its full form, even includes an example where we do suggest the form with the explicit named lifetime, if you start with So its really a matter of style, and I suspect the prevailing style is towards what @estebank is proposing here |
We can expand this code to also suggest
but the code itself to make that suggestion might be quite brittle. |
I think the form suggested here is quite reasonable and I suspect doing the more elaborate suggestion is more complex impl wise... To reduce Niko's backlog, r? @pnkfelix |
Fallback to `static_impl_trait` for nice error message by peeking at the return type and the lifetime type. Point at the return type instead of the return expr/stmt in NLL mode.
68c28b6
to
30c247f
Compare
I had a talk with Niko and he clarified what a better approach would be. I looked around and arrived to the current solution, with which I'm relatively happy with (even though I think we could improve the |
| | ||
LL | fn elided(x: &i32) -> impl Copy { x } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: you can add a constraint to the return type to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 3:1 |
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.
Btw... "constraint" here is odd; we typically use "bound" for things in A + B + ...
.
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.
We've been using that wording for 6 months, but I agree we could improve on the wording on these errors.
@@ -84,6 +86,13 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { | |||
{ | |||
return None; | |||
} | |||
if let FunctionRetTy::Return(ty) = &fndecl.output { | |||
if let (TyKind::Def(_, _), ty::ReStatic) = (&ty.node, sub) { | |||
// This is an impl Trait return that evaluates de need of 'static. |
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.
"evaluates de need of 'static" ?
What is this?
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.
A partial rewording of the prior comment 😬
I believe something along the lines of "that evaluates to a need of static".
@bors r+ |
📌 Commit 30c247f has been approved by |
…, r=pnkfelix Suggest using anonymous lifetime in `impl Trait` return Fix rust-lang#48467. r? @nikomatsakis
…, r=pnkfelix Suggest using anonymous lifetime in `impl Trait` return Fix rust-lang#48467. r? @nikomatsakis
Rollup of 7 pull requests Successful merges: - #58507 (Add a -Z time option which prints only passes which runs once) - #58919 (Suggest using anonymous lifetime in `impl Trait` return) - #59041 (fixes #56766) - #59586 (Fixed URL in cargotest::TEST_REPOS) - #59595 (Update rustc-guide submodule) - #59601 (Fix small typo) - #59603 (stabilize ptr::hash) Failed merges: r? @ghost
Fix #48467.
r? @nikomatsakis