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),

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()

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
801801

802802
let item = tcx.hir().foreign_item(item.id);
803803
match &item.kind {
804-
hir::ForeignItemKind::Fn(fn_decl, _, _) => {
804+
hir::ForeignItemKind::Fn(fn_decl, _, _, _) => {
805805
require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
806806
}
807807
hir::ForeignItemKind::Static(..) => {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn equate_intrinsic_type<'tcx>(
2929
let (own_counts, span) = match tcx.hir_node_by_def_id(def_id) {
3030
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. })
3131
| hir::Node::ForeignItem(hir::ForeignItem {
32-
kind: hir::ForeignItemKind::Fn(.., generics),
32+
kind: hir::ForeignItemKind::Fn(.., generics, _),
3333
..
3434
}) => {
3535
let own_counts = tcx.generics_of(def_id).own_counts();

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,11 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
13211321
icx.lowerer().lower_fn_ty(hir_id, header.safety, header.abi, decl, Some(generics), None)
13221322
}
13231323

1324-
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
1324+
ForeignItem(&hir::ForeignItem {
1325+
kind: ForeignItemKind::Fn(fn_decl, _, _, safety), ..
1326+
}) => {
13251327
let abi = tcx.hir().get_foreign_abi(hir_id);
1326-
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
1328+
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi, safety)
13271329
}
13281330

13291331
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
@@ -1695,11 +1697,12 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
16951697
def_id: LocalDefId,
16961698
decl: &'tcx hir::FnDecl<'tcx>,
16971699
abi: abi::Abi,
1700+
safety: hir::Safety,
16981701
) -> ty::PolyFnSig<'tcx> {
16991702
let safety = if abi == abi::Abi::RustIntrinsic {
17001703
intrinsic_operation_unsafety(tcx, def_id)
17011704
} else {
1702-
hir::Safety::Unsafe
1705+
safety
17031706
};
17041707
let hir_id = tcx.local_def_id_to_hir_id(def_id);
17051708
let fty =

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
603603

604604
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
605605
match item.kind {
606-
hir::ForeignItemKind::Fn(_, _, generics) => {
606+
hir::ForeignItemKind::Fn(_, _, generics, _) => {
607607
self.visit_early_late(item.hir_id(), generics, |this| {
608608
intravisit::walk_foreign_item(this, item);
609609
})

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
464464
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
465465
Ty::new_fn_def(tcx, def_id.to_def_id(), args)
466466
}
467-
ForeignItemKind::Static(t, _) => icx.lower_ty(t),
467+
ForeignItemKind::Static(t, _, _) => icx.lower_ty(t),
468468
ForeignItemKind::Type => Ty::new_foreign(tcx, def_id.to_def_id()),
469469
},
470470

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn diagnostic_hir_wf_check<'tcx>(
158158
},
159159
hir::Node::Field(field) => vec![field.ty],
160160
hir::Node::ForeignItem(ForeignItem {
161-
kind: ForeignItemKind::Static(ty, _), ..
161+
kind: ForeignItemKind::Static(ty, _, _), ..
162162
}) => vec![*ty],
163163
hir::Node::GenericParam(hir::GenericParam {
164164
kind: hir::GenericParamKind::Type { default: Some(ty), .. },

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ impl<'a> State<'a> {
345345
self.maybe_print_comment(item.span.lo());
346346
self.print_outer_attributes(self.attrs(item.hir_id()));
347347
match item.kind {
348-
hir::ForeignItemKind::Fn(decl, arg_names, generics) => {
348+
hir::ForeignItemKind::Fn(decl, arg_names, generics, safety) => {
349349
self.head("");
350350
self.print_fn(
351351
decl,
352352
hir::FnHeader {
353-
safety: hir::Safety::Safe,
353+
safety,
354354
constness: hir::Constness::NotConst,
355355
abi: Abi::Rust,
356356
asyncness: hir::IsAsync::NotAsync,
@@ -364,7 +364,8 @@ impl<'a> State<'a> {
364364
self.word(";");
365365
self.end() // end the outer fn box
366366
}
367-
hir::ForeignItemKind::Static(t, m) => {
367+
hir::ForeignItemKind::Static(t, m, safety) => {
368+
self.print_safety(safety);
368369
self.head("static");
369370
if m.is_mut() {
370371
self.word_space("mut");

compiler/rustc_lint/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,13 +1741,13 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
17411741
let abi = cx.tcx.hir().get_foreign_abi(it.hir_id());
17421742

17431743
match it.kind {
1744-
hir::ForeignItemKind::Fn(decl, _, _) if !vis.is_internal_abi(abi) => {
1744+
hir::ForeignItemKind::Fn(decl, _, _, _) if !vis.is_internal_abi(abi) => {
17451745
vis.check_foreign_fn(it.owner_id.def_id, decl);
17461746
}
1747-
hir::ForeignItemKind::Static(ty, _) if !vis.is_internal_abi(abi) => {
1747+
hir::ForeignItemKind::Static(ty, _, _) if !vis.is_internal_abi(abi) => {
17481748
vis.check_foreign_static(it.owner_id, ty.span);
17491749
}
1750-
hir::ForeignItemKind::Fn(decl, _, _) => vis.check_fn(it.owner_id.def_id, decl),
1750+
hir::ForeignItemKind::Fn(decl, _, _, _) => vis.check_fn(it.owner_id.def_id, decl),
17511751
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => (),
17521752
}
17531753
}

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ impl<'hir> Map<'hir> {
886886
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
887887
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
888888
Node::ForeignItem(item) => match item.kind {
889-
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
889+
ForeignItemKind::Fn(decl, _, _, _) => until_within(item.span, decl.output.span()),
890890
_ => named_span(item.span, item.ident, None),
891891
},
892892
Node::Ctor(_) => return self.span(self.tcx.parent_hir_id(hir_id)),

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn provide(providers: &mut Providers) {
201201
..
202202
})
203203
| Node::ForeignItem(&ForeignItem {
204-
kind: ForeignItemKind::Fn(_, idents, _),
204+
kind: ForeignItemKind::Fn(_, idents, _, _),
205205
..
206206
}) = tcx.hir_node(tcx.local_def_id_to_hir_id(def_id))
207207
{

0 commit comments

Comments
 (0)