Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember mutability in DefKind::Static. #95436

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ fn do_mir_borrowck<'a, 'tcx>(

let tcx = infcx.tcx;
let param_env = tcx.param_env(def.did);
let id = tcx.hir().local_def_id_to_hir_id(def.did);

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

let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def.did).is_fn_or_closure();
let borrow_set =
Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));

Expand Down Expand Up @@ -287,8 +286,9 @@ fn do_mir_borrowck<'a, 'tcx>(
.pass_name("borrowck")
.iterate_to_fixpoint();

let def_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
let movable_generator = !matches!(
tcx.hir().get(id),
tcx.hir().get(def_hir_id),
Node::Expr(&hir::Expr {
kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
..
Expand Down Expand Up @@ -383,7 +383,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let scope = mbcx.body.source_info(location).scope;
let lint_root = match &mbcx.body.source_scopes[scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root,
_ => id,
_ => def_hir_id,
};

// Span and message don't matter; we overwrite them below anyway
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
let tcx = self.infcx.tcx;
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.did.to_def_id());

match tcx.hir().body_owner_kind(self.mir_hir_id) {
match tcx.hir().body_owner_kind(self.mir_def.did) {
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
let defining_ty = if self.mir_def.did.to_def_id() == typeck_root_def_id {
tcx.type_of(typeck_root_def_id)
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 @@ -38,7 +38,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
6 changes: 3 additions & 3 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub enum DefKind {
Const,
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
Static,
Static(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 @@ -128,7 +128,7 @@ impl DefKind {
"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 @@ -202,7 +202,7 @@ impl DefKind {
DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static
| DefKind::Static(..)
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst => Some(Namespace::ValueNS),
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 @@ -1530,7 +1530,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
12 changes: 1 addition & 11 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,21 +1442,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn is_foreign_item(self, id: DefIndex) -> bool {
match self.kind(id) {
EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => {
true
}
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
_ => false,
}
}

fn static_mutability(self, id: DefIndex) -> Option<hir::Mutability> {
match self.kind(id) {
EntryKind::ImmStatic | EntryKind::ForeignImmStatic => Some(hir::Mutability::Not),
EntryKind::MutStatic | EntryKind::ForeignMutStatic => Some(hir::Mutability::Mut),
_ => None,
}
}

#[inline]
fn def_key(self, index: DefIndex) -> DefKey {
*self
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
static_mutability => { cdata.static_mutability(def_id.index) }
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,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 @@ -826,7 +826,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 @@ -870,7 +870,7 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst
| DefKind::Static
| DefKind::Static(..)
| DefKind::Const => (true, false),
// Full-fledged functions
DefKind::AssocFn | DefKind::Fn => {
Expand Down Expand Up @@ -915,7 +915,7 @@ fn should_encode_variances(def_kind: DefKind) -> bool {
| DefKind::AssocConst
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Static
| DefKind::Static(..)
| DefKind::Const
| DefKind::ForeignMod
| DefKind::TyAlias
Expand Down Expand Up @@ -949,7 +949,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 @@ -1385,8 +1385,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.encode_ident_span(def_id, item.ident);

let entry_kind = match item.kind {
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
hir::ItemKind::Static(..) => EntryKind::Static,
hir::ItemKind::Const(_, body_id) => {
let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
let const_data = self.encode_rendered_const_for_body(body_id);
Expand Down Expand Up @@ -1874,11 +1873,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
}
hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic);
}
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic);
hir::ForeignItemKind::Static(..) => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
}
hir::ForeignItemKind::Type => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,8 @@ define_tables! {
enum EntryKind {
AnonConst,
Const,
ImmStatic,
MutStatic,
ForeignImmStatic,
ForeignMutStatic,
Static,
ForeignStatic,
ForeignMod,
ForeignType,
GlobalAsm,
Expand Down
39 changes: 18 additions & 21 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'hir> Map<'hir> {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let def_kind = match self.find(hir_id)? {
Node::Item(item) => match item.kind {
ItemKind::Static(..) => DefKind::Static,
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
ItemKind::Const(..) => DefKind::Const,
ItemKind::Fn(..) => DefKind::Fn,
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
Expand All @@ -248,7 +248,7 @@ impl<'hir> Map<'hir> {
},
Node::ForeignItem(item) => match item.kind {
ForeignItemKind::Fn(..) => DefKind::Fn,
ForeignItemKind::Static(..) => DefKind::Static,
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
ForeignItemKind::Type => DefKind::ForeignTy,
},
Node::TraitItem(item) => match item.kind {
Expand Down Expand Up @@ -471,19 +471,15 @@ impl<'hir> Map<'hir> {
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
///
/// Panics if `LocalDefId` does not have an associated body.
pub fn body_owner_kind(self, id: HirId) -> BodyOwnerKind {
match self.get(id) {
Node::Item(&Item { kind: ItemKind::Const(..), .. })
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
| Node::AnonConst(_) => BodyOwnerKind::Const,
Node::Ctor(..)
| Node::Item(&Item { kind: ItemKind::Fn(..), .. })
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. })
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Fn(..), .. }) => BodyOwnerKind::Fn,
Node::Item(&Item { kind: ItemKind::Static(_, m, _), .. }) => BodyOwnerKind::Static(m),
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => BodyOwnerKind::Closure,
node => bug!("{:#?} is not a body node", node),
pub fn body_owner_kind(self, def_id: LocalDefId) -> BodyOwnerKind {
match self.tcx.def_kind(def_id) {
DefKind::Const | DefKind::AssocConst | DefKind::InlineConst | DefKind::AnonConst => {
BodyOwnerKind::Const
}
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
DefKind::Closure | DefKind::Generator => BodyOwnerKind::Closure,
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
}
}

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

BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id.to_def_id()) => return None,
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
ConstContext::ConstFn
}
BodyOwnerKind::Fn
if self.tcx.has_attr(did.to_def_id(), sym::default_method_body_is_const) =>
if self.tcx.has_attr(def_id.to_def_id(), sym::default_method_body_is_const) =>
{
ConstContext::ConstFn
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,9 +950,8 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
match (kind, body.source.promoted) {
(_, Some(i)) => write!(w, "{:?} in ", i)?,
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
(DefKind::Static, _) => {
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?
}
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
(DefKind::Static(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
6 changes: 0 additions & 6 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,6 @@ rustc_queries! {
separate_provide_extern
}

/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
query static_mutability(def_id: DefId) -> Option<hir::Mutability> {
desc { |tcx| "looking up static mutability of `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}

/// Returns `Some(generator_kind)` if the node pointed to by `def_id` is a generator.
query generator_kind(def_id: DefId) -> Option<hir::GeneratorKind> {
desc { |tcx| "looking up generator kind of `{}`", tcx.def_path_str(def_id) }
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 @@ -2103,7 +2103,7 @@ impl<'tcx> TyCtxt<'tcx> {
match instance {
ty::InstanceDef::Item(def) => match self.def_kind(def.did) {
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 @@ -1185,7 +1185,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted: None }) => {
match self.tcx().def_kind(def.did) {
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
p!(print_value_path(def.did, substs))
}
_ => {
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,14 @@ 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 {
self.static_mutability(def_id).is_some()
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 }
}

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

/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
#[inline]
pub fn is_mutable_static(self, def_id: DefId) -> bool {
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ crate fn mir_built<'tcx>(
/// Construct the MIR for a given `DefId`.
fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
let id = tcx.hir().local_def_id_to_hir_id(def.did);
let body_owner_kind = tcx.hir().body_owner_kind(id);
let body_owner_kind = tcx.hir().body_owner_kind(def.did);
let typeck_results = tcx.typeck_opt_const_arg(def);

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

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<'tcx> Cx<'tcx> {
}
}

Res::Def(DefKind::Static, def_id) => {
Res::Def(DefKind::Static(_), def_id) => {
InlineAsmOperand::SymStatic { def_id }
}

Expand Down Expand Up @@ -901,7 +901,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.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let kind = if self.tcx.is_thread_local_static(id) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
_ => {
let pattern_error = match res {
Res::Def(DefKind::ConstParam, _) => PatternError::ConstParamInPattern(span),
Res::Def(DefKind::Static, _) => PatternError::StaticInPattern(span),
Res::Def(DefKind::Static(_), _) => PatternError::StaticInPattern(span),
_ => PatternError::NonConstPath(span),
};
self.errors.push(pattern_error);
Expand Down
Loading