Skip to content

Commit da6ea31

Browse files
committed
Change DefKind::Static to a struct variant
1 parent 67a2192 commit da6ea31

File tree

47 files changed

+90
-86
lines changed

Some content is hidden

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

47 files changed

+90
-86
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
195195
.get_partial_res(sym.id)
196196
.and_then(|res| res.full_res())
197197
.and_then(|res| match res {
198-
Res::Def(DefKind::Static(_), def_id) => Some(def_id),
198+
Res::Def(DefKind::Static { .. }, def_id) => Some(def_id),
199199
_ => None,
200200
});
201201

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
8383

8484
// Only consider nodes that actually have exported symbols.
8585
match tcx.def_kind(def_id) {
86-
DefKind::Fn | DefKind::Static(_) => {}
86+
DefKind::Fn | DefKind::Static { .. } => {}
8787
DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
8888
_ => return None,
8989
};
@@ -479,7 +479,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
479479
let target = &tcx.sess.target.llvm_target;
480480
// WebAssembly cannot export data symbols, so reduce their export level
481481
if target.contains("emscripten") {
482-
if let DefKind::Static(_) = tcx.def_kind(sym_def_id) {
482+
if let DefKind::Static { .. } = tcx.def_kind(sym_def_id) {
483483
return SymbolExportLevel::Rust;
484484
}
485485
}

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
3737
|| matches!(
3838
ecx.tcx.def_kind(cid.instance.def_id()),
3939
DefKind::Const
40-
| DefKind::Static(_)
40+
| DefKind::Static { .. }
4141
| DefKind::ConstParam
4242
| DefKind::AnonConst
4343
| DefKind::InlineConst

compiler/rustc_const_eval/src/interpret/validity.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,16 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
465465
// Special handling for pointers to statics (irrespective of their type).
466466
assert!(!self.ecx.tcx.is_thread_local_static(did));
467467
assert!(self.ecx.tcx.is_static(did));
468-
let is_mut =
469-
matches!(self.ecx.tcx.def_kind(did), DefKind::Static(Mutability::Mut))
470-
|| !self
471-
.ecx
472-
.tcx
473-
.type_of(did)
474-
.no_bound_vars()
475-
.expect("statics should not have generic parameters")
476-
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
468+
let is_mut = matches!(
469+
self.ecx.tcx.def_kind(did),
470+
DefKind::Static { mt: Mutability::Mut }
471+
) || !self
472+
.ecx
473+
.tcx
474+
.type_of(did)
475+
.no_bound_vars()
476+
.expect("statics should not have generic parameters")
477+
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
477478
// Mode-specific checks
478479
match self.ctfe_mode {
479480
Some(

compiler/rustc_hir/src/def.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ pub enum DefKind {
7575
Const,
7676
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
7777
ConstParam,
78-
Static(ast::Mutability),
78+
Static {
79+
/// Whether it's a `static mut` or just a `static`.
80+
mt: ast::Mutability,
81+
},
7982
/// Refers to the struct or enum variant's constructor.
8083
///
8184
/// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
@@ -136,7 +139,7 @@ impl DefKind {
136139
DefKind::Fn => "function",
137140
DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate",
138141
DefKind::Mod => "module",
139-
DefKind::Static(..) => "static",
142+
DefKind::Static { .. } => "static",
140143
DefKind::Enum => "enum",
141144
DefKind::Variant => "variant",
142145
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
@@ -209,7 +212,7 @@ impl DefKind {
209212
DefKind::Fn
210213
| DefKind::Const
211214
| DefKind::ConstParam
212-
| DefKind::Static(..)
215+
| DefKind::Static { .. }
213216
| DefKind::Ctor(..)
214217
| DefKind::AssocFn
215218
| DefKind::AssocConst => Some(Namespace::ValueNS),
@@ -248,7 +251,7 @@ impl DefKind {
248251
DefKind::Fn
249252
| DefKind::Const
250253
| DefKind::ConstParam
251-
| DefKind::Static(..)
254+
| DefKind::Static { .. }
252255
| DefKind::AssocFn
253256
| DefKind::AssocConst
254257
| DefKind::Field => DefPathData::ValueNs(name),
@@ -278,7 +281,7 @@ impl DefKind {
278281
| DefKind::AssocFn
279282
| DefKind::Ctor(..)
280283
| DefKind::Closure
281-
| DefKind::Static(_) => true,
284+
| DefKind::Static { .. } => true,
282285
DefKind::Mod
283286
| DefKind::Struct
284287
| DefKind::Union

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ impl Expr<'_> {
16161616
pub fn is_place_expr(&self, mut allow_projections_from: impl FnMut(&Self) -> bool) -> bool {
16171617
match self.kind {
16181618
ExprKind::Path(QPath::Resolved(_, ref path)) => {
1619-
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static(_), _) | Res::Err)
1619+
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static { .. }, _) | Res::Err)
16201620
}
16211621

16221622
// Type ascription inherits its place expression kind from its

compiler/rustc_hir/src/target.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Target {
107107
match item.kind {
108108
ItemKind::ExternCrate(..) => Target::ExternCrate,
109109
ItemKind::Use(..) => Target::Use,
110-
ItemKind::Static(..) => Target::Static,
110+
ItemKind::Static { .. } => Target::Static,
111111
ItemKind::Const(..) => Target::Const,
112112
ItemKind::Fn(..) => Target::Fn,
113113
ItemKind::Macro(..) => Target::MacroDef,
@@ -130,7 +130,7 @@ impl Target {
130130
match def_kind {
131131
DefKind::ExternCrate => Target::ExternCrate,
132132
DefKind::Use => Target::Use,
133-
DefKind::Static(..) => Target::Static,
133+
DefKind::Static { .. } => Target::Static,
134134
DefKind::Const => Target::Const,
135135
DefKind::Fn => Target::Fn,
136136
DefKind::Macro(..) => Target::MacroDef,

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19371937
}
19381938

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

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
226226
Ok(l) => l,
227227
// Foreign statics that overflow their allowed size should emit an error
228228
Err(LayoutError::SizeOverflow(_))
229-
if matches!(tcx.def_kind(def_id), DefKind::Static(_)
229+
if matches!(tcx.def_kind(def_id), DefKind::Static{..}
230230
if tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod) =>
231231
{
232232
tcx.dcx().emit_err(errors::TooLargeStatic { span });
@@ -514,7 +514,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
514514
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
515515
let _indenter = indenter();
516516
match tcx.def_kind(def_id) {
517-
DefKind::Static(..) => {
517+
DefKind::Static { .. } => {
518518
tcx.ensure().typeck(def_id);
519519
maybe_check_static_with_link_section(tcx, def_id);
520520
check_static_inhabited(tcx, def_id);

compiler/rustc_hir_analysis/src/check/errs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> {
4949
if let hir::ExprKind::Path(qpath) = expr.kind
5050
&& let hir::QPath::Resolved(_, path) = qpath
5151
&& let hir::def::Res::Def(def_kind, _) = path.res
52-
&& let hir::def::DefKind::Static(mt) = def_kind
52+
&& let hir::def::DefKind::Static { mt } = def_kind
5353
&& matches!(mt, Mutability::Mut)
5454
{
5555
return Some(qpath_to_string(&qpath));

compiler/rustc_hir_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
203203
tcx.hir().par_body_owners(|item_def_id| {
204204
let def_kind = tcx.def_kind(item_def_id);
205205
match def_kind {
206-
DefKind::Static(_) => tcx.ensure().eval_static_initializer(item_def_id),
206+
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
207207
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
208208
_ => (),
209209
}

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
699699
hir::Path {
700700
res:
701701
hir::def::Res::Def(
702-
hir::def::DefKind::Static(_) | hir::def::DefKind::Const,
702+
hir::def::DefKind::Static { .. } | hir::def::DefKind::Const,
703703
def_id,
704704
),
705705
..

compiler/rustc_hir_typeck/src/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
398398
)
399399
| Res::SelfCtor(..) => Ok(self.cat_rvalue(hir_id, expr_ty)),
400400

401-
Res::Def(DefKind::Static(_), _) => {
401+
Res::Def(DefKind::Static { .. }, _) => {
402402
Ok(PlaceWithHirId::new(hir_id, expr_ty, PlaceBase::StaticItem, Vec::new()))
403403
}
404404

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<T> Trait<T> for X {
372372
&& matches!(
373373
tcx.def_kind(body_owner_def_id),
374374
DefKind::Fn
375-
| DefKind::Static(_)
375+
| DefKind::Static { .. }
376376
| DefKind::Const
377377
| DefKind::AssocFn
378378
| DefKind::AssocConst

compiler/rustc_metadata/src/rmeta/encoder.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
851851
| DefKind::LifetimeParam
852852
| DefKind::Fn
853853
| DefKind::Const
854-
| DefKind::Static(_)
854+
| DefKind::Static { .. }
855855
| DefKind::Ctor(..)
856856
| DefKind::AssocFn
857857
| DefKind::AssocConst
@@ -882,7 +882,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
882882
| DefKind::AssocTy
883883
| DefKind::Fn
884884
| DefKind::Const
885-
| DefKind::Static(_)
885+
| DefKind::Static { .. }
886886
| DefKind::AssocFn
887887
| DefKind::AssocConst
888888
| DefKind::Macro(_)
@@ -924,7 +924,7 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
924924
| DefKind::Fn
925925
| DefKind::Const
926926
| DefKind::ConstParam
927-
| DefKind::Static(_)
927+
| DefKind::Static { .. }
928928
| DefKind::Ctor(..)
929929
| DefKind::AssocFn
930930
| DefKind::AssocConst
@@ -956,7 +956,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
956956
| DefKind::AssocTy
957957
| DefKind::Fn
958958
| DefKind::Const
959-
| DefKind::Static(..)
959+
| DefKind::Static { .. }
960960
| DefKind::Ctor(..)
961961
| DefKind::AssocFn
962962
| DefKind::AssocConst
@@ -989,7 +989,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
989989
| DefKind::AssocConst
990990
| DefKind::TyParam
991991
| DefKind::ConstParam
992-
| DefKind::Static(..)
992+
| DefKind::Static { .. }
993993
| DefKind::Const
994994
| DefKind::Fn
995995
| DefKind::ForeignMod
@@ -1084,7 +1084,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
10841084
| DefKind::AssocConst
10851085
| DefKind::TyParam
10861086
| DefKind::ConstParam
1087-
| DefKind::Static(..)
1087+
| DefKind::Static { .. }
10881088
| DefKind::Const
10891089
| DefKind::ForeignMod
10901090
| DefKind::Impl { .. }
@@ -1116,7 +1116,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
11161116
| DefKind::AssocTy
11171117
| DefKind::Fn
11181118
| DefKind::Const
1119-
| DefKind::Static(..)
1119+
| DefKind::Static { .. }
11201120
| DefKind::Ctor(..)
11211121
| DefKind::AssocFn
11221122
| DefKind::AssocConst
@@ -1148,7 +1148,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11481148
| DefKind::Field
11491149
| DefKind::Fn
11501150
| DefKind::Const
1151-
| DefKind::Static(..)
1151+
| DefKind::Static { .. }
11521152
| DefKind::TyAlias
11531153
| DefKind::ForeignTy
11541154
| DefKind::Impl { .. }
@@ -1207,7 +1207,7 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
12071207
| DefKind::Variant
12081208
| DefKind::Field
12091209
| DefKind::Const
1210-
| DefKind::Static(..)
1210+
| DefKind::Static { .. }
12111211
| DefKind::Ctor(..)
12121212
| DefKind::TyAlias
12131213
| DefKind::OpaqueTy
@@ -1248,7 +1248,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12481248
| DefKind::Const
12491249
| DefKind::AssocConst
12501250
| DefKind::AnonConst
1251-
| DefKind::Static(..)
1251+
| DefKind::Static { .. }
12521252
| DefKind::TyAlias
12531253
| DefKind::OpaqueTy
12541254
| DefKind::Impl { of_trait: false }
@@ -1280,7 +1280,7 @@ fn should_encode_const(def_kind: DefKind) -> bool {
12801280
| DefKind::Ctor(..)
12811281
| DefKind::Field
12821282
| DefKind::Fn
1283-
| DefKind::Static(..)
1283+
| DefKind::Static { .. }
12841284
| DefKind::TyAlias
12851285
| DefKind::OpaqueTy
12861286
| DefKind::ForeignTy
@@ -1454,7 +1454,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14541454
.coroutine_for_closure
14551455
.set_some(def_id.index, self.tcx.coroutine_for_closure(def_id).into());
14561456
}
1457-
if let DefKind::Static(_) = def_kind {
1457+
if let DefKind::Static { .. } = def_kind {
14581458
if !self.tcx.is_foreign_item(def_id) {
14591459
let data = self.tcx.eval_static_initializer(def_id).unwrap();
14601460
record!(self.tables.eval_static_initializer[def_id] <- data);

compiler/rustc_metadata/src/rmeta/table.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ fixed_size_enum! {
155155
( Impl { of_trait: false } )
156156
( Impl { of_trait: true } )
157157
( Closure )
158-
( Static(ast::Mutability::Not) )
159-
( Static(ast::Mutability::Mut) )
158+
( Static{mt:ast::Mutability::Not} )
159+
( Static{mt:ast::Mutability::Mut} )
160160
( Ctor(CtorOf::Struct, CtorKind::Fn) )
161161
( Ctor(CtorOf::Struct, CtorKind::Const) )
162162
( Ctor(CtorOf::Variant, CtorKind::Fn) )

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'hir> Map<'hir> {
344344
DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
345345
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
346346
DefKind::Closure => BodyOwnerKind::Closure,
347-
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
347+
DefKind::Static { mt } => BodyOwnerKind::Static(mt),
348348
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
349349
}
350350
}

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
498498
match (kind, body.source.promoted) {
499499
(_, Some(i)) => write!(w, "{i:?} in ")?,
500500
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
501-
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
502-
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
501+
(DefKind::Static { mt: hir::Mutability::Not }, _) => write!(w, "static ")?,
502+
(DefKind::Static { mt: hir::Mutability::Mut }, _) => write!(w, "static mut ")?,
503503
(_, _) if is_function => write!(w, "fn ")?,
504504
(DefKind::AnonConst | DefKind::InlineConst, _) => {} // things like anon const, not an item
505505
_ => bug!("Unexpected def kind {:?}", kind),

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ impl<'tcx> TyCtxt<'tcx> {
16981698
debug!("returned from def_kind: {:?}", def_kind);
16991699
match def_kind {
17001700
DefKind::Const
1701-
| DefKind::Static(..)
1701+
| DefKind::Static { .. }
17021702
| DefKind::AssocConst
17031703
| DefKind::Ctor(..)
17041704
| DefKind::AnonConst

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
350350
| DefKind::TyAlias
351351
| DefKind::Fn
352352
| DefKind::Const
353-
| DefKind::Static(_) = kind
353+
| DefKind::Static { .. } = kind
354354
{
355355
} else {
356356
// If not covered above, like for example items out of `impl` blocks, fallback.

compiler/rustc_middle/src/ty/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,12 @@ impl<'tcx> TyCtxt<'tcx> {
616616
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
617617
#[inline]
618618
pub fn is_static(self, def_id: DefId) -> bool {
619-
matches!(self.def_kind(def_id), DefKind::Static(_))
619+
matches!(self.def_kind(def_id), DefKind::Static { .. })
620620
}
621621

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

627627
/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
631631
| DefKind::AssocConst
632632
| DefKind::AnonConst
633633
| DefKind::InlineConst
634-
| DefKind::Static(_) => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
634+
| DefKind::Static { .. } => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
635635
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
636636
let sig = tcx.liberate_late_bound_regions(
637637
def_id.to_def_id(),

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ impl<'tcx> Cx<'tcx> {
939939

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

0 commit comments

Comments
 (0)