about summary refs log tree commit diff
path: root/compiler/rustc_infer/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-11-17 15:57:57 +0100
committerGitHub <noreply@github.com>2021-11-17 15:57:57 +0100
commitd7b86880d24ab7a7be60e6c3167142111c4d1970 (patch)
tree12bd0c80adfbd4904d23198315169ffc88472587 /compiler/rustc_infer/src
parent07342828c5066d6a7763535b9281b5cf021dea5c (diff)
parent130b9e9e3b058e76e43788fac67538c064fc8a97 (diff)
downloadrust-d7b86880d24ab7a7be60e6c3167142111c4d1970.tar.gz
rust-d7b86880d24ab7a7be60e6c3167142111c4d1970.zip
Rollup merge of #90667 - rukai:improve_static_lifetime_diagnostics, r=estebank
Improve diagnostics when a static lifetime is expected

Makes progress towards https://github.com/rust-lang/rust/issues/90600

The diagnostics here were previously entirely removed due to giving a misleading suggestion but if we instead provide an informative label in that same location it should better help the user understand the situation.

I included the example from the issue as it demonstrates an area where the diagnostics are still lacking.
Happy to remove that if its just adding noise atm.
Diffstat (limited to 'compiler/rustc_infer/src')
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs65
1 files changed, 24 insertions, 41 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
index 0878f8550da..eb1c80ecb01 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
@@ -3,8 +3,6 @@
 use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
-use rustc_hir::intravisit::Visitor;
-use rustc_hir::FnRetTy;
 use rustc_middle::ty;
 
 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
@@ -48,19 +46,24 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
             return None; // inapplicable
         };
 
+        // Suggesting to add a `'static` lifetime to a parameter is nearly always incorrect,
+        // and can steer users down the wrong path.
+        if *named == ty::ReStatic {
+            return None;
+        }
+
         debug!("try_report_named_anon_conflict: named = {:?}", named);
         debug!("try_report_named_anon_conflict: anon_param_info = {:?}", anon_param_info);
         debug!("try_report_named_anon_conflict: region_info = {:?}", region_info);
 
-        let (param, new_ty, new_ty_span, br, is_first, scope_def_id, is_impl_item) = (
-            anon_param_info.param,
-            anon_param_info.param_ty,
-            anon_param_info.param_ty_span,
-            anon_param_info.bound_region,
-            anon_param_info.is_first,
-            region_info.def_id,
-            region_info.is_impl_item,
-        );
+        let param = anon_param_info.param;
+        let new_ty = anon_param_info.param_ty;
+        let new_ty_span = anon_param_info.param_ty_span;
+        let br = anon_param_info.bound_region;
+        let is_first = anon_param_info.is_first;
+        let scope_def_id = region_info.def_id;
+        let is_impl_item = region_info.is_impl_item;
+
         match br {
             ty::BrAnon(_) => {}
             _ => {
@@ -75,26 +78,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
             return None;
         }
 
-        if let Some((_, fndecl)) = find_anon_type(self.tcx(), anon, &br) {
-            if self.is_self_anon(is_first, scope_def_id) {
-                return None;
-            }
-
-            if let FnRetTy::Return(ty) = &fndecl.output {
-                let mut v = ty::TraitObjectVisitor(vec![], self.tcx().hir());
-                v.visit_ty(ty);
-
-                debug!("try_report_named_anon_conflict: ret ty {:?}", ty);
-                if sub == &ty::ReStatic
-                    && v.0.into_iter().any(|t| t.span.desugaring_kind().is_none())
-                {
-                    // If the failure is due to a `'static` requirement coming from a `dyn` or
-                    // `impl` Trait that *isn't* caused by `async fn` desugaring, handle this case
-                    // better in `static_impl_trait`.
-                    debug!("try_report_named_anon_conflict: impl Trait + 'static");
-                    return None;
-                }
-            }
+        if find_anon_type(self.tcx(), anon, &br).is_some()
+            && self.is_self_anon(is_first, scope_def_id)
+        {
+            return None;
         }
 
         let (error_var, span_label_var) = match param.pat.simple_ident() {
@@ -114,16 +101,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
         );
 
         diag.span_label(span, format!("lifetime `{}` required", named));
-        // Suggesting `'static` is nearly always incorrect, and can steer users
-        // down the wrong path.
-        if *named != ty::ReStatic {
-            diag.span_suggestion(
-                new_ty_span,
-                &format!("add explicit lifetime `{}` to {}", named, span_label_var),
-                new_ty.to_string(),
-                Applicability::Unspecified,
-            );
-        }
+        diag.span_suggestion(
+            new_ty_span,
+            &format!("add explicit lifetime `{}` to {}", named, span_label_var),
+            new_ty.to_string(),
+            Applicability::Unspecified,
+        );
 
         Some(diag)
     }