Skip to content

Commit 2a37712

Browse files
committed
Handle safety keyword for extern block inner items
1 parent bbddc9b commit 2a37712

Some content is hidden

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

52 files changed

+168
-84
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,8 @@ pub enum IsAuto {
25012501
pub enum Safety {
25022502
/// `unsafe` an item is explicitly marked as `unsafe`.
25032503
Unsafe(Span),
2504+
/// `safe` an item is explicitly marked as `safe`.
2505+
Safe(Span),
25042506
/// Default means no value was provided, it will take a default value given the context in
25052507
/// which is used.
25062508
Default,
@@ -3171,6 +3173,7 @@ pub struct StaticItem {
31713173
#[derive(Clone, Encodable, Decodable, Debug)]
31723174
pub struct StaticForeignItem {
31733175
pub ty: P<Ty>,
3176+
pub safety: Safety,
31743177
pub mutability: Mutability,
31753178
pub expr: Option<P<Expr>>,
31763179
}
@@ -3179,6 +3182,7 @@ impl From<StaticItem> for StaticForeignItem {
31793182
fn from(static_item: StaticItem) -> StaticForeignItem {
31803183
StaticForeignItem {
31813184
ty: static_item.ty,
3185+
safety: Safety::Default,
31823186
mutability: static_item.mutability,
31833187
expr: static_item.expr,
31843188
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T)
862862
fn visit_safety<T: MutVisitor>(safety: &mut Safety, vis: &mut T) {
863863
match safety {
864864
Safety::Unsafe(span) => vis.visit_span(span),
865+
Safety::Safe(span) => vis.visit_span(span),
865866
Safety::Default => {}
866867
}
867868
}
@@ -1289,7 +1290,12 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12891290
impl NoopVisitItemKind for ForeignItemKind {
12901291
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
12911292
match self {
1292-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
1293+
ForeignItemKind::Static(box StaticForeignItem {
1294+
ty,
1295+
mutability: _,
1296+
expr,
1297+
safety: _,
1298+
}) => {
12931299
visitor.visit_ty(ty);
12941300
visit_opt(expr, |expr| visitor.visit_expr(expr));
12951301
}

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)
@@ -577,6 +578,7 @@ impl Token {
577578
kw::Impl,
578579
kw::Unsafe,
579580
kw::Const,
581+
kw::Safe,
580582
kw::Static,
581583
kw::Union,
582584
kw::Macro,

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,12 @@ impl WalkItemKind for ForeignItemKind {
658658
) -> V::Result {
659659
let &Item { id, span, ident, ref vis, .. } = item;
660660
match self {
661-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
661+
ForeignItemKind::Static(box StaticForeignItem {
662+
ty,
663+
mutability: _,
664+
expr,
665+
safety: _,
666+
}) => {
662667
try_visit!(visitor.visit_ty(ty));
663668
visit_opt!(visitor, visit_expr, expr);
664669
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
safety: self.lower_safety(*safety),
391+
safety: self.lower_safety(*safety, hir::Safety::Safe),
392392
polarity,
393393
defaultness,
394394
defaultness_span,
@@ -418,7 +418,7 @@ 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 safety = this.lower_safety(*safety);
421+
let safety = this.lower_safety(*safety, hir::Safety::Safe);
422422
(safety, items, bounds)
423423
},
424424
);
@@ -660,13 +660,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
660660
this.lower_fn_params_to_names(fdec),
661661
)
662662
});
663+
let safety = self.lower_safety(sig.header.safety, hir::Safety::Unsafe);
663664

664-
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
665+
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics, safety)
665666
}
666-
ForeignItemKind::Static(box StaticForeignItem { ty, mutability, expr: _ }) => {
667+
ForeignItemKind::Static(box StaticForeignItem {
668+
ty,
669+
mutability,
670+
expr: _,
671+
safety,
672+
}) => {
667673
let ty = self
668674
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
669-
hir::ForeignItemKind::Static(ty, *mutability)
675+
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
676+
677+
hir::ForeignItemKind::Static(ty, *mutability, safety)
670678
}
671679
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
672680
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
@@ -1360,7 +1368,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13601368
hir::IsAsync::NotAsync
13611369
};
13621370
hir::FnHeader {
1363-
safety: self.lower_safety(h.safety),
1371+
safety: self.lower_safety(h.safety, hir::Safety::Safe),
13641372
asyncness: asyncness,
13651373
constness: self.lower_constness(h.constness),
13661374
abi: self.lower_extern(h.ext),
@@ -1410,10 +1418,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
14101418
}
14111419
}
14121420

1413-
pub(super) fn lower_safety(&mut self, s: Safety) -> hir::Safety {
1421+
pub(super) fn lower_safety(&mut self, s: Safety, default: hir::Safety) -> hir::Safety {
14141422
match s {
14151423
Safety::Unsafe(_) => hir::Safety::Unsafe,
1416-
Safety::Default => hir::Safety::Safe,
1424+
Safety::Default => default,
1425+
Safety::Safe(_) => hir::Safety::Safe,
14171426
}
14181427
}
14191428

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13211321
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
13221322
hir::TyKind::BareFn(self.arena.alloc(hir::BareFnTy {
13231323
generic_params,
1324-
safety: self.lower_safety(f.safety),
1324+
safety: self.lower_safety(f.safety, hir::Safety::Safe),
13251325
abi: self.lower_extern(f.ext),
13261326
decl: self.lower_fn_decl(&f.decl, t.id, t.span, FnDeclKind::Pointer, None),
13271327
param_names: self.lower_fn_params_to_names(&f.decl),

compiler/rustc_ast_passes/src/ast_validation.rs

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

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,7 @@ impl<'a> State<'a> {
19731973
fn print_safety(&mut self, s: ast::Safety) {
19741974
match s {
19751975
ast::Safety::Default => {}
1976+
ast::Safety::Safe(_) => self.word_nbsp("safe"),
19761977
ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
19771978
}
19781979
}

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

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

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,9 +3475,9 @@ impl ForeignItem<'_> {
34753475
#[derive(Debug, Clone, Copy, HashStable_Generic)]
34763476
pub enum ForeignItemKind<'hir> {
34773477
/// A foreign function.
3478-
Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>),
3478+
Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>, Safety),
34793479
/// A foreign static item (`static ext: u8`).
3480-
Static(&'hir Ty<'hir>, Mutability),
3480+
Static(&'hir Ty<'hir>, Mutability, Safety),
34813481
/// A foreign type.
34823482
Type,
34833483
}
@@ -3545,7 +3545,7 @@ impl<'hir> OwnerNode<'hir> {
35453545
| OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
35463546
| OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl),
35473547
OwnerNode::ForeignItem(ForeignItem {
3548-
kind: ForeignItemKind::Fn(fn_decl, _, _),
3548+
kind: ForeignItemKind::Fn(fn_decl, _, _, _),
35493549
..
35503550
}) => Some(fn_decl),
35513551
_ => None,
@@ -3728,9 +3728,9 @@ impl<'hir> Node<'hir> {
37283728
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
37293729
| Node::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl),
37303730
Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl, .. }), .. })
3731-
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
3732-
Some(fn_decl)
3733-
}
3731+
| Node::ForeignItem(ForeignItem {
3732+
kind: ForeignItemKind::Fn(fn_decl, _, _, _), ..
3733+
}) => Some(fn_decl),
37343734
_ => None,
37353735
}
37363736
}
@@ -3813,7 +3813,7 @@ impl<'hir> Node<'hir> {
38133813
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
38143814
match self {
38153815
Node::ForeignItem(ForeignItem {
3816-
kind: ForeignItemKind::Fn(_, _, generics), ..
3816+
kind: ForeignItemKind::Fn(_, _, generics, _), ..
38173817
})
38183818
| Node::TraitItem(TraitItem { generics, .. })
38193819
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),

0 commit comments

Comments
 (0)