Skip to content

Commit 6e798b4

Browse files
committed
Add new safety enum to be used in inner extern block items
1 parent ba6f9aa commit 6e798b4

File tree

96 files changed

+402
-304
lines changed

Some content is hidden

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

96 files changed

+402
-304
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 17 additions & 6 deletions
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>,
@@ -2491,6 +2491,17 @@ pub enum Unsafe {
24912491
No,
24922492
}
24932493

2494+
/// Safety of items (for now only used on inner extern block items).
2495+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
2496+
#[derive(HashStable_Generic)]
2497+
pub enum Safety {
2498+
/// `unsafe` an item is explicitly marked as `unsafe`.
2499+
Unsafe(Span),
2500+
/// Default means no value was provided, it will take a default value given the context in
2501+
/// which is used.
2502+
Default,
2503+
}
2504+
24942505
/// Describes what kind of coroutine markers, if any, a function has.
24952506
///
24962507
/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
@@ -3011,8 +3022,8 @@ impl Extern {
30113022
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
30123023
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
30133024
pub struct FnHeader {
3014-
/// The `unsafe` keyword, if any
3015-
pub unsafety: Unsafe,
3025+
/// The safety keyword, if any
3026+
pub safety: Safety,
30163027
/// Whether this is `async`, `gen`, or nothing.
30173028
pub coroutine_kind: Option<CoroutineKind>,
30183029
/// The `const` keyword, if any
@@ -3024,8 +3035,8 @@ pub struct FnHeader {
30243035
impl FnHeader {
30253036
/// Does this function header have any qualifiers or is it empty?
30263037
pub fn has_qualifiers(&self) -> bool {
3027-
let Self { unsafety, coroutine_kind, constness, ext } = self;
3028-
matches!(unsafety, Unsafe::Yes(_))
3038+
let Self { safety, coroutine_kind, constness, ext } = self;
3039+
matches!(safety, Safety::Unsafe(_))
30293040
|| coroutine_kind.is_some()
30303041
|| matches!(constness, Const::Yes(_))
30313042
|| !matches!(ext, Extern::None)
@@ -3035,7 +3046,7 @@ impl FnHeader {
30353046
impl Default for FnHeader {
30363047
fn default() -> FnHeader {
30373048
FnHeader {
3038-
unsafety: Unsafe::No,
3049+
safety: Safety::Default,
30393050
coroutine_kind: None,
30403051
constness: Const::No,
30413052
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_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);
@@ -866,6 +866,13 @@ fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
866866
}
867867
}
868868

869+
fn visit_safety<T: MutVisitor>(safety: &mut Safety, vis: &mut T) {
870+
match safety {
871+
Safety::Unsafe(span) => vis.visit_span(span),
872+
Safety::Default => {}
873+
}
874+
}
875+
869876
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
870877
fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
871878
match polarity {
@@ -1254,10 +1261,10 @@ fn visit_const_item<T: MutVisitor>(
12541261
}
12551262

12561263
fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
1257-
let FnHeader { unsafety, coroutine_kind, constness, ext: _ } = header;
1264+
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
12581265
visit_constness(constness, vis);
12591266
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
1260-
visit_unsafety(unsafety, vis);
1267+
visit_safety(safety, vis);
12611268
}
12621269

12631270
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::Safety::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
@@ -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),
@@ -1417,6 +1417,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14171417
}
14181418
}
14191419

1420+
pub(super) fn lower_safety(&mut self, u: Safety) -> hir::Safety {
1421+
match u {
1422+
Safety::Unsafe(_) => hir::Safety::Unsafe,
1423+
Safety::Default => hir::Safety::Default,
1424+
}
1425+
}
1426+
14201427
/// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
14211428
/// the carried impl trait definitions and bounds.
14221429
#[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_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+
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
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ impl<'a> State<'a> {
11501150
self.pclose();
11511151
}
11521152
ast::TyKind::BareFn(f) => {
1153-
self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params);
1153+
self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
11541154
}
11551155
ast::TyKind::Path(None, path) => {
11561156
self.print_path(path, false, 0);
@@ -1908,7 +1908,7 @@ impl<'a> State<'a> {
19081908
fn print_ty_fn(
19091909
&mut self,
19101910
ext: ast::Extern,
1911-
unsafety: ast::Unsafe,
1911+
safety: ast::Safety,
19121912
decl: &ast::FnDecl,
19131913
name: Option<Ident>,
19141914
generic_params: &[ast::GenericParam],
@@ -1924,15 +1924,15 @@ impl<'a> State<'a> {
19241924
},
19251925
span: DUMMY_SP,
19261926
};
1927-
let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
1927+
let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
19281928
self.print_fn(decl, header, name, &generics);
19291929
self.end();
19301930
}
19311931

19321932
fn print_fn_header_info(&mut self, header: ast::FnHeader) {
19331933
self.print_constness(header.constness);
19341934
header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
1935-
self.print_unsafety(header.unsafety);
1935+
self.print_safety(header.safety);
19361936

19371937
match header.ext {
19381938
ast::Extern::None => {}
@@ -1956,6 +1956,13 @@ impl<'a> State<'a> {
19561956
}
19571957
}
19581958

1959+
fn print_safety(&mut self, s: ast::Safety) {
1960+
match s {
1961+
ast::Safety::Default => {}
1962+
ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
1963+
}
1964+
}
1965+
19591966
fn print_constness(&mut self, s: ast::Const) {
19601967
match s {
19611968
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
@@ -1098,7 +1098,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10981098
liberated_sig.inputs().iter().copied(),
10991099
peeled_ty,
11001100
liberated_sig.c_variadic,
1101-
hir::Unsafety::Normal,
1101+
hir::Safety::Default,
11021102
rustc_target::spec::abi::Abi::Rust,
11031103
)),
11041104
);

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
9898
user_provided_sig.inputs().iter().copied(),
9999
output_ty,
100100
user_provided_sig.c_variadic,
101-
user_provided_sig.unsafety,
101+
user_provided_sig.safety,
102102
user_provided_sig.abi,
103103
);
104104
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,13 +2007,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20072007
}
20082008
}
20092009

2010-
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(unsafety)) => {
2010+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(safety)) => {
20112011
let sig = match op.ty(body, tcx).kind() {
20122012
ty::Closure(_, args) => args.as_closure().sig(),
20132013
_ => bug!(),
20142014
};
20152015
let ty_fn_ptr_from =
2016-
Ty::new_fn_ptr(tcx, tcx.signature_unclosure(sig, *unsafety));
2016+
Ty::new_fn_ptr(tcx, tcx.signature_unclosure(sig, *safety));
20172017

20182018
if let Err(terr) = self.eq_types(
20192019
*ty,

0 commit comments

Comments
 (0)