diff options
| author | bors <bors@rust-lang.org> | 2024-01-05 12:34:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-05 12:34:00 +0000 |
| commit | 6bc08a725f888a06ea3c6844f3d0cc2d2ebc5142 (patch) | |
| tree | 4f93fdaecda5d73047d62044e6d3ff75fcfcff79 /compiler | |
| parent | 8d39ec1825024f3014e1f847942ac5bbfcf055b0 (diff) | |
| parent | 7832ebbd4ff090aced6e338ff92e1353bbe88f76 (diff) | |
| download | rust-6bc08a725f888a06ea3c6844f3d0cc2d2ebc5142.tar.gz rust-6bc08a725f888a06ea3c6844f3d0cc2d2ebc5142.zip | |
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.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_interface/src/passes.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_interface/src/tests.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/mod.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_mir_build/src/check_unsafety.rs | 17 | ||||
| -rw-r--r-- | compiler/rustc_mir_build/src/lib.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/check_unsafety.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/lib.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_session/src/options.rs | 4 |
9 files changed, 33 insertions, 26 deletions
diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 559874641c3..ce76c2cba93 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -735,9 +735,9 @@ 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) }); }); diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index c4a1f3a0e51..75410db1e36 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -822,7 +822,7 @@ fn test_unstable_options_tracking_hash() { tracked!(stack_protector, StackProtector::All); tracked!(teach, true); tracked!(thinlto, Some(true)); - tracked!(thir_unsafeck, true); + tracked!(thir_unsafeck, false); tracked!(tiny_const_eval_limit, true); tracked!(tls_model, Some(TlsModel::GeneralDynamic)); tracked!(translate_remapped_path_to_local_path, false); diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 5c425fef27e..0a1a72b442a 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -720,7 +720,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, } @@ -942,7 +942,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. diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 2810182c0a0..bf5e59ba78d 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -869,15 +869,14 @@ 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 { + /// The result of unsafety-checking this `LocalDefId` with the old checker. + query mir_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 } } diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 2e8b6c19ec7..b4a02fae454 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -14,7 +14,7 @@ use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::symbol::Symbol; -use rustc_span::Span; +use rustc_span::{sym, Span}; use std::mem; use std::ops::Bound; @@ -144,11 +144,17 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { let hir_context = self.tcx.local_def_id_to_hir_id(def); let safety_context = mem::replace(&mut self.safety_context, SafetyContext::Safe); let mut inner_visitor = UnsafetyVisitor { + tcx: self.tcx, thir: inner_thir, hir_context, safety_context, + body_target_features: self.body_target_features, + assignment_info: self.assignment_info, + in_union_destructure: false, + param_env: self.param_env, + inside_adt: false, warnings: self.warnings, - ..*self + suggest_unsafe_block: self.suggest_unsafe_block, }; inner_visitor.visit_expr(&inner_thir[expr]); // Unsafe blocks can be used in the inner body, make sure to take it into account @@ -886,14 +892,15 @@ impl UnsafeOpKind { } } -pub fn thir_check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) { - // THIR unsafeck is gated under `-Z thir-unsafeck` +pub fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) { + // THIR unsafeck can be disabled with `-Z thir-unsafeck=off` if !tcx.sess.opts.unstable_opts.thir_unsafeck { return; } // Closures and inline consts are handled by their owner, if it has a body - if tcx.is_typeck_child(def.to_def_id()) { + // Also, don't safety check custom MIR + if tcx.is_typeck_child(def.to_def_id()) || tcx.has_attr(def, sym::custom_mir) { return; } diff --git a/compiler/rustc_mir_build/src/lib.rs b/compiler/rustc_mir_build/src/lib.rs index a776e917de5..430c4ee3da7 100644 --- a/compiler/rustc_mir_build/src/lib.rs +++ b/compiler/rustc_mir_build/src/lib.rs @@ -31,7 +31,7 @@ pub fn provide(providers: &mut Providers) { providers.mir_built = build::mir_built; providers.closure_saved_names_of_captured_variables = build::closure_saved_names_of_captured_variables; - providers.thir_check_unsafety = check_unsafety::thir_check_unsafety; + providers.check_unsafety = check_unsafety::check_unsafety; providers.thir_body = thir::cx::thir_body; providers.thir_tree = thir::print::thir_tree; providers.thir_flat = thir::print::thir_flat; diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index d94d96c1115..582c2c0c6b6 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -131,7 +131,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> { &AggregateKind::Closure(def_id, _) | &AggregateKind::Coroutine(def_id, _) => { let def_id = def_id.expect_local(); let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = - self.tcx.unsafety_check_result(def_id); + self.tcx.mir_unsafety_check_result(def_id); self.register_violations(violations, used_unsafe_blocks.items().copied()); } }, @@ -153,7 +153,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> { if self.tcx.def_kind(def_id) == DefKind::InlineConst { let local_def_id = def_id.expect_local(); let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = - self.tcx.unsafety_check_result(local_def_id); + self.tcx.mir_unsafety_check_result(local_def_id); self.register_violations(violations, used_unsafe_blocks.items().copied()); } } @@ -390,7 +390,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { } pub(crate) fn provide(providers: &mut Providers) { - *providers = Providers { unsafety_check_result, ..*providers }; + *providers = Providers { mir_unsafety_check_result, ..*providers }; } /// Context information for [`UnusedUnsafeVisitor`] traversal, @@ -490,7 +490,7 @@ fn check_unused_unsafe( unused_unsafes } -fn unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResult { +fn mir_unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResult { debug!("unsafety_violations({:?})", def); // N.B., this borrow is valid because all the consumers of @@ -538,7 +538,8 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) { return; } - let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id); + let UnsafetyCheckResult { violations, unused_unsafes, .. } = + tcx.mir_unsafety_check_result(def_id); // Only suggest wrapping the entire function body in an unsafe block once let mut suggest_unsafe_block = true; diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 5562ae7f3bd..164b6b9c4f5 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -285,9 +285,9 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs { /// FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query). /// We used to have this for pre-miri MIR based const eval. fn mir_const(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> { - // Unsafety check uses the raw mir, so make sure it is run. + // MIR unsafety check uses the raw mir, so make sure it is run. if !tcx.sess.opts.unstable_opts.thir_unsafeck { - tcx.ensure_with_value().unsafety_check_result(def); + tcx.ensure_with_value().mir_unsafety_check_result(def); } // has_ffi_unwind_calls query uses the raw mir, so make sure it is run. diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 8274fd05bc0..0b0b67ef890 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1919,8 +1919,8 @@ written to standard error output)"), #[rustc_lint_opt_deny_field_access("use `Session::lto` instead of this field")] thinlto: Option<bool> = (None, parse_opt_bool, [TRACKED], "enable ThinLTO when possible"), - thir_unsafeck: bool = (false, parse_bool, [TRACKED], - "use the THIR unsafety checker (default: no)"), + thir_unsafeck: bool = (true, parse_bool, [TRACKED], + "use the THIR unsafety checker (default: yes)"), /// We default to 1 here since we want to behave like /// a sequential compiler for now. This'll likely be adjusted /// in the future. Note that -Zthreads=0 is the way to get |
