Skip to content

Commit

Permalink
Add safe/unsafe to static inside extern blocks using DefKind
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed May 8, 2024
1 parent 884d2f8 commit c04eca6
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 26 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ fn intern_as_new_static<'tcx>(
let feed = tcx.create_def(
static_id,
sym::nested,
DefKind::Static { mutability: alloc.0.mutability, nested: true },
DefKind::Static {
safety: hir::Safety::Default,
mutability: alloc.0.mutability,
nested: true,
},
);
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,9 @@ fn mutability<'mir, 'tcx: 'mir>(
// We're not using `try_global_alloc` since dangling pointers have already been handled.
match ecx.tcx.global_alloc(alloc_id) {
GlobalAlloc::Static(did) => {
let DefKind::Static { mutability, nested } = ecx.tcx.def_kind(did) else { bug!() };
let DefKind::Static { safety: _, mutability, nested } = ecx.tcx.def_kind(did) else {
bug!()
};
if nested {
assert!(
ecx.memory.alloc_map.get(alloc_id).is_none(),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub enum DefKind {
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
Static {
/// Whether it's a `unsafe static`, `safe static` (inside extern only) or just a `static`.
safety: hir::Safety,
/// Whether it's a `static mut` or just a `static`.
mutability: ast::Mutability,
/// Whether it's an anonymous static generated for nested allocations.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3910,7 +3910,7 @@ mod size_asserts {
static_assert_size!(PathSegment<'_>, 48);
static_assert_size!(PatKind<'_>, 48);
static_assert_size!(QPath<'_>, 24);
static_assert_size!(Res, 12);
static_assert_size!(Res, 16);
static_assert_size!(Stmt<'_>, 32);
static_assert_size!(StmtKind<'_>, 16);
static_assert_size!(TraitItem<'_>, 88);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn path_if_static_mut(tcx: TyCtxt<'_>, expr: &hir::Expr<'_>) -> Option<String> {
if let hir::ExprKind::Path(qpath) = expr.kind
&& let hir::QPath::Resolved(_, path) = qpath
&& let hir::def::Res::Def(def_kind, _) = path.res
&& let hir::def::DefKind::Static { mutability: Mutability::Mut, nested: false } = def_kind
&& let hir::def::DefKind::Static { safety: _, mutability: Mutability::Mut, nested: false } =
def_kind
{
return Some(qpath_to_string(&tcx, &qpath));
}
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ fixed_size_enum! {
( Impl { of_trait: false } )
( Impl { of_trait: true } )
( Closure )
( Static { mutability: ast::Mutability::Not, nested: false } )
( Static { mutability: ast::Mutability::Mut, nested: false } )
( Static { mutability: ast::Mutability::Not, nested: true } )
( Static { mutability: ast::Mutability::Mut, nested: true } )
( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Not, nested: false } )
( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Not, nested: false } )
( Static { safety: hir::Safety::Default, mutability: ast::Mutability::Not, nested: false } )
( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Mut, nested: false } )
( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Mut, nested: false } )
( Static { safety: hir::Safety::Default, mutability: ast::Mutability::Mut, nested: false } )
( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Not, nested: true } )
( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Not, nested: true } )
( Static { safety: hir::Safety::Default, mutability: ast::Mutability::Not, nested: true } )
( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Mut, nested: true } )
( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Mut, nested: true } )
( Static { safety: hir::Safety::Default, mutability: ast::Mutability::Mut, nested: true } )
( Ctor(CtorOf::Struct, CtorKind::Fn) )
( Ctor(CtorOf::Struct, CtorKind::Const) )
( Ctor(CtorOf::Variant, CtorKind::Fn) )
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ impl<'hir> Map<'hir> {
DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
DefKind::Closure => BodyOwnerKind::Closure,
DefKind::Static { mutability, nested: false } => BodyOwnerKind::Static(mutability),
DefKind::Static { safety: _, mutability, nested: false } => {
BodyOwnerKind::Static(mutability)
}
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
}
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,11 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
match (kind, body.source.promoted) {
(_, Some(_)) => write!(w, "const ")?, // promoteds are the closest to consts
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
(DefKind::Static { mutability: hir::Mutability::Not, nested: false }, _) => {
// FIXME handle this properly
(DefKind::Static { safety: _, mutability: hir::Mutability::Not, nested: false }, _) => {
write!(w, "static ")?
}
(DefKind::Static { mutability: hir::Mutability::Mut, nested: false }, _) => {
(DefKind::Static { safety: _, mutability: hir::Mutability::Mut, nested: false }, _) => {
write!(w, "static mut ")?
}
(_, _) if is_function => write!(w, "fn ")?,
Expand Down
16 changes: 4 additions & 12 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::build::ExprCategory;
use crate::errors::*;

use rustc_errors::DiagArgValue;
use rustc_hir::def::DefKind;
use rustc_hir::{self as hir, BindingMode, ByRef, HirId, Mutability};
use rustc_hir::{ForeignItemKind, Node};
use rustc_middle::mir::BorrowKind;
use rustc_middle::thir::visit::Visitor;
use rustc_middle::thir::*;
Expand Down Expand Up @@ -436,17 +436,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
if self.tcx.is_mutable_static(def_id) {
self.requires_unsafe(expr.span, UseOfMutableStatic);
} else if self.tcx.is_foreign_item(def_id) {
// FIXME use def_kind query and add safety attribute to the kind
if def_id.is_local() {
let hir_id = self.tcx.local_def_id_to_hir_id(def_id.expect_local());
if let Node::ForeignItem(hir::ForeignItem {
kind: ForeignItemKind::Static(_, _, hir::Safety::Safe),
..
}) = self.tcx.hir_node(hir_id)
{
} else {
self.requires_unsafe(expr.span, UseOfExternStatic);
}
if let DefKind::Static { safety: hir::Safety::Safe, .. } =
self.tcx.def_kind(def_id)
{
} else {
self.requires_unsafe(expr.span, UseOfExternStatic);
}
Expand Down
19 changes: 16 additions & 3 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{ImplTraitContext, Resolver};
use rustc_ast::visit::FnKind;
use rustc_ast::*;
use rustc_expand::expand::AstFragment;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::LocalDefId;
use rustc_span::hygiene::LocalExpnId;
Expand Down Expand Up @@ -127,7 +128,11 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
ItemKind::Union(..) => DefKind::Union,
ItemKind::ExternCrate(..) => DefKind::ExternCrate,
ItemKind::TyAlias(..) => DefKind::TyAlias,
ItemKind::Static(s) => DefKind::Static { mutability: s.mutability, nested: false },
ItemKind::Static(s) => DefKind::Static {
safety: hir::Safety::Default,
mutability: s.mutability,
nested: false,
},
ItemKind::Const(..) => DefKind::Const,
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
ItemKind::MacroDef(..) => {
Expand Down Expand Up @@ -213,8 +218,16 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
ty: _,
mutability,
expr: _,
safety: _,
}) => DefKind::Static { mutability, nested: false },
safety,
}) => {
let safety = match safety {
ast::Safety::Unsafe(_) => hir::Safety::Unsafe,
ast::Safety::Default => hir::Safety::Default,
ast::Safety::Safe(_) => hir::Safety::Safe,
};

DefKind::Static { safety, mutability, nested: false }
}
ForeignItemKind::Fn(_) => DefKind::Fn,
ForeignItemKind::TyAlias(_) => DefKind::ForeignTy,
ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(fi.id),
Expand Down

0 comments on commit c04eca6

Please sign in to comment.