Skip to content

Rename Unsafe to Safety #125077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
@@ -2105,7 +2105,7 @@ impl Ty {

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

/// Safety of items.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
#[derive(HashStable_Generic)]
pub enum Unsafe {
Yes(Span),
No,
pub enum Safety {
/// `unsafe` an item is explicitly marked as `unsafe`.
Unsafe(Span),
/// Default means no value was provided, it will take a default value given the context in
/// which is used.
Default,
}

/// Describes what kind of coroutine markers, if any, a function has.
@@ -2692,7 +2696,7 @@ pub struct ModSpans {
pub struct ForeignMod {
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
/// semantically by Rust.
pub unsafety: Unsafe,
pub safety: Safety,
pub abi: Option<StrLit>,
pub items: ThinVec<P<ForeignItem>>,
}
@@ -3011,8 +3015,8 @@ impl Extern {
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
pub struct FnHeader {
/// The `unsafe` keyword, if any
pub unsafety: Unsafe,
/// Whether this is `unsafe`, or has a default safety
pub safety: Safety,
/// Whether this is `async`, `gen`, or nothing.
pub coroutine_kind: Option<CoroutineKind>,
/// The `const` keyword, if any
@@ -3024,8 +3028,8 @@ pub struct FnHeader {
impl FnHeader {
/// Does this function header have any qualifiers or is it empty?
pub fn has_qualifiers(&self) -> bool {
let Self { unsafety, coroutine_kind, constness, ext } = self;
matches!(unsafety, Unsafe::Yes(_))
let Self { safety, coroutine_kind, constness, ext } = self;
matches!(safety, Safety::Unsafe(_))
|| coroutine_kind.is_some()
|| matches!(constness, Const::Yes(_))
|| !matches!(ext, Extern::None)
@@ -3035,7 +3039,7 @@ impl FnHeader {
impl Default for FnHeader {
fn default() -> FnHeader {
FnHeader {
unsafety: Unsafe::No,
safety: Safety::Default,
coroutine_kind: None,
constness: Const::No,
ext: Extern::None,
@@ -3045,7 +3049,7 @@ impl Default for FnHeader {

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Trait {
pub unsafety: Unsafe,
pub safety: Safety,
pub is_auto: IsAuto,
pub generics: Generics,
pub bounds: GenericBounds,
@@ -3101,7 +3105,7 @@ pub struct TyAlias {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Impl {
pub defaultness: Defaultness,
pub unsafety: Unsafe,
pub safety: Safety,
pub generics: Generics,
pub constness: Const,
pub polarity: ImplPolarity,
@@ -3209,7 +3213,7 @@ pub enum ItemKind {
/// E.g., `mod foo;` or `mod foo { .. }`.
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
/// semantically by Rust.
Mod(Unsafe, ModKind),
Mod(Safety, ModKind),
/// An external module (`extern`).
///
/// E.g., `extern {}` or `extern "C" {}`.
32 changes: 16 additions & 16 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
@@ -499,8 +499,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
vis.visit_mt(mt);
}
TyKind::BareFn(bft) => {
let BareFnTy { unsafety, ext: _, generic_params, decl, decl_span } = bft.deref_mut();
visit_unsafety(unsafety, vis);
let BareFnTy { safety, ext: _, generic_params, decl, decl_span } = bft.deref_mut();
visit_safety(safety, vis);
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
vis.visit_fn_decl(decl);
vis.visit_span(decl_span);
@@ -543,8 +543,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
}

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

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

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

@@ -1092,8 +1092,8 @@ impl NoopVisitItemKind for ItemKind {
vis.visit_generics(generics);
visit_opt(body, |body| vis.visit_block(body));
}
ItemKind::Mod(unsafety, mod_kind) => {
visit_unsafety(unsafety, vis);
ItemKind::Mod(safety, mod_kind) => {
visit_safety(safety, vis);
match mod_kind {
ModKind::Loaded(items, _inline, ModSpans { inner_span, inject_use_span }) => {
vis.visit_span(inner_span);
@@ -1130,7 +1130,7 @@ impl NoopVisitItemKind for ItemKind {
}
ItemKind::Impl(box Impl {
defaultness,
unsafety,
safety,
generics,
constness,
polarity,
@@ -1139,16 +1139,16 @@ impl NoopVisitItemKind for ItemKind {
items,
}) => {
visit_defaultness(defaultness, vis);
visit_unsafety(unsafety, vis);
visit_safety(safety, vis);
vis.visit_generics(generics);
visit_constness(constness, vis);
visit_polarity(polarity, vis);
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
vis.visit_ty(self_ty);
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
}
ItemKind::Trait(box Trait { unsafety, is_auto: _, generics, bounds, items }) => {
visit_unsafety(unsafety, vis);
ItemKind::Trait(box Trait { safety, is_auto: _, generics, bounds, items }) => {
visit_safety(safety, vis);
vis.visit_generics(generics);
visit_bounds(bounds, vis);
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
@@ -1254,10 +1254,10 @@ fn visit_const_item<T: MutVisitor>(
}

fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety, coroutine_kind, constness, ext: _ } = header;
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
visit_constness(constness, vis);
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
visit_unsafety(unsafety, vis);
visit_safety(safety, vis);
}

pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
@@ -366,7 +366,7 @@ impl WalkItemKind for ItemKind {
}
ItemKind::Impl(box Impl {
defaultness: _,
unsafety: _,
safety: _,
generics,
constness: _,
polarity: _,
@@ -384,7 +384,7 @@ impl WalkItemKind for ItemKind {
try_visit!(visitor.visit_generics(generics));
try_visit!(visitor.visit_variant_data(struct_definition));
}
ItemKind::Trait(box Trait { unsafety: _, is_auto: _, generics, bounds, items }) => {
ItemKind::Trait(box Trait { safety: _, is_auto: _, generics, bounds, items }) => {
try_visit!(visitor.visit_generics(generics));
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Asyncness::No => hir::IsAsync::NotAsync,
};
hir::FnHeader {
unsafety: sig.unsafety,
safety: sig.safety,
constness: self.tcx.constness(sig_id),
asyncness,
abi: sig.abi,
@@ -341,7 +341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn generate_header_error(&self) -> hir::FnHeader {
hir::FnHeader {
unsafety: hir::Unsafety::Normal,
safety: hir::Safety::Safe,
constness: hir::Constness::NotConst,
asyncness: hir::IsAsync::NotAsync,
abi: abi::Abi::Rust,
24 changes: 12 additions & 12 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -323,7 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ItemKind::Union(vdata, generics)
}
ItemKind::Impl(box Impl {
unsafety,
safety,
polarity,
defaultness,
constness,
@@ -388,7 +388,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
};
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
unsafety: self.lower_unsafety(*unsafety),
safety: self.lower_safety(*safety),
polarity,
defaultness,
defaultness_span,
@@ -398,14 +398,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
items: new_impl_items,
}))
}
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
ItemKind::Trait(box Trait { is_auto, safety, generics, bounds, items }) => {
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
let constness = attrs
.unwrap_or(&[])
.iter()
.find(|x| x.has_name(sym::const_trait))
.map_or(Const::No, |x| Const::Yes(x.span));
let (generics, (unsafety, items, bounds)) = self.lower_generics(
let (generics, (safety, items, bounds)) = self.lower_generics(
generics,
constness,
id,
@@ -418,11 +418,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
let items = this.arena.alloc_from_iter(
items.iter().map(|item| this.lower_trait_item_ref(item)),
);
let unsafety = this.lower_unsafety(*unsafety);
(unsafety, items, bounds)
let safety = this.lower_safety(*safety);
(safety, items, bounds)
},
);
hir::ItemKind::Trait(*is_auto, unsafety, generics, bounds, items)
hir::ItemKind::Trait(*is_auto, safety, generics, bounds, items)
}
ItemKind::TraitAlias(generics, bounds) => {
let (generics, bounds) = self.lower_generics(
@@ -1360,7 +1360,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::IsAsync::NotAsync
};
hir::FnHeader {
unsafety: self.lower_unsafety(h.unsafety),
safety: self.lower_safety(h.safety),
asyncness: asyncness,
constness: self.lower_constness(h.constness),
abi: self.lower_extern(h.ext),
@@ -1410,10 +1410,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety {
match u {
Unsafe::Yes(_) => hir::Unsafety::Unsafe,
Unsafe::No => hir::Unsafety::Normal,
pub(super) fn lower_safety(&mut self, s: Safety) -> hir::Safety {
match s {
Safety::Unsafe(_) => hir::Safety::Unsafe,
Safety::Default => hir::Safety::Safe,
}
}

2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1324,7 +1324,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
hir::TyKind::BareFn(self.arena.alloc(hir::BareFnTy {
generic_params,
unsafety: self.lower_unsafety(f.unsafety),
safety: self.lower_safety(f.safety),
abi: self.lower_extern(f.ext),
decl: self.lower_fn_decl(&f.decl, t.id, t.span, FnDeclKind::Pointer, None),
param_names: self.lower_fn_params_to_names(&f.decl),
26 changes: 13 additions & 13 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
@@ -521,17 +521,17 @@ impl<'a> AstValidator<'a> {
fn check_foreign_fn_headerless(
&self,
// Deconstruct to ensure exhaustiveness
FnHeader { unsafety, coroutine_kind, constness, ext }: FnHeader,
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
) {
let report_err = |span| {
self.dcx().emit_err(errors::FnQualifierInExtern {
span: span,
block: self.current_extern_span(),
});
};
match unsafety {
Unsafe::Yes(span) => report_err(span),
Unsafe::No => (),
match safety {
Safety::Unsafe(span) => report_err(span),
Safety::Default => (),
}
match coroutine_kind {
Some(knd) => report_err(knd.span()),
@@ -592,7 +592,7 @@ impl<'a> AstValidator<'a> {
(Some(FnCtxt::Free), Some(header)) => match header.ext {
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _)
| Extern::Implicit(_)
if matches!(header.unsafety, Unsafe::Yes(_)) =>
if matches!(header.safety, Safety::Unsafe(_)) =>
{
return;
}
@@ -891,7 +891,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

match &item.kind {
ItemKind::Impl(box Impl {
unsafety,
safety,
polarity,
defaultness: _,
constness,
@@ -910,7 +910,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// which isn't allowed. Not a problem for this obscure, obsolete syntax.
this.dcx().emit_fatal(errors::ObsoleteAuto { span: item.span });
}
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)
if let (&Safety::Unsafe(span), &ImplPolarity::Negative(sp)) = (safety, polarity)
{
this.dcx().emit_err(errors::UnsafeNegativeImpl {
span: sp.to(t.path.span),
@@ -933,7 +933,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
return; // Avoid visiting again.
}
ItemKind::Impl(box Impl {
unsafety,
safety,
polarity,
defaultness,
constness,
@@ -956,7 +956,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
&item.vis,
errors::VisibilityNotPermittedNote::IndividualImplItems,
);
if let &Unsafe::Yes(span) = unsafety {
if let &Safety::Unsafe(span) = safety {
this.dcx().emit_err(errors::InherentImplCannotUnsafe {
span: self_ty.span,
annotation_span: span,
@@ -1020,13 +1020,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
walk_list!(self, visit_attribute, &item.attrs);
return; // Avoid visiting again.
}
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => {
let old_item = mem::replace(&mut self.extern_mod, Some(item));
self.visibility_not_permitted(
&item.vis,
errors::VisibilityNotPermittedNote::IndividualForeignItems,
);
if let &Unsafe::Yes(span) = unsafety {
if let &Safety::Unsafe(span) = safety {
self.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" });
}
if abi.is_none() {
@@ -1078,8 +1078,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
walk_list!(self, visit_attribute, &item.attrs);
return; // Avoid visiting again
}
ItemKind::Mod(unsafety, mod_kind) => {
if let &Unsafe::Yes(span) = unsafety {
ItemKind::Mod(safety, mod_kind) => {
if let &Safety::Unsafe(span) = safety {
self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });
}
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
Loading