Skip to content

Commit e36f233

Browse files
committed
Add new safety enum to be used in inner extern block items
1 parent 3dd5387 commit e36f233

File tree

92 files changed

+394
-296
lines changed

Some content is hidden

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

92 files changed

+394
-296
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ impl Ty {
20992099

21002100
#[derive(Clone, Encodable, Decodable, Debug)]
21012101
pub struct BareFnTy {
2102-
pub unsafety: Unsafe,
2102+
pub safety: Safety,
21032103
pub ext: Extern,
21042104
pub generic_params: ThinVec<GenericParam>,
21052105
pub decl: P<FnDecl>,
@@ -2485,6 +2485,17 @@ pub enum Unsafe {
24852485
No,
24862486
}
24872487

2488+
/// Safety of items (for now only used on inner extern block items).
2489+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
2490+
#[derive(HashStable_Generic)]
2491+
pub enum Safety {
2492+
/// `unsafe` an item is explicitly marked as `unsafe`.
2493+
Unsafe(Span),
2494+
/// Default means no value was provided, it will take a default value given the context in
2495+
/// which is used.
2496+
Default,
2497+
}
2498+
24882499
/// Describes what kind of coroutine markers, if any, a function has.
24892500
///
24902501
/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
@@ -3001,8 +3012,8 @@ impl Extern {
30013012
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
30023013
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
30033014
pub struct FnHeader {
3004-
/// The `unsafe` keyword, if any
3005-
pub unsafety: Unsafe,
3015+
/// The safety keyword, if any
3016+
pub safety: Safety,
30063017
/// Whether this is `async`, `gen`, or nothing.
30073018
pub coroutine_kind: Option<CoroutineKind>,
30083019
/// The `const` keyword, if any
@@ -3014,8 +3025,8 @@ pub struct FnHeader {
30143025
impl FnHeader {
30153026
/// Does this function header have any qualifiers or is it empty?
30163027
pub fn has_qualifiers(&self) -> bool {
3017-
let Self { unsafety, coroutine_kind, constness, ext } = self;
3018-
matches!(unsafety, Unsafe::Yes(_))
3028+
let Self { safety, coroutine_kind, constness, ext } = self;
3029+
matches!(safety, Safety::Unsafe(_))
30193030
|| coroutine_kind.is_some()
30203031
|| matches!(constness, Const::Yes(_))
30213032
|| !matches!(ext, Extern::None)
@@ -3025,7 +3036,7 @@ impl FnHeader {
30253036
impl Default for FnHeader {
30263037
fn default() -> FnHeader {
30273038
FnHeader {
3028-
unsafety: Unsafe::No,
3039+
safety: Safety::Default,
30293040
coroutine_kind: None,
30303041
constness: Const::No,
30313042
ext: Extern::None,

Diff for: compiler/rustc_ast/src/mut_visit.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
499499
vis.visit_mt(mt);
500500
}
501501
TyKind::BareFn(bft) => {
502-
let BareFnTy { unsafety, ext: _, generic_params, decl, decl_span } = bft.deref_mut();
503-
visit_unsafety(unsafety, vis);
502+
let BareFnTy { safety, ext: _, generic_params, decl, decl_span } = bft.deref_mut();
503+
visit_safety(safety, vis);
504504
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
505505
vis.visit_fn_decl(decl);
506506
vis.visit_span(decl_span);
@@ -864,6 +864,13 @@ fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
864864
}
865865
}
866866

867+
fn visit_safety<T: MutVisitor>(safety: &mut Safety, vis: &mut T) {
868+
match safety {
869+
Safety::Unsafe(span) => vis.visit_span(span),
870+
Safety::Default => {}
871+
}
872+
}
873+
867874
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
868875
fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
869876
match polarity {
@@ -1226,10 +1233,10 @@ fn visit_const_item<T: MutVisitor>(
12261233
}
12271234

12281235
fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
1229-
let FnHeader { unsafety, coroutine_kind, constness, ext: _ } = header;
1236+
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
12301237
visit_constness(constness, vis);
12311238
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
1232-
visit_unsafety(unsafety, vis);
1239+
visit_safety(safety, vis);
12331240
}
12341241

12351242
pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {

Diff for: compiler/rustc_ast_lowering/src/delegation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188
Asyncness::No => hir::IsAsync::NotAsync,
189189
};
190190
hir::FnHeader {
191-
unsafety: sig.unsafety,
191+
safety: sig.safety,
192192
constness: self.tcx.constness(sig_id),
193193
asyncness,
194194
abi: sig.abi,
@@ -341,7 +341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
341341

342342
fn generate_header_error(&self) -> hir::FnHeader {
343343
hir::FnHeader {
344-
unsafety: hir::Unsafety::Normal,
344+
safety: hir::Safety::Default,
345345
constness: hir::Constness::NotConst,
346346
asyncness: hir::IsAsync::NotAsync,
347347
abi: abi::Abi::Rust,

Diff for: compiler/rustc_ast_lowering/src/item.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13521352
hir::IsAsync::NotAsync
13531353
};
13541354
hir::FnHeader {
1355-
unsafety: self.lower_unsafety(h.unsafety),
1355+
safety: self.lower_safety(h.safety),
13561356
asyncness: asyncness,
13571357
constness: self.lower_constness(h.constness),
13581358
abi: self.lower_extern(h.ext),
@@ -1409,6 +1409,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14091409
}
14101410
}
14111411

1412+
pub(super) fn lower_safety(&mut self, u: Safety) -> hir::Safety {
1413+
match u {
1414+
Safety::Unsafe(_) => hir::Safety::Unsafe,
1415+
Safety::Default => hir::Safety::Default,
1416+
}
1417+
}
1418+
14121419
/// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
14131420
/// the carried impl trait definitions and bounds.
14141421
#[instrument(level = "debug", skip(self, f))]

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13211321
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
13221322
hir::TyKind::BareFn(self.arena.alloc(hir::BareFnTy {
13231323
generic_params,
1324-
unsafety: self.lower_unsafety(f.unsafety),
1324+
safety: self.lower_safety(f.safety),
13251325
abi: self.lower_extern(f.ext),
13261326
decl: self.lower_fn_decl(&f.decl, t.id, t.span, FnDeclKind::Pointer, None),
13271327
param_names: self.lower_fn_params_to_names(&f.decl),

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -521,17 +521,17 @@ impl<'a> AstValidator<'a> {
521521
fn check_foreign_fn_headerless(
522522
&self,
523523
// Deconstruct to ensure exhaustiveness
524-
FnHeader { unsafety, coroutine_kind, constness, ext }: FnHeader,
524+
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
525525
) {
526526
let report_err = |span| {
527527
self.dcx().emit_err(errors::FnQualifierInExtern {
528528
span: span,
529529
block: self.current_extern_span(),
530530
});
531531
};
532-
match unsafety {
533-
Unsafe::Yes(span) => report_err(span),
534-
Unsafe::No => (),
532+
match safety {
533+
Safety::Unsafe(span) => report_err(span),
534+
Safety::Default => (),
535535
}
536536
match coroutine_kind {
537537
Some(knd) => report_err(knd.span()),
@@ -592,7 +592,7 @@ impl<'a> AstValidator<'a> {
592592
(Some(FnCtxt::Free), Some(header)) => match header.ext {
593593
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _)
594594
| Extern::Implicit(_)
595-
if matches!(header.unsafety, Unsafe::Yes(_)) =>
595+
if matches!(header.safety, Safety::Unsafe(_)) =>
596596
{
597597
return;
598598
}

Diff for: compiler/rustc_ast_pretty/src/pprust/state.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ impl<'a> State<'a> {
11381138
self.pclose();
11391139
}
11401140
ast::TyKind::BareFn(f) => {
1141-
self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params);
1141+
self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
11421142
}
11431143
ast::TyKind::Path(None, path) => {
11441144
self.print_path(path, false, 0);
@@ -1892,7 +1892,7 @@ impl<'a> State<'a> {
18921892
fn print_ty_fn(
18931893
&mut self,
18941894
ext: ast::Extern,
1895-
unsafety: ast::Unsafe,
1895+
safety: ast::Safety,
18961896
decl: &ast::FnDecl,
18971897
name: Option<Ident>,
18981898
generic_params: &[ast::GenericParam],
@@ -1908,15 +1908,15 @@ impl<'a> State<'a> {
19081908
},
19091909
span: DUMMY_SP,
19101910
};
1911-
let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
1911+
let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
19121912
self.print_fn(decl, header, name, &generics);
19131913
self.end();
19141914
}
19151915

19161916
fn print_fn_header_info(&mut self, header: ast::FnHeader) {
19171917
self.print_constness(header.constness);
19181918
header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
1919-
self.print_unsafety(header.unsafety);
1919+
self.print_safety(header.safety);
19201920

19211921
match header.ext {
19221922
ast::Extern::None => {}
@@ -1940,6 +1940,13 @@ impl<'a> State<'a> {
19401940
}
19411941
}
19421942

1943+
fn print_safety(&mut self, s: ast::Safety) {
1944+
match s {
1945+
ast::Safety::Default => {}
1946+
ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
1947+
}
1948+
}
1949+
19431950
fn print_constness(&mut self, s: ast::Const) {
19441951
match s {
19451952
ast::Const::No => {}

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10981098
liberated_sig.inputs().iter().copied(),
10991099
peeled_ty,
11001100
liberated_sig.c_variadic,
1101-
hir::Unsafety::Normal,
1101+
hir::Safety::Default,
11021102
rustc_target::spec::abi::Abi::Rust,
11031103
)),
11041104
);

Diff for: compiler/rustc_borrowck/src/type_check/input_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
101101
user_provided_sig.inputs().iter().copied(),
102102
output_ty,
103103
user_provided_sig.c_variadic,
104-
user_provided_sig.unsafety,
104+
user_provided_sig.safety,
105105
user_provided_sig.abi,
106106
);
107107
}

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2064,13 +2064,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20642064
}
20652065
}
20662066

2067-
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(unsafety)) => {
2067+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(safety)) => {
20682068
let sig = match op.ty(body, tcx).kind() {
20692069
ty::Closure(_, args) => args.as_closure().sig(),
20702070
_ => bug!(),
20712071
};
20722072
let ty_fn_ptr_from =
2073-
Ty::new_fn_ptr(tcx, tcx.signature_unclosure(sig, *unsafety));
2073+
Ty::new_fn_ptr(tcx, tcx.signature_unclosure(sig, *safety));
20742074

20752075
if let Err(terr) = self.eq_types(
20762076
*ty,

Diff for: compiler/rustc_builtin_macros/src/alloc_error_handler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::util::check_builtin_macro_attribute;
33

44
use rustc_ast::ptr::P;
55
use rustc_ast::{self as ast, FnHeader, FnSig, Generics, StmtKind};
6-
use rustc_ast::{Fn, ItemKind, Stmt, TyKind, Unsafe};
6+
use rustc_ast::{Fn, ItemKind, Safety, Stmt, TyKind};
77
use rustc_expand::base::{Annotatable, ExtCtxt};
88
use rustc_span::symbol::{kw, sym, Ident};
99
use rustc_span::Span;
@@ -78,7 +78,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
7878
let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
7979
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
8080
let decl = cx.fn_decl(params, never);
81-
let header = FnHeader { unsafety: Unsafe::Yes(span), ..FnHeader::default() };
81+
let header = FnHeader { safety: Safety::Unsafe(span), ..FnHeader::default() };
8282
let sig = FnSig { decl, header, span: span };
8383

8484
let body = Some(cx.block_expr(call));

Diff for: compiler/rustc_builtin_macros/src/global_allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::expand::allocator::{
66
};
77
use rustc_ast::ptr::P;
88
use rustc_ast::{self as ast, AttrVec, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
9-
use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
9+
use rustc_ast::{Fn, ItemKind, Mutability, Safety, Stmt, Ty, TyKind};
1010
use rustc_expand::base::{Annotatable, ExtCtxt};
1111
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1212
use rustc_span::Span;
@@ -73,7 +73,7 @@ impl AllocFnFactory<'_, '_> {
7373
let result = self.call_allocator(method.name, args);
7474
let output_ty = self.ret_ty(&method.output);
7575
let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
76-
let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() };
76+
let header = FnHeader { safety: Safety::Unsafe(self.span), ..FnHeader::default() };
7777
let sig = FnSig { decl, header, span: self.span };
7878
let body = Some(self.cx.block_expr(result));
7979
let kind = ItemKind::Fn(Box::new(Fn {

Diff for: compiler/rustc_builtin_macros/src/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn check_test_signature(
549549
let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
550550
let dcx = cx.dcx();
551551

552-
if let ast::Unsafe::Yes(span) = f.sig.header.unsafety {
552+
if let ast::Safety::Unsafe(span) = f.sig.header.safety {
553553
return Err(dcx.emit_err(errors::TestBadFn { span: i.span, cause: span, kind: "unsafe" }));
554554
}
555555

Diff for: compiler/rustc_codegen_cranelift/src/value_and_place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ pub(crate) fn assert_assignable<'tcx>(
880880
let FnSig {
881881
inputs_and_output: types_from,
882882
c_variadic: c_variadic_from,
883-
unsafety: unsafety_from,
883+
safety: unsafety_from,
884884
abi: abi_from,
885885
} = from_sig;
886886
let to_sig = fx
@@ -889,7 +889,7 @@ pub(crate) fn assert_assignable<'tcx>(
889889
let FnSig {
890890
inputs_and_output: types_to,
891891
c_variadic: c_variadic_to,
892-
unsafety: unsafety_to,
892+
safety: unsafety_to,
893893
abi: abi_to,
894894
} = to_sig;
895895
let mut types_from = types_from.iter();

Diff for: compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
670670
let step3 = self.or(left, right);
671671

672672
// Fourth step.
673-
if width == 8 {
674-
step3
675-
} else {
676-
self.gcc_bswap(step3, width)
677-
}
673+
if width == 8 { step3 } else { self.gcc_bswap(step3, width) }
678674
}
679675
128 => {
680676
// TODO(antoyo): find a more efficient implementation?
@@ -1225,7 +1221,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(
12251221
iter::once(i8p),
12261222
tcx.types.unit,
12271223
false,
1228-
rustc_hir::Unsafety::Unsafe,
1224+
rustc_hir::Safety::Unsafe,
12291225
Abi::Rust,
12301226
)),
12311227
);
@@ -1236,7 +1232,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(
12361232
[i8p, i8p].iter().cloned(),
12371233
tcx.types.unit,
12381234
false,
1239-
rustc_hir::Unsafety::Unsafe,
1235+
rustc_hir::Safety::Unsafe,
12401236
Abi::Rust,
12411237
)),
12421238
);
@@ -1245,7 +1241,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(
12451241
[try_fn_ty, i8p, catch_fn_ty],
12461242
tcx.types.i32,
12471243
false,
1248-
rustc_hir::Unsafety::Unsafe,
1244+
rustc_hir::Safety::Unsafe,
12491245
Abi::Rust,
12501246
));
12511247
let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen);

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ fn get_rust_try_fn<'ll, 'tcx>(
986986
[i8p],
987987
tcx.types.unit,
988988
false,
989-
hir::Unsafety::Unsafe,
989+
hir::Safety::Unsafe,
990990
Abi::Rust,
991991
)),
992992
);
@@ -997,7 +997,7 @@ fn get_rust_try_fn<'ll, 'tcx>(
997997
[i8p, i8p],
998998
tcx.types.unit,
999999
false,
1000-
hir::Unsafety::Unsafe,
1000+
hir::Safety::Unsafe,
10011001
Abi::Rust,
10021002
)),
10031003
);
@@ -1006,7 +1006,7 @@ fn get_rust_try_fn<'ll, 'tcx>(
10061006
[try_fn_ty, i8p, catch_fn_ty],
10071007
tcx.types.i32,
10081008
false,
1009-
hir::Unsafety::Unsafe,
1009+
hir::Safety::Unsafe,
10101010
Abi::Rust,
10111011
));
10121012
let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen);

Diff for: compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
276276
sym::target_feature => {
277277
if !tcx.is_closure_like(did.to_def_id())
278278
&& let Some(fn_sig) = fn_sig()
279-
&& fn_sig.skip_binder().unsafety() == hir::Unsafety::Normal
279+
&& fn_sig.skip_binder().safety() == hir::Safety::Default
280280
{
281281
if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
282282
// The `#[target_feature]` attribute is allowed on

0 commit comments

Comments
 (0)