Skip to content

Commit 6bc08a7

Browse files
committed
Auto merge of #117673 - matthewjasper:thir-unsafeck-stabilization, r=cjgillot
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 8d39ec1 + 7832ebb commit 6bc08a7

File tree

284 files changed

+879
-4844
lines changed

Some content is hidden

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

284 files changed

+879
-4844
lines changed

compiler/rustc_interface/src/passes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
735735

736736
sess.time("MIR_borrow_checking", || {
737737
tcx.hir().par_body_owners(|def_id| {
738-
// Run THIR unsafety check because it's responsible for stealing
739-
// and deallocating THIR when enabled.
740-
tcx.ensure().thir_check_unsafety(def_id);
738+
// Run unsafety check because it's responsible for stealing and
739+
// deallocating THIR.
740+
tcx.ensure().check_unsafety(def_id);
741741
tcx.ensure().mir_borrowck(def_id)
742742
});
743743
});

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ fn test_unstable_options_tracking_hash() {
822822
tracked!(stack_protector, StackProtector::All);
823823
tracked!(teach, true);
824824
tracked!(thinlto, Some(true));
825-
tracked!(thir_unsafeck, true);
825+
tracked!(thir_unsafeck, false);
826826
tracked!(tiny_const_eval_limit, true);
827827
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
828828
tracked!(translate_remapped_path_to_local_path, false);

compiler/rustc_middle/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ pub struct SourceInfo {
720720
pub span: Span,
721721

722722
/// The source scope, keeping track of which bindings can be
723-
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
723+
/// seen by debuginfo, active lint levels, etc.
724724
pub scope: SourceScope,
725725
}
726726

@@ -942,7 +942,7 @@ pub struct LocalDecl<'tcx> {
942942

943943
/// Extra information about a some locals that's used for diagnostics and for
944944
/// classifying variables into local variables, statics, etc, which is needed e.g.
945-
/// for unsafety checking.
945+
/// for borrow checking.
946946
///
947947
/// Not used for non-StaticRef temporaries, the return place, or anonymous
948948
/// function parameters.

compiler/rustc_middle/src/query/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,14 @@ rustc_queries! {
869869
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
870870
}
871871

872-
/// The result of unsafety-checking this `LocalDefId`.
873-
query unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
872+
/// The result of unsafety-checking this `LocalDefId` with the old checker.
873+
query mir_unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
874874
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
875875
cache_on_disk_if { true }
876876
}
877877

878-
/// Unsafety-check this `LocalDefId` with THIR unsafeck. This should be
879-
/// used with `-Zthir-unsafeck`.
880-
query thir_check_unsafety(key: LocalDefId) {
878+
/// Unsafety-check this `LocalDefId`.
879+
query check_unsafety(key: LocalDefId) {
881880
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
882881
cache_on_disk_if { true }
883882
}

compiler/rustc_mir_build/src/check_unsafety.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
1414
use rustc_session::lint::Level;
1515
use rustc_span::def_id::{DefId, LocalDefId};
1616
use rustc_span::symbol::Symbol;
17-
use rustc_span::Span;
17+
use rustc_span::{sym, Span};
1818

1919
use std::mem;
2020
use std::ops::Bound;
@@ -144,11 +144,17 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
144144
let hir_context = self.tcx.local_def_id_to_hir_id(def);
145145
let safety_context = mem::replace(&mut self.safety_context, SafetyContext::Safe);
146146
let mut inner_visitor = UnsafetyVisitor {
147+
tcx: self.tcx,
147148
thir: inner_thir,
148149
hir_context,
149150
safety_context,
151+
body_target_features: self.body_target_features,
152+
assignment_info: self.assignment_info,
153+
in_union_destructure: false,
154+
param_env: self.param_env,
155+
inside_adt: false,
150156
warnings: self.warnings,
151-
..*self
157+
suggest_unsafe_block: self.suggest_unsafe_block,
152158
};
153159
inner_visitor.visit_expr(&inner_thir[expr]);
154160
// Unsafe blocks can be used in the inner body, make sure to take it into account
@@ -886,14 +892,15 @@ impl UnsafeOpKind {
886892
}
887893
}
888894

889-
pub fn thir_check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
890-
// THIR unsafeck is gated under `-Z thir-unsafeck`
895+
pub fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
896+
// THIR unsafeck can be disabled with `-Z thir-unsafeck=off`
891897
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
892898
return;
893899
}
894900

895901
// Closures and inline consts are handled by their owner, if it has a body
896-
if tcx.is_typeck_child(def.to_def_id()) {
902+
// Also, don't safety check custom MIR
903+
if tcx.is_typeck_child(def.to_def_id()) || tcx.has_attr(def, sym::custom_mir) {
897904
return;
898905
}
899906

compiler/rustc_mir_build/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn provide(providers: &mut Providers) {
3131
providers.mir_built = build::mir_built;
3232
providers.closure_saved_names_of_captured_variables =
3333
build::closure_saved_names_of_captured_variables;
34-
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
34+
providers.check_unsafety = check_unsafety::check_unsafety;
3535
providers.thir_body = thir::cx::thir_body;
3636
providers.thir_tree = thir::print::thir_tree;
3737
providers.thir_flat = thir::print::thir_flat;

compiler/rustc_mir_transform/src/check_unsafety.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
131131
&AggregateKind::Closure(def_id, _) | &AggregateKind::Coroutine(def_id, _) => {
132132
let def_id = def_id.expect_local();
133133
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
134-
self.tcx.unsafety_check_result(def_id);
134+
self.tcx.mir_unsafety_check_result(def_id);
135135
self.register_violations(violations, used_unsafe_blocks.items().copied());
136136
}
137137
},
@@ -153,7 +153,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
153153
if self.tcx.def_kind(def_id) == DefKind::InlineConst {
154154
let local_def_id = def_id.expect_local();
155155
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
156-
self.tcx.unsafety_check_result(local_def_id);
156+
self.tcx.mir_unsafety_check_result(local_def_id);
157157
self.register_violations(violations, used_unsafe_blocks.items().copied());
158158
}
159159
}
@@ -390,7 +390,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
390390
}
391391

392392
pub(crate) fn provide(providers: &mut Providers) {
393-
*providers = Providers { unsafety_check_result, ..*providers };
393+
*providers = Providers { mir_unsafety_check_result, ..*providers };
394394
}
395395

396396
/// Context information for [`UnusedUnsafeVisitor`] traversal,
@@ -490,7 +490,7 @@ fn check_unused_unsafe(
490490
unused_unsafes
491491
}
492492

493-
fn unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResult {
493+
fn mir_unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResult {
494494
debug!("unsafety_violations({:?})", def);
495495

496496
// N.B., this borrow is valid because all the consumers of
@@ -538,7 +538,8 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
538538
return;
539539
}
540540

541-
let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id);
541+
let UnsafetyCheckResult { violations, unused_unsafes, .. } =
542+
tcx.mir_unsafety_check_result(def_id);
542543
// Only suggest wrapping the entire function body in an unsafe block once
543544
let mut suggest_unsafe_block = true;
544545

compiler/rustc_mir_transform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
285285
/// FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query).
286286
/// We used to have this for pre-miri MIR based const eval.
287287
fn mir_const(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
288-
// Unsafety check uses the raw mir, so make sure it is run.
288+
// MIR unsafety check uses the raw mir, so make sure it is run.
289289
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
290-
tcx.ensure_with_value().unsafety_check_result(def);
290+
tcx.ensure_with_value().mir_unsafety_check_result(def);
291291
}
292292

293293
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1919,8 +1919,8 @@ written to standard error output)"),
19191919
#[rustc_lint_opt_deny_field_access("use `Session::lto` instead of this field")]
19201920
thinlto: Option<bool> = (None, parse_opt_bool, [TRACKED],
19211921
"enable ThinLTO when possible"),
1922-
thir_unsafeck: bool = (false, parse_bool, [TRACKED],
1923-
"use the THIR unsafety checker (default: no)"),
1922+
thir_unsafeck: bool = (true, parse_bool, [TRACKED],
1923+
"use the THIR unsafety checker (default: yes)"),
19241924
/// We default to 1 here since we want to behave like
19251925
/// a sequential compiler for now. This'll likely be adjusted
19261926
/// in the future. Note that -Zthreads=0 is the way to get

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::path::{Path, PathBuf};
1010

1111
const ENTRY_LIMIT: usize = 900;
1212
// FIXME: The following limits should be reduced eventually.
13-
const ISSUES_ENTRY_LIMIT: usize = 1852;
13+
const ISSUES_ENTRY_LIMIT: usize = 1849;
1414
const ROOT_ENTRY_LIMIT: usize = 867;
1515

1616
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[

tests/ui/asm/aarch64/const.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// only-aarch64
22
// run-pass
33
// needs-asm-support
4-
// revisions: mirunsafeck thirunsafeck
5-
// [thirunsafeck]compile-flags: -Z thir-unsafeck
64

75
#![feature(asm_const)]
86

tests/ui/asm/bad-arch.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// compile-flags: --target sparc-unknown-linux-gnu
22
// needs-llvm-components: sparc
3-
// revisions: mirunsafeck thirunsafeck
4-
// [thirunsafeck]compile-flags: -Z thir-unsafeck
53

64
#![feature(no_core, lang_items, rustc_attrs)]
75
#![no_core]

tests/ui/asm/bad-arch.mirunsafeck.stderr tests/ui/asm/bad-arch.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0472]: inline assembly is unsupported on this target
2-
--> $DIR/bad-arch.rs:22:9
2+
--> $DIR/bad-arch.rs:20:9
33
|
44
LL | asm!("");
55
| ^^^^^^^^
66

77
error[E0472]: inline assembly is unsupported on this target
8-
--> $DIR/bad-arch.rs:27:1
8+
--> $DIR/bad-arch.rs:25:1
99
|
1010
LL | global_asm!("");
1111
| ^^^^^^^^^^^^^^^

tests/ui/asm/bad-arch.thirunsafeck.stderr

-17
This file was deleted.

0 commit comments

Comments
 (0)