Skip to content

Commit a40c595

Browse files
committed
Auto merge of #95436 - cjgillot:static-mut, r=oli-obk
Remember mutability in `DefKind::Static`. This allows to compute the `BodyOwnerKind` from `DefKind` only, and removes a direct dependency of some MIR queries onto HIR. As a side effect, it also simplifies metadata, since we don't need 4 flavours of `EntryKind::*Static` any more.
2 parents c5cf08d + 21a554c commit a40c595

File tree

40 files changed

+98
-128
lines changed

40 files changed

+98
-128
lines changed

Diff for: compiler/rustc_borrowck/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ fn do_mir_borrowck<'a, 'tcx>(
156156

157157
let tcx = infcx.tcx;
158158
let param_env = tcx.param_env(def.did);
159-
let id = tcx.hir().local_def_id_to_hir_id(def.did);
160159

161160
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
162161
for var_debug_info in &input_body.var_debug_info {
@@ -226,7 +225,7 @@ fn do_mir_borrowck<'a, 'tcx>(
226225
.iterate_to_fixpoint()
227226
.into_results_cursor(&body);
228227

229-
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
228+
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def.did).is_fn_or_closure();
230229
let borrow_set =
231230
Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
232231

@@ -289,8 +288,9 @@ fn do_mir_borrowck<'a, 'tcx>(
289288
.pass_name("borrowck")
290289
.iterate_to_fixpoint();
291290

291+
let def_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
292292
let movable_generator = !matches!(
293-
tcx.hir().get(id),
293+
tcx.hir().get(def_hir_id),
294294
Node::Expr(&hir::Expr {
295295
kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
296296
..
@@ -385,7 +385,7 @@ fn do_mir_borrowck<'a, 'tcx>(
385385
let scope = mbcx.body.source_info(location).scope;
386386
let lint_root = match &mbcx.body.source_scopes[scope].local_data {
387387
ClearCrossCrate::Set(data) => data.lint_root,
388-
_ => id,
388+
_ => def_hir_id,
389389
};
390390

391391
// Span and message don't matter; we overwrite them below anyway

Diff for: compiler/rustc_borrowck/src/universal_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
524524
let tcx = self.infcx.tcx;
525525
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.did.to_def_id());
526526

527-
match tcx.hir().body_owner_kind(self.mir_hir_id) {
527+
match tcx.hir().body_owner_kind(self.mir_def.did) {
528528
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
529529
let defining_ty = if self.mir_def.did.to_def_id() == typeck_root_def_id {
530530
tcx.type_of(typeck_root_def_id)

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

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

Diff for: compiler/rustc_hir/src/def.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub enum DefKind {
7878
Const,
7979
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
8080
ConstParam,
81-
Static,
81+
Static(ast::Mutability),
8282
/// Refers to the struct or enum variant's constructor.
8383
///
8484
/// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
@@ -128,7 +128,7 @@ impl DefKind {
128128
"crate"
129129
}
130130
DefKind::Mod => "module",
131-
DefKind::Static => "static",
131+
DefKind::Static(..) => "static",
132132
DefKind::Enum => "enum",
133133
DefKind::Variant => "variant",
134134
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
@@ -202,7 +202,7 @@ impl DefKind {
202202
DefKind::Fn
203203
| DefKind::Const
204204
| DefKind::ConstParam
205-
| DefKind::Static
205+
| DefKind::Static(..)
206206
| DefKind::Ctor(..)
207207
| DefKind::AssocFn
208208
| DefKind::AssocConst => Some(Namespace::ValueNS),

Diff for: compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ impl Expr<'_> {
15301530
pub fn is_place_expr(&self, mut allow_projections_from: impl FnMut(&Self) -> bool) -> bool {
15311531
match self.kind {
15321532
ExprKind::Path(QPath::Resolved(_, ref path)) => {
1533-
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static, _) | Res::Err)
1533+
matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static(_), _) | Res::Err)
15341534
}
15351535

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

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1464,21 +1464,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14641464

14651465
fn is_foreign_item(self, id: DefIndex) -> bool {
14661466
match self.kind(id) {
1467-
EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => {
1468-
true
1469-
}
1467+
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
14701468
_ => false,
14711469
}
14721470
}
14731471

1474-
fn static_mutability(self, id: DefIndex) -> Option<hir::Mutability> {
1475-
match self.kind(id) {
1476-
EntryKind::ImmStatic | EntryKind::ForeignImmStatic => Some(hir::Mutability::Not),
1477-
EntryKind::MutStatic | EntryKind::ForeignMutStatic => Some(hir::Mutability::Mut),
1478-
_ => None,
1479-
}
1480-
}
1481-
14821472
#[inline]
14831473
fn def_key(self, index: DefIndex) -> DefKey {
14841474
*self

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
165165
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
166166
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
167167
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
168-
static_mutability => { cdata.static_mutability(def_id.index) }
169168
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
170169
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
171170
is_mir_available => { cdata.is_item_mir_available(def_id.index) }

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
797797
| DefKind::AssocTy
798798
| DefKind::Fn
799799
| DefKind::Const
800-
| DefKind::Static
800+
| DefKind::Static(..)
801801
| DefKind::Ctor(..)
802802
| DefKind::AssocFn
803803
| DefKind::AssocConst
@@ -831,7 +831,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
831831
| DefKind::AssocConst
832832
| DefKind::TyParam
833833
| DefKind::ConstParam
834-
| DefKind::Static
834+
| DefKind::Static(..)
835835
| DefKind::Const
836836
| DefKind::Fn
837837
| DefKind::ForeignMod
@@ -875,7 +875,7 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
875875
DefKind::AnonConst
876876
| DefKind::InlineConst
877877
| DefKind::AssocConst
878-
| DefKind::Static
878+
| DefKind::Static(..)
879879
| DefKind::Const => (true, false),
880880
// Full-fledged functions
881881
DefKind::AssocFn | DefKind::Fn => {
@@ -920,7 +920,7 @@ fn should_encode_variances(def_kind: DefKind) -> bool {
920920
| DefKind::AssocConst
921921
| DefKind::TyParam
922922
| DefKind::ConstParam
923-
| DefKind::Static
923+
| DefKind::Static(..)
924924
| DefKind::Const
925925
| DefKind::ForeignMod
926926
| DefKind::TyAlias
@@ -954,7 +954,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
954954
| DefKind::AssocTy
955955
| DefKind::Fn
956956
| DefKind::Const
957-
| DefKind::Static
957+
| DefKind::Static(..)
958958
| DefKind::Ctor(..)
959959
| DefKind::AssocFn
960960
| DefKind::AssocConst
@@ -1390,8 +1390,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13901390
self.encode_ident_span(def_id, item.ident);
13911391

13921392
let entry_kind = match item.kind {
1393-
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
1394-
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
1393+
hir::ItemKind::Static(..) => EntryKind::Static,
13951394
hir::ItemKind::Const(_, body_id) => {
13961395
let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
13971396
let const_data = self.encode_rendered_const_for_body(body_id);
@@ -1906,11 +1905,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19061905
};
19071906
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
19081907
}
1909-
hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => {
1910-
record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic);
1911-
}
1912-
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => {
1913-
record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic);
1908+
hir::ForeignItemKind::Static(..) => {
1909+
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
19141910
}
19151911
hir::ForeignItemKind::Type => {
19161912
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,8 @@ define_tables! {
338338
enum EntryKind {
339339
AnonConst,
340340
Const,
341-
ImmStatic,
342-
MutStatic,
343-
ForeignImmStatic,
344-
ForeignMutStatic,
341+
Static,
342+
ForeignStatic,
345343
ForeignMod,
346344
ForeignType,
347345
GlobalAsm,

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'hir> Map<'hir> {
228228
let hir_id = self.local_def_id_to_hir_id(local_def_id);
229229
let def_kind = match self.find(hir_id)? {
230230
Node::Item(item) => match item.kind {
231-
ItemKind::Static(..) => DefKind::Static,
231+
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
232232
ItemKind::Const(..) => DefKind::Const,
233233
ItemKind::Fn(..) => DefKind::Fn,
234234
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
@@ -248,7 +248,7 @@ impl<'hir> Map<'hir> {
248248
},
249249
Node::ForeignItem(item) => match item.kind {
250250
ForeignItemKind::Fn(..) => DefKind::Fn,
251-
ForeignItemKind::Static(..) => DefKind::Static,
251+
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
252252
ForeignItemKind::Type => DefKind::ForeignTy,
253253
},
254254
Node::TraitItem(item) => match item.kind {
@@ -471,19 +471,15 @@ impl<'hir> Map<'hir> {
471471
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
472472
///
473473
/// Panics if `LocalDefId` does not have an associated body.
474-
pub fn body_owner_kind(self, id: HirId) -> BodyOwnerKind {
475-
match self.get(id) {
476-
Node::Item(&Item { kind: ItemKind::Const(..), .. })
477-
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
478-
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
479-
| Node::AnonConst(_) => BodyOwnerKind::Const,
480-
Node::Ctor(..)
481-
| Node::Item(&Item { kind: ItemKind::Fn(..), .. })
482-
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. })
483-
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Fn(..), .. }) => BodyOwnerKind::Fn,
484-
Node::Item(&Item { kind: ItemKind::Static(_, m, _), .. }) => BodyOwnerKind::Static(m),
485-
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => BodyOwnerKind::Closure,
486-
node => bug!("{:#?} is not a body node", node),
474+
pub fn body_owner_kind(self, def_id: LocalDefId) -> BodyOwnerKind {
475+
match self.tcx.def_kind(def_id) {
476+
DefKind::Const | DefKind::AssocConst | DefKind::InlineConst | DefKind::AnonConst => {
477+
BodyOwnerKind::Const
478+
}
479+
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
480+
DefKind::Closure | DefKind::Generator => BodyOwnerKind::Closure,
481+
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
482+
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
487483
}
488484
}
489485

@@ -494,16 +490,17 @@ impl<'hir> Map<'hir> {
494490
/// This should only be used for determining the context of a body, a return
495491
/// value of `Some` does not always suggest that the owner of the body is `const`,
496492
/// just that it has to be checked as if it were.
497-
pub fn body_const_context(self, did: LocalDefId) -> Option<ConstContext> {
498-
let hir_id = self.local_def_id_to_hir_id(did);
499-
let ccx = match self.body_owner_kind(hir_id) {
493+
pub fn body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
494+
let ccx = match self.body_owner_kind(def_id) {
500495
BodyOwnerKind::Const => ConstContext::Const,
501496
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
502497

503-
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
504-
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
498+
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id.to_def_id()) => return None,
499+
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
500+
ConstContext::ConstFn
501+
}
505502
BodyOwnerKind::Fn
506-
if self.tcx.has_attr(did.to_def_id(), sym::default_method_body_is_const) =>
503+
if self.tcx.has_attr(def_id.to_def_id(), sym::default_method_body_is_const) =>
507504
{
508505
ConstContext::ConstFn
509506
}

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,8 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
950950
match (kind, body.source.promoted) {
951951
(_, Some(i)) => write!(w, "{:?} in ", i)?,
952952
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
953-
(DefKind::Static, _) => {
954-
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?
955-
}
953+
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
954+
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
956955
(_, _) if is_function => write!(w, "fn ")?,
957956
(DefKind::AnonConst | DefKind::InlineConst, _) => {} // things like anon const, not an item
958957
_ => bug!("Unexpected def kind {:?}", kind),

Diff for: compiler/rustc_middle/src/query/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,6 @@ rustc_queries! {
586586
separate_provide_extern
587587
}
588588

589-
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
590-
query static_mutability(def_id: DefId) -> Option<hir::Mutability> {
591-
desc { |tcx| "looking up static mutability of `{}`", tcx.def_path_str(def_id) }
592-
separate_provide_extern
593-
}
594-
595589
/// Returns `Some(generator_kind)` if the node pointed to by `def_id` is a generator.
596590
query generator_kind(def_id: DefId) -> Option<hir::GeneratorKind> {
597591
desc { |tcx| "looking up generator kind of `{}`", tcx.def_path_str(def_id) }

Diff for: compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ impl<'tcx> TyCtxt<'tcx> {
21472147
match instance {
21482148
ty::InstanceDef::Item(def) => match self.def_kind(def.did) {
21492149
DefKind::Const
2150-
| DefKind::Static
2150+
| DefKind::Static(..)
21512151
| DefKind::AssocConst
21522152
| DefKind::Ctor(..)
21532153
| DefKind::AnonConst

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ pub trait PrettyPrinter<'tcx>:
11881188
}
11891189
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted: None }) => {
11901190
match self.tcx().def_kind(def.did) {
1191-
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
1191+
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
11921192
p!(print_value_path(def.did, substs))
11931193
}
11941194
_ => {

Diff for: compiler/rustc_middle/src/ty/util.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,14 @@ impl<'tcx> TyCtxt<'tcx> {
533533
}
534534

535535
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
536+
#[inline]
536537
pub fn is_static(self, def_id: DefId) -> bool {
537-
self.static_mutability(def_id).is_some()
538+
matches!(self.def_kind(def_id), DefKind::Static(_))
539+
}
540+
541+
#[inline]
542+
pub fn static_mutability(self, def_id: DefId) -> Option<hir::Mutability> {
543+
if let DefKind::Static(mt) = self.def_kind(def_id) { Some(mt) } else { None }
538544
}
539545

540546
/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
@@ -543,6 +549,7 @@ impl<'tcx> TyCtxt<'tcx> {
543549
}
544550

545551
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
552+
#[inline]
546553
pub fn is_mutable_static(self, def_id: DefId) -> bool {
547554
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
548555
}

Diff for: compiler/rustc_mir_build/src/build/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ crate fn mir_built<'tcx>(
4141
/// Construct the MIR for a given `DefId`.
4242
fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
4343
let id = tcx.hir().local_def_id_to_hir_id(def.did);
44-
let body_owner_kind = tcx.hir().body_owner_kind(id);
44+
let body_owner_kind = tcx.hir().body_owner_kind(def.did);
4545
let typeck_results = tcx.typeck_opt_const_arg(def);
4646

4747
// Ensure unsafeck and abstract const building is ran before we steal the THIR.
@@ -802,7 +802,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
802802
check_overflow |= tcx.sess.overflow_checks();
803803
// Constants always need overflow checks.
804804
check_overflow |= matches!(
805-
tcx.hir().body_owner_kind(hir_id),
805+
tcx.hir().body_owner_kind(def.did),
806806
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_)
807807
);
808808

Diff for: compiler/rustc_mir_build/src/thir/cx/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'tcx> Cx<'tcx> {
523523
}
524524
}
525525

526-
Res::Def(DefKind::Static, def_id) => {
526+
Res::Def(DefKind::Static(_), def_id) => {
527527
InlineAsmOperand::SymStatic { def_id }
528528
}
529529

@@ -901,7 +901,7 @@ impl<'tcx> Cx<'tcx> {
901901

902902
// We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
903903
// a constant reference (or constant raw pointer for `static mut`) in MIR
904-
Res::Def(DefKind::Static, id) => {
904+
Res::Def(DefKind::Static(_), id) => {
905905
let ty = self.tcx.static_ptr_ty(id);
906906
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
907907
let kind = if self.tcx.is_thread_local_static(id) {

Diff for: compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
420420
_ => {
421421
let pattern_error = match res {
422422
Res::Def(DefKind::ConstParam, _) => PatternError::ConstParamInPattern(span),
423-
Res::Def(DefKind::Static, _) => PatternError::StaticInPattern(span),
423+
Res::Def(DefKind::Static(_), _) => PatternError::StaticInPattern(span),
424424
_ => PatternError::NonConstPath(span),
425425
};
426426
self.errors.push(pattern_error);

0 commit comments

Comments
 (0)