Skip to content

Commit 2645e60

Browse files
committed
Remove MIR unsafe check
This also remove safety information from MIR.
1 parent 5dbaafd commit 2645e60

File tree

202 files changed

+659
-2246
lines changed

Some content is hidden

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

202 files changed

+659
-2246
lines changed

compiler/rustc_interface/src/passes.rs

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

749749
sess.time("MIR_effect_checking", || {
750750
for def_id in tcx.hir().body_owners() {
751-
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
752-
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
753-
}
754751
tcx.ensure().has_ffi_unwind_calls(def_id);
755752

756753
// 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
@@ -840,7 +840,6 @@ fn test_unstable_options_tracking_hash() {
840840
tracked!(stack_protector, StackProtector::All);
841841
tracked!(teach, true);
842842
tracked!(thinlto, Some(true));
843-
tracked!(thir_unsafeck, false);
844843
tracked!(tiny_const_eval_limit, true);
845844
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
846845
tracked!(translate_remapped_path_to_local_path, false);

compiler/rustc_middle/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ macro_rules! arena_types {
3535
)>,
3636
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
3737
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
38-
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
3938
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
4039
[] const_allocs: rustc_middle::mir::interpret::Allocation,
4140
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,

compiler/rustc_middle/src/mir/mod.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -845,17 +845,6 @@ impl<'tcx> Body<'tcx> {
845845
}
846846
}
847847

848-
#[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]
849-
pub enum Safety {
850-
Safe,
851-
/// Unsafe because of compiler-generated unsafe code, like `await` desugaring
852-
BuiltinUnsafe,
853-
/// Unsafe because of an unsafe fn
854-
FnUnsafe,
855-
/// Unsafe because of an `unsafe` block
856-
ExplicitUnsafe(hir::HirId),
857-
}
858-
859848
impl<'tcx> Index<BasicBlock> for Body<'tcx> {
860849
type Output = BasicBlockData<'tcx>;
861850

@@ -1611,8 +1600,6 @@ pub struct SourceScopeData<'tcx> {
16111600
pub struct SourceScopeLocalData {
16121601
/// An `HirId` with lint levels equivalent to this scope's lint levels.
16131602
pub lint_root: hir::HirId,
1614-
/// The unsafe block that contains this node.
1615-
pub safety: Safety,
16161603
}
16171604

16181605
/// A collection of projections into user types.
@@ -1888,7 +1875,7 @@ mod size_asserts {
18881875
// tidy-alphabetical-start
18891876
static_assert_size!(BasicBlockData<'_>, 144);
18901877
static_assert_size!(LocalDecl<'_>, 40);
1891-
static_assert_size!(SourceScopeData<'_>, 72);
1878+
static_assert_size!(SourceScopeData<'_>, 64);
18921879
static_assert_size!(Statement<'_>, 32);
18931880
static_assert_size!(StatementKind<'_>, 16);
18941881
static_assert_size!(Terminator<'_>, 112);

compiler/rustc_middle/src/mir/query.rs

-63
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use crate::mir;
44
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
55
use rustc_data_structures::fx::FxIndexMap;
6-
use rustc_data_structures::unord::UnordSet;
76
use rustc_errors::ErrorGuaranteed;
8-
use rustc_hir as hir;
97
use rustc_hir::def_id::LocalDefId;
108
use rustc_index::bit_set::BitMatrix;
119
use rustc_index::{Idx, IndexVec};
@@ -18,67 +16,6 @@ use std::fmt::{self, Debug};
1816

1917
use super::{ConstValue, SourceInfo};
2018

21-
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
22-
pub enum UnsafetyViolationKind {
23-
/// Unsafe operation outside `unsafe`.
24-
General,
25-
/// Unsafe operation in an `unsafe fn` but outside an `unsafe` block.
26-
/// Has to be handled as a lint for backwards compatibility.
27-
UnsafeFn,
28-
}
29-
30-
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
31-
pub enum UnsafetyViolationDetails {
32-
CallToUnsafeFunction,
33-
UseOfInlineAssembly,
34-
InitializingTypeWith,
35-
CastOfPointerToInt,
36-
UseOfMutableStatic,
37-
UseOfExternStatic,
38-
DerefOfRawPointer,
39-
AccessToUnionField,
40-
MutationOfLayoutConstrainedField,
41-
BorrowOfLayoutConstrainedField,
42-
CallToFunctionWith {
43-
/// Target features enabled in callee's `#[target_feature]` but missing in
44-
/// caller's `#[target_feature]`.
45-
missing: Vec<Symbol>,
46-
/// Target features in `missing` that are enabled at compile time
47-
/// (e.g., with `-C target-feature`).
48-
build_enabled: Vec<Symbol>,
49-
},
50-
}
51-
52-
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
53-
pub struct UnsafetyViolation {
54-
pub source_info: SourceInfo,
55-
pub lint_root: hir::HirId,
56-
pub kind: UnsafetyViolationKind,
57-
pub details: UnsafetyViolationDetails,
58-
}
59-
60-
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
61-
pub enum UnusedUnsafe {
62-
/// `unsafe` block contains no unsafe operations
63-
/// > ``unnecessary `unsafe` block``
64-
Unused,
65-
/// `unsafe` block nested under another (used) `unsafe` block
66-
/// > ``… because it's nested under this `unsafe` block``
67-
InUnsafeBlock(hir::HirId),
68-
}
69-
70-
#[derive(TyEncodable, TyDecodable, HashStable, Debug)]
71-
pub struct UnsafetyCheckResult {
72-
/// Violations that are propagated *upwards* from this function.
73-
pub violations: Vec<UnsafetyViolation>,
74-
75-
/// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
76-
pub used_unsafe_blocks: UnordSet<hir::HirId>,
77-
78-
/// This is `Some` iff the item is not a closure.
79-
pub unused_unsafes: Option<Vec<(hir::HirId, UnusedUnsafe)>>,
80-
}
81-
8219
rustc_index::newtype_index! {
8320
#[derive(HashStable)]
8421
#[encodable]

compiler/rustc_middle/src/query/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,6 @@ rustc_queries! {
877877
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
878878
}
879879

880-
/// The result of unsafety-checking this `LocalDefId` with the old checker.
881-
query mir_unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
882-
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
883-
cache_on_disk_if { true }
884-
}
885-
886880
/// Unsafety-check this `LocalDefId`.
887881
query check_unsafety(key: LocalDefId) {
888882
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/ty/codec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ impl_decodable_via_ref! {
458458
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
459459
&'tcx traits::ImplSource<'tcx, ()>,
460460
&'tcx mir::Body<'tcx>,
461-
&'tcx mir::UnsafetyCheckResult,
462461
&'tcx mir::BorrowCheckResult<'tcx>,
463462
&'tcx mir::coverage::CodeRegion,
464463
&'tcx ty::List<ty::BoundVariableKind>,

compiler/rustc_mir_build/src/build/block.rs

+5-40
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1313
ast_block: BlockId,
1414
source_info: SourceInfo,
1515
) -> BlockAnd<()> {
16-
let Block { region_scope, span, ref stmts, expr, targeted_by_break, safety_mode } =
16+
let Block { region_scope, span, ref stmts, expr, targeted_by_break, safety_mode: _ } =
1717
self.thir[ast_block];
1818
self.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
1919
if targeted_by_break {
2020
this.in_breakable_scope(None, destination, span, |this| {
21-
Some(this.ast_block_stmts(
22-
destination,
23-
block,
24-
span,
25-
stmts,
26-
expr,
27-
safety_mode,
28-
region_scope,
29-
))
21+
Some(this.ast_block_stmts(destination, block, span, stmts, expr, region_scope))
3022
})
3123
} else {
32-
this.ast_block_stmts(
33-
destination,
34-
block,
35-
span,
36-
stmts,
37-
expr,
38-
safety_mode,
39-
region_scope,
40-
)
24+
this.ast_block_stmts(destination, block, span, stmts, expr, region_scope)
4125
}
4226
})
4327
}
@@ -49,7 +33,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4933
span: Span,
5034
stmts: &[StmtId],
5135
expr: Option<ExprId>,
52-
safety_mode: BlockSafety,
5336
region_scope: Scope,
5437
) -> BlockAnd<()> {
5538
let this = self;
@@ -72,13 +55,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7255
// First we build all the statements in the block.
7356
let mut let_scope_stack = Vec::with_capacity(8);
7457
let outer_source_scope = this.source_scope;
75-
let outer_in_scope_unsafe = this.in_scope_unsafe;
7658
// This scope information is kept for breaking out of the parent remainder scope in case
7759
// one let-else pattern matching fails.
7860
// By doing so, we can be sure that even temporaries that receive extended lifetime
7961
// assignments are dropped, too.
8062
let mut last_remainder_scope = region_scope;
81-
this.update_source_scope_for_safety_mode(span, safety_mode);
8263

8364
let source_info = this.source_info(span);
8465
for stmt in stmts {
@@ -202,7 +183,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
202183
let_scope_stack.push(remainder_scope);
203184

204185
let visibility_scope =
205-
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
186+
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
206187

207188
let initializer_span = this.thir[*initializer].span;
208189
let scope = (*init_scope, source_info);
@@ -271,7 +252,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
271252
let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);
272253

273254
let visibility_scope =
274-
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
255+
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
275256

276257
// Evaluate the initializer, if present.
277258
if let Some(init) = *initializer {
@@ -364,22 +345,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
364345
}
365346
// Restore the original source scope.
366347
this.source_scope = outer_source_scope;
367-
this.in_scope_unsafe = outer_in_scope_unsafe;
368348
block.unit()
369349
}
370-
371-
/// If we are entering an unsafe block, create a new source scope
372-
fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) {
373-
debug!("update_source_scope_for({:?}, {:?})", span, safety_mode);
374-
let new_unsafety = match safety_mode {
375-
BlockSafety::Safe => return,
376-
BlockSafety::BuiltinUnsafe => Safety::BuiltinUnsafe,
377-
BlockSafety::ExplicitUnsafe(hir_id) => {
378-
self.in_scope_unsafe = Safety::ExplicitUnsafe(hir_id);
379-
Safety::ExplicitUnsafe(hir_id)
380-
}
381-
};
382-
383-
self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(new_unsafety));
384-
}
385350
}

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ pub(super) fn build_custom_mir<'tcx>(
7272
parent_scope: None,
7373
inlined: None,
7474
inlined_parent_scope: None,
75-
local_data: ClearCrossCrate::Set(SourceScopeLocalData {
76-
lint_root: hir_id,
77-
safety: Safety::Safe,
78-
}),
75+
local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
7976
});
8077
body.injection_phase = Some(parse_attribute(attr));
8178

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

+4-11
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
118118
ExprKind::Box { value } => {
119119
let value_ty = this.thir[value].ty;
120120
let tcx = this.tcx;
121-
122-
// `exchange_malloc` is unsafe but box is safe, so need a new scope.
123-
let synth_scope = this.new_source_scope(
124-
expr_span,
125-
LintLevel::Inherited,
126-
Some(Safety::BuiltinUnsafe),
127-
);
128-
let synth_info = SourceInfo { span: expr_span, scope: synth_scope };
121+
let source_info = this.source_info(expr_span);
129122

130123
let size = this.temp(tcx.types.usize, expr_span);
131124
this.cfg.push_assign(
132125
block,
133-
synth_info,
126+
source_info,
134127
size,
135128
Rvalue::NullaryOp(NullOp::SizeOf, value_ty),
136129
);
137130

138131
let align = this.temp(tcx.types.usize, expr_span);
139132
this.cfg.push_assign(
140133
block,
141-
synth_info,
134+
source_info,
142135
align,
143136
Rvalue::NullaryOp(NullOp::AlignOf, value_ty),
144137
);
@@ -154,7 +147,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
154147
let success = this.cfg.start_new_block();
155148
this.cfg.terminate(
156149
block,
157-
synth_info,
150+
source_info,
158151
TerminatorKind::Call {
159152
func: exchange_malloc,
160153
args: vec![

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6969
// FIXME: Does this need extra logic to handle let-chains?
7070
let source_info = if this.is_let(cond) {
7171
let variable_scope =
72-
this.new_source_scope(then_span, LintLevel::Inherited, None);
72+
this.new_source_scope(then_span, LintLevel::Inherited);
7373
this.source_scope = variable_scope;
7474
SourceInfo { span: then_span, scope: variable_scope }
7575
} else {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
732732
&mut |this, name, mode, var, span, ty, user_ty| {
733733
if visibility_scope.is_none() {
734734
visibility_scope =
735-
Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
735+
Some(this.new_source_scope(scope_span, LintLevel::Inherited));
736736
}
737737
let source_info = SourceInfo { span, scope: this.source_scope };
738738
let visibility_scope = visibility_scope.unwrap();

0 commit comments

Comments
 (0)