Skip to content

Commit fd40d0c

Browse files
committed
rustc: move MIR source_scope_local_data's ClearCrossCrate to be around elements.
1 parent 2539b5f commit fd40d0c

File tree

8 files changed

+94
-104
lines changed

8 files changed

+94
-104
lines changed

src/librustc/mir/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub struct Body<'tcx> {
106106

107107
/// Crate-local information for each source scope, that can't (and
108108
/// needn't) be tracked across crates.
109-
pub source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
109+
pub source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
110110

111111
/// The yield type of the function, if it is a generator.
112112
pub yield_ty: Option<Ty<'tcx>>,
@@ -163,7 +163,7 @@ impl<'tcx> Body<'tcx> {
163163
pub fn new(
164164
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
165165
source_scopes: IndexVec<SourceScope, SourceScopeData>,
166-
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
166+
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
167167
local_decls: LocalDecls<'tcx>,
168168
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
169169
arg_count: usize,
@@ -429,6 +429,13 @@ pub enum ClearCrossCrate<T> {
429429
}
430430

431431
impl<T> ClearCrossCrate<T> {
432+
pub fn as_ref(&'a self) -> ClearCrossCrate<&'a T> {
433+
match self {
434+
ClearCrossCrate::Clear => ClearCrossCrate::Clear,
435+
ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
436+
}
437+
}
438+
432439
pub fn assert_crate_local(self) -> T {
433440
match self {
434441
ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),

src/librustc_mir/borrow_check/mod.rs

+34-33
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,10 @@ fn do_mir_borrowck<'a, 'tcx>(
300300
let mut initial_diag =
301301
mbcx.report_conflicting_borrow(location, (&place, span), bk, &borrow);
302302

303-
let lint_root = if let ClearCrossCrate::Set(ref vsi) = mbcx.body.source_scope_local_data {
304-
let scope = mbcx.body.source_info(location).scope;
305-
vsi[scope].lint_root
306-
} else {
307-
id
303+
let scope = mbcx.body.source_info(location).scope;
304+
let lint_root = match &mbcx.body.source_scope_local_data[scope] {
305+
ClearCrossCrate::Set(data) => data.lint_root,
306+
_ => id,
308307
};
309308

310309
// Span and message don't matter; we overwrite them below anyway
@@ -338,38 +337,40 @@ fn do_mir_borrowck<'a, 'tcx>(
338337
debug!("mbcx.used_mut: {:?}", mbcx.used_mut);
339338
let used_mut = mbcx.used_mut;
340339
for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) {
341-
if let ClearCrossCrate::Set(ref vsi) = mbcx.body.source_scope_local_data {
342-
let local_decl = &mbcx.body.local_decls[local];
343-
344-
// Skip over locals that begin with an underscore or have no name
345-
match mbcx.local_names[local] {
346-
Some(name) => if name.as_str().starts_with("_") {
347-
continue;
348-
},
349-
None => continue,
350-
}
340+
let local_decl = &mbcx.body.local_decls[local];
341+
let lint_root = match &mbcx.body.source_scope_local_data[local_decl.source_info.scope] {
342+
ClearCrossCrate::Set(data) => data.lint_root,
343+
_ => continue,
344+
};
351345

352-
let span = local_decl.source_info.span;
353-
if span.desugaring_kind().is_some() {
354-
// If the `mut` arises as part of a desugaring, we should ignore it.
346+
// Skip over locals that begin with an underscore or have no name
347+
match mbcx.local_names[local] {
348+
Some(name) => if name.as_str().starts_with("_") {
355349
continue;
356-
}
350+
},
351+
None => continue,
352+
}
357353

358-
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
359-
tcx.struct_span_lint_hir(
360-
UNUSED_MUT,
361-
vsi[local_decl.source_info.scope].lint_root,
362-
span,
363-
"variable does not need to be mutable",
364-
)
365-
.span_suggestion_short(
366-
mut_span,
367-
"remove this `mut`",
368-
String::new(),
369-
Applicability::MachineApplicable,
370-
)
371-
.emit();
354+
let span = local_decl.source_info.span;
355+
if span.desugaring_kind().is_some() {
356+
// If the `mut` arises as part of a desugaring, we should ignore it.
357+
continue;
372358
}
359+
360+
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
361+
tcx.struct_span_lint_hir(
362+
UNUSED_MUT,
363+
lint_root,
364+
span,
365+
"variable does not need to be mutable",
366+
)
367+
.span_suggestion_short(
368+
mut_span,
369+
"remove this `mut`",
370+
String::new(),
371+
Applicability::MachineApplicable,
372+
)
373+
.emit();
373374
}
374375

375376
// Buffer any move errors that we collected and de-duplicated.

src/librustc_mir/build/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ struct Builder<'a, 'tcx> {
309309
/// The vector of all scopes that we have created thus far;
310310
/// we track this for debuginfo later.
311311
source_scopes: IndexVec<SourceScope, SourceScopeData>,
312-
source_scope_local_data: IndexVec<SourceScope, SourceScopeLocalData>,
312+
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
313313
source_scope: SourceScope,
314314

315315
/// The guard-context: each time we build the guard expression for
@@ -741,7 +741,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
741741
Body::new(
742742
self.cfg.basic_blocks,
743743
self.source_scopes,
744-
ClearCrossCrate::Set(self.source_scope_local_data),
744+
self.source_scope_local_data,
745745
self.local_decls,
746746
self.canonical_user_type_annotations,
747747
self.arg_count,
@@ -941,7 +941,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
941941
self.hir.root_lint_level
942942
);
943943
let parent_root = tcx.maybe_lint_level_root_bounded(
944-
self.source_scope_local_data[original_source_scope].lint_root,
944+
self.source_scope_local_data[original_source_scope]
945+
.as_ref()
946+
.assert_crate_local()
947+
.lint_root,
945948
self.hir.root_lint_level,
946949
);
947950
if current_root != parent_root {

src/librustc_mir/build/scope.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
430430
// We estimate the true lint roots here to avoid creating a lot of source scopes.
431431

432432
let parent_root = tcx.maybe_lint_level_root_bounded(
433-
self.source_scope_local_data[source_scope].lint_root,
433+
self.source_scope_local_data[source_scope]
434+
.as_ref()
435+
.assert_crate_local()
436+
.lint_root,
434437
self.hir.root_lint_level,
435438
);
436439
let current_root = tcx.maybe_lint_level_root_bounded(
@@ -657,13 +660,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
657660
lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
658661
lint_root
659662
} else {
660-
self.source_scope_local_data[parent].lint_root
663+
self.source_scope_local_data[parent].as_ref().assert_crate_local().lint_root
661664
},
662665
safety: safety.unwrap_or_else(|| {
663-
self.source_scope_local_data[parent].safety
666+
self.source_scope_local_data[parent].as_ref().assert_crate_local().safety
664667
})
665668
};
666-
self.source_scope_local_data.push(scope_local_data);
669+
self.source_scope_local_data.push(ClearCrossCrate::Set(scope_local_data));
667670
scope
668671
}
669672

src/librustc_mir/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
849849
} else {
850850
block.terminator().source_info
851851
};
852-
match body.source_scope_local_data {
853-
mir::ClearCrossCrate::Set(ref ivs) => Some(ivs[source_info.scope].lint_root),
852+
match &body.source_scope_local_data[source_info.scope] {
853+
mir::ClearCrossCrate::Set(data) => Some(data.lint_root),
854854
mir::ClearCrossCrate::Clear => None,
855855
}
856856
});

src/librustc_mir/shim.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
201201
IndexVec::from_elem_n(
202202
SourceScopeData { span: span, parent_scope: None }, 1
203203
),
204-
ClearCrossCrate::Clear,
204+
IndexVec::from_elem_n(
205+
ClearCrossCrate::Clear, 1
206+
),
205207
local_decls_for_sig(&sig, span),
206208
IndexVec::new(),
207209
sig.inputs().len(),
@@ -367,7 +369,9 @@ impl CloneShimBuilder<'tcx> {
367369
IndexVec::from_elem_n(
368370
SourceScopeData { span: self.span, parent_scope: None }, 1
369371
),
370-
ClearCrossCrate::Clear,
372+
IndexVec::from_elem_n(
373+
ClearCrossCrate::Clear, 1
374+
),
371375
self.local_decls,
372376
IndexVec::new(),
373377
self.sig.inputs().len(),
@@ -827,7 +831,9 @@ fn build_call_shim<'tcx>(
827831
IndexVec::from_elem_n(
828832
SourceScopeData { span: span, parent_scope: None }, 1
829833
),
830-
ClearCrossCrate::Clear,
834+
IndexVec::from_elem_n(
835+
ClearCrossCrate::Clear, 1
836+
),
831837
local_decls,
832838
IndexVec::new(),
833839
sig.inputs().len(),
@@ -913,7 +919,9 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &Body<'_> {
913919
IndexVec::from_elem_n(
914920
SourceScopeData { span: span, parent_scope: None }, 1
915921
),
916-
ClearCrossCrate::Clear,
922+
IndexVec::from_elem_n(
923+
ClearCrossCrate::Clear, 1
924+
),
917925
local_decls,
918926
IndexVec::new(),
919927
sig.inputs().len(),

src/librustc_mir/transform/check_unsafety.rs

+9-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_index::vec::IndexVec;
3-
use rustc_data_structures::sync::Lrc;
42

53
use rustc::ty::query::Providers;
64
use rustc::ty::{self, TyCtxt};
@@ -24,7 +22,6 @@ pub struct UnsafetyChecker<'a, 'tcx> {
2422
body: &'a Body<'tcx>,
2523
const_context: bool,
2624
min_const_fn: bool,
27-
source_scope_local_data: &'a IndexVec<SourceScope, SourceScopeLocalData>,
2825
violations: Vec<UnsafetyViolation>,
2926
source_info: SourceInfo,
3027
tcx: TyCtxt<'tcx>,
@@ -39,7 +36,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
3936
const_context: bool,
4037
min_const_fn: bool,
4138
body: &'a Body<'tcx>,
42-
source_scope_local_data: &'a IndexVec<SourceScope, SourceScopeLocalData>,
4339
tcx: TyCtxt<'tcx>,
4440
param_env: ty::ParamEnv<'tcx>,
4541
) -> Self {
@@ -51,7 +47,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
5147
body,
5248
const_context,
5349
min_const_fn,
54-
source_scope_local_data,
5550
violations: vec![],
5651
source_info: SourceInfo {
5752
span: body.span,
@@ -219,8 +214,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
219214
if context.is_borrow() {
220215
if util::is_disaligned(self.tcx, self.body, self.param_env, place) {
221216
let source_info = self.source_info;
222-
let lint_root =
223-
self.source_scope_local_data[source_info.scope].lint_root;
217+
let lint_root = self.body.source_scope_local_data[source_info.scope]
218+
.as_ref()
219+
.assert_crate_local()
220+
.lint_root;
224221
self.register_violations(&[UnsafetyViolation {
225222
source_info,
226223
description: Symbol::intern("borrow of packed field"),
@@ -346,7 +343,10 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
346343
fn register_violations(&mut self,
347344
violations: &[UnsafetyViolation],
348345
unsafe_blocks: &[(hir::HirId, bool)]) {
349-
let safety = self.source_scope_local_data[self.source_info.scope].safety;
346+
let safety = self.body.source_scope_local_data[self.source_info.scope]
347+
.as_ref()
348+
.assert_crate_local()
349+
.safety;
350350
let within_unsafe = match safety {
351351
// `unsafe` blocks are required in safe code
352352
Safety::Safe => {
@@ -516,17 +516,6 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult
516516
// `mir_built` force this.
517517
let body = &tcx.mir_built(def_id).borrow();
518518

519-
let source_scope_local_data = match body.source_scope_local_data {
520-
ClearCrossCrate::Set(ref data) => data,
521-
ClearCrossCrate::Clear => {
522-
debug!("unsafety_violations: {:?} - remote, skipping", def_id);
523-
return UnsafetyCheckResult {
524-
violations: Lrc::new([]),
525-
unsafe_blocks: Lrc::new([])
526-
}
527-
}
528-
};
529-
530519
let param_env = tcx.param_env(def_id);
531520

532521
let id = tcx.hir().as_local_hir_id(def_id).unwrap();
@@ -536,9 +525,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult
536525
hir::BodyOwnerKind::Const |
537526
hir::BodyOwnerKind::Static(_) => (true, false),
538527
};
539-
let mut checker = UnsafetyChecker::new(
540-
const_context, min_const_fn,
541-
body, source_scope_local_data, tcx, param_env);
528+
let mut checker = UnsafetyChecker::new(const_context, min_const_fn, body, tcx, param_env);
542529
checker.visit_body(body);
543530

544531
check_unused_unsafe(tcx, def_id, &checker.used_unsafe, &mut checker.inherited_blocks);

0 commit comments

Comments
 (0)