Skip to content

Commit 10e8413

Browse files
committed
Add new fn safety enum for functions
1 parent aed2187 commit 10e8413

File tree

92 files changed

+400
-298
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

+400
-298
lines changed

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

+13-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: FnSafety,
21032103
pub ext: Extern,
21042104
pub generic_params: ThinVec<GenericParam>,
21052105
pub decl: P<FnDecl>,
@@ -2485,6 +2485,13 @@ pub enum Unsafe {
24852485
No,
24862486
}
24872487

2488+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
2489+
#[derive(HashStable_Generic)]
2490+
pub enum FnSafety {
2491+
Unsafe(Span),
2492+
Default,
2493+
}
2494+
24882495
/// Describes what kind of coroutine markers, if any, a function has.
24892496
///
24902497
/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
@@ -3001,8 +3008,8 @@ impl Extern {
30013008
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
30023009
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
30033010
pub struct FnHeader {
3004-
/// The `unsafe` keyword, if any
3005-
pub unsafety: Unsafe,
3011+
/// The safety keyword, if any
3012+
pub safety: FnSafety,
30063013
/// Whether this is `async`, `gen`, or nothing.
30073014
pub coroutine_kind: Option<CoroutineKind>,
30083015
/// The `const` keyword, if any
@@ -3014,8 +3021,8 @@ pub struct FnHeader {
30143021
impl FnHeader {
30153022
/// Does this function header have any qualifiers or is it empty?
30163023
pub fn has_qualifiers(&self) -> bool {
3017-
let Self { unsafety, coroutine_kind, constness, ext } = self;
3018-
matches!(unsafety, Unsafe::Yes(_))
3024+
let Self { safety, coroutine_kind, constness, ext } = self;
3025+
matches!(safety, FnSafety::Unsafe(_))
30193026
|| coroutine_kind.is_some()
30203027
|| matches!(constness, Const::Yes(_))
30213028
|| !matches!(ext, Extern::None)
@@ -3025,7 +3032,7 @@ impl FnHeader {
30253032
impl Default for FnHeader {
30263033
fn default() -> FnHeader {
30273034
FnHeader {
3028-
unsafety: Unsafe::No,
3035+
safety: FnSafety::Default,
30293036
coroutine_kind: None,
30303037
constness: Const::No,
30313038
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_fn_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_fn_safety<T: MutVisitor>(safety: &mut FnSafety, vis: &mut T) {
868+
match safety {
869+
FnSafety::Unsafe(span) => vis.visit_span(span),
870+
FnSafety::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_fn_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::FnSafety::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
@@ -1351,7 +1351,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13511351
hir::IsAsync::NotAsync
13521352
};
13531353
hir::FnHeader {
1354-
unsafety: self.lower_unsafety(h.unsafety),
1354+
safety: self.lower_fn_safety(h.safety),
13551355
asyncness: asyncness,
13561356
constness: self.lower_constness(h.constness),
13571357
abi: self.lower_extern(h.ext),
@@ -1408,6 +1408,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14081408
}
14091409
}
14101410

1411+
pub(super) fn lower_fn_safety(&mut self, u: FnSafety) -> hir::FnSafety {
1412+
match u {
1413+
FnSafety::Unsafe(_) => hir::FnSafety::Unsafe,
1414+
FnSafety::Default => hir::FnSafety::Default,
1415+
}
1416+
}
1417+
14111418
/// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
14121419
/// the carried impl trait definitions and bounds.
14131420
#[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
@@ -1324,7 +1324,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13241324
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
13251325
hir::TyKind::BareFn(self.arena.alloc(hir::BareFnTy {
13261326
generic_params,
1327-
unsafety: self.lower_unsafety(f.unsafety),
1327+
safety: self.lower_fn_safety(f.safety),
13281328
abi: self.lower_extern(f.ext),
13291329
decl: self.lower_fn_decl(&f.decl, t.id, t.span, FnDeclKind::Pointer, None),
13301330
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+
FnSafety::Unsafe(span) => report_err(span),
534+
FnSafety::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, FnSafety::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::FnSafety,
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_fn_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_fn_safety(&mut self, s: ast::FnSafety) {
1944+
match s {
1945+
ast::FnSafety::Default => {}
1946+
ast::FnSafety::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
@@ -1097,7 +1097,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10971097
liberated_sig.inputs().iter().copied(),
10981098
peeled_ty,
10991099
liberated_sig.c_variadic,
1100-
hir::Unsafety::Normal,
1100+
hir::FnSafety::Default,
11011101
rustc_target::spec::abi::Abi::Rust,
11021102
)),
11031103
);

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
@@ -2063,13 +2063,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20632063
}
20642064
}
20652065

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

20742074
if let Err(terr) = self.eq_types(
20752075
*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, FnSafety, ItemKind, 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: FnSafety::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, FnSafety, ItemKind, Mutability, 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: FnSafety::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
@@ -548,7 +548,7 @@ fn check_test_signature(
548548
let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
549549
let dcx = cx.dcx();
550550

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

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
Ty::new_unit(tcx),
12271223
false,
1228-
rustc_hir::Unsafety::Unsafe,
1224+
rustc_hir::FnSafety::Unsafe,
12291225
Abi::Rust,
12301226
)),
12311227
);
@@ -1236,7 +1232,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(
12361232
[i8p, i8p].iter().cloned(),
12371233
Ty::new_unit(tcx),
12381234
false,
1239-
rustc_hir::Unsafety::Unsafe,
1235+
rustc_hir::FnSafety::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::FnSafety::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
Ty::new_unit(tcx),
988988
false,
989-
hir::Unsafety::Unsafe,
989+
hir::FnSafety::Unsafe,
990990
Abi::Rust,
991991
)),
992992
);
@@ -997,7 +997,7 @@ fn get_rust_try_fn<'ll, 'tcx>(
997997
[i8p, i8p],
998998
Ty::new_unit(tcx),
999999
false,
1000-
hir::Unsafety::Unsafe,
1000+
hir::FnSafety::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::FnSafety::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::FnSafety::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)