Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cb0865

Browse files
committedApr 27, 2024
Add new fn safety enum for functions
1 parent aed2187 commit 2cb0865

File tree

63 files changed

+299
-206
lines changed

Some content is hidden

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

63 files changed

+299
-206
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) {

0 commit comments

Comments
 (0)
Please sign in to comment.