@@ -85,6 +85,7 @@ use std::mem;
85
85
86
86
use crate :: build:: { BlockAnd , BlockAndExtension , BlockFrame , Builder , CFG } ;
87
87
use rustc_data_structures:: fx:: FxHashMap ;
88
+ use rustc_hir:: HirId ;
88
89
use rustc_index:: vec:: IndexVec ;
89
90
use rustc_middle:: middle:: region;
90
91
use rustc_middle:: mir:: * ;
@@ -567,25 +568,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
567
568
F : FnOnce ( & mut Builder < ' a , ' tcx > ) -> BlockAnd < R > ,
568
569
{
569
570
let source_scope = self . source_scope ;
570
- let tcx = self . tcx ;
571
571
if let LintLevel :: Explicit ( current_hir_id) = lint_level {
572
- // Use `maybe_lint_level_root_bounded` with `root_lint_level` as a bound
573
- // to avoid adding Hir dependencies on our parents.
574
- // We estimate the true lint roots here to avoid creating a lot of source scopes.
575
-
576
- let parent_root = tcx. maybe_lint_level_root_bounded (
577
- self . source_scopes [ source_scope] . local_data . as_ref ( ) . assert_crate_local ( ) . lint_root ,
578
- self . hir_id ,
579
- ) ;
580
- let current_root = tcx. maybe_lint_level_root_bounded ( current_hir_id, self . hir_id ) ;
581
-
582
- if parent_root != current_root {
583
- self . source_scope = self . new_source_scope (
584
- region_scope. 1 . span ,
585
- LintLevel :: Explicit ( current_root) ,
586
- None ,
587
- ) ;
588
- }
572
+ let parent_id =
573
+ self . source_scopes [ source_scope] . local_data . as_ref ( ) . assert_crate_local ( ) . lint_root ;
574
+ self . maybe_new_source_scope ( region_scope. 1 . span , None , current_hir_id, parent_id) ;
589
575
}
590
576
self . push_scope ( region_scope) ;
591
577
let mut block;
@@ -758,6 +744,40 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
758
744
) )
759
745
}
760
746
747
+ /// Possibly creates a new source scope if `current_root` and `parent_root`
748
+ /// are different, or if -Zmaximal-hir-to-mir-coverage is enabled.
749
+ pub ( crate ) fn maybe_new_source_scope (
750
+ & mut self ,
751
+ span : Span ,
752
+ safety : Option < Safety > ,
753
+ current_id : HirId ,
754
+ parent_id : HirId ,
755
+ ) {
756
+ let ( current_root, parent_root) =
757
+ if self . tcx . sess . opts . unstable_opts . maximal_hir_to_mir_coverage {
758
+ // Some consumers of rustc need to map MIR locations back to HIR nodes. Currently the
759
+ // the only part of rustc that tracks MIR -> HIR is the `SourceScopeLocalData::lint_root`
760
+ // field that tracks lint levels for MIR locations. Normally the number of source scopes
761
+ // is limited to the set of nodes with lint annotations. The -Zmaximal-hir-to-mir-coverage
762
+ // flag changes this behavior to maximize the number of source scopes, increasing the
763
+ // granularity of the MIR->HIR mapping.
764
+ ( current_id, parent_id)
765
+ } else {
766
+ // Use `maybe_lint_level_root_bounded` with `self.hir_id` as a bound
767
+ // to avoid adding Hir dependencies on our parents.
768
+ // We estimate the true lint roots here to avoid creating a lot of source scopes.
769
+ (
770
+ self . tcx . maybe_lint_level_root_bounded ( current_id, self . hir_id ) ,
771
+ self . tcx . maybe_lint_level_root_bounded ( parent_id, self . hir_id ) ,
772
+ )
773
+ } ;
774
+
775
+ if current_root != parent_root {
776
+ let lint_level = LintLevel :: Explicit ( current_root) ;
777
+ self . source_scope = self . new_source_scope ( span, lint_level, safety) ;
778
+ }
779
+ }
780
+
761
781
/// Creates a new source scope, nested in the current one.
762
782
pub ( crate ) fn new_source_scope (
763
783
& mut self ,
0 commit comments