@@ -394,6 +394,11 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
394
394
395
395
/// Fields used to add information to diagnostic errors.
396
396
diagnostic_metadata : DiagnosticMetadata < ' ast > ,
397
+
398
+ /// Whether to report resolution errors for item bodies.
399
+ ///
400
+ /// In particular, rustdoc uses this to avoid giving errors for `cfg()` items.
401
+ ignore_bodies : bool ,
397
402
}
398
403
399
404
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -627,7 +632,10 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
627
632
}
628
633
629
634
impl < ' a , ' b , ' ast > LateResolutionVisitor < ' a , ' b , ' ast > {
630
- fn new ( resolver : & ' b mut Resolver < ' a > ) -> LateResolutionVisitor < ' a , ' b , ' ast > {
635
+ fn new (
636
+ resolver : & ' b mut Resolver < ' a > ,
637
+ ignore_bodies : bool ,
638
+ ) -> LateResolutionVisitor < ' a , ' b , ' ast > {
631
639
// During late resolution we only track the module component of the parent scope,
632
640
// although it may be useful to track other components as well for diagnostics.
633
641
let graph_root = resolver. graph_root ;
@@ -644,6 +652,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
644
652
label_ribs : Vec :: new ( ) ,
645
653
current_trait_ref : None ,
646
654
diagnostic_metadata : DiagnosticMetadata :: default ( ) ,
655
+ ignore_bodies,
647
656
}
648
657
}
649
658
@@ -757,7 +766,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
757
766
return if self . is_label_valid_from_rib ( i) {
758
767
Some ( * id)
759
768
} else {
760
- self . r . report_error (
769
+ self . report_error (
761
770
original_span,
762
771
ResolutionError :: UnreachableLabel {
763
772
name : label. name ,
@@ -775,7 +784,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
775
784
suggestion = suggestion. or_else ( || self . suggestion_for_label_in_rib ( i, label) ) ;
776
785
}
777
786
778
- self . r . report_error (
787
+ self . report_error (
779
788
original_span,
780
789
ResolutionError :: UndeclaredLabel { name : label. name , suggestion } ,
781
790
) ;
@@ -1008,7 +1017,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1008
1017
if seen_bindings. contains_key ( & ident) {
1009
1018
let span = seen_bindings. get ( & ident) . unwrap ( ) ;
1010
1019
let err = ResolutionError :: NameAlreadyUsedInParameterList ( ident. name , * span) ;
1011
- self . r . report_error ( param. ident . span , err) ;
1020
+ self . report_error ( param. ident . span , err) ;
1012
1021
}
1013
1022
seen_bindings. entry ( ident) . or_insert ( param. ident . span ) ;
1014
1023
@@ -1274,7 +1283,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1274
1283
. is_err ( )
1275
1284
{
1276
1285
let path = & self . current_trait_ref . as_ref ( ) . unwrap ( ) . 1 . path ;
1277
- self . r . report_error ( span, err ( ident. name , & path_names_to_string ( path) ) ) ;
1286
+ self . report_error ( span, err ( ident. name , & path_names_to_string ( path) ) ) ;
1278
1287
}
1279
1288
}
1280
1289
}
@@ -1390,7 +1399,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1390
1399
if inconsistent_vars. contains_key ( name) {
1391
1400
v. could_be_path = false ;
1392
1401
}
1393
- self . r . report_error (
1402
+ self . report_error (
1394
1403
* v. origin . iter ( ) . next ( ) . unwrap ( ) ,
1395
1404
ResolutionError :: VariableNotBoundInPattern ( v) ,
1396
1405
) ;
@@ -1400,7 +1409,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1400
1409
let mut inconsistent_vars = inconsistent_vars. iter ( ) . collect :: < Vec < _ > > ( ) ;
1401
1410
inconsistent_vars. sort ( ) ;
1402
1411
for ( name, v) in inconsistent_vars {
1403
- self . r . report_error ( v. 0 , ResolutionError :: VariableBoundWithDifferentMode ( * name, v. 1 ) ) ;
1412
+ self . report_error ( v. 0 , ResolutionError :: VariableBoundWithDifferentMode ( * name, v. 1 ) ) ;
1404
1413
}
1405
1414
1406
1415
// 5) Finally bubble up all the binding maps.
@@ -1550,7 +1559,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1550
1559
// `Variant(a, a)`:
1551
1560
_ => IdentifierBoundMoreThanOnceInSamePattern ,
1552
1561
} ;
1553
- self . r . report_error ( ident. span , error ( ident. name ) ) ;
1562
+ self . report_error ( ident. span , error ( ident. name ) ) ;
1554
1563
}
1555
1564
1556
1565
// Record as bound if it's valid:
@@ -1624,7 +1633,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1624
1633
// to something unusable as a pattern (e.g., constructor function),
1625
1634
// but we still conservatively report an error, see
1626
1635
// issues/33118#issuecomment-233962221 for one reason why.
1627
- self . r . report_error (
1636
+ self . report_error (
1628
1637
ident. span ,
1629
1638
ResolutionError :: BindingShadowsSomethingUnacceptable (
1630
1639
pat_src. descr ( ) ,
@@ -1809,7 +1818,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1809
1818
1810
1819
Err ( err) => {
1811
1820
if let Some ( err) = report_errors_for_call ( self , err) {
1812
- self . r . report_error ( err. span , err. node ) ;
1821
+ self . report_error ( err. span , err. node ) ;
1813
1822
}
1814
1823
1815
1824
PartialRes :: new ( Res :: Err )
@@ -1843,6 +1852,15 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1843
1852
if let Some ( LexicalScopeBinding :: Res ( res) ) = binding { res != Res :: Err } else { false }
1844
1853
}
1845
1854
1855
+ /// A wrapper around [`Resolver::report_error`].
1856
+ ///
1857
+ /// This doesn't emit errors for function bodies if `ignore_bodies` is set.
1858
+ fn report_error ( & self , span : Span , resolution_error : ResolutionError < ' _ > ) {
1859
+ if !self . ignore_bodies || self . diagnostic_metadata . current_function . is_none ( ) {
1860
+ self . r . report_error ( span, resolution_error) ;
1861
+ }
1862
+ }
1863
+
1846
1864
// Resolve in alternative namespaces if resolution in the primary namespace fails.
1847
1865
fn resolve_qpath_anywhere (
1848
1866
& mut self ,
@@ -2339,8 +2357,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2339
2357
}
2340
2358
2341
2359
impl < ' a > Resolver < ' a > {
2342
- pub ( crate ) fn late_resolve_crate ( & mut self , krate : & Crate ) {
2343
- let mut late_resolution_visitor = LateResolutionVisitor :: new ( self ) ;
2360
+ pub ( crate ) fn late_resolve_crate ( & mut self , krate : & Crate , ignore_bodies : bool ) {
2361
+ let mut late_resolution_visitor = LateResolutionVisitor :: new ( self , ignore_bodies ) ;
2344
2362
visit:: walk_crate ( & mut late_resolution_visitor, krate) ;
2345
2363
for ( id, span) in late_resolution_visitor. diagnostic_metadata . unused_labels . iter ( ) {
2346
2364
self . lint_buffer . buffer_lint ( lint:: builtin:: UNUSED_LABELS , * id, * span, "unused label" ) ;
0 commit comments