Skip to content

Commit 21a554c

Browse files
committed
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.
1 parent 11909e3 commit 21a554c

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
@@ -154,7 +154,6 @@ fn do_mir_borrowck<'a, 'tcx>(
154154

155155
let tcx = infcx.tcx;
156156
let param_env = tcx.param_env(def.did);
157-
let id = tcx.hir().local_def_id_to_hir_id(def.did);
158157

159158
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
160159
for var_debug_info in &input_body.var_debug_info {
@@ -224,7 +223,7 @@ fn do_mir_borrowck<'a, 'tcx>(
224223
.iterate_to_fixpoint()
225224
.into_results_cursor(&body);
226225

227-
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
226+
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def.did).is_fn_or_closure();
228227
let borrow_set =
229228
Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
230229

@@ -287,8 +286,9 @@ fn do_mir_borrowck<'a, 'tcx>(
287286
.pass_name("borrowck")
288287
.iterate_to_fixpoint();
289288

289+
let def_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
290290
let movable_generator = !matches!(
291-
tcx.hir().get(id),
291+
tcx.hir().get(def_hir_id),
292292
Node::Expr(&hir::Expr {
293293
kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
294294
..
@@ -383,7 +383,7 @@ fn do_mir_borrowck<'a, 'tcx>(
383383
let scope = mbcx.body.source_info(location).scope;
384384
let lint_root = match &mbcx.body.source_scopes[scope].local_data {
385385
ClearCrossCrate::Set(data) => data.lint_root,
386-
_ => id,
386+
_ => def_hir_id,
387387
};
388388

389389
// 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
@@ -1442,21 +1442,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14421442

14431443
fn is_foreign_item(self, id: DefIndex) -> bool {
14441444
match self.kind(id) {
1445-
EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => {
1446-
true
1447-
}
1445+
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
14481446
_ => false,
14491447
}
14501448
}
14511449

1452-
fn static_mutability(self, id: DefIndex) -> Option<hir::Mutability> {
1453-
match self.kind(id) {
1454-
EntryKind::ImmStatic | EntryKind::ForeignImmStatic => Some(hir::Mutability::Not),
1455-
EntryKind::MutStatic | EntryKind::ForeignMutStatic => Some(hir::Mutability::Mut),
1456-
_ => None,
1457-
}
1458-
}
1459-
14601450
#[inline]
14611451
fn def_key(self, index: DefIndex) -> DefKey {
14621452
*self

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

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
153153
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
154154
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
155155
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
156-
static_mutability => { cdata.static_mutability(def_id.index) }
157156
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
158157
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
159158
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
@@ -792,7 +792,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
792792
| DefKind::AssocTy
793793
| DefKind::Fn
794794
| DefKind::Const
795-
| DefKind::Static
795+
| DefKind::Static(..)
796796
| DefKind::Ctor(..)
797797
| DefKind::AssocFn
798798
| DefKind::AssocConst
@@ -826,7 +826,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
826826
| DefKind::AssocConst
827827
| DefKind::TyParam
828828
| DefKind::ConstParam
829-
| DefKind::Static
829+
| DefKind::Static(..)
830830
| DefKind::Const
831831
| DefKind::Fn
832832
| DefKind::ForeignMod
@@ -870,7 +870,7 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
870870
DefKind::AnonConst
871871
| DefKind::InlineConst
872872
| DefKind::AssocConst
873-
| DefKind::Static
873+
| DefKind::Static(..)
874874
| DefKind::Const => (true, false),
875875
// Full-fledged functions
876876
DefKind::AssocFn | DefKind::Fn => {
@@ -915,7 +915,7 @@ fn should_encode_variances(def_kind: DefKind) -> bool {
915915
| DefKind::AssocConst
916916
| DefKind::TyParam
917917
| DefKind::ConstParam
918-
| DefKind::Static
918+
| DefKind::Static(..)
919919
| DefKind::Const
920920
| DefKind::ForeignMod
921921
| DefKind::TyAlias
@@ -949,7 +949,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
949949
| DefKind::AssocTy
950950
| DefKind::Fn
951951
| DefKind::Const
952-
| DefKind::Static
952+
| DefKind::Static(..)
953953
| DefKind::Ctor(..)
954954
| DefKind::AssocFn
955955
| DefKind::AssocConst
@@ -1385,8 +1385,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13851385
self.encode_ident_span(def_id, item.ident);
13861386

13871387
let entry_kind = match item.kind {
1388-
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
1389-
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
1388+
hir::ItemKind::Static(..) => EntryKind::Static,
13901389
hir::ItemKind::Const(_, body_id) => {
13911390
let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
13921391
let const_data = self.encode_rendered_const_for_body(body_id);
@@ -1874,11 +1873,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18741873
};
18751874
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
18761875
}
1877-
hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => {
1878-
record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic);
1879-
}
1880-
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => {
1881-
record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic);
1876+
hir::ForeignItemKind::Static(..) => {
1877+
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
18821878
}
18831879
hir::ForeignItemKind::Type => {
18841880
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
@@ -331,10 +331,8 @@ define_tables! {
331331
enum EntryKind {
332332
AnonConst,
333333
Const,
334-
ImmStatic,
335-
MutStatic,
336-
ForeignImmStatic,
337-
ForeignMutStatic,
334+
Static,
335+
ForeignStatic,
338336
ForeignMod,
339337
ForeignType,
340338
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
@@ -2103,7 +2103,7 @@ impl<'tcx> TyCtxt<'tcx> {
21032103
match instance {
21042104
ty::InstanceDef::Item(def) => match self.def_kind(def.did) {
21052105
DefKind::Const
2106-
| DefKind::Static
2106+
| DefKind::Static(..)
21072107
| DefKind::AssocConst
21082108
| DefKind::Ctor(..)
21092109
| DefKind::AnonConst

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

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

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)