@@ -85,6 +85,7 @@ use std::mem;
8585
8686use crate :: build:: { BlockAnd , BlockAndExtension , BlockFrame , Builder , CFG } ;
8787use rustc_data_structures:: fx:: FxHashMap ;
88+ use rustc_hir:: HirId ;
8889use rustc_index:: vec:: IndexVec ;
8990use rustc_middle:: middle:: region;
9091use rustc_middle:: mir:: * ;
@@ -567,25 +568,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
567568 F : FnOnce ( & mut Builder < ' a , ' tcx > ) -> BlockAnd < R > ,
568569 {
569570 let source_scope = self . source_scope ;
570- let tcx = self . tcx ;
571571 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) ;
589575 }
590576 self . push_scope ( region_scope) ;
591577 let mut block;
@@ -758,6 +744,40 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
758744 ) )
759745 }
760746
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+
761781 /// Creates a new source scope, nested in the current one.
762782 pub ( crate ) fn new_source_scope (
763783 & mut self ,
0 commit comments