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.
This commit is contained in:
Camille GILLOT 2022-03-29 17:11:12 +02:00
parent 11909e3588
commit 21a554caf6
40 changed files with 98 additions and 128 deletions

View File

@ -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 {
@ -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));
@ -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)),
..
@ -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

View File

@ -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)

View File

@ -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

View File

@ -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
@ -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",
@ -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),

View File

@ -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

View File

@ -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

View File

@ -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) }

View File

@ -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
@ -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
@ -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 => {
@ -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
@ -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
@ -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);
@ -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);

View File

@ -331,10 +331,8 @@ define_tables! {
enum EntryKind {
AnonConst,
Const,
ImmStatic,
MutStatic,
ForeignImmStatic,
ForeignMutStatic,
Static,
ForeignStatic,
ForeignMod,
ForeignType,
GlobalAsm,

View File

@ -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),
@ -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 {
@ -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),
}
}
@ -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
}

View File

@ -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),

View File

@ -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) }

View File

@ -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

View File

@ -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))
}
_ => {

View File

@ -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.
@ -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)
}

View File

@ -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.
@ -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(_)
);

View File

@ -523,7 +523,7 @@ impl<'tcx> Cx<'tcx> {
}
}
Res::Def(DefKind::Static, def_id) => {
Res::Def(DefKind::Static(_), def_id) => {
InlineAsmOperand::SymStatic { def_id }
}
@ -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) {

View File

@ -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);

View File

@ -59,11 +59,10 @@ impl<'tcx> MirPass<'tcx> for Inline {
}
fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
let def_id = body.source.def_id();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let def_id = body.source.def_id().expect_local();
// Only do inlining into fn bodies.
if !tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() {
return false;
}
if body.source.promoted.is_some() {
@ -77,9 +76,10 @@ fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
}
let param_env = tcx.param_env_reveal_all_normalized(def_id);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let param_env = rustc_trait_selection::traits::normalize_param_env_or_error(
tcx,
def_id,
def_id.to_def_id(),
param_env,
ObligationCause::misc(body.span, hir_id),
);

View File

@ -158,7 +158,7 @@ fn mark_used_by_default_parameters<'tcx>(
| DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static
| DefKind::Static(_)
| DefKind::Ctor(_, _)
| DefKind::AssocFn
| DefKind::AssocConst

View File

@ -734,7 +734,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
let body_id = body.id();
let owner_id = self.tcx.hir().body_owner(body_id);
let owner_id = self.tcx.hir().body_owner_def_id(body_id);
debug!(
"visit_body(id={:?}, span={:?}, body.id={:?}, cx.parent={:?})",

View File

@ -553,7 +553,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
}
match def_kind {
// No type privacy, so can be directly marked as reachable.
DefKind::Const | DefKind::Static | DefKind::TraitAlias | DefKind::TyAlias => {
DefKind::Const | DefKind::Static(_) | DefKind::TraitAlias | DefKind::TyAlias => {
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
self.update(def_id, level);
}
@ -1243,12 +1243,12 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
let def = def.filter(|(kind, _)| {
matches!(
kind,
DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Static
DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Static(_)
)
});
if let Some((kind, def_id)) = def {
let is_local_static =
if let DefKind::Static = kind { def_id.is_local() } else { false };
if let DefKind::Static(_) = kind { def_id.is_local() } else { false };
if !self.item_is_accessible(def_id) && !is_local_static {
let sess = self.tcx.sess;
let sm = sess.source_map();

View File

@ -701,8 +701,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
// These items live in the value namespace.
ItemKind::Static(..) => {
let res = Res::Def(DefKind::Static, def_id);
ItemKind::Static(_, mt, _) => {
let res = Res::Def(DefKind::Static(mt), def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Const(..) => {
@ -907,7 +907,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let def_id = local_def_id.to_def_id();
let (def_kind, ns) = match item.kind {
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
ForeignItemKind::Static(..) => (DefKind::Static, ValueNS),
ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS),
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
ForeignItemKind::MacCall(_) => unreachable!(),
};
@ -963,7 +963,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
Res::Def(
DefKind::Fn
| DefKind::AssocFn
| DefKind::Static
| DefKind::Static(_)
| DefKind::Const
| DefKind::AssocConst
| DefKind::Ctor(..),

View File

@ -300,7 +300,7 @@ impl<'a> PathSource<'a> {
Res::Def(
DefKind::Ctor(_, CtorKind::Const | CtorKind::Fn)
| DefKind::Const
| DefKind::Static
| DefKind::Static(_)
| DefKind::Fn
| DefKind::AssocFn
| DefKind::AssocConst
@ -1830,7 +1830,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
Some(res)
}
Res::Def(DefKind::Ctor(..) | DefKind::Const | DefKind::Static, _) => {
Res::Def(DefKind::Ctor(..) | DefKind::Const | DefKind::Static(_), _) => {
// This is unambiguously a fresh binding, either syntactically
// (e.g., `IDENT @ PAT` or `ref IDENT`) or because `IDENT` resolves
// to something unusable as a pattern (e.g., constructor function),

View File

@ -701,7 +701,7 @@ impl<'tcx> SaveContext<'tcx> {
let parent_def_id = self.tcx.parent(def_id).unwrap();
Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(parent_def_id) })
}
Res::Def(HirDefKind::Static | HirDefKind::Const | HirDefKind::AssocConst, _) => {
Res::Def(HirDefKind::Static(_) | HirDefKind::Const | HirDefKind::AssocConst, _) => {
Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_def_id(res.def_id()) })
}
Res::Def(HirDefKind::AssocFn, decl_id) => {

View File

@ -2186,7 +2186,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));
}

View File

@ -184,7 +184,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,
),
..

View File

@ -52,7 +52,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
wbcx.visit_node_id(param.pat.span, param.hir_id);
}
// Type only exists for constants and statics, not functions.
match self.tcx.hir().body_owner_kind(item_id) {
match self.tcx.hir().body_owner_kind(item_def_id) {
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => {
wbcx.visit_node_id(body.value.span, item_id);
}

View File

@ -85,7 +85,6 @@ pub fn provide(providers: &mut Providers) {
impl_trait_ref,
impl_polarity,
is_foreign_item,
static_mutability,
generator_kind,
codegen_fn_attrs,
asm_target_features,
@ -2602,20 +2601,6 @@ fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
}
}
fn static_mutability(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::Mutability> {
match tcx.hir().get_if_local(def_id) {
Some(
Node::Item(&hir::Item { kind: hir::ItemKind::Static(_, mutbl, _), .. })
| Node::ForeignItem(&hir::ForeignItem {
kind: hir::ForeignItemKind::Static(_, mutbl),
..
}),
) => Some(mutbl),
Some(_) => None,
_ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
}
}
fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
match tcx.hir().get_if_local(def_id) {
Some(Node::Expr(&rustc_hir::Expr {

View File

@ -723,10 +723,8 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
let upvars = tcx.upvars_mentioned(self.body_owner);
// For purposes of this function, generator and closures are equivalent.
let body_owner_is_closure = matches!(
tcx.hir().body_owner_kind(tcx.hir().local_def_id_to_hir_id(self.body_owner)),
hir::BodyOwnerKind::Closure,
);
let body_owner_is_closure =
matches!(tcx.hir().body_owner_kind(self.body_owner), hir::BodyOwnerKind::Closure,);
// If we have a nested closure, we want to include the fake reads present in the nested closure.
if let Some(fake_reads) = self.mc.typeck_results.closure_fake_reads.get(&closure_def_id) {

View File

@ -404,7 +404,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
)
| Res::SelfCtor(..) => Ok(self.cat_rvalue(hir_id, span, expr_ty)),
Res::Def(DefKind::Static, _) => {
Res::Def(DefKind::Static(_), _) => {
Ok(PlaceWithHirId::new(hir_id, expr_ty, PlaceBase::StaticItem, Vec::new()))
}

View File

@ -100,7 +100,7 @@ crate fn try_inline(
record_extern_fqn(cx, did, ItemType::Module);
clean::ModuleItem(build_module(cx, did, visited))
}
Res::Def(DefKind::Static, did) => {
Res::Def(DefKind::Static(_), did) => {
record_extern_fqn(cx, did, ItemType::Static);
clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
}

View File

@ -397,7 +397,7 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
// These should be added to the cache using `record_extern_fqn`.
Res::Def(
kind @ (AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
| Union | Mod | ForeignTy | Const | Static | Macro(..) | TraitAlias),
| Union | Mod | ForeignTy | Const | Static(_) | Macro(..) | TraitAlias),
i,
) => (i, kind.into()),
// This is part of a trait definition or trait impl; document the trait.

View File

@ -111,7 +111,7 @@ impl From<DefKind> for ItemType {
DefKind::Fn => Self::Function,
DefKind::Mod => Self::Module,
DefKind::Const => Self::Constant,
DefKind::Static => Self::Static,
DefKind::Static(_) => Self::Static,
DefKind::Struct => Self::Struct,
DefKind::Union => Self::Union,
DefKind::Trait => Self::Trait,

View File

@ -10,6 +10,7 @@ use rustc_hir::def::{
PerNS,
};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
use rustc_hir::Mutability;
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt};
use rustc_middle::{bug, span_bug, ty};
use rustc_session::lint::Lint;
@ -130,7 +131,7 @@ impl Res {
DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst => {
"const"
}
DefKind::Static => "static",
DefKind::Static(_) => "static",
DefKind::Macro(MacroKind::Derive) => "derive",
// Now handle things that don't have a specific disambiguator
_ => match kind
@ -1737,7 +1738,7 @@ impl Disambiguator {
"union" => Kind(DefKind::Union),
"module" | "mod" => Kind(DefKind::Mod),
"const" | "constant" => Kind(DefKind::Const),
"static" => Kind(DefKind::Static),
"static" => Kind(DefKind::Static(Mutability::Not)),
"function" | "fn" | "method" => Kind(DefKind::Fn),
"derive" => Kind(DefKind::Macro(MacroKind::Derive)),
"type" => NS(Namespace::TypeNS),
@ -2091,7 +2092,7 @@ fn resolution_failure(
return;
}
Trait | TyAlias | ForeignTy | OpaqueTy | TraitAlias | TyParam
| Static => "associated item",
| Static(_) => "associated item",
Impl | GlobalAsm => unreachable!("not a path"),
}
} else {

View File

@ -139,11 +139,11 @@ impl<'tcx> LateLintPass<'tcx> for Arithmetic {
}
fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner = cx.tcx.hir().body_owner_def_id(body.id());
match cx.tcx.hir().body_owner_kind(body_owner) {
hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
let body_span = cx.tcx.hir().span(body_owner);
let body_span = cx.tcx.def_span(body_owner);
if let Some(span) = self.const_span {
if span.contains(body_span) {

View File

@ -273,7 +273,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
}
return false; // no need to walk further *on the variable*
}
Res::Def(DefKind::Static | DefKind::Const, ..) => {
Res::Def(DefKind::Static (_)| DefKind::Const, ..) => {
if index_used_directly {
self.indexed_directly.insert(
seqvar.segments[0].ident.name,

View File

@ -104,7 +104,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
Res::Local(hir_id) => {
self.ids.insert(hir_id);
},
Res::Def(DefKind::Static, def_id) => {
Res::Def(DefKind::Static(_), def_id) => {
let mutable = self.cx.tcx.is_mutable_static(def_id);
self.def_ids.insert(def_id, mutable);
},

View File

@ -93,7 +93,7 @@ pub(super) fn check<'tcx>(
},
hir::ExprKind::Path(ref p) => matches!(
cx.qpath_res(p, arg.hir_id),
hir::def::Res::Def(hir::def::DefKind::Const | hir::def::DefKind::Static, _)
hir::def::Res::Def(hir::def::DefKind::Const | hir::def::DefKind::Static(_), _)
),
_ => false,
}

View File

@ -139,14 +139,20 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
if !matches!(hir.body_owner_kind(hir.body_owner(body.id())), BodyOwnerKind::Closure) {
if !matches!(
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
BodyOwnerKind::Closure
) {
self.bindings.push(FxHashMap::default());
}
}
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
if !matches!(hir.body_owner_kind(hir.body_owner(body.id())), BodyOwnerKind::Closure) {
if !matches!(
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
BodyOwnerKind::Closure
) {
self.bindings.pop();
}
}