Skip to content

Commit d595884

Browse files
committed
Move -Z maximal-hir-to-mir-coverage implementation to new maybe_new_source_scope method
1 parent 3bf7d88 commit d595884

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

compiler/rustc_middle/src/lint.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ impl TyCtxt<'_> {
173173
/// Walks upwards from `id` to find a node which might change lint levels with attributes.
174174
/// It stops at `bound` and just returns it if reached.
175175
pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId {
176-
// Some consumers of rustc need to map MIR locations back to HIR nodes. Currently the
177-
// the only part of rustc that tracks MIR -> HIR is the `SourceScopeLocalData::lint_root`
178-
// field that tracks lint levels for MIR locations. Normally the number of source scopes
179-
// is limited to the set of nodes with lint annotations. The -Zmaximal-hir-to-mir-coverage
180-
// flag changes this behavior to maximize the number of source scopes, increasing the
181-
// granularity of the MIR->HIR mapping.
182-
if self.sess.opts.unstable_opts.maximal_hir_to_mir_coverage {
183-
return id;
184-
}
185-
186176
let hir = self.hir();
187177
loop {
188178
if id == bound {

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -948,20 +948,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
948948
original_source_scope: SourceScope,
949949
pattern_span: Span,
950950
) {
951-
let tcx = self.tcx;
952-
let current_root = tcx.maybe_lint_level_root_bounded(arg_hir_id, self.hir_id);
953-
let parent_root = tcx.maybe_lint_level_root_bounded(
954-
self.source_scopes[original_source_scope]
955-
.local_data
956-
.as_ref()
957-
.assert_crate_local()
958-
.lint_root,
959-
self.hir_id,
960-
);
961-
if current_root != parent_root {
962-
self.source_scope =
963-
self.new_source_scope(pattern_span, LintLevel::Explicit(current_root), None);
964-
}
951+
let parent_id = self.source_scopes[original_source_scope]
952+
.local_data
953+
.as_ref()
954+
.assert_crate_local()
955+
.lint_root;
956+
self.maybe_new_source_scope(pattern_span, None, arg_hir_id, parent_id);
965957
}
966958

967959
fn get_unit_temp(&mut self) -> Place<'tcx> {

compiler/rustc_mir_build/src/build/scope.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use std::mem;
8585

8686
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
8787
use rustc_data_structures::fx::FxHashMap;
88+
use rustc_hir::HirId;
8889
use rustc_index::vec::IndexVec;
8990
use rustc_middle::middle::region;
9091
use 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

Comments
 (0)