Skip to content

Commit 9ee6473

Browse files
committed
Auto merge of #117673 - matthewjasper:thir-unsafeck-stabilization, 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.
2 parents ff0b4b6 + e33cb9f commit 9ee6473

File tree

426 files changed

+1142
-6385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

426 files changed

+1142
-6385
lines changed

compiler/rustc_interface/src/passes.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -742,18 +742,15 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
742742

743743
sess.time("MIR_borrow_checking", || {
744744
tcx.hir().par_body_owners(|def_id| {
745-
// Run THIR unsafety check because it's responsible for stealing
746-
// and deallocating THIR when enabled.
747-
tcx.ensure().thir_check_unsafety(def_id);
745+
// Run unsafety check because it's responsible for stealing and
746+
// deallocating THIR.
747+
tcx.ensure().check_unsafety(def_id);
748748
tcx.ensure().mir_borrowck(def_id)
749749
});
750750
});
751751

752752
sess.time("MIR_effect_checking", || {
753753
for def_id in tcx.hir().body_owners() {
754-
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
755-
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
756-
}
757754
tcx.ensure().has_ffi_unwind_calls(def_id);
758755

759756
// If we need to codegen, ensure that we emit all errors from

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,6 @@ fn test_unstable_options_tracking_hash() {
819819
tracked!(stack_protector, StackProtector::All);
820820
tracked!(teach, true);
821821
tracked!(thinlto, Some(true));
822-
tracked!(thir_unsafeck, true);
823822
tracked!(tiny_const_eval_limit, true);
824823
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
825824
tracked!(trait_solver, TraitSolver::NextCoherence);

compiler/rustc_middle/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ macro_rules! arena_types {
4242
[] output_filenames: std::sync::Arc<rustc_session::config::OutputFilenames>,
4343
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
4444
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
45-
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
4645
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
4746
[] const_allocs: rustc_middle::mir::interpret::Allocation,
4847
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,

compiler/rustc_middle/src/mir/mod.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -614,17 +614,6 @@ impl<'tcx> Body<'tcx> {
614614
}
615615
}
616616

617-
#[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
618-
pub enum Safety {
619-
Safe,
620-
/// Unsafe because of compiler-generated unsafe code, like `await` desugaring
621-
BuiltinUnsafe,
622-
/// Unsafe because of an unsafe fn
623-
FnUnsafe,
624-
/// Unsafe because of an `unsafe` block
625-
ExplicitUnsafe(hir::HirId),
626-
}
627-
628617
impl<'tcx> Index<BasicBlock> for Body<'tcx> {
629618
type Output = BasicBlockData<'tcx>;
630619

@@ -720,7 +709,7 @@ pub struct SourceInfo {
720709
pub span: Span,
721710

722711
/// The source scope, keeping track of which bindings can be
723-
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
712+
/// seen by debuginfo, active lint levels, etc.
724713
pub scope: SourceScope,
725714
}
726715

@@ -940,7 +929,7 @@ pub struct LocalDecl<'tcx> {
940929

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

13811368
/// A collection of projections into user types.
@@ -1631,7 +1618,7 @@ mod size_asserts {
16311618
// tidy-alphabetical-start
16321619
static_assert_size!(BasicBlockData<'_>, 136);
16331620
static_assert_size!(LocalDecl<'_>, 40);
1634-
static_assert_size!(SourceScopeData<'_>, 72);
1621+
static_assert_size!(SourceScopeData<'_>, 64);
16351622
static_assert_size!(Statement<'_>, 32);
16361623
static_assert_size!(StatementKind<'_>, 16);
16371624
static_assert_size!(Terminator<'_>, 104);

compiler/rustc_middle/src/query/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,8 @@ rustc_queries! {
867867
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
868868
}
869869

870-
/// The result of unsafety-checking this `LocalDefId`.
871-
query unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
872-
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
873-
cache_on_disk_if { true }
874-
}
875-
876-
/// Unsafety-check this `LocalDefId` with THIR unsafeck. This should be
877-
/// used with `-Zthir-unsafeck`.
878-
query thir_check_unsafety(key: LocalDefId) {
870+
/// Unsafety-check this `LocalDefId`.
871+
query check_unsafety(key: LocalDefId) {
879872
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
880873
cache_on_disk_if { true }
881874
}

compiler/rustc_middle/src/ty/codec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ impl_decodable_via_ref! {
431431
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
432432
&'tcx traits::ImplSource<'tcx, ()>,
433433
&'tcx mir::Body<'tcx>,
434-
&'tcx mir::UnsafetyCheckResult,
435434
&'tcx mir::BorrowCheckResult<'tcx>,
436435
&'tcx mir::coverage::CodeRegion,
437436
&'tcx ty::List<ty::BoundVariableKind>,

compiler/rustc_mir_build/src/build/block.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2020
ref stmts,
2121
expr,
2222
targeted_by_break,
23-
safety_mode,
23+
safety_mode: _,
2424
} = self.thir[ast_block];
2525
let expr = expr.map(|expr| &self.thir[expr]);
2626
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
@@ -33,20 +33,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3333
span,
3434
&stmts,
3535
expr,
36-
safety_mode,
3736
region_scope,
3837
))
3938
})
4039
} else {
41-
this.ast_block_stmts(
42-
destination,
43-
block,
44-
span,
45-
&stmts,
46-
expr,
47-
safety_mode,
48-
region_scope,
49-
)
40+
this.ast_block_stmts(destination, block, span, &stmts, expr, region_scope)
5041
}
5142
})
5243
})
@@ -59,7 +50,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5950
span: Span,
6051
stmts: &[StmtId],
6152
expr: Option<&Expr<'tcx>>,
62-
safety_mode: BlockSafety,
6353
region_scope: Scope,
6454
) -> BlockAnd<()> {
6555
let this = self;
@@ -82,13 +72,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8272
// First we build all the statements in the block.
8373
let mut let_scope_stack = Vec::with_capacity(8);
8474
let outer_source_scope = this.source_scope;
85-
let outer_in_scope_unsafe = this.in_scope_unsafe;
8675
// This scope information is kept for breaking out of the parent remainder scope in case
8776
// one let-else pattern matching fails.
8877
// By doing so, we can be sure that even temporaries that receive extended lifetime
8978
// assignments are dropped, too.
9079
let mut last_remainder_scope = region_scope;
91-
this.update_source_scope_for_safety_mode(span, safety_mode);
9280

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

219207
let visibility_scope =
220-
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
208+
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
221209

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

294282
let visibility_scope =
295-
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
283+
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
296284

297285
// Evaluate the initializer, if present.
298286
if let Some(init) = initializer {
@@ -390,22 +378,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
390378
}
391379
// Restore the original source scope.
392380
this.source_scope = outer_source_scope;
393-
this.in_scope_unsafe = outer_in_scope_unsafe;
394381
block.unit()
395382
}
396-
397-
/// If we are entering an unsafe block, create a new source scope
398-
fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) {
399-
debug!("update_source_scope_for({:?}, {:?})", span, safety_mode);
400-
let new_unsafety = match safety_mode {
401-
BlockSafety::Safe => return,
402-
BlockSafety::BuiltinUnsafe => Safety::BuiltinUnsafe,
403-
BlockSafety::ExplicitUnsafe(hir_id) => {
404-
self.in_scope_unsafe = Safety::ExplicitUnsafe(hir_id);
405-
Safety::ExplicitUnsafe(hir_id)
406-
}
407-
};
408-
409-
self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(new_unsafety));
410-
}
411383
}

compiler/rustc_mir_build/src/build/custom/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ pub(super) fn build_custom_mir<'tcx>(
7070
parent_scope: None,
7171
inlined: None,
7272
inlined_parent_scope: None,
73-
local_data: ClearCrossCrate::Set(SourceScopeLocalData {
74-
lint_root: hir_id,
75-
safety: Safety::Safe,
76-
}),
73+
local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
7774
});
7875
body.injection_phase = Some(parse_attribute(attr));
7976

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
131131
let value = &this.thir[value];
132132
let tcx = this.tcx;
133133

134-
// `exchange_malloc` is unsafe but box is safe, so need a new scope.
135-
let synth_scope = this.new_source_scope(
136-
expr_span,
137-
LintLevel::Inherited,
138-
Some(Safety::BuiltinUnsafe),
139-
);
140-
let synth_info = SourceInfo { span: expr_span, scope: synth_scope };
141-
142134
let size = this.temp(tcx.types.usize, expr_span);
143135
this.cfg.push_assign(
144136
block,
145-
synth_info,
137+
source_info,
146138
size,
147139
Rvalue::NullaryOp(NullOp::SizeOf, value.ty),
148140
);
149141

150142
let align = this.temp(tcx.types.usize, expr_span);
151143
this.cfg.push_assign(
152144
block,
153-
synth_info,
145+
source_info,
154146
align,
155147
Rvalue::NullaryOp(NullOp::AlignOf, value.ty),
156148
);
@@ -166,7 +158,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
166158
let success = this.cfg.start_new_block();
167159
this.cfg.terminate(
168160
block,
169-
synth_info,
161+
source_info,
170162
TerminatorKind::Call {
171163
func: exchange_malloc,
172164
args: vec![Operand::Move(size), Operand::Move(align)],

compiler/rustc_mir_build/src/build/expr/into.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6262
LintLevel::Inherited,
6363
|this| {
6464
let source_info = if this.is_let(cond) {
65-
let variable_scope = this.new_source_scope(
66-
then_expr.span,
67-
LintLevel::Inherited,
68-
None,
69-
);
65+
let variable_scope =
66+
this.new_source_scope(then_expr.span, LintLevel::Inherited);
7067
this.source_scope = variable_scope;
7168
SourceInfo { span: then_expr.span, scope: variable_scope }
7269
} else {

compiler/rustc_mir_build/src/build/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
705705
&mut |this, mutability, name, mode, var, span, ty, user_ty| {
706706
if visibility_scope.is_none() {
707707
visibility_scope =
708-
Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
708+
Some(this.new_source_scope(scope_span, LintLevel::Inherited));
709709
}
710710
let source_info = SourceInfo { span, scope: this.source_scope };
711711
let visibility_scope = visibility_scope.unwrap();

0 commit comments

Comments
 (0)