55//! used between functions, and they operate in a purely top-down
66//! way. Therefore, we break lifetime name resolution into a separate pass.
77
8+ use crate :: diagnostics:: {
9+ add_missing_lifetime_specifiers_label, report_missing_lifetime_specifiers,
10+ } ;
811use rustc:: hir:: map:: Map ;
912use rustc:: lint;
1013use rustc:: middle:: resolve_lifetime:: * ;
11- use rustc:: session:: Session ;
1214use rustc:: ty:: { self , DefIdTree , GenericParamDefKind , TyCtxt } ;
1315use rustc:: { bug, span_bug} ;
1416use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
15- use rustc_errors:: { pluralize , struct_span_err, Applicability , DiagnosticBuilder } ;
17+ use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder } ;
1618use rustc_hir as hir;
1719use rustc_hir:: def:: { DefKind , Res } ;
1820use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , LocalDefId , LOCAL_CRATE } ;
@@ -1320,9 +1322,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13201322 where
13211323 F : for < ' b > FnOnce ( ScopeRef < ' _ > , & mut LifetimeContext < ' b , ' tcx > ) ,
13221324 {
1323- let LifetimeContext { tcx, map, lifetime_uses, missing_named_lifetime_spots , .. } = self ;
1325+ let LifetimeContext { tcx, map, lifetime_uses, .. } = self ;
13241326 let labels_in_fn = take ( & mut self . labels_in_fn ) ;
13251327 let xcrate_object_lifetime_defaults = take ( & mut self . xcrate_object_lifetime_defaults ) ;
1328+ let missing_named_lifetime_spots = take ( & mut self . missing_named_lifetime_spots ) ;
13261329 let mut this = LifetimeContext {
13271330 tcx : * tcx,
13281331 map : map,
@@ -1332,14 +1335,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13321335 labels_in_fn,
13331336 xcrate_object_lifetime_defaults,
13341337 lifetime_uses,
1335- missing_named_lifetime_spots : missing_named_lifetime_spots . to_vec ( ) ,
1338+ missing_named_lifetime_spots,
13361339 } ;
13371340 debug ! ( "entering scope {:?}" , this. scope) ;
13381341 f ( self . scope , & mut this) ;
13391342 this. check_uses_for_lifetimes_defined_by_scope ( ) ;
13401343 debug ! ( "exiting scope {:?}" , this. scope) ;
13411344 self . labels_in_fn = this. labels_in_fn ;
13421345 self . xcrate_object_lifetime_defaults = this. xcrate_object_lifetime_defaults ;
1346+ self . missing_named_lifetime_spots = this. missing_named_lifetime_spots ;
13431347 }
13441348
13451349 /// helper method to determine the span to remove when suggesting the
@@ -2894,74 +2898,3 @@ fn insert_late_bound_lifetimes(
28942898 }
28952899 }
28962900}
2897-
2898- fn report_missing_lifetime_specifiers (
2899- sess : & Session ,
2900- span : Span ,
2901- count : usize ,
2902- ) -> DiagnosticBuilder < ' _ > {
2903- struct_span_err ! ( sess, span, E0106 , "missing lifetime specifier{}" , pluralize!( count) )
2904- }
2905-
2906- fn add_missing_lifetime_specifiers_label (
2907- err : & mut DiagnosticBuilder < ' _ > ,
2908- span : Span ,
2909- count : usize ,
2910- lifetime_names : & FxHashSet < ast:: Ident > ,
2911- snippet : Option < & str > ,
2912- missing_named_lifetime_spots : & [ & hir:: Generics < ' _ > ] ,
2913- ) {
2914- if count > 1 {
2915- err. span_label ( span, format ! ( "expected {} lifetime parameters" , count) ) ;
2916- } else {
2917- let suggest_existing = |err : & mut DiagnosticBuilder < ' _ > , sugg| {
2918- err. span_suggestion (
2919- span,
2920- "consider using the named lifetime" ,
2921- sugg,
2922- Applicability :: MaybeIncorrect ,
2923- ) ;
2924- } ;
2925- let suggest_new = |err : & mut DiagnosticBuilder < ' _ > , sugg| {
2926- err. span_label ( span, "expected named lifetime parameter" ) ;
2927-
2928- if let Some ( generics) = missing_named_lifetime_spots. iter ( ) . last ( ) {
2929- let mut introduce_suggestion = vec ! [ ] ;
2930- introduce_suggestion. push ( match & generics. params {
2931- [ ] => ( generics. span , "<'lifetime>" . to_string ( ) ) ,
2932- [ param, ..] => ( param. span . shrink_to_lo ( ) , "'lifetime, " . to_string ( ) ) ,
2933- } ) ;
2934- introduce_suggestion. push ( ( span, sugg) ) ;
2935- err. multipart_suggestion (
2936- "consider introducing a named lifetime parameter" ,
2937- introduce_suggestion,
2938- Applicability :: MaybeIncorrect ,
2939- ) ;
2940- }
2941- } ;
2942-
2943- match ( lifetime_names. len ( ) , lifetime_names. iter ( ) . next ( ) , snippet) {
2944- ( 1 , Some ( name) , Some ( "&" ) ) => {
2945- suggest_existing ( err, format ! ( "&{} " , name) ) ;
2946- }
2947- ( 1 , Some ( name) , Some ( "'_" ) ) => {
2948- suggest_existing ( err, name. to_string ( ) ) ;
2949- }
2950- ( 1 , Some ( name) , Some ( snippet) ) if !snippet. ends_with ( ">" ) => {
2951- suggest_existing ( err, format ! ( "{}<{}>" , snippet, name) ) ;
2952- }
2953- ( 0 , _, Some ( "&" ) ) => {
2954- suggest_new ( err, "&'lifetime " . to_string ( ) ) ;
2955- }
2956- ( 0 , _, Some ( "'_" ) ) => {
2957- suggest_new ( err, "'lifetime" . to_string ( ) ) ;
2958- }
2959- ( 0 , _, Some ( snippet) ) if !snippet. ends_with ( ">" ) => {
2960- suggest_new ( err, format ! ( "{}<'lifetime>" , snippet) ) ;
2961- }
2962- _ => {
2963- err. span_label ( span, "expected lifetime parameter" ) ;
2964- }
2965- }
2966- }
2967- }
0 commit comments