Skip to content

Commit

Permalink
Auto merge of rust-lang#117673 - matthewjasper:thir-unsafeck-stabiliz…
Browse files Browse the repository at this point in the history
…ation, r=<try>

Stabilize THIR unsafeck

- Removes `-Zthir-unsafeck`, stabilizing the behaviour of `-Zthir-unsafeck=on`.
- Removes MIR unsafeck.
- Union patterns are now unsafe unless the field is matched to a wildcard pattern.

Opening for a crater run in case we need a compatibility lint.
  • Loading branch information
bors committed Nov 8, 2023
2 parents ff0b4b6 + e33cb9f commit 9ee6473
Show file tree
Hide file tree
Showing 426 changed files with 1,142 additions and 6,385 deletions.
9 changes: 3 additions & 6 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,18 +742,15 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {

sess.time("MIR_borrow_checking", || {
tcx.hir().par_body_owners(|def_id| {
// Run THIR unsafety check because it's responsible for stealing
// and deallocating THIR when enabled.
tcx.ensure().thir_check_unsafety(def_id);
// Run unsafety check because it's responsible for stealing and
// deallocating THIR.
tcx.ensure().check_unsafety(def_id);
tcx.ensure().mir_borrowck(def_id)
});
});

sess.time("MIR_effect_checking", || {
for def_id in tcx.hir().body_owners() {
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
}
tcx.ensure().has_ffi_unwind_calls(def_id);

// If we need to codegen, ensure that we emit all errors from
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,6 @@ fn test_unstable_options_tracking_hash() {
tracked!(stack_protector, StackProtector::All);
tracked!(teach, true);
tracked!(thinlto, Some(true));
tracked!(thir_unsafeck, true);
tracked!(tiny_const_eval_limit, true);
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
tracked!(trait_solver, TraitSolver::NextCoherence);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ macro_rules! arena_types {
[] output_filenames: std::sync::Arc<rustc_session::config::OutputFilenames>,
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
[] const_allocs: rustc_middle::mir::interpret::Allocation,
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,
Expand Down
19 changes: 3 additions & 16 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,6 @@ impl<'tcx> Body<'tcx> {
}
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
pub enum Safety {
Safe,
/// Unsafe because of compiler-generated unsafe code, like `await` desugaring
BuiltinUnsafe,
/// Unsafe because of an unsafe fn
FnUnsafe,
/// Unsafe because of an `unsafe` block
ExplicitUnsafe(hir::HirId),
}

impl<'tcx> Index<BasicBlock> for Body<'tcx> {
type Output = BasicBlockData<'tcx>;

Expand Down Expand Up @@ -720,7 +709,7 @@ pub struct SourceInfo {
pub span: Span,

/// The source scope, keeping track of which bindings can be
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
/// seen by debuginfo, active lint levels, etc.
pub scope: SourceScope,
}

Expand Down Expand Up @@ -940,7 +929,7 @@ pub struct LocalDecl<'tcx> {

/// Extra information about a some locals that's used for diagnostics and for
/// classifying variables into local variables, statics, etc, which is needed e.g.
/// for unsafety checking.
/// for borrow checking.
///
/// Not used for non-StaticRef temporaries, the return place, or anonymous
/// function parameters.
Expand Down Expand Up @@ -1374,8 +1363,6 @@ pub struct SourceScopeData<'tcx> {
pub struct SourceScopeLocalData {
/// An `HirId` with lint levels equivalent to this scope's lint levels.
pub lint_root: hir::HirId,
/// The unsafe block that contains this node.
pub safety: Safety,
}

/// A collection of projections into user types.
Expand Down Expand Up @@ -1631,7 +1618,7 @@ mod size_asserts {
// tidy-alphabetical-start
static_assert_size!(BasicBlockData<'_>, 136);
static_assert_size!(LocalDecl<'_>, 40);
static_assert_size!(SourceScopeData<'_>, 72);
static_assert_size!(SourceScopeData<'_>, 64);
static_assert_size!(Statement<'_>, 32);
static_assert_size!(StatementKind<'_>, 16);
static_assert_size!(Terminator<'_>, 104);
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,15 +867,8 @@ rustc_queries! {
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
}

/// The result of unsafety-checking this `LocalDefId`.
query unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}

/// Unsafety-check this `LocalDefId` with THIR unsafeck. This should be
/// used with `-Zthir-unsafeck`.
query thir_check_unsafety(key: LocalDefId) {
/// Unsafety-check this `LocalDefId`.
query check_unsafety(key: LocalDefId) {
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ impl_decodable_via_ref! {
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
&'tcx traits::ImplSource<'tcx, ()>,
&'tcx mir::Body<'tcx>,
&'tcx mir::UnsafetyCheckResult,
&'tcx mir::BorrowCheckResult<'tcx>,
&'tcx mir::coverage::CodeRegion,
&'tcx ty::List<ty::BoundVariableKind>,
Expand Down
36 changes: 4 additions & 32 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ref stmts,
expr,
targeted_by_break,
safety_mode,
safety_mode: _,
} = self.thir[ast_block];
let expr = expr.map(|expr| &self.thir[expr]);
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
Expand All @@ -33,20 +33,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
span,
&stmts,
expr,
safety_mode,
region_scope,
))
})
} else {
this.ast_block_stmts(
destination,
block,
span,
&stmts,
expr,
safety_mode,
region_scope,
)
this.ast_block_stmts(destination, block, span, &stmts, expr, region_scope)
}
})
})
Expand All @@ -59,7 +50,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
span: Span,
stmts: &[StmtId],
expr: Option<&Expr<'tcx>>,
safety_mode: BlockSafety,
region_scope: Scope,
) -> BlockAnd<()> {
let this = self;
Expand All @@ -82,13 +72,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// First we build all the statements in the block.
let mut let_scope_stack = Vec::with_capacity(8);
let outer_source_scope = this.source_scope;
let outer_in_scope_unsafe = this.in_scope_unsafe;
// This scope information is kept for breaking out of the parent remainder scope in case
// one let-else pattern matching fails.
// By doing so, we can be sure that even temporaries that receive extended lifetime
// assignments are dropped, too.
let mut last_remainder_scope = region_scope;
this.update_source_scope_for_safety_mode(span, safety_mode);

let source_info = this.source_info(span);
for stmt in stmts {
Expand Down Expand Up @@ -217,7 +205,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let_scope_stack.push(remainder_scope);

let visibility_scope =
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));

let init = &this.thir[*initializer];
let initializer_span = init.span;
Expand Down Expand Up @@ -292,7 +280,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);

let visibility_scope =
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));

// Evaluate the initializer, if present.
if let Some(init) = initializer {
Expand Down Expand Up @@ -390,22 +378,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
// Restore the original source scope.
this.source_scope = outer_source_scope;
this.in_scope_unsafe = outer_in_scope_unsafe;
block.unit()
}

/// If we are entering an unsafe block, create a new source scope
fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) {
debug!("update_source_scope_for({:?}, {:?})", span, safety_mode);
let new_unsafety = match safety_mode {
BlockSafety::Safe => return,
BlockSafety::BuiltinUnsafe => Safety::BuiltinUnsafe,
BlockSafety::ExplicitUnsafe(hir_id) => {
self.in_scope_unsafe = Safety::ExplicitUnsafe(hir_id);
Safety::ExplicitUnsafe(hir_id)
}
};

self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(new_unsafety));
}
}
5 changes: 1 addition & 4 deletions compiler/rustc_mir_build/src/build/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ pub(super) fn build_custom_mir<'tcx>(
parent_scope: None,
inlined: None,
inlined_parent_scope: None,
local_data: ClearCrossCrate::Set(SourceScopeLocalData {
lint_root: hir_id,
safety: Safety::Safe,
}),
local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
});
body.injection_phase = Some(parse_attribute(attr));

Expand Down
14 changes: 3 additions & 11 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let value = &this.thir[value];
let tcx = this.tcx;

// `exchange_malloc` is unsafe but box is safe, so need a new scope.
let synth_scope = this.new_source_scope(
expr_span,
LintLevel::Inherited,
Some(Safety::BuiltinUnsafe),
);
let synth_info = SourceInfo { span: expr_span, scope: synth_scope };

let size = this.temp(tcx.types.usize, expr_span);
this.cfg.push_assign(
block,
synth_info,
source_info,
size,
Rvalue::NullaryOp(NullOp::SizeOf, value.ty),
);

let align = this.temp(tcx.types.usize, expr_span);
this.cfg.push_assign(
block,
synth_info,
source_info,
align,
Rvalue::NullaryOp(NullOp::AlignOf, value.ty),
);
Expand All @@ -166,7 +158,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let success = this.cfg.start_new_block();
this.cfg.terminate(
block,
synth_info,
source_info,
TerminatorKind::Call {
func: exchange_malloc,
args: vec![Operand::Move(size), Operand::Move(align)],
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
LintLevel::Inherited,
|this| {
let source_info = if this.is_let(cond) {
let variable_scope = this.new_source_scope(
then_expr.span,
LintLevel::Inherited,
None,
);
let variable_scope =
this.new_source_scope(then_expr.span, LintLevel::Inherited);
this.source_scope = variable_scope;
SourceInfo { span: then_expr.span, scope: variable_scope }
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut |this, mutability, name, mode, var, span, ty, user_ty| {
if visibility_scope.is_none() {
visibility_scope =
Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
Some(this.new_source_scope(scope_span, LintLevel::Inherited));
}
let source_info = SourceInfo { span, scope: this.source_scope };
let visibility_scope = visibility_scope.unwrap();
Expand Down
Loading

0 comments on commit 9ee6473

Please sign in to comment.