Skip to content

Commit 1ba8c95

Browse files
authored
Rollup merge of rust-lang#102323 - Stoozy:master, r=cjgillot
Trying to suggest additional lifetime parameter `@cjgillot` This is what I have so far for rust-lang#100615
2 parents 25f1916 + 2657f9d commit 1ba8c95

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

compiler/rustc_resolve/src/late/lifetimes.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -1333,12 +1333,41 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13331333
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
13341334
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
13351335
{
1336-
rustc_session::parse::feature_err(
1336+
let mut diag = rustc_session::parse::feature_err(
13371337
&self.tcx.sess.parse_sess,
13381338
sym::anonymous_lifetime_in_impl_trait,
13391339
lifetime_ref.span,
13401340
"anonymous lifetimes in `impl Trait` are unstable",
1341-
).emit();
1341+
);
1342+
1343+
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
1344+
Some(generics) => {
1345+
1346+
let new_param_sugg_tuple;
1347+
1348+
new_param_sugg_tuple = match generics.span_for_param_suggestion() {
1349+
Some(_) => {
1350+
Some((self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned()))
1351+
},
1352+
None => Some((generics.span, "<'a>".to_owned()))
1353+
};
1354+
1355+
let mut multi_sugg_vec = vec![(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned())];
1356+
1357+
if let Some(new_tuple) = new_param_sugg_tuple{
1358+
multi_sugg_vec.push(new_tuple);
1359+
}
1360+
1361+
diag.span_label(lifetime_ref.span, "expected named lifetime parameter");
1362+
diag.multipart_suggestion("consider introducing a named lifetime parameter",
1363+
multi_sugg_vec,
1364+
rustc_errors::Applicability::MaybeIncorrect);
1365+
1366+
},
1367+
None => { }
1368+
}
1369+
1370+
diag.emit();
13421371
return;
13431372
}
13441373
scope = s;

src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ error[E0658]: anonymous lifetimes in `impl Trait` are unstable
2626
--> $DIR/impl-trait-missing-lifetime-gated.rs:5:31
2727
|
2828
LL | fn f(_: impl Iterator<Item = &'_ ()>) {}
29-
| ^^
29+
| ^^ expected named lifetime parameter
3030
|
3131
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable
32+
help: consider introducing a named lifetime parameter
33+
|
34+
LL | fn f<'a>(_: impl Iterator<Item = &'_'a ()>) {}
35+
| ++++ ++
3236

3337
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
3438
--> $DIR/impl-trait-missing-lifetime-gated.rs:8:31
3539
|
3640
LL | fn g(x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
37-
| ^^
41+
| ^^ expected named lifetime parameter
3842
|
3943
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable
44+
help: consider introducing a named lifetime parameter
45+
|
46+
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() }
47+
| ++++ ++
4048

4149
error: aborting due to 4 previous errors
4250

0 commit comments

Comments
 (0)