@@ -394,6 +394,11 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
394394
395395 /// Fields used to add information to diagnostic errors.
396396 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 ,
397402}
398403
399404/// 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> {
627632}
628633
629634impl < ' 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 > {
631639 // During late resolution we only track the module component of the parent scope,
632640 // although it may be useful to track other components as well for diagnostics.
633641 let graph_root = resolver. graph_root ;
@@ -644,6 +652,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
644652 label_ribs : Vec :: new ( ) ,
645653 current_trait_ref : None ,
646654 diagnostic_metadata : DiagnosticMetadata :: default ( ) ,
655+ ignore_bodies,
647656 }
648657 }
649658
@@ -757,7 +766,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
757766 return if self . is_label_valid_from_rib ( i) {
758767 Some ( * id)
759768 } else {
760- self . r . report_error (
769+ self . report_error (
761770 original_span,
762771 ResolutionError :: UnreachableLabel {
763772 name : label. name ,
@@ -775,7 +784,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
775784 suggestion = suggestion. or_else ( || self . suggestion_for_label_in_rib ( i, label) ) ;
776785 }
777786
778- self . r . report_error (
787+ self . report_error (
779788 original_span,
780789 ResolutionError :: UndeclaredLabel { name : label. name , suggestion } ,
781790 ) ;
@@ -1008,7 +1017,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10081017 if seen_bindings. contains_key ( & ident) {
10091018 let span = seen_bindings. get ( & ident) . unwrap ( ) ;
10101019 let err = ResolutionError :: NameAlreadyUsedInParameterList ( ident. name , * span) ;
1011- self . r . report_error ( param. ident . span , err) ;
1020+ self . report_error ( param. ident . span , err) ;
10121021 }
10131022 seen_bindings. entry ( ident) . or_insert ( param. ident . span ) ;
10141023
@@ -1274,7 +1283,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12741283 . is_err ( )
12751284 {
12761285 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) ) ) ;
12781287 }
12791288 }
12801289 }
@@ -1390,7 +1399,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
13901399 if inconsistent_vars. contains_key ( name) {
13911400 v. could_be_path = false ;
13921401 }
1393- self . r . report_error (
1402+ self . report_error (
13941403 * v. origin . iter ( ) . next ( ) . unwrap ( ) ,
13951404 ResolutionError :: VariableNotBoundInPattern ( v) ,
13961405 ) ;
@@ -1400,7 +1409,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14001409 let mut inconsistent_vars = inconsistent_vars. iter ( ) . collect :: < Vec < _ > > ( ) ;
14011410 inconsistent_vars. sort ( ) ;
14021411 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 ) ) ;
14041413 }
14051414
14061415 // 5) Finally bubble up all the binding maps.
@@ -1550,7 +1559,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15501559 // `Variant(a, a)`:
15511560 _ => IdentifierBoundMoreThanOnceInSamePattern ,
15521561 } ;
1553- self . r . report_error ( ident. span , error ( ident. name ) ) ;
1562+ self . report_error ( ident. span , error ( ident. name ) ) ;
15541563 }
15551564
15561565 // Record as bound if it's valid:
@@ -1624,7 +1633,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16241633 // to something unusable as a pattern (e.g., constructor function),
16251634 // but we still conservatively report an error, see
16261635 // issues/33118#issuecomment-233962221 for one reason why.
1627- self . r . report_error (
1636+ self . report_error (
16281637 ident. span ,
16291638 ResolutionError :: BindingShadowsSomethingUnacceptable (
16301639 pat_src. descr ( ) ,
@@ -1809,7 +1818,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18091818
18101819 Err ( err) => {
18111820 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 ) ;
18131822 }
18141823
18151824 PartialRes :: new ( Res :: Err )
@@ -1843,6 +1852,15 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18431852 if let Some ( LexicalScopeBinding :: Res ( res) ) = binding { res != Res :: Err } else { false }
18441853 }
18451854
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+
18461864 // Resolve in alternative namespaces if resolution in the primary namespace fails.
18471865 fn resolve_qpath_anywhere (
18481866 & mut self ,
@@ -2339,8 +2357,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23392357}
23402358
23412359impl < ' 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 ) ;
23442362 visit:: walk_crate ( & mut late_resolution_visitor, krate) ;
23452363 for ( id, span) in late_resolution_visitor. diagnostic_metadata . unused_labels . iter ( ) {
23462364 self . lint_buffer . buffer_lint ( lint:: builtin:: UNUSED_LABELS , * id, * span, "unused label" ) ;
0 commit comments