Skip to content

Commit 30a9978

Browse files
committed
rustc: move MIR source_scope_local_data's ClearCrossCrate to be around elements.
1 parent 25d8a94 commit 30a9978

File tree

8 files changed

+88
-115
lines changed

8 files changed

+88
-115
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>>,
@@ -167,7 +167,7 @@ impl<'tcx> Body<'tcx> {
167167
pub fn new(
168168
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
169169
source_scopes: IndexVec<SourceScope, SourceScopeData>,
170-
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
170+
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
171171
local_decls: LocalDecls<'tcx>,
172172
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
173173
arg_count: usize,
@@ -435,6 +435,13 @@ pub enum ClearCrossCrate<T> {
435435
}
436436

437437
impl<T> ClearCrossCrate<T> {
438+
pub fn as_ref(&'a self) -> ClearCrossCrate<&'a T> {
439+
match self {
440+
ClearCrossCrate::Clear => ClearCrossCrate::Clear,
441+
ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
442+
}
443+
}
444+
438445
pub fn assert_crate_local(self) -> T {
439446
match self {
440447
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,
@@ -942,7 +942,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
942942
self.hir.root_lint_level
943943
);
944944
let parent_root = tcx.maybe_lint_level_root_bounded(
945-
self.source_scope_local_data[original_source_scope].lint_root,
945+
self.source_scope_local_data[original_source_scope]
946+
.as_ref()
947+
.assert_crate_local()
948+
.lint_root,
946949
self.hir.root_lint_level,
947950
);
948951
if current_root != parent_root {

src/librustc_mir/build/scope.rs

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

438438
let parent_root = tcx.maybe_lint_level_root_bounded(
439-
self.source_scope_local_data[source_scope].lint_root,
439+
self.source_scope_local_data[source_scope]
440+
.as_ref()
441+
.assert_crate_local()
442+
.lint_root,
440443
self.hir.root_lint_level,
441444
);
442445
let current_root = tcx.maybe_lint_level_root_bounded(
@@ -663,13 +666,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
663666
lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
664667
lint_root
665668
} else {
666-
self.source_scope_local_data[parent].lint_root
669+
self.source_scope_local_data[parent].as_ref().assert_crate_local().lint_root
667670
},
668671
safety: safety.unwrap_or_else(|| {
669-
self.source_scope_local_data[parent].safety
672+
self.source_scope_local_data[parent].as_ref().assert_crate_local().safety
670673
})
671674
};
672-
self.source_scope_local_data.push(scope_local_data);
675+
self.source_scope_local_data.push(ClearCrossCrate::Set(scope_local_data));
673676
scope
674677
}
675678

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

+6-15
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
198198

199199
let mut body = new_body(
200200
blocks,
201-
IndexVec::from_elem_n(
202-
SourceScopeData { span, parent_scope: None }, 1
203-
),
204201
local_decls_for_sig(&sig, span),
205202
sig.inputs().len(),
206203
span);
@@ -244,15 +241,18 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
244241

245242
fn new_body<'tcx>(
246243
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
247-
source_scopes: IndexVec<SourceScope, SourceScopeData>,
248244
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
249245
arg_count: usize,
250246
span: Span,
251247
) -> Body<'tcx> {
252248
Body::new(
253249
basic_blocks,
254-
source_scopes,
255-
ClearCrossCrate::Clear,
250+
IndexVec::from_elem_n(
251+
SourceScopeData { span, parent_scope: None }, 1
252+
),
253+
IndexVec::from_elem_n(
254+
ClearCrossCrate::Clear, 1
255+
),
256256
local_decls,
257257
IndexVec::new(),
258258
arg_count,
@@ -380,9 +380,6 @@ impl CloneShimBuilder<'tcx> {
380380
fn into_mir(self) -> Body<'tcx> {
381381
new_body(
382382
self.blocks,
383-
IndexVec::from_elem_n(
384-
SourceScopeData { span: self.span, parent_scope: None }, 1
385-
),
386383
self.local_decls,
387384
self.sig.inputs().len(),
388385
self.span,
@@ -836,9 +833,6 @@ fn build_call_shim<'tcx>(
836833

837834
let mut body = new_body(
838835
blocks,
839-
IndexVec::from_elem_n(
840-
SourceScopeData { span, parent_scope: None }, 1
841-
),
842836
local_decls,
843837
sig.inputs().len(),
844838
span,
@@ -919,9 +913,6 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &Body<'_> {
919913

920914
let body = new_body(
921915
IndexVec::from_elem_n(start_block, 1),
922-
IndexVec::from_elem_n(
923-
SourceScopeData { span, parent_scope: None }, 1
924-
),
925916
local_decls,
926917
sig.inputs().len(),
927918
span,

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)