-
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
Trying to suggest additional lifetime parameter #102323
Changes from 4 commits
887515a
365457b
24c8e27
e7cb6ad
ef930a2
2657f9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1318,13 +1318,49 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id) | ||
&& !self.tcx.features().anonymous_lifetime_in_impl_trait | ||
{ | ||
rustc_session::parse::feature_err( | ||
&self.tcx.sess.parse_sess, | ||
sym::anonymous_lifetime_in_impl_trait, | ||
lifetime_ref.span, | ||
"anonymous lifetimes in `impl Trait` are unstable", | ||
).emit(); | ||
return; | ||
|
||
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) { | ||
Some(generics) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now you have
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems if I put "'a" for the message, it replaces the ampersand symbol from the original posters input. Should I keep this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
for i in 0..generics.params.len() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only use a single parameter. Could you separate the choice of param from the suggestion / move the suggestion code out of the loop? |
||
|
||
if !generics.span.contains(generics.params[i].span) { | ||
|
||
let mut diag = rustc_session::parse::feature_err( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is common to both branches. Could you move it above the match? |
||
&self.tcx.sess.parse_sess, | ||
sym::anonymous_lifetime_in_impl_trait, | ||
lifetime_ref.span, | ||
"anonymous lifetimes in `impl Trait` are unstable", | ||
); | ||
|
||
diag.span_label(lifetime_ref.span, "expected named lifetime parameter"); | ||
diag.multipart_suggestion("consider introducing a named lifetime parameter", | ||
vec![ | ||
(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This suggestion is only correct if you have a |
||
match generics.span_for_param_suggestion() { | ||
Some(_) => { | ||
(self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned()) | ||
} | ||
None => (generics.span, "<'a>".to_owned()), | ||
|
||
} | ||
], rustc_errors::Applicability::MaybeIncorrect); | ||
diag.emit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also common to both branches. Could you move this below the match? |
||
|
||
return; | ||
} | ||
} | ||
cjgillot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
None => { | ||
rustc_session::parse::feature_err( | ||
&self.tcx.sess.parse_sess, | ||
sym::anonymous_lifetime_in_impl_trait, | ||
lifetime_ref.span, | ||
"anonymous lifetimes in `impl Trait` are unstable", | ||
).emit(); | ||
return; | ||
} | ||
} | ||
|
||
} | ||
scope = s; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,17 +26,25 @@ error[E0658]: anonymous lifetimes in `impl Trait` are unstable | |
--> $DIR/impl-trait-missing-lifetime-gated.rs:5:31 | ||
| | ||
LL | fn f(_: impl Iterator<Item = &'_ ()>) {} | ||
| ^^ | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable | ||
help: consider introducing a named lifetime parameter | ||
| | ||
LL | fn f<'a>(_: impl Iterator<Item = &'_'a ()>) {} | ||
| ++++ ++ | ||
|
||
error[E0658]: anonymous lifetimes in `impl Trait` are unstable | ||
--> $DIR/impl-trait-missing-lifetime-gated.rs:8:31 | ||
| | ||
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() } | ||
| ^^ | ||
| ^^ expected named lifetime parameter | ||
| | ||
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable | ||
help: consider introducing a named lifetime parameter | ||
| | ||
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() } | ||
| ++++ ++ | ||
Comment on lines
+44
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the resulting code here, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replacing the anon lifetime in the return type seems like an outsized change. I'd rather stabilize this feature sooner, to get rid of the question. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything else I should do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The question is: do you want to take a bit more time in this PR to fix the |
||
|
||
error: aborting due to 4 previous errors | ||
|
||
|
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.
You can keep this a feature error. You can keep this line, but instead of calling
emit
immediately, store it in a mut variablediag
, decorate it usingspan_...
methods, and callemit
when you're done.