33use crate :: infer:: error_reporting:: nice_region_error:: find_anon_type:: find_anon_type;
44use crate :: infer:: error_reporting:: nice_region_error:: NiceRegionError ;
55use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder } ;
6- use rustc_hir:: intravisit:: Visitor ;
7- use rustc_hir:: FnRetTy ;
86use rustc_middle:: ty;
97
108impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
@@ -48,19 +46,24 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4846 return None ; // inapplicable
4947 } ;
5048
49+ // Suggesting to add a `'static` lifetime to a parameter is nearly always incorrect,
50+ // and can steer users down the wrong path.
51+ if * named == ty:: ReStatic {
52+ return None ;
53+ }
54+
5155 debug ! ( "try_report_named_anon_conflict: named = {:?}" , named) ;
5256 debug ! ( "try_report_named_anon_conflict: anon_param_info = {:?}" , anon_param_info) ;
5357 debug ! ( "try_report_named_anon_conflict: region_info = {:?}" , region_info) ;
5458
55- let ( param, new_ty, new_ty_span, br, is_first, scope_def_id, is_impl_item) = (
56- anon_param_info. param ,
57- anon_param_info. param_ty ,
58- anon_param_info. param_ty_span ,
59- anon_param_info. bound_region ,
60- anon_param_info. is_first ,
61- region_info. def_id ,
62- region_info. is_impl_item ,
63- ) ;
59+ let param = anon_param_info. param ;
60+ let new_ty = anon_param_info. param_ty ;
61+ let new_ty_span = anon_param_info. param_ty_span ;
62+ let br = anon_param_info. bound_region ;
63+ let is_first = anon_param_info. is_first ;
64+ let scope_def_id = region_info. def_id ;
65+ let is_impl_item = region_info. is_impl_item ;
66+
6467 match br {
6568 ty:: BrAnon ( _) => { }
6669 _ => {
@@ -75,26 +78,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7578 return None ;
7679 }
7780
78- if let Some ( ( _, fndecl) ) = find_anon_type ( self . tcx ( ) , anon, & br) {
79- if self . is_self_anon ( is_first, scope_def_id) {
80- return None ;
81- }
82-
83- if let FnRetTy :: Return ( ty) = & fndecl. output {
84- let mut v = ty:: TraitObjectVisitor ( vec ! [ ] , self . tcx ( ) . hir ( ) ) ;
85- v. visit_ty ( ty) ;
86-
87- debug ! ( "try_report_named_anon_conflict: ret ty {:?}" , ty) ;
88- if sub == & ty:: ReStatic
89- && v. 0 . into_iter ( ) . any ( |t| t. span . desugaring_kind ( ) . is_none ( ) )
90- {
91- // If the failure is due to a `'static` requirement coming from a `dyn` or
92- // `impl` Trait that *isn't* caused by `async fn` desugaring, handle this case
93- // better in `static_impl_trait`.
94- debug ! ( "try_report_named_anon_conflict: impl Trait + 'static" ) ;
95- return None ;
96- }
97- }
81+ if find_anon_type ( self . tcx ( ) , anon, & br) . is_some ( )
82+ && self . is_self_anon ( is_first, scope_def_id)
83+ {
84+ return None ;
9885 }
9986
10087 let ( error_var, span_label_var) = match param. pat . simple_ident ( ) {
@@ -114,16 +101,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
114101 ) ;
115102
116103 diag. span_label ( span, format ! ( "lifetime `{}` required" , named) ) ;
117- // Suggesting `'static` is nearly always incorrect, and can steer users
118- // down the wrong path.
119- if * named != ty:: ReStatic {
120- diag. span_suggestion (
121- new_ty_span,
122- & format ! ( "add explicit lifetime `{}` to {}" , named, span_label_var) ,
123- new_ty. to_string ( ) ,
124- Applicability :: Unspecified ,
125- ) ;
126- }
104+ diag. span_suggestion (
105+ new_ty_span,
106+ & format ! ( "add explicit lifetime `{}` to {}" , named, span_label_var) ,
107+ new_ty. to_string ( ) ,
108+ Applicability :: Unspecified ,
109+ ) ;
127110
128111 Some ( diag)
129112 }
0 commit comments