about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-10-10 10:23:04 +0900
committerGitHub <noreply@github.com>2022-10-10 10:23:04 +0900
commit0db05f16c897ac19e09269eb6a7061b888fb7851 (patch)
tree8c9ced4e483bca078eaa6af1bee48f843e6d191d
parentc50e64d872af4b2f0b61889326ab9f792e1273a1 (diff)
parent2657f9d5da0a9bff0764df824e26f2169e2e398d (diff)
downloadrust-0db05f16c897ac19e09269eb6a7061b888fb7851.tar.gz
rust-0db05f16c897ac19e09269eb6a7061b888fb7851.zip
Rollup merge of #102323 - Stoozy:master, r=cjgillot
Trying to suggest additional lifetime parameter

``@cjgillot`` This is what I have so far for #100615
-rw-r--r--compiler/rustc_resolve/src/late/lifetimes.rs33
-rw-r--r--src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr12
2 files changed, 41 insertions, 4 deletions
diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs
index 8fa6160d436..c18d5d06d64 100644
--- a/compiler/rustc_resolve/src/late/lifetimes.rs
+++ b/compiler/rustc_resolve/src/late/lifetimes.rs
@@ -1333,12 +1333,41 @@ 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(
+                        let mut diag =  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();
+                        );
+
+                        match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
+                            Some(generics) => {
+
+                                let new_param_sugg_tuple;
+
+                                new_param_sugg_tuple = match generics.span_for_param_suggestion() {
+                                    Some(_) => {
+                                        Some((self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned()))
+                                    },
+                                    None => Some((generics.span, "<'a>".to_owned()))
+                                };
+
+                                let mut multi_sugg_vec = vec![(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned())];
+
+                                if let Some(new_tuple) =  new_param_sugg_tuple{
+                                    multi_sugg_vec.push(new_tuple);
+                                }
+
+                                diag.span_label(lifetime_ref.span, "expected named lifetime parameter");
+                                diag.multipart_suggestion("consider introducing a named lifetime parameter",
+                                multi_sugg_vec,
+                                rustc_errors::Applicability::MaybeIncorrect);
+
+                            },
+                            None => { }
+                        }
+
+                        diag.emit();
                         return;
                     }
                     scope = s;
diff --git a/src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr b/src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr
index e82a6f769e0..9833da13ffc 100644
--- a/src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr
+++ b/src/test/ui/suggestions/impl-trait-missing-lifetime-gated.stderr
@@ -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() }
+   |     ++++                            ++
 
 error: aborting due to 4 previous errors