Skip to content

Commit bac72cf

Browse files
committed
Add safe/unsafe to static inside extern blocks
1 parent b4cbdb7 commit bac72cf

File tree

27 files changed

+152
-57
lines changed

27 files changed

+152
-57
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,7 @@ pub struct DelegationMac {
31643164
#[derive(Clone, Encodable, Decodable, Debug)]
31653165
pub struct StaticItem {
31663166
pub ty: P<Ty>,
3167+
pub safety: Safety,
31673168
pub mutability: Mutability,
31683169
pub expr: Option<P<Expr>>,
31693170
}
@@ -3182,7 +3183,7 @@ impl From<StaticItem> for StaticForeignItem {
31823183
fn from(static_item: StaticItem) -> StaticForeignItem {
31833184
StaticForeignItem {
31843185
ty: static_item.ty,
3185-
safety: Safety::Default,
3186+
safety: static_item.safety,
31863187
mutability: static_item.mutability,
31873188
expr: static_item.expr,
31883189
}
@@ -3193,6 +3194,7 @@ impl From<StaticForeignItem> for StaticItem {
31933194
fn from(static_item: StaticForeignItem) -> StaticItem {
31943195
StaticItem {
31953196
ty: static_item.ty,
3197+
safety: static_item.safety,
31963198
mutability: static_item.mutability,
31973199
expr: static_item.expr,
31983200
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ impl NoopVisitItemKind for ItemKind {
10801080
match self {
10811081
ItemKind::ExternCrate(_orig_name) => {}
10821082
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
1083-
ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
1083+
ItemKind::Static(box StaticItem { ty, safety: _, mutability: _, expr }) => {
10841084
vis.visit_ty(ty);
10851085
visit_opt(expr, |expr| vis.visit_expr(expr));
10861086
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl WalkItemKind for ItemKind {
334334
match self {
335335
ItemKind::ExternCrate(_) => {}
336336
ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, item.id, false)),
337-
ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
337+
ItemKind::Static(box StaticItem { ty, safety: _, mutability: _, expr }) => {
338338
try_visit!(visitor.visit_ty(ty));
339339
visit_opt!(visitor, visit_expr, expr);
340340
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
181181

182182
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
183183
}
184-
ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => {
184+
ItemKind::Static(box ast::StaticItem { ty: t, safety: _, mutability: m, expr: e }) => {
185185
let (ty, body_id) =
186186
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
187187
hir::ItemKind::Static(ty, *m, body_id)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ impl<'a> State<'a> {
171171
self.print_use_tree(tree);
172172
self.word(";");
173173
}
174-
ast::ItemKind::Static(box StaticItem { ty, mutability: mutbl, expr: body }) => {
174+
ast::ItemKind::Static(box StaticItem { ty, safety, mutability: mutbl, expr: body }) => {
175+
self.print_safety(*safety);
175176
self.print_item_const(
176177
item.ident,
177178
Some(*mutbl),

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn intern_as_new_static<'tcx>(
102102
let feed = tcx.create_def(
103103
static_id,
104104
sym::nested,
105-
DefKind::Static { mutability: alloc.0.mutability, nested: true },
105+
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
106106
);
107107
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
108108

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,9 @@ fn mutability<'tcx>(ecx: &InterpCx<'tcx, impl Machine<'tcx>>, alloc_id: AllocId)
711711
// We're not using `try_global_alloc` since dangling pointers have already been handled.
712712
match ecx.tcx.global_alloc(alloc_id) {
713713
GlobalAlloc::Static(did) => {
714-
let DefKind::Static { mutability, nested } = ecx.tcx.def_kind(did) else { bug!() };
714+
let DefKind::Static { safety: _, mutability, nested } = ecx.tcx.def_kind(did) else {
715+
bug!()
716+
};
715717
if nested {
716718
assert!(
717719
ecx.memory.alloc_map.get(alloc_id).is_none(),

compiler/rustc_expand/src/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,10 @@ impl<'a> ExtCtxt<'a> {
631631
span,
632632
name,
633633
AttrVec::new(),
634-
ast::ItemKind::Static(ast::StaticItem { ty, mutability, expr: Some(expr) }.into()),
634+
ast::ItemKind::Static(
635+
ast::StaticItem { ty, safety: ast::Safety::Default, mutability, expr: Some(expr) }
636+
.into(),
637+
),
635638
)
636639
}
637640

compiler/rustc_hir/src/def.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub enum DefKind {
7676
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
7777
ConstParam,
7878
Static {
79+
/// Whether it's a `unsafe static`, `safe static` (inside extern only) or just a `static`.
80+
safety: hir::Safety,
7981
/// Whether it's a `static mut` or just a `static`.
8082
mutability: ast::Mutability,
8183
/// Whether it's an anonymous static generated for nested allocations.

compiler/rustc_hir_analysis/src/check/errs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fn path_if_static_mut(tcx: TyCtxt<'_>, expr: &hir::Expr<'_>) -> Option<String> {
4141
if let hir::ExprKind::Path(qpath) = expr.kind
4242
&& let hir::QPath::Resolved(_, path) = qpath
4343
&& let hir::def::Res::Def(def_kind, _) = path.res
44-
&& let hir::def::DefKind::Static { mutability: Mutability::Mut, nested: false } = def_kind
44+
&& let hir::def::DefKind::Static { safety: _, mutability: Mutability::Mut, nested: false } =
45+
def_kind
4546
{
4647
return Some(qpath_to_string(&tcx, &qpath));
4748
}

0 commit comments

Comments
 (0)