@@ -376,19 +376,6 @@ struct DiagnosticMetadata<'ast> {
376
376
current_let_binding : Option < ( Span , Option < Span > , Option < Span > ) > ,
377
377
}
378
378
379
- /// Keeps track of whether errors should be reported.
380
- ///
381
- /// Used by rustdoc to ignore errors in function bodies.
382
- /// This is just a fancy boolean so it can have doc-comments.
383
- #[ derive( Copy , Clone , Debug ) ]
384
- pub enum IgnoreState {
385
- /// We are at global scope or in a trait implementation, so all errors should be reported.
386
- Report ,
387
- /// We are in a function body, so errors shouldn't be reported.
388
- Ignore ,
389
- // Note that we don't need to worry about macros, which must always be resolved (or we wouldn't have gotten to the late pass).
390
- }
391
-
392
379
struct LateResolutionVisitor < ' a , ' b , ' ast > {
393
380
r : & ' b mut Resolver < ' a > ,
394
381
@@ -408,12 +395,12 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
408
395
/// Fields used to add information to diagnostic errors.
409
396
diagnostic_metadata : DiagnosticMetadata < ' ast > ,
410
397
411
- /// State used to know whether to ignore resolution errors for item bodies.
398
+ /// State used to know whether to ignore resolution errors for function bodies.
412
399
///
413
400
/// In particular, rustdoc uses this to avoid giving errors for `cfg()` items.
414
401
/// In most cases this will be `None`, in which case errors will always be reported.
415
402
/// If it is `Some(_)`, then it will be updated when entering a nested function or trait body.
416
- ignore_bodies : Option < IgnoreState > ,
403
+ in_func_body : bool ,
417
404
}
418
405
419
406
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -517,18 +504,18 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
517
504
518
505
visit:: walk_fn_ret_ty ( this, & declaration. output ) ;
519
506
520
- let previous_ignore = this. ignore_bodies . take ( ) ;
521
- // Ignore errors in function bodies if originally passed `ignore_state: true`
507
+ let previous_state = this. in_func_body ;
508
+ // Ignore errors in function bodies if this is rustdoc
522
509
// Be sure not to set this until the function signature has been resolved.
523
- this. ignore_bodies = previous_ignore . and ( Some ( IgnoreState :: Ignore ) ) ;
510
+ this. in_func_body = true ;
524
511
// Resolve the function body, potentially inside the body of an async closure
525
512
match fn_kind {
526
513
FnKind :: Fn ( .., body) => walk_list ! ( this, visit_block, body) ,
527
514
FnKind :: Closure ( _, body) => this. visit_expr ( body) ,
528
515
} ;
529
516
530
517
debug ! ( "(resolving function) leaving function" ) ;
531
- this. ignore_bodies = previous_ignore ;
518
+ this. in_func_body = previous_state ;
532
519
} )
533
520
} ) ;
534
521
self . diagnostic_metadata . current_function = previous_value;
@@ -652,10 +639,7 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
652
639
}
653
640
654
641
impl < ' a , ' b , ' ast > LateResolutionVisitor < ' a , ' b , ' ast > {
655
- fn new (
656
- resolver : & ' b mut Resolver < ' a > ,
657
- ignore_bodies : IgnoreState ,
658
- ) -> LateResolutionVisitor < ' a , ' b , ' ast > {
642
+ fn new ( resolver : & ' b mut Resolver < ' a > ) -> LateResolutionVisitor < ' a , ' b , ' ast > {
659
643
// During late resolution we only track the module component of the parent scope,
660
644
// although it may be useful to track other components as well for diagnostics.
661
645
let graph_root = resolver. graph_root ;
@@ -672,11 +656,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
672
656
label_ribs : Vec :: new ( ) ,
673
657
current_trait_ref : None ,
674
658
diagnostic_metadata : DiagnosticMetadata :: default ( ) ,
675
- ignore_bodies : match ignore_bodies {
676
- // errors at module scope should always be reported
677
- IgnoreState :: Ignore => Some ( IgnoreState :: Report ) ,
678
- IgnoreState :: Report => None ,
679
- } ,
659
+ // errors at module scope should always be reported
660
+ in_func_body : false ,
680
661
}
681
662
}
682
663
@@ -1194,9 +1175,9 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1194
1175
impl_items : & ' ast [ P < AssocItem > ] ,
1195
1176
) {
1196
1177
debug ! ( "resolve_implementation" ) ;
1197
- let old_ignore = self . ignore_bodies . take ( ) ;
1178
+ let old_ignore = self . in_func_body ;
1198
1179
// Never ignore errors in trait implementations.
1199
- self . ignore_bodies = old_ignore . and ( Some ( IgnoreState :: Report ) ) ;
1180
+ self . in_func_body = false ;
1200
1181
// If applicable, create a rib for the type parameters.
1201
1182
self . with_generic_param_rib ( generics, ItemRibKind ( HasGenericParams :: Yes ) , |this| {
1202
1183
// Dummy self type for better errors if `Self` is used in the trait path.
@@ -1292,7 +1273,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1292
1273
} ) ;
1293
1274
} ) ;
1294
1275
} ) ;
1295
- self . ignore_bodies = old_ignore;
1276
+ self . in_func_body = old_ignore;
1296
1277
}
1297
1278
1298
1279
fn check_trait_item < F > ( & mut self , ident : Ident , ns : Namespace , span : Span , err : F )
@@ -1900,20 +1881,17 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1900
1881
1901
1882
/// A wrapper around [`Resolver::report_error`].
1902
1883
///
1903
- /// This doesn't emit errors for function bodies if `ignore_bodies` is set.
1884
+ /// This doesn't emit errors for function bodies if this is r
1904
1885
fn report_error ( & self , span : Span , resolution_error : ResolutionError < ' _ > ) {
1905
1886
if self . should_report_errs ( ) {
1906
1887
self . r . report_error ( span, resolution_error) ;
1907
1888
}
1908
1889
}
1909
1890
1910
1891
#[ inline]
1892
+ /// If we're actually rustdoc then avoid giving a name resolution error for `cfg()` items.
1911
1893
fn should_report_errs ( & self ) -> bool {
1912
- debug ! ( "should_report_errs(state={:?})" , self . ignore_bodies) ;
1913
- match self . ignore_bodies {
1914
- None | Some ( IgnoreState :: Report ) => true ,
1915
- Some ( IgnoreState :: Ignore ) => false ,
1916
- }
1894
+ !( self . r . session . opts . actually_rustdoc && self . in_func_body )
1917
1895
}
1918
1896
1919
1897
// Resolve in alternative namespaces if resolution in the primary namespace fails.
@@ -2412,8 +2390,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2412
2390
}
2413
2391
2414
2392
impl < ' a > Resolver < ' a > {
2415
- pub ( crate ) fn late_resolve_crate ( & mut self , krate : & Crate , ignore_bodies : IgnoreState ) {
2416
- let mut late_resolution_visitor = LateResolutionVisitor :: new ( self , ignore_bodies ) ;
2393
+ pub ( crate ) fn late_resolve_crate ( & mut self , krate : & Crate ) {
2394
+ let mut late_resolution_visitor = LateResolutionVisitor :: new ( self ) ;
2417
2395
visit:: walk_crate ( & mut late_resolution_visitor, krate) ;
2418
2396
for ( id, span) in late_resolution_visitor. diagnostic_metadata . unused_labels . iter ( ) {
2419
2397
self . lint_buffer . buffer_lint ( lint:: builtin:: UNUSED_LABELS , * id, * span, "unused label" ) ;
0 commit comments