Skip to content

Commit 5da6fe9

Browse files
committed
Add new fn safety enum for functions
1 parent aed2187 commit 5da6fe9

File tree

89 files changed

+387
-293
lines changed

Some content is hidden

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

89 files changed

+387
-293
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 13 additions & 6 deletions
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,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 11 additions & 4 deletions
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) {

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 2 additions & 2 deletions
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,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 1 deletion
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))]

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
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),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 5 deletions
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
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 4 deletions
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 => {}

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
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
);

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 1 addition & 1 deletion
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
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
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,

0 commit comments

Comments
 (0)