Skip to content

Commit 968b620

Browse files
committed
Auto merge of #45785 - arielb1:unsafe-fixes, r=eddyb
fixes to MIR effectck r? @eddyb beta-nominating because regression (MIR effectck is new)
2 parents c0d326f + 12aedc8 commit 968b620

12 files changed

+282
-84
lines changed

src/librustc/dep_graph/dep_node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ define_dep_nodes!( <'tcx>
469469
// Represents the MIR for a fn; also used as the task node for
470470
// things read/modify that MIR.
471471
[] MirConstQualif(DefId),
472+
[] MirBuilt(DefId),
472473
[] MirConst(DefId),
473474
[] MirValidated(DefId),
474475
[] MirOptimized(DefId),
@@ -477,7 +478,7 @@ define_dep_nodes!( <'tcx>
477478
[] BorrowCheckKrate,
478479
[] BorrowCheck(DefId),
479480
[] MirBorrowCheck(DefId),
480-
[] UnsafetyViolations(DefId),
481+
[] UnsafetyCheckResult(DefId),
481482

482483
[] Reachability,
483484
[] MirKeys,
@@ -782,4 +783,3 @@ impl WorkProductId {
782783
impl_stable_hash_for!(struct ::dep_graph::WorkProductId {
783784
hash
784785
});
785-

src/librustc/ich/impls_mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
3434
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref });
3535
impl_stable_hash_for!(struct mir::BasicBlockData<'tcx> { statements, terminator, is_cleanup });
3636
impl_stable_hash_for!(struct mir::UnsafetyViolation { source_info, description, lint_node_id });
37+
impl_stable_hash_for!(struct mir::UnsafetyCheckResult { violations, unsafe_blocks });
3738

3839
impl<'gcx> HashStable<StableHashingContext<'gcx>>
3940
for mir::Terminator<'gcx> {

src/librustc/mir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::cell::Ref;
3333
use std::fmt::{self, Debug, Formatter, Write};
3434
use std::{iter, u32};
3535
use std::ops::{Index, IndexMut};
36+
use std::rc::Rc;
3637
use std::vec::IntoIter;
3738
use syntax::ast::{self, Name};
3839
use syntax_pos::Span;
@@ -1683,6 +1684,15 @@ pub struct UnsafetyViolation {
16831684
pub lint_node_id: Option<ast::NodeId>,
16841685
}
16851686

1687+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1688+
pub struct UnsafetyCheckResult {
1689+
/// Violations that are propagated *upwards* from this function
1690+
pub violations: Rc<[UnsafetyViolation]>,
1691+
/// unsafe blocks in this function, along with whether they are used. This is
1692+
/// used for the "unused_unsafe" lint.
1693+
pub unsafe_blocks: Rc<[(ast::NodeId, bool)]>,
1694+
}
1695+
16861696
/// The layout of generator state
16871697
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
16881698
pub struct GeneratorLayout<'tcx> {

src/librustc/ty/maps/mod.rs

+6-3
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
///
@@ -167,9 +171,8 @@ define_maps! { <'tcx>
167171
/// expression defining the closure.
168172
[] fn closure_kind: ClosureKind(DefId) -> ty::ClosureKind,
169173

170-
/// Unsafety violations for this def ID.
171-
[] fn unsafety_violations: UnsafetyViolations(DefId)
172-
-> Rc<[mir::UnsafetyViolation]>,
174+
/// The result of unsafety-checking this def-id.
175+
[] fn unsafety_check_result: UnsafetyCheckResult(DefId) -> mir::UnsafetyCheckResult,
173176

174177
/// The signature of functions and closures.
175178
[] fn fn_sig: FnSignature(DefId) -> ty::PolyFnSig<'tcx>,

src/librustc/ty/maps/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,15 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
739739
force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
740740
},
741741
DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
742+
DepKind::MirBuilt => { force!(mir_built, def_id!()); }
742743
DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
743744
DepKind::MirConst => { force!(mir_const, def_id!()); }
744745
DepKind::MirValidated => { force!(mir_validated, def_id!()); }
745746
DepKind::MirOptimized => { force!(optimized_mir, def_id!()); }
746747

747748
DepKind::BorrowCheck => { force!(borrowck, def_id!()); }
748749
DepKind::MirBorrowCheck => { force!(mir_borrowck, def_id!()); }
749-
DepKind::UnsafetyViolations => { force!(unsafety_violations, def_id!()); }
750+
DepKind::UnsafetyCheckResult => { force!(unsafety_check_result, def_id!()); }
750751
DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
751752
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
752753
DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
@@ -879,4 +880,3 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
879880

880881
true
881882
}
882-

src/librustc_mir/transform/check_unsafety.rs

+103-67
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct UnsafetyChecker<'a, 'tcx: 'a> {
3434
tcx: TyCtxt<'a, 'tcx, 'tcx>,
3535
param_env: ty::ParamEnv<'tcx>,
3636
used_unsafe: FxHashSet<ast::NodeId>,
37+
inherited_blocks: Vec<(ast::NodeId, bool)>,
3738
}
3839

3940
impl<'a, 'gcx, 'tcx> UnsafetyChecker<'a, 'tcx> {
@@ -52,6 +53,7 @@ impl<'a, 'gcx, 'tcx> UnsafetyChecker<'a, 'tcx> {
5253
tcx,
5354
param_env,
5455
used_unsafe: FxHashSet(),
56+
inherited_blocks: vec![],
5557
}
5658
}
5759
}
@@ -75,7 +77,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
7577
TerminatorKind::Return |
7678
TerminatorKind::Unreachable |
7779
TerminatorKind::FalseEdges { .. } => {
78-
// safe (at least as emitted during MIR construction)
80+
// safe (at least as emitted during MIR construction)
7981
}
8082

8183
TerminatorKind::Call { ref func, .. } => {
@@ -117,12 +119,20 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
117119
rvalue: &Rvalue<'tcx>,
118120
location: Location)
119121
{
120-
if let &Rvalue::Aggregate(
121-
box AggregateKind::Closure(def_id, _),
122-
_
123-
) = rvalue {
124-
let unsafety_violations = self.tcx.unsafety_violations(def_id);
125-
self.register_violations(&unsafety_violations);
122+
if let &Rvalue::Aggregate(box ref aggregate, _) = rvalue {
123+
match aggregate {
124+
&AggregateKind::Array(..) |
125+
&AggregateKind::Tuple |
126+
&AggregateKind::Adt(..) => {}
127+
&AggregateKind::Closure(def_id, _) |
128+
&AggregateKind::Generator(def_id, _, _) => {
129+
let UnsafetyCheckResult {
130+
violations, unsafe_blocks
131+
} = self.tcx.unsafety_check_result(def_id);
132+
self.inherited_blocks.extend(unsafe_blocks.iter().cloned());
133+
self.register_violations(&violations, &unsafe_blocks);
134+
}
135+
}
126136
}
127137
self.super_rvalue(rvalue, location);
128138
}
@@ -189,7 +199,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
189199
source_info,
190200
description: "use of extern static",
191201
lint_node_id: Some(lint_root)
192-
}]);
202+
}], &[]);
193203
}
194204
}
195205
}
@@ -222,41 +232,49 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
222232
let source_info = self.source_info;
223233
self.register_violations(&[UnsafetyViolation {
224234
source_info, description, lint_node_id: None
225-
}]);
235+
}], &[]);
226236
}
227237

228-
fn register_violations(&mut self, violations: &[UnsafetyViolation]) {
229-
match self.visibility_scope_info[self.source_info.scope].safety {
238+
fn register_violations(&mut self,
239+
violations: &[UnsafetyViolation],
240+
unsafe_blocks: &[(ast::NodeId, bool)]) {
241+
let within_unsafe = match self.visibility_scope_info[self.source_info.scope].safety {
230242
Safety::Safe => {
231243
for violation in violations {
232244
if !self.violations.contains(violation) {
233245
self.violations.push(violation.clone())
234246
}
235247
}
248+
249+
false
236250
}
237-
Safety::BuiltinUnsafe | Safety::FnUnsafe => {}
251+
Safety::BuiltinUnsafe | Safety::FnUnsafe => true,
238252
Safety::ExplicitUnsafe(node_id) => {
239253
if !violations.is_empty() {
240254
self.used_unsafe.insert(node_id);
241255
}
256+
true
242257
}
243-
}
258+
};
259+
self.inherited_blocks.extend(unsafe_blocks.iter().map(|&(node_id, is_used)| {
260+
(node_id, is_used && !within_unsafe)
261+
}));
244262
}
245263
}
246264

247265
pub(crate) fn provide(providers: &mut Providers) {
248266
*providers = Providers {
249-
unsafety_violations,
267+
unsafety_check_result,
250268
..*providers
251269
};
252270
}
253271

254-
struct UnusedUnsafeVisitor<'a, 'tcx: 'a> {
255-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
256-
used_unsafe: FxHashSet<ast::NodeId>
272+
struct UnusedUnsafeVisitor<'a> {
273+
used_unsafe: &'a FxHashSet<ast::NodeId>,
274+
unsafe_blocks: &'a mut Vec<(ast::NodeId, bool)>,
257275
}
258276

259-
impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a, 'tcx> {
277+
impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a> {
260278
fn nested_visit_map<'this>(&'this mut self) ->
261279
hir::intravisit::NestedVisitorMap<'this, 'tcx>
262280
{
@@ -267,50 +285,15 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a, 'tcx>
267285
hir::intravisit::walk_block(self, block);
268286

269287
if let hir::UnsafeBlock(hir::UserProvided) = block.rules {
270-
if !self.used_unsafe.contains(&block.id) {
271-
self.report_unused_unsafe(block);
272-
}
273-
}
274-
}
275-
}
276-
277-
impl<'a, 'tcx> UnusedUnsafeVisitor<'a, 'tcx> {
278-
/// Return the NodeId for an enclosing scope that is also `unsafe`
279-
fn is_enclosed(&self, id: ast::NodeId) -> Option<(String, ast::NodeId)> {
280-
let parent_id = self.tcx.hir.get_parent_node(id);
281-
if parent_id != id {
282-
if self.used_unsafe.contains(&parent_id) {
283-
Some(("block".to_string(), parent_id))
284-
} else if let Some(hir::map::NodeItem(&hir::Item {
285-
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
286-
..
287-
})) = self.tcx.hir.find(parent_id) {
288-
Some(("fn".to_string(), parent_id))
289-
} else {
290-
self.is_enclosed(parent_id)
291-
}
292-
} else {
293-
None
294-
}
295-
}
296-
297-
fn report_unused_unsafe(&self, block: &'tcx hir::Block) {
298-
let mut db = self.tcx.struct_span_lint_node(UNUSED_UNSAFE,
299-
block.id,
300-
block.span,
301-
"unnecessary `unsafe` block");
302-
db.span_label(block.span, "unnecessary `unsafe` block");
303-
if let Some((kind, id)) = self.is_enclosed(block.id) {
304-
db.span_note(self.tcx.hir.span(id),
305-
&format!("because it's nested under this `unsafe` {}", kind));
288+
self.unsafe_blocks.push((block.id, self.used_unsafe.contains(&block.id)));
306289
}
307-
db.emit();
308290
}
309291
}
310292

311293
fn check_unused_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
312294
def_id: DefId,
313-
used_unsafe: FxHashSet<ast::NodeId>)
295+
used_unsafe: &FxHashSet<ast::NodeId>,
296+
unsafe_blocks: &'a mut Vec<(ast::NodeId, bool)>)
314297
{
315298
let body_id =
316299
tcx.hir.as_local_node_id(def_id).and_then(|node_id| {
@@ -328,25 +311,27 @@ fn check_unused_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
328311
debug!("check_unused_unsafe({:?}, body={:?}, used_unsafe={:?})",
329312
def_id, body, used_unsafe);
330313

331-
hir::intravisit::Visitor::visit_body(
332-
&mut UnusedUnsafeVisitor { tcx, used_unsafe },
333-
body);
314+
let mut visitor = UnusedUnsafeVisitor { used_unsafe, unsafe_blocks };
315+
hir::intravisit::Visitor::visit_body(&mut visitor, body);
334316
}
335317

336-
fn unsafety_violations<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) ->
337-
Rc<[UnsafetyViolation]>
318+
fn unsafety_check_result<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
319+
-> UnsafetyCheckResult
338320
{
339321
debug!("unsafety_violations({:?})", def_id);
340322

341323
// NB: this borrow is valid because all the consumers of
342-
// `mir_const` force this.
343-
let mir = &tcx.mir_const(def_id).borrow();
324+
// `mir_built` force this.
325+
let mir = &tcx.mir_built(def_id).borrow();
344326

345327
let visibility_scope_info = match mir.visibility_scope_info {
346328
ClearOnDecode::Set(ref data) => data,
347329
ClearOnDecode::Clear => {
348330
debug!("unsafety_violations: {:?} - remote, skipping", def_id);
349-
return Rc::new([])
331+
return UnsafetyCheckResult {
332+
violations: Rc::new([]),
333+
unsafe_blocks: Rc::new([])
334+
}
350335
}
351336
};
352337

@@ -355,8 +340,43 @@ fn unsafety_violations<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) ->
355340
mir, visibility_scope_info, tcx, param_env);
356341
checker.visit_mir(mir);
357342

358-
check_unused_unsafe(tcx, def_id, checker.used_unsafe);
359-
checker.violations.into()
343+
check_unused_unsafe(tcx, def_id, &checker.used_unsafe, &mut checker.inherited_blocks);
344+
UnsafetyCheckResult {
345+
violations: checker.violations.into(),
346+
unsafe_blocks: checker.inherited_blocks.into()
347+
}
348+
}
349+
350+
/// Return the NodeId for an enclosing scope that is also `unsafe`
351+
fn is_enclosed(tcx: TyCtxt,
352+
used_unsafe: &FxHashSet<ast::NodeId>,
353+
id: ast::NodeId) -> Option<(String, ast::NodeId)> {
354+
let parent_id = tcx.hir.get_parent_node(id);
355+
if parent_id != id {
356+
if used_unsafe.contains(&parent_id) {
357+
Some(("block".to_string(), parent_id))
358+
} else if let Some(hir::map::NodeItem(&hir::Item {
359+
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
360+
..
361+
})) = tcx.hir.find(parent_id) {
362+
Some(("fn".to_string(), parent_id))
363+
} else {
364+
is_enclosed(tcx, used_unsafe, parent_id)
365+
}
366+
} else {
367+
None
368+
}
369+
}
370+
371+
fn report_unused_unsafe(tcx: TyCtxt, used_unsafe: &FxHashSet<ast::NodeId>, id: ast::NodeId) {
372+
let span = tcx.hir.span(id);
373+
let mut db = tcx.struct_span_lint_node(UNUSED_UNSAFE, id, span, "unnecessary `unsafe` block");
374+
db.span_label(span, "unnecessary `unsafe` block");
375+
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
376+
db.span_note(tcx.hir.span(id),
377+
&format!("because it's nested under this `unsafe` {}", kind));
378+
}
379+
db.emit();
360380
}
361381

362382
pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
@@ -367,9 +387,14 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
367387
_ => {}
368388
};
369389

390+
let UnsafetyCheckResult {
391+
violations,
392+
unsafe_blocks
393+
} = tcx.unsafety_check_result(def_id);
394+
370395
for &UnsafetyViolation {
371396
source_info, description, lint_node_id
372-
} in &*tcx.unsafety_violations(def_id) {
397+
} in violations.iter() {
373398
// Report an error.
374399
if let Some(lint_node_id) = lint_node_id {
375400
tcx.lint_node(SAFE_EXTERN_STATICS,
@@ -385,4 +410,15 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
385410
.emit();
386411
}
387412
}
413+
414+
let mut unsafe_blocks: Vec<_> = unsafe_blocks.into_iter().collect();
415+
unsafe_blocks.sort();
416+
let used_unsafe: FxHashSet<_> = unsafe_blocks.iter()
417+
.flat_map(|&&(id, used)| if used { Some(id) } else { None })
418+
.collect();
419+
for &(block_id, is_used) in unsafe_blocks {
420+
if !is_used {
421+
report_unused_unsafe(tcx, &used_unsafe, block_id);
422+
}
423+
}
388424
}

0 commit comments

Comments
 (0)