Skip to content

Commit cd279a5

Browse files
committed
run unsafety checking before dead block collection
Fixes #45087.
1 parent a6b1a81 commit cd279a5

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

src/librustc/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ define_dep_nodes!( <'tcx>
473473
// Represents the MIR for a fn; also used as the task node for
474474
// things read/modify that MIR.
475475
[] MirConstQualif(DefId),
476+
[] MirBuilt(DefId),
476477
[] MirConst(DefId),
477478
[] MirValidated(DefId),
478479
[] MirOptimized(DefId),
@@ -812,4 +813,3 @@ impl WorkProductId {
812813
impl_stable_hash_for!(struct ::dep_graph::WorkProductId {
813814
hash
814815
});
815-

src/librustc/ty/maps/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ define_maps! { <'tcx>
151151
/// the value isn't known except to the pass itself.
152152
[] fn mir_const_qualif: MirConstQualif(DefId) -> (u8, Rc<IdxSetBuf<mir::Local>>),
153153

154+
/// Fetch the MIR for a given def-id right after it's built - this includes
155+
/// unreachable code.
156+
[] fn mir_built: MirBuilt(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
157+
154158
/// Fetch the MIR for a given def-id up till the point where it is
155159
/// ready for const evaluation.
156160
///

src/librustc/ty/maps/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
712712
force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
713713
},
714714
DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
715+
DepKind::MirBuilt => { force!(mir_built, def_id!()); }
715716
DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
716717
DepKind::MirConst => { force!(mir_const, def_id!()); }
717718
DepKind::MirValidated => { force!(mir_validated, def_id!()); }
@@ -852,4 +853,3 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
852853

853854
true
854855
}
855-

src/librustc_mir/transform/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ fn unsafety_violations<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) ->
344344
debug!("unsafety_violations({:?})", def_id);
345345

346346
// NB: this borrow is valid because all the consumers of
347-
// `mir_const` force this.
348-
let mir = &tcx.mir_const(def_id).borrow();
347+
// `mir_built` force this.
348+
let mir = &tcx.mir_built(def_id).borrow();
349349

350350
let visibility_scope_info = match mir.visibility_scope_info {
351351
ClearOnDecode::Set(ref data) => data,

src/librustc_mir/transform/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub(crate) fn provide(providers: &mut Providers) {
5050
self::check_unsafety::provide(providers);
5151
*providers = Providers {
5252
mir_keys,
53+
mir_built,
5354
mir_const,
5455
mir_validated,
5556
optimized_mir,
@@ -103,9 +104,17 @@ fn mir_keys<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, krate: CrateNum)
103104
Rc::new(set)
104105
}
105106

107+
fn mir_built<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
108+
let mir = build::mir_build(tcx, def_id);
109+
tcx.alloc_steal_mir(mir)
110+
}
111+
106112
fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
107-
let mut mir = build::mir_build(tcx, def_id);
113+
// Unsafety check uses the raw mir, so make sure it is run
114+
let _ = tcx.unsafety_violations(def_id);
115+
108116
let source = MirSource::from_local_def_id(tcx, def_id);
117+
let mut mir = tcx.mir_built(def_id).steal();
109118
transform::run_suite(tcx, source, MIR_CONST, &mut mir);
110119
tcx.alloc_steal_mir(mir)
111120
}
@@ -117,7 +126,6 @@ fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
117126
// this point, before we steal the mir-const result.
118127
let _ = tcx.mir_const_qualif(def_id);
119128
}
120-
let _ = tcx.unsafety_violations(def_id);
121129

122130
let mut mir = tcx.mir_const(def_id).steal();
123131
transform::run_suite(tcx, source, MIR_VALIDATED, &mut mir);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
return;
13+
*(1 as *mut u32) = 42;
14+
//~^ ERROR dereference of raw pointer requires unsafe
15+
}

0 commit comments

Comments
 (0)