Skip to content

Commit

Permalink
Change DefKind::Static to a struct variant
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 12, 2024
1 parent 12e2846 commit 9816915
Show file tree
Hide file tree
Showing 47 changed files with 90 additions and 86 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.get_partial_res(sym.id)
.and_then(|res| res.full_res())
.and_then(|res| match res {
Res::Def(DefKind::Static(_), def_id) => Some(def_id),
Res::Def(DefKind::Static { .. }, def_id) => Some(def_id),
_ => None,
});

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S

// Only consider nodes that actually have exported symbols.
match tcx.def_kind(def_id) {
DefKind::Fn | DefKind::Static(_) => {}
DefKind::Fn | DefKind::Static { .. } => {}
DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
_ => return None,
};
Expand Down Expand Up @@ -483,7 +483,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
let target = &tcx.sess.target.llvm_target;
// WebAssembly cannot export data symbols, so reduce their export level
if target.contains("emscripten") {
if let DefKind::Static(_) = tcx.def_kind(sym_def_id) {
if let DefKind::Static { .. } = tcx.def_kind(sym_def_id) {
return SymbolExportLevel::Rust;
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
|| matches!(
ecx.tcx.def_kind(cid.instance.def_id()),
DefKind::Const
| DefKind::Static(_)
| DefKind::Static { .. }
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,16 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
// Special handling for pointers to statics (irrespective of their type).
assert!(!self.ecx.tcx.is_thread_local_static(did));
assert!(self.ecx.tcx.is_static(did));
let is_mut =
matches!(self.ecx.tcx.def_kind(did), DefKind::Static(Mutability::Mut))
|| !self
.ecx
.tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
let is_mut = matches!(
self.ecx.tcx.def_kind(did),
DefKind::Static { mt: Mutability::Mut }
) || !self
.ecx
.tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
// Mode-specific checks
match self.ctfe_mode {
Some(
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ pub enum DefKind {
Const,
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
Static(ast::Mutability),
Static {
/// Whether it's a `static mut` or just a `static`.
mt: ast::Mutability,
},
/// Refers to the struct or enum variant's constructor.
///
/// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
Expand Down Expand Up @@ -136,7 +139,7 @@ impl DefKind {
DefKind::Fn => "function",
DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate",
DefKind::Mod => "module",
DefKind::Static(..) => "static",
DefKind::Static { .. } => "static",
DefKind::Enum => "enum",
DefKind::Variant => "variant",
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
Expand Down Expand Up @@ -209,7 +212,7 @@ impl DefKind {
DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst => Some(Namespace::ValueNS),
Expand Down Expand Up @@ -248,7 +251,7 @@ impl DefKind {
DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::Field => DefPathData::ValueNs(name),
Expand Down Expand Up @@ -278,7 +281,7 @@ impl DefKind {
| DefKind::AssocFn
| DefKind::Ctor(..)
| DefKind::Closure
| DefKind::Static(_) => true,
| DefKind::Static { .. } => true,
DefKind::Mod
| DefKind::Struct
| DefKind::Union
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 @@ -1616,7 +1616,7 @@ impl Expr<'_> {
pub fn is_place_expr(&self, mut allow_projections_from: impl FnMut(&Self) -> bool) -> bool {
match self.kind {
ExprKind::Path(QPath::Resolved(_, ref path)) => {
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static(_), _) | Res::Err)
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static { .. }, _) | Res::Err)
}

// Type ascription inherits its place expression kind from its
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Target {
match item.kind {
ItemKind::ExternCrate(..) => Target::ExternCrate,
ItemKind::Use(..) => Target::Use,
ItemKind::Static(..) => Target::Static,
ItemKind::Static { .. } => Target::Static,
ItemKind::Const(..) => Target::Const,
ItemKind::Fn(..) => Target::Fn,
ItemKind::Macro(..) => Target::MacroDef,
Expand All @@ -130,7 +130,7 @@ impl Target {
match def_kind {
DefKind::ExternCrate => Target::ExternCrate,
DefKind::Use => Target::Use,
DefKind::Static(..) => Target::Static,
DefKind::Static { .. } => Target::Static,
DefKind::Const => Target::Const,
DefKind::Fn => Target::Fn,
DefKind::Macro(..) => Target::MacroDef,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}

// Case 3. Reference to a top-level value.
DefKind::Fn | DefKind::Const | DefKind::ConstParam | DefKind::Static(_) => {
DefKind::Fn | DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } => {
path_segs.push(PathSeg(def_id, last));
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
Ok(l) => l,
// Foreign statics that overflow their allowed size should emit an error
Err(LayoutError::SizeOverflow(_))
if matches!(tcx.def_kind(def_id), DefKind::Static(_)
if matches!(tcx.def_kind(def_id), DefKind::Static{..}
if tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod) =>
{
tcx.dcx().emit_err(errors::TooLargeStatic { span });
Expand Down Expand Up @@ -505,7 +505,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let _indenter = indenter();
match tcx.def_kind(def_id) {
DefKind::Static(..) => {
DefKind::Static { .. } => {
tcx.ensure().typeck(def_id);
maybe_check_static_with_link_section(tcx, def_id);
check_static_inhabited(tcx, def_id);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn is_path_static_mut(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(mt) = def_kind
&& let hir::def::DefKind::Static { mt } = def_kind
&& matches!(mt, Mutability::Mut)
{
return Some(qpath_to_string(&qpath));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
tcx.hir().par_body_owners(|item_def_id| {
let def_kind = tcx.def_kind(item_def_id);
match def_kind {
DefKind::Static(_) => tcx.ensure().eval_static_initializer(item_def_id),
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
_ => (),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Path {
res:
hir::def::Res::Def(
hir::def::DefKind::Static(_) | hir::def::DefKind::Const,
hir::def::DefKind::Static { .. } | hir::def::DefKind::Const,
def_id,
),
..
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
)
| Res::SelfCtor(..) => Ok(self.cat_rvalue(hir_id, expr_ty)),

Res::Def(DefKind::Static(_), _) => {
Res::Def(DefKind::Static { .. }, _) => {
Ok(PlaceWithHirId::new(hir_id, expr_ty, PlaceBase::StaticItem, Vec::new()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl<T> Trait<T> for X {
&& matches!(
tcx.def_kind(body_owner_def_id),
DefKind::Fn
| DefKind::Static(_)
| DefKind::Static { .. }
| DefKind::Const
| DefKind::AssocFn
| DefKind::AssocConst
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
| DefKind::LifetimeParam
| DefKind::Fn
| DefKind::Const
| DefKind::Static(_)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
Expand Down Expand Up @@ -894,7 +894,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
| DefKind::AssocTy
| DefKind::Fn
| DefKind::Const
| DefKind::Static(_)
| DefKind::Static { .. }
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::Macro(_)
Expand Down Expand Up @@ -936,7 +936,7 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
| DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static(_)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
Expand Down Expand Up @@ -968,7 +968,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
| DefKind::AssocTy
| DefKind::Fn
| DefKind::Const
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
Expand Down Expand Up @@ -1001,7 +1001,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
| DefKind::AssocConst
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Const
| DefKind::Fn
| DefKind::ForeignMod
Expand Down Expand Up @@ -1099,7 +1099,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
| DefKind::AssocConst
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Const
| DefKind::ForeignMod
| DefKind::Impl { .. }
Expand Down Expand Up @@ -1131,7 +1131,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
| DefKind::AssocTy
| DefKind::Fn
| DefKind::Const
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
Expand Down Expand Up @@ -1163,7 +1163,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
| DefKind::Field
| DefKind::Fn
| DefKind::Const
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::TyAlias
| DefKind::ForeignTy
| DefKind::Impl { .. }
Expand Down Expand Up @@ -1222,7 +1222,7 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
| DefKind::Variant
| DefKind::Field
| DefKind::Const
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::TyAlias
| DefKind::OpaqueTy
Expand Down Expand Up @@ -1263,7 +1263,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
| DefKind::Const
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::Impl { of_trait: false }
Expand Down Expand Up @@ -1295,7 +1295,7 @@ fn should_encode_const(def_kind: DefKind) -> bool {
| DefKind::Ctor(..)
| DefKind::Field
| DefKind::Fn
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::ForeignTy
Expand Down Expand Up @@ -1469,7 +1469,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
.coroutine_for_closure
.set_some(def_id.index, self.tcx.coroutine_for_closure(def_id).into());
}
if let DefKind::Static(_) = def_kind {
if let DefKind::Static { .. } = def_kind {
if !self.tcx.is_foreign_item(def_id) {
let data = self.tcx.eval_static_initializer(def_id).unwrap();
record!(self.tables.eval_static_initializer[def_id] <- data);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ fixed_size_enum! {
( Impl { of_trait: false } )
( Impl { of_trait: true } )
( Closure )
( Static(ast::Mutability::Not) )
( Static(ast::Mutability::Mut) )
( Static{mt:ast::Mutability::Not} )
( Static{mt:ast::Mutability::Mut} )
( Ctor(CtorOf::Struct, CtorKind::Fn) )
( Ctor(CtorOf::Struct, CtorKind::Const) )
( Ctor(CtorOf::Variant, CtorKind::Fn) )
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl<'hir> Map<'hir> {
DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
DefKind::Closure => BodyOwnerKind::Closure,
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
DefKind::Static { mt } => BodyOwnerKind::Static(mt),
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ 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(hir::Mutability::Not), _) => write!(w, "static ")?,
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
(DefKind::Static { mt: hir::Mutability::Not }, _) => write!(w, "static ")?,
(DefKind::Static { mt: hir::Mutability::Mut }, _) => write!(w, "static mut ")?,
(_, _) if is_function => write!(w, "fn ")?,
(DefKind::AnonConst | DefKind::InlineConst, _) => {} // things like anon const, not an item
_ => bug!("Unexpected def kind {:?}", kind),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ impl<'tcx> TyCtxt<'tcx> {
debug!("returned from def_kind: {:?}", def_kind);
match def_kind {
DefKind::Const
| DefKind::Static(..)
| DefKind::Static { .. }
| DefKind::AssocConst
| DefKind::Ctor(..)
| DefKind::AnonConst
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
| DefKind::TyAlias
| DefKind::Fn
| DefKind::Const
| DefKind::Static(_) = kind
| DefKind::Static { .. } = kind
{
} else {
// If not covered above, like for example items out of `impl` blocks, fallback.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,12 @@ impl<'tcx> TyCtxt<'tcx> {
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
#[inline]
pub fn is_static(self, def_id: DefId) -> bool {
matches!(self.def_kind(def_id), DefKind::Static(_))
matches!(self.def_kind(def_id), DefKind::Static { .. })
}

#[inline]
pub fn static_mutability(self, def_id: DefId) -> Option<hir::Mutability> {
if let DefKind::Static(mt) = self.def_kind(def_id) { Some(mt) } else { None }
if let DefKind::Static { mt } = self.def_kind(def_id) { Some(mt) } else { None }
}

/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Static(_) => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
| DefKind::Static { .. } => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
let sig = tcx.liberate_late_bound_regions(
def_id.to_def_id(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ impl<'tcx> Cx<'tcx> {

// We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
// a constant reference (or constant raw pointer for `static mut`) in MIR
Res::Def(DefKind::Static(_), id) => {
Res::Def(DefKind::Static { .. }, id) => {
let ty = self.tcx.static_ptr_ty(id);
let temp_lifetime = self
.rvalue_scopes
Expand Down
Loading

0 comments on commit 9816915

Please sign in to comment.