@@ -323,7 +323,7 @@ impl<'a> PathSource<'a> {
323
323
}
324
324
325
325
#[ derive( Default ) ]
326
- struct DiagnosticMetadata {
326
+ struct DiagnosticMetadata < ' ast > {
327
327
/// The current trait's associated types' ident, used for diagnostic suggestions.
328
328
current_trait_assoc_types : Vec < Ident > ,
329
329
@@ -333,6 +333,13 @@ struct DiagnosticMetadata {
333
333
/// The current self item if inside an ADT (used for better errors).
334
334
current_self_item : Option < NodeId > ,
335
335
336
+ /// The current trait (used to suggest).
337
+ current_item : Option < & ' ast Item > ,
338
+
339
+ /// When processing generics and encountering a type not found, suggest introducing a type
340
+ /// param.
341
+ currently_processing_generics : bool ,
342
+
336
343
/// The current enclosing function (used for better errors).
337
344
current_function : Option < Span > ,
338
345
@@ -347,7 +354,7 @@ struct DiagnosticMetadata {
347
354
current_let_binding : Option < ( Span , Option < Span > , Option < Span > ) > ,
348
355
}
349
356
350
- struct LateResolutionVisitor < ' a , ' b > {
357
+ struct LateResolutionVisitor < ' a , ' b , ' ast > {
351
358
r : & ' b mut Resolver < ' a > ,
352
359
353
360
/// The module that represents the current item scope.
@@ -364,30 +371,32 @@ struct LateResolutionVisitor<'a, 'b> {
364
371
current_trait_ref : Option < ( Module < ' a > , TraitRef ) > ,
365
372
366
373
/// Fields used to add information to diagnostic errors.
367
- diagnostic_metadata : DiagnosticMetadata ,
374
+ diagnostic_metadata : DiagnosticMetadata < ' ast > ,
368
375
}
369
376
370
377
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
371
- impl < ' a , ' tcx > Visitor < ' tcx > for LateResolutionVisitor < ' a , ' _ > {
372
- fn visit_item ( & mut self , item : & ' tcx Item ) {
378
+ impl < ' a , ' ast > Visitor < ' ast > for LateResolutionVisitor < ' a , ' _ , ' ast > {
379
+ fn visit_item ( & mut self , item : & ' ast Item ) {
380
+ let prev = replace ( & mut self . diagnostic_metadata . current_item , Some ( item) ) ;
373
381
self . resolve_item ( item) ;
382
+ self . diagnostic_metadata . current_item = prev;
374
383
}
375
- fn visit_arm ( & mut self , arm : & ' tcx Arm ) {
384
+ fn visit_arm ( & mut self , arm : & ' ast Arm ) {
376
385
self . resolve_arm ( arm) ;
377
386
}
378
- fn visit_block ( & mut self , block : & ' tcx Block ) {
387
+ fn visit_block ( & mut self , block : & ' ast Block ) {
379
388
self . resolve_block ( block) ;
380
389
}
381
- fn visit_anon_const ( & mut self , constant : & ' tcx AnonConst ) {
390
+ fn visit_anon_const ( & mut self , constant : & ' ast AnonConst ) {
382
391
debug ! ( "visit_anon_const {:?}" , constant) ;
383
392
self . with_constant_rib ( |this| {
384
393
visit:: walk_anon_const ( this, constant) ;
385
394
} ) ;
386
395
}
387
- fn visit_expr ( & mut self , expr : & ' tcx Expr ) {
396
+ fn visit_expr ( & mut self , expr : & ' ast Expr ) {
388
397
self . resolve_expr ( expr, None ) ;
389
398
}
390
- fn visit_local ( & mut self , local : & ' tcx Local ) {
399
+ fn visit_local ( & mut self , local : & ' ast Local ) {
391
400
let local_spans = match local. pat . kind {
392
401
// We check for this to avoid tuple struct fields.
393
402
PatKind :: Wild => None ,
@@ -401,7 +410,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
401
410
self . resolve_local ( local) ;
402
411
self . diagnostic_metadata . current_let_binding = original;
403
412
}
404
- fn visit_ty ( & mut self , ty : & ' tcx Ty ) {
413
+ fn visit_ty ( & mut self , ty : & ' ast Ty ) {
405
414
match ty. kind {
406
415
TyKind :: Path ( ref qself, ref path) => {
407
416
self . smart_resolve_path ( ty. id , qself. as_ref ( ) , path, PathSource :: Type ) ;
@@ -417,7 +426,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
417
426
}
418
427
visit:: walk_ty ( self , ty) ;
419
428
}
420
- fn visit_poly_trait_ref ( & mut self , tref : & ' tcx PolyTraitRef , m : & ' tcx TraitBoundModifier ) {
429
+ fn visit_poly_trait_ref ( & mut self , tref : & ' ast PolyTraitRef , m : & ' ast TraitBoundModifier ) {
421
430
self . smart_resolve_path (
422
431
tref. trait_ref . ref_id ,
423
432
None ,
@@ -426,7 +435,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
426
435
) ;
427
436
visit:: walk_poly_trait_ref ( self , tref, m) ;
428
437
}
429
- fn visit_foreign_item ( & mut self , foreign_item : & ' tcx ForeignItem ) {
438
+ fn visit_foreign_item ( & mut self , foreign_item : & ' ast ForeignItem ) {
430
439
match foreign_item. kind {
431
440
ForeignItemKind :: Fn ( _, ref generics) => {
432
441
self . with_generic_param_rib ( generics, ItemRibKind ( HasGenericParams :: Yes ) , |this| {
@@ -443,7 +452,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
443
452
}
444
453
}
445
454
}
446
- fn visit_fn ( & mut self , fn_kind : FnKind < ' tcx > , declaration : & ' tcx FnDecl , sp : Span , _: NodeId ) {
455
+ fn visit_fn ( & mut self , fn_kind : FnKind < ' ast > , declaration : & ' ast FnDecl , sp : Span , _: NodeId ) {
447
456
let previous_value = replace ( & mut self . diagnostic_metadata . current_function , Some ( sp) ) ;
448
457
debug ! ( "(resolving function) entering function" ) ;
449
458
let rib_kind = match fn_kind {
@@ -472,7 +481,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
472
481
self . diagnostic_metadata . current_function = previous_value;
473
482
}
474
483
475
- fn visit_generics ( & mut self , generics : & ' tcx Generics ) {
484
+ fn visit_generics ( & mut self , generics : & ' ast Generics ) {
476
485
// For type parameter defaults, we have to ban access
477
486
// to following type parameters, as the InternalSubsts can only
478
487
// provide previous type parameters as they're built. We
@@ -534,11 +543,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
534
543
}
535
544
}
536
545
537
- fn visit_generic_arg ( & mut self , arg : & ' tcx GenericArg ) {
546
+ fn visit_generic_arg ( & mut self , arg : & ' ast GenericArg ) {
538
547
debug ! ( "visit_generic_arg({:?})" , arg) ;
548
+ let prev = replace ( & mut self . diagnostic_metadata . currently_processing_generics , true ) ;
539
549
match arg {
540
550
GenericArg :: Type ( ref ty) => {
541
- // We parse const arguments as path types as we cannot distiguish them durring
551
+ // We parse const arguments as path types as we cannot distiguish them during
542
552
// parsing. We try to resolve that ambiguity by attempting resolution the type
543
553
// namespace first, and if that fails we try again in the value namespace. If
544
554
// resolution in the value namespace succeeds, we have an generic const argument on
@@ -556,7 +566,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
556
566
)
557
567
. is_some ( )
558
568
} ;
559
-
560
569
if !check_ns ( TypeNS ) && check_ns ( ValueNS ) {
561
570
// This must be equivalent to `visit_anon_const`, but we cannot call it
562
571
// directly due to visitor lifetimes so we have to copy-paste some code.
@@ -574,6 +583,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
574
583
this. visit_path ( path, ty. id ) ;
575
584
} ) ;
576
585
586
+ self . diagnostic_metadata . currently_processing_generics = prev;
577
587
return ;
578
588
}
579
589
}
@@ -584,11 +594,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
584
594
GenericArg :: Lifetime ( lt) => self . visit_lifetime ( lt) ,
585
595
GenericArg :: Const ( ct) => self . visit_anon_const ( ct) ,
586
596
}
597
+ self . diagnostic_metadata . currently_processing_generics = prev;
587
598
}
588
599
}
589
600
590
- impl < ' a , ' b > LateResolutionVisitor < ' a , ' _ > {
591
- fn new ( resolver : & ' b mut Resolver < ' a > ) -> LateResolutionVisitor < ' a , ' b > {
601
+ impl < ' a , ' b , ' ast > LateResolutionVisitor < ' a , ' b , ' ast > {
602
+ fn new ( resolver : & ' b mut Resolver < ' a > ) -> LateResolutionVisitor < ' a , ' b , ' ast > {
592
603
// During late resolution we only track the module component of the parent scope,
593
604
// although it may be useful to track other components as well for diagnostics.
594
605
let graph_root = resolver. graph_root ;
@@ -724,7 +735,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
724
735
None
725
736
}
726
737
727
- fn resolve_adt ( & mut self , item : & Item , generics : & Generics ) {
738
+ fn resolve_adt ( & mut self , item : & ' ast Item , generics : & ' ast Generics ) {
728
739
debug ! ( "resolve_adt" ) ;
729
740
self . with_current_self_item ( item, |this| {
730
741
this. with_generic_param_rib ( generics, ItemRibKind ( HasGenericParams :: Yes ) , |this| {
@@ -778,7 +789,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
778
789
}
779
790
}
780
791
781
- fn resolve_item ( & mut self , item : & Item ) {
792
+ fn resolve_item ( & mut self , item : & ' ast Item ) {
782
793
let name = item. ident . name ;
783
794
debug ! ( "(resolving item) resolving {} ({:?})" , name, item. kind) ;
784
795
@@ -1024,16 +1035,15 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1024
1035
let mut new_id = None ;
1025
1036
if let Some ( trait_ref) = opt_trait_ref {
1026
1037
let path: Vec < _ > = Segment :: from_path ( & trait_ref. path ) ;
1027
- let res = self
1028
- . smart_resolve_path_fragment (
1029
- trait_ref. ref_id ,
1030
- None ,
1031
- & path,
1032
- trait_ref. path . span ,
1033
- PathSource :: Trait ( AliasPossibility :: No ) ,
1034
- CrateLint :: SimplePath ( trait_ref. ref_id ) ,
1035
- )
1036
- . base_res ( ) ;
1038
+ let res = self . smart_resolve_path_fragment (
1039
+ trait_ref. ref_id ,
1040
+ None ,
1041
+ & path,
1042
+ trait_ref. path . span ,
1043
+ PathSource :: Trait ( AliasPossibility :: No ) ,
1044
+ CrateLint :: SimplePath ( trait_ref. ref_id ) ,
1045
+ ) ;
1046
+ let res = res. base_res ( ) ;
1037
1047
if res != Res :: Err {
1038
1048
new_id = Some ( res. def_id ( ) ) ;
1039
1049
let span = trait_ref. path . span ;
@@ -1070,11 +1080,11 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1070
1080
1071
1081
fn resolve_implementation (
1072
1082
& mut self ,
1073
- generics : & Generics ,
1074
- opt_trait_reference : & Option < TraitRef > ,
1075
- self_type : & Ty ,
1083
+ generics : & ' ast Generics ,
1084
+ opt_trait_reference : & ' ast Option < TraitRef > ,
1085
+ self_type : & ' ast Ty ,
1076
1086
item_id : NodeId ,
1077
- impl_items : & [ AssocItem ] ,
1087
+ impl_items : & ' ast [ AssocItem ] ,
1078
1088
) {
1079
1089
debug ! ( "resolve_implementation" ) ;
1080
1090
// If applicable, create a rib for the type parameters.
@@ -1179,7 +1189,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1179
1189
}
1180
1190
}
1181
1191
1182
- fn resolve_params ( & mut self , params : & [ Param ] ) {
1192
+ fn resolve_params ( & mut self , params : & ' ast [ Param ] ) {
1183
1193
let mut bindings = smallvec ! [ ( PatBoundCtx :: Product , Default :: default ( ) ) ] ;
1184
1194
for Param { pat, ty, .. } in params {
1185
1195
self . resolve_pattern ( pat, PatternSource :: FnParam , & mut bindings) ;
@@ -1188,7 +1198,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1188
1198
}
1189
1199
}
1190
1200
1191
- fn resolve_local ( & mut self , local : & Local ) {
1201
+ fn resolve_local ( & mut self , local : & ' ast Local ) {
1192
1202
// Resolve the type.
1193
1203
walk_list ! ( self , visit_ty, & local. ty) ;
1194
1204
@@ -1307,7 +1317,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1307
1317
}
1308
1318
1309
1319
/// Check the consistency of the outermost or-patterns.
1310
- fn check_consistent_bindings_top ( & mut self , pat : & Pat ) {
1320
+ fn check_consistent_bindings_top ( & mut self , pat : & ' ast Pat ) {
1311
1321
pat. walk ( & mut |pat| match pat. kind {
1312
1322
PatKind :: Or ( ref ps) => {
1313
1323
self . check_consistent_bindings ( ps) ;
@@ -1317,7 +1327,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1317
1327
} )
1318
1328
}
1319
1329
1320
- fn resolve_arm ( & mut self , arm : & Arm ) {
1330
+ fn resolve_arm ( & mut self , arm : & ' ast Arm ) {
1321
1331
self . with_rib ( ValueNS , NormalRibKind , |this| {
1322
1332
this. resolve_pattern_top ( & arm. pat , PatternSource :: Match ) ;
1323
1333
walk_list ! ( this, visit_expr, & arm. guard) ;
@@ -1326,14 +1336,14 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1326
1336
}
1327
1337
1328
1338
/// Arising from `source`, resolve a top level pattern.
1329
- fn resolve_pattern_top ( & mut self , pat : & Pat , pat_src : PatternSource ) {
1339
+ fn resolve_pattern_top ( & mut self , pat : & ' ast Pat , pat_src : PatternSource ) {
1330
1340
let mut bindings = smallvec ! [ ( PatBoundCtx :: Product , Default :: default ( ) ) ] ;
1331
1341
self . resolve_pattern ( pat, pat_src, & mut bindings) ;
1332
1342
}
1333
1343
1334
1344
fn resolve_pattern (
1335
1345
& mut self ,
1336
- pat : & Pat ,
1346
+ pat : & ' ast Pat ,
1337
1347
pat_src : PatternSource ,
1338
1348
bindings : & mut SmallVec < [ ( PatBoundCtx , FxHashSet < Ident > ) ; 1 ] > ,
1339
1349
) {
@@ -1544,7 +1554,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1544
1554
id : NodeId ,
1545
1555
qself : Option < & QSelf > ,
1546
1556
path : & Path ,
1547
- source : PathSource < ' _ > ,
1557
+ source : PathSource < ' ast > ,
1548
1558
) {
1549
1559
self . smart_resolve_path_fragment (
1550
1560
id,
@@ -1562,7 +1572,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1562
1572
qself : Option < & QSelf > ,
1563
1573
path : & [ Segment ] ,
1564
1574
span : Span ,
1565
- source : PathSource < ' _ > ,
1575
+ source : PathSource < ' ast > ,
1566
1576
crate_lint : CrateLint ,
1567
1577
) -> PartialRes {
1568
1578
let ns = source. namespace ( ) ;
@@ -1573,7 +1583,9 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1573
1583
let def_id = this. parent_scope . module . normal_ancestor_id ;
1574
1584
let node_id = this. r . definitions . as_local_node_id ( def_id) . unwrap ( ) ;
1575
1585
let better = res. is_some ( ) ;
1576
- this. r . use_injections . push ( UseError { err, candidates, node_id, better } ) ;
1586
+ let suggestion =
1587
+ if res. is_none ( ) { this. report_missing_type_error ( path) } else { None } ;
1588
+ this. r . use_injections . push ( UseError { err, candidates, node_id, better, suggestion } ) ;
1577
1589
PartialRes :: new ( Res :: Err )
1578
1590
} ;
1579
1591
@@ -1838,11 +1850,11 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1838
1850
}
1839
1851
}
1840
1852
1841
- fn resolve_labeled_block ( & mut self , label : Option < Label > , id : NodeId , block : & Block ) {
1853
+ fn resolve_labeled_block ( & mut self , label : Option < Label > , id : NodeId , block : & ' ast Block ) {
1842
1854
self . with_resolved_label ( label, id, |this| this. visit_block ( block) ) ;
1843
1855
}
1844
1856
1845
- fn resolve_block ( & mut self , block : & Block ) {
1857
+ fn resolve_block ( & mut self , block : & ' ast Block ) {
1846
1858
debug ! ( "(resolving block) entering block" ) ;
1847
1859
// Move down in the graph, if there's an anonymous module rooted here.
1848
1860
let orig_module = self . parent_scope . module ;
@@ -1885,7 +1897,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
1885
1897
debug ! ( "(resolving block) leaving block" ) ;
1886
1898
}
1887
1899
1888
- fn resolve_expr ( & mut self , expr : & Expr , parent : Option < & Expr > ) {
1900
+ fn resolve_expr ( & mut self , expr : & ' ast Expr , parent : Option < & ' ast Expr > ) {
1889
1901
// First, record candidate traits for this expression if it could
1890
1902
// result in the invocation of a method call.
1891
1903
@@ -2023,7 +2035,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
2023
2035
}
2024
2036
}
2025
2037
2026
- fn record_candidate_traits_for_expr_if_necessary ( & mut self , expr : & Expr ) {
2038
+ fn record_candidate_traits_for_expr_if_necessary ( & mut self , expr : & ' ast Expr ) {
2027
2039
match expr. kind {
2028
2040
ExprKind :: Field ( _, ident) => {
2029
2041
// FIXME(#6890): Even though you can't treat a method like a
0 commit comments