Skip to content

Commit 542a307

Browse files
committed
Handle safe safety keyword for extern block inner items
1 parent 3a0ee07 commit 542a307

File tree

57 files changed

+176
-82
lines changed

Some content is hidden

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

57 files changed

+176
-82
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,8 @@ pub enum Unsafe {
24912491
pub enum Safety {
24922492
/// `unsafe` an item is explicitly marked as `unsafe`.
24932493
Unsafe(Span),
2494+
/// `safe` an item is explicitly marked as `safe`.
2495+
Safe(Span),
24942496
/// Default means no value was provided, it will take a default value given the context in
24952497
/// which is used.
24962498
Default,
@@ -3142,6 +3144,7 @@ pub struct StaticItem {
31423144
#[derive(Clone, Encodable, Decodable, Debug)]
31433145
pub struct StaticForeignItem {
31443146
pub ty: P<Ty>,
3147+
pub safety: Safety,
31453148
pub mutability: Mutability,
31463149
pub expr: Option<P<Expr>>,
31473150
}
@@ -3150,6 +3153,7 @@ impl From<StaticItem> for StaticForeignItem {
31503153
fn from(static_item: StaticItem) -> StaticForeignItem {
31513154
StaticForeignItem {
31523155
ty: static_item.ty,
3156+
safety: Safety::Default,
31533157
mutability: static_item.mutability,
31543158
expr: static_item.expr,
31553159
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
867867
fn visit_fn_safety<T: MutVisitor>(safety: &mut Safety, vis: &mut T) {
868868
match safety {
869869
Safety::Unsafe(span) => vis.visit_span(span),
870+
Safety::Safe(span) => vis.visit_span(span),
870871
Safety::Default => {}
871872
}
872873
}
@@ -1268,7 +1269,12 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12681269
impl NoopVisitItemKind for ForeignItemKind {
12691270
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
12701271
match self {
1271-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
1272+
ForeignItemKind::Static(box StaticForeignItem {
1273+
ty,
1274+
mutability: _,
1275+
expr,
1276+
safety: _,
1277+
}) => {
12721278
visitor.visit_ty(ty);
12731279
visit_opt(expr, |expr| visitor.visit_expr(expr));
12741280
}

compiler/rustc_ast/src/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: IdentIsRaw) -> boo
210210
kw::Unsafe,
211211
kw::While,
212212
kw::Yield,
213+
kw::Safe,
213214
kw::Static,
214215
]
215216
.contains(&name)
@@ -563,6 +564,7 @@ impl Token {
563564
kw::Impl,
564565
kw::Unsafe,
565566
kw::Const,
567+
kw::Safe,
566568
kw::Static,
567569
kw::Union,
568570
kw::Macro,

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,12 @@ impl WalkItemKind for ForeignItemKind {
642642
) -> V::Result {
643643
let &Item { id, span, ident, ref vis, .. } = item;
644644
match self {
645-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
645+
ForeignItemKind::Static(box StaticForeignItem {
646+
ty,
647+
mutability: _,
648+
expr,
649+
safety: _,
650+
}) => {
646651
try_visit!(visitor.visit_ty(ty));
647652
visit_opt!(visitor, visit_expr, expr);
648653
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
659659
this.lower_fn_params_to_names(fdec),
660660
)
661661
});
662+
let safety = self.lower_fn_safety(sig.header.safety);
662663

663-
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
664+
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics, safety)
664665
}
665-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability, expr: _ }) => {
666+
ForeignItemKind::Static(box StaticForeignItem {
667+
ty,
668+
mutability,
669+
expr: _,
670+
safety,
671+
}) => {
666672
let ty = self
667673
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
668-
hir::ForeignItemKind::Static(ty, *mutability)
674+
let safety = self.lower_fn_safety(*safety);
675+
676+
hir::ForeignItemKind::Static(ty, *mutability, safety)
669677
}
670678
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
671679
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
@@ -1411,6 +1419,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14111419
pub(super) fn lower_fn_safety(&mut self, u: Safety) -> hir::Safety {
14121420
match u {
14131421
Safety::Unsafe(_) => hir::Safety::Unsafe,
1422+
Safety::Safe(_) => hir::Safety::Safe,
14141423
Safety::Default => hir::Safety::Default,
14151424
}
14161425
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11901190
self.check_foreign_ty_genericless(generics, where_clauses);
11911191
self.check_foreign_item_ascii_only(fi.ident);
11921192
}
1193-
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability: _, expr }) => {
1193+
ForeignItemKind::Static(box StaticForeignItem { expr, .. }) => {
11941194
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span));
11951195
self.check_foreign_item_ascii_only(fi.ident);
11961196
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,7 @@ impl<'a> State<'a> {
19431943
fn print_fn_safety(&mut self, s: ast::Safety) {
19441944
match s {
19451945
ast::Safety::Default => {}
1946+
ast::Safety::Safe(_) => self.word_nbsp("safe"),
19461947
ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
19471948
}
19481949
}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ impl<'a> State<'a> {
3030
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
3131
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
3232
}
33-
ast::ForeignItemKind::Static(box ast::StaticForeignItem { ty, mutability, expr }) => {
33+
ast::ForeignItemKind::Static(box ast::StaticForeignItem {
34+
ty,
35+
mutability,
36+
expr,
37+
safety,
38+
}) => {
39+
self.print_fn_safety(*safety);
3440
self.print_item_const(
3541
ident,
3642
Some(*mutability),

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,13 +3208,15 @@ impl fmt::Display for Unsafety {
32083208
#[derive(Encodable, Decodable, HashStable_Generic)]
32093209
pub enum Safety {
32103210
Unsafe,
3211+
Safe,
32113212
Default,
32123213
}
32133214

32143215
impl Safety {
32153216
pub fn prefix_str(&self) -> &'static str {
32163217
match self {
32173218
Self::Unsafe => "unsafe ",
3219+
Self::Safe => "safe ",
32183220
Self::Default => "",
32193221
}
32203222
}
@@ -3224,6 +3226,7 @@ impl fmt::Display for Safety {
32243226
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32253227
f.write_str(match *self {
32263228
Self::Unsafe => "unsafe",
3229+
Self::Safe => "safe",
32273230
Self::Default => "normal",
32283231
})
32293232
}
@@ -3461,9 +3464,9 @@ impl ForeignItem<'_> {
34613464
#[derive(Debug, Clone, Copy, HashStable_Generic)]
34623465
pub enum ForeignItemKind<'hir> {
34633466
/// A foreign function.
3464-
Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>),
3467+
Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>, Safety),
34653468
/// A foreign static item (`static ext: u8`).
3466-
Static(&'hir Ty<'hir>, Mutability),
3469+
Static(&'hir Ty<'hir>, Mutability, Safety),
34673470
/// A foreign type.
34683471
Type,
34693472
}
@@ -3533,7 +3536,7 @@ impl<'hir> OwnerNode<'hir> {
35333536
| OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
35343537
| OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl),
35353538
OwnerNode::ForeignItem(ForeignItem {
3536-
kind: ForeignItemKind::Fn(fn_decl, _, _),
3539+
kind: ForeignItemKind::Fn(fn_decl, _, _, _),
35373540
..
35383541
}) => Some(fn_decl),
35393542
_ => None,
@@ -3720,9 +3723,9 @@ impl<'hir> Node<'hir> {
37203723
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
37213724
| Node::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl),
37223725
Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl, .. }), .. })
3723-
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
3724-
Some(fn_decl)
3725-
}
3726+
| Node::ForeignItem(ForeignItem {
3727+
kind: ForeignItemKind::Fn(fn_decl, _, _, _), ..
3728+
}) => Some(fn_decl),
37263729
_ => None,
37273730
}
37283731
}
@@ -3806,7 +3809,7 @@ impl<'hir> Node<'hir> {
38063809
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
38073810
match self {
38083811
Node::ForeignItem(ForeignItem {
3809-
kind: ForeignItemKind::Fn(_, _, generics), ..
3812+
kind: ForeignItemKind::Fn(_, _, generics, _), ..
38103813
})
38113814
| Node::TraitItem(TraitItem { generics, .. })
38123815
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,14 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(
608608
try_visit!(visitor.visit_ident(foreign_item.ident));
609609

610610
match foreign_item.kind {
611-
ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
611+
ForeignItemKind::Fn(ref function_declaration, param_names, ref generics, _) => {
612612
try_visit!(visitor.visit_generics(generics));
613613
try_visit!(visitor.visit_fn_decl(function_declaration));
614614
walk_list!(visitor, visit_ident, param_names.iter().copied());
615615
}
616-
ForeignItemKind::Static(ref typ, _) => try_visit!(visitor.visit_ty(typ)),
616+
ForeignItemKind::Static(ref typ, _, _) => {
617+
try_visit!(visitor.visit_ty(typ));
618+
}
617619
ForeignItemKind::Type => (),
618620
}
619621
V::Result::output()

0 commit comments

Comments
 (0)