5
5
//! used between functions, and they operate in a purely top-down
6
6
//! way. Therefore, we break lifetime name resolution into a separate pass.
7
7
8
+ use crate :: diagnostics:: {
9
+ add_missing_lifetime_specifiers_label, report_missing_lifetime_specifiers,
10
+ } ;
8
11
use rustc:: hir:: map:: Map ;
9
12
use rustc:: lint;
10
13
use rustc:: middle:: resolve_lifetime:: * ;
11
- use rustc:: session:: Session ;
12
14
use rustc:: ty:: { self , DefIdTree , GenericParamDefKind , TyCtxt } ;
13
15
use rustc:: { bug, span_bug} ;
14
16
use 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 } ;
16
18
use rustc_hir as hir;
17
19
use rustc_hir:: def:: { DefKind , Res } ;
18
20
use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , LocalDefId , LOCAL_CRATE } ;
@@ -1320,9 +1322,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1320
1322
where
1321
1323
F : for < ' b > FnOnce ( ScopeRef < ' _ > , & mut LifetimeContext < ' b , ' tcx > ) ,
1322
1324
{
1323
- let LifetimeContext { tcx, map, lifetime_uses, missing_named_lifetime_spots , .. } = self ;
1325
+ let LifetimeContext { tcx, map, lifetime_uses, .. } = self ;
1324
1326
let labels_in_fn = take ( & mut self . labels_in_fn ) ;
1325
1327
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 ) ;
1326
1329
let mut this = LifetimeContext {
1327
1330
tcx : * tcx,
1328
1331
map : map,
@@ -1332,14 +1335,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1332
1335
labels_in_fn,
1333
1336
xcrate_object_lifetime_defaults,
1334
1337
lifetime_uses,
1335
- missing_named_lifetime_spots : missing_named_lifetime_spots . to_vec ( ) ,
1338
+ missing_named_lifetime_spots,
1336
1339
} ;
1337
1340
debug ! ( "entering scope {:?}" , this. scope) ;
1338
1341
f ( self . scope , & mut this) ;
1339
1342
this. check_uses_for_lifetimes_defined_by_scope ( ) ;
1340
1343
debug ! ( "exiting scope {:?}" , this. scope) ;
1341
1344
self . labels_in_fn = this. labels_in_fn ;
1342
1345
self . xcrate_object_lifetime_defaults = this. xcrate_object_lifetime_defaults ;
1346
+ self . missing_named_lifetime_spots = this. missing_named_lifetime_spots ;
1343
1347
}
1344
1348
1345
1349
/// helper method to determine the span to remove when suggesting the
@@ -2894,74 +2898,3 @@ fn insert_late_bound_lifetimes(
2894
2898
}
2895
2899
}
2896
2900
}
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