Skip to content

Commit eb1a5c9

Browse files
committed
Auto merge of #125077 - spastorino:add-new-fnsafety-enum2, r=jackh726
Rename Unsafe to Safety Alternative to #124455, which is to just have one Safety enum to use everywhere, this opens the posibility of adding `ast::Safety::Safe` that's useful for unsafe extern blocks. This leaves us today with: ```rust enum ast::Safety { Unsafe(Span), Default, // Safe (going to be added for unsafe extern blocks) } enum hir::Safety { Unsafe, Safe, } ``` We would convert from `ast::Safety::Default` into the right Safety level according the context.
2 parents c00957a + 6b46a91 commit eb1a5c9

File tree

115 files changed

+460
-494
lines changed

Some content is hidden

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

115 files changed

+460
-494
lines changed

compiler/rustc_ast/src/ast.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ impl Ty {
21052105

21062106
#[derive(Clone, Encodable, Decodable, Debug)]
21072107
pub struct BareFnTy {
2108-
pub unsafety: Unsafe,
2108+
pub safety: Safety,
21092109
pub ext: Extern,
21102110
pub generic_params: ThinVec<GenericParam>,
21112111
pub decl: P<FnDecl>,
@@ -2484,11 +2484,15 @@ pub enum IsAuto {
24842484
No,
24852485
}
24862486

2487+
/// Safety of items.
24872488
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
24882489
#[derive(HashStable_Generic)]
2489-
pub enum Unsafe {
2490-
Yes(Span),
2491-
No,
2490+
pub enum Safety {
2491+
/// `unsafe` an item is explicitly marked as `unsafe`.
2492+
Unsafe(Span),
2493+
/// Default means no value was provided, it will take a default value given the context in
2494+
/// which is used.
2495+
Default,
24922496
}
24932497

24942498
/// Describes what kind of coroutine markers, if any, a function has.
@@ -2692,7 +2696,7 @@ pub struct ModSpans {
26922696
pub struct ForeignMod {
26932697
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
26942698
/// semantically by Rust.
2695-
pub unsafety: Unsafe,
2699+
pub safety: Safety,
26962700
pub abi: Option<StrLit>,
26972701
pub items: ThinVec<P<ForeignItem>>,
26982702
}
@@ -3011,8 +3015,8 @@ impl Extern {
30113015
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
30123016
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
30133017
pub struct FnHeader {
3014-
/// The `unsafe` keyword, if any
3015-
pub unsafety: Unsafe,
3018+
/// Whether this is `unsafe`, or has a default safety
3019+
pub safety: Safety,
30163020
/// Whether this is `async`, `gen`, or nothing.
30173021
pub coroutine_kind: Option<CoroutineKind>,
30183022
/// The `const` keyword, if any
@@ -3024,8 +3028,8 @@ pub struct FnHeader {
30243028
impl FnHeader {
30253029
/// Does this function header have any qualifiers or is it empty?
30263030
pub fn has_qualifiers(&self) -> bool {
3027-
let Self { unsafety, coroutine_kind, constness, ext } = self;
3028-
matches!(unsafety, Unsafe::Yes(_))
3031+
let Self { safety, coroutine_kind, constness, ext } = self;
3032+
matches!(safety, Safety::Unsafe(_))
30293033
|| coroutine_kind.is_some()
30303034
|| matches!(constness, Const::Yes(_))
30313035
|| !matches!(ext, Extern::None)
@@ -3035,7 +3039,7 @@ impl FnHeader {
30353039
impl Default for FnHeader {
30363040
fn default() -> FnHeader {
30373041
FnHeader {
3038-
unsafety: Unsafe::No,
3042+
safety: Safety::Default,
30393043
coroutine_kind: None,
30403044
constness: Const::No,
30413045
ext: Extern::None,
@@ -3045,7 +3049,7 @@ impl Default for FnHeader {
30453049

30463050
#[derive(Clone, Encodable, Decodable, Debug)]
30473051
pub struct Trait {
3048-
pub unsafety: Unsafe,
3052+
pub safety: Safety,
30493053
pub is_auto: IsAuto,
30503054
pub generics: Generics,
30513055
pub bounds: GenericBounds,
@@ -3101,7 +3105,7 @@ pub struct TyAlias {
31013105
#[derive(Clone, Encodable, Decodable, Debug)]
31023106
pub struct Impl {
31033107
pub defaultness: Defaultness,
3104-
pub unsafety: Unsafe,
3108+
pub safety: Safety,
31053109
pub generics: Generics,
31063110
pub constness: Const,
31073111
pub polarity: ImplPolarity,
@@ -3209,7 +3213,7 @@ pub enum ItemKind {
32093213
/// E.g., `mod foo;` or `mod foo { .. }`.
32103214
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
32113215
/// semantically by Rust.
3212-
Mod(Unsafe, ModKind),
3216+
Mod(Safety, ModKind),
32133217
/// An external module (`extern`).
32143218
///
32153219
/// E.g., `extern {}` or `extern "C" {}`.

compiler/rustc_ast/src/mut_visit.rs

+16-16
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);
@@ -543,8 +543,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
543543
}
544544

545545
fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) {
546-
let ForeignMod { unsafety, abi: _, items } = foreign_mod;
547-
visit_unsafety(unsafety, vis);
546+
let ForeignMod { safety, abi: _, items } = foreign_mod;
547+
visit_safety(safety, vis);
548548
items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
549549
}
550550

@@ -859,10 +859,10 @@ fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T)
859859
}
860860

861861
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
862-
fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
863-
match unsafety {
864-
Unsafe::Yes(span) => vis.visit_span(span),
865-
Unsafe::No => {}
862+
fn visit_safety<T: MutVisitor>(safety: &mut Safety, vis: &mut T) {
863+
match safety {
864+
Safety::Unsafe(span) => vis.visit_span(span),
865+
Safety::Default => {}
866866
}
867867
}
868868

@@ -1092,8 +1092,8 @@ impl NoopVisitItemKind for ItemKind {
10921092
vis.visit_generics(generics);
10931093
visit_opt(body, |body| vis.visit_block(body));
10941094
}
1095-
ItemKind::Mod(unsafety, mod_kind) => {
1096-
visit_unsafety(unsafety, vis);
1095+
ItemKind::Mod(safety, mod_kind) => {
1096+
visit_safety(safety, vis);
10971097
match mod_kind {
10981098
ModKind::Loaded(items, _inline, ModSpans { inner_span, inject_use_span }) => {
10991099
vis.visit_span(inner_span);
@@ -1130,7 +1130,7 @@ impl NoopVisitItemKind for ItemKind {
11301130
}
11311131
ItemKind::Impl(box Impl {
11321132
defaultness,
1133-
unsafety,
1133+
safety,
11341134
generics,
11351135
constness,
11361136
polarity,
@@ -1139,16 +1139,16 @@ impl NoopVisitItemKind for ItemKind {
11391139
items,
11401140
}) => {
11411141
visit_defaultness(defaultness, vis);
1142-
visit_unsafety(unsafety, vis);
1142+
visit_safety(safety, vis);
11431143
vis.visit_generics(generics);
11441144
visit_constness(constness, vis);
11451145
visit_polarity(polarity, vis);
11461146
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
11471147
vis.visit_ty(self_ty);
11481148
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
11491149
}
1150-
ItemKind::Trait(box Trait { unsafety, is_auto: _, generics, bounds, items }) => {
1151-
visit_unsafety(unsafety, vis);
1150+
ItemKind::Trait(box Trait { safety, is_auto: _, generics, bounds, items }) => {
1151+
visit_safety(safety, vis);
11521152
vis.visit_generics(generics);
11531153
visit_bounds(bounds, vis);
11541154
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
@@ -1254,10 +1254,10 @@ fn visit_const_item<T: MutVisitor>(
12541254
}
12551255

12561256
fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
1257-
let FnHeader { unsafety, coroutine_kind, constness, ext: _ } = header;
1257+
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
12581258
visit_constness(constness, vis);
12591259
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
1260-
visit_unsafety(unsafety, vis);
1260+
visit_safety(safety, vis);
12611261
}
12621262

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

compiler/rustc_ast/src/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl WalkItemKind for ItemKind {
366366
}
367367
ItemKind::Impl(box Impl {
368368
defaultness: _,
369-
unsafety: _,
369+
safety: _,
370370
generics,
371371
constness: _,
372372
polarity: _,
@@ -384,7 +384,7 @@ impl WalkItemKind for ItemKind {
384384
try_visit!(visitor.visit_generics(generics));
385385
try_visit!(visitor.visit_variant_data(struct_definition));
386386
}
387-
ItemKind::Trait(box Trait { unsafety: _, is_auto: _, generics, bounds, items }) => {
387+
ItemKind::Trait(box Trait { safety: _, is_auto: _, generics, bounds, items }) => {
388388
try_visit!(visitor.visit_generics(generics));
389389
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
390390
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);

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::Safe,
345345
constness: hir::Constness::NotConst,
346346
asyncness: hir::IsAsync::NotAsync,
347347
abi: abi::Abi::Rust,

compiler/rustc_ast_lowering/src/item.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
323323
hir::ItemKind::Union(vdata, generics)
324324
}
325325
ItemKind::Impl(box Impl {
326-
unsafety,
326+
safety,
327327
polarity,
328328
defaultness,
329329
constness,
@@ -388,7 +388,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
388388
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
389389
};
390390
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
391-
unsafety: self.lower_unsafety(*unsafety),
391+
safety: self.lower_safety(*safety),
392392
polarity,
393393
defaultness,
394394
defaultness_span,
@@ -398,14 +398,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
398398
items: new_impl_items,
399399
}))
400400
}
401-
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
401+
ItemKind::Trait(box Trait { is_auto, safety, generics, bounds, items }) => {
402402
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
403403
let constness = attrs
404404
.unwrap_or(&[])
405405
.iter()
406406
.find(|x| x.has_name(sym::const_trait))
407407
.map_or(Const::No, |x| Const::Yes(x.span));
408-
let (generics, (unsafety, items, bounds)) = self.lower_generics(
408+
let (generics, (safety, items, bounds)) = self.lower_generics(
409409
generics,
410410
constness,
411411
id,
@@ -418,11 +418,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
418418
let items = this.arena.alloc_from_iter(
419419
items.iter().map(|item| this.lower_trait_item_ref(item)),
420420
);
421-
let unsafety = this.lower_unsafety(*unsafety);
422-
(unsafety, items, bounds)
421+
let safety = this.lower_safety(*safety);
422+
(safety, items, bounds)
423423
},
424424
);
425-
hir::ItemKind::Trait(*is_auto, unsafety, generics, bounds, items)
425+
hir::ItemKind::Trait(*is_auto, safety, generics, bounds, items)
426426
}
427427
ItemKind::TraitAlias(generics, bounds) => {
428428
let (generics, bounds) = self.lower_generics(
@@ -1360,7 +1360,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13601360
hir::IsAsync::NotAsync
13611361
};
13621362
hir::FnHeader {
1363-
unsafety: self.lower_unsafety(h.unsafety),
1363+
safety: self.lower_safety(h.safety),
13641364
asyncness: asyncness,
13651365
constness: self.lower_constness(h.constness),
13661366
abi: self.lower_extern(h.ext),
@@ -1410,10 +1410,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
14101410
}
14111411
}
14121412

1413-
pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety {
1414-
match u {
1415-
Unsafe::Yes(_) => hir::Unsafety::Unsafe,
1416-
Unsafe::No => hir::Unsafety::Normal,
1413+
pub(super) fn lower_safety(&mut self, s: Safety) -> hir::Safety {
1414+
match s {
1415+
Safety::Unsafe(_) => hir::Safety::Unsafe,
1416+
Safety::Default => hir::Safety::Safe,
14171417
}
14181418
}
14191419

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_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

+13-13
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
}
@@ -891,7 +891,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
891891

892892
match &item.kind {
893893
ItemKind::Impl(box Impl {
894-
unsafety,
894+
safety,
895895
polarity,
896896
defaultness: _,
897897
constness,
@@ -910,7 +910,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
910910
// which isn't allowed. Not a problem for this obscure, obsolete syntax.
911911
this.dcx().emit_fatal(errors::ObsoleteAuto { span: item.span });
912912
}
913-
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)
913+
if let (&Safety::Unsafe(span), &ImplPolarity::Negative(sp)) = (safety, polarity)
914914
{
915915
this.dcx().emit_err(errors::UnsafeNegativeImpl {
916916
span: sp.to(t.path.span),
@@ -933,7 +933,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
933933
return; // Avoid visiting again.
934934
}
935935
ItemKind::Impl(box Impl {
936-
unsafety,
936+
safety,
937937
polarity,
938938
defaultness,
939939
constness,
@@ -956,7 +956,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
956956
&item.vis,
957957
errors::VisibilityNotPermittedNote::IndividualImplItems,
958958
);
959-
if let &Unsafe::Yes(span) = unsafety {
959+
if let &Safety::Unsafe(span) = safety {
960960
this.dcx().emit_err(errors::InherentImplCannotUnsafe {
961961
span: self_ty.span,
962962
annotation_span: span,
@@ -1020,13 +1020,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10201020
walk_list!(self, visit_attribute, &item.attrs);
10211021
return; // Avoid visiting again.
10221022
}
1023-
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
1023+
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => {
10241024
let old_item = mem::replace(&mut self.extern_mod, Some(item));
10251025
self.visibility_not_permitted(
10261026
&item.vis,
10271027
errors::VisibilityNotPermittedNote::IndividualForeignItems,
10281028
);
1029-
if let &Unsafe::Yes(span) = unsafety {
1029+
if let &Safety::Unsafe(span) = safety {
10301030
self.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" });
10311031
}
10321032
if abi.is_none() {
@@ -1078,8 +1078,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10781078
walk_list!(self, visit_attribute, &item.attrs);
10791079
return; // Avoid visiting again
10801080
}
1081-
ItemKind::Mod(unsafety, mod_kind) => {
1082-
if let &Unsafe::Yes(span) = unsafety {
1081+
ItemKind::Mod(safety, mod_kind) => {
1082+
if let &Safety::Unsafe(span) = safety {
10831083
self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });
10841084
}
10851085
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).

0 commit comments

Comments
 (0)