Introduce query static_mutability

This commit is contained in:
Vadim Petrochenkov 2019-04-19 23:32:26 +03:00
parent 4d9c6cd722
commit 286a469a16
5 changed files with 33 additions and 28 deletions

View file

@ -238,6 +238,9 @@ rustc_queries! {
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
query is_foreign_item(_: DefId) -> bool {}
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
query static_mutability(_: DefId) -> Option<hir::Mutability> {}
/// Get a map with the variance of every item; use `item_variance`
/// instead.
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {

View file

@ -1,9 +1,8 @@
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
use crate::hir::def::Def;
use crate::hir;
use crate::hir::def_id::DefId;
use crate::hir::map::DefPathData;
use crate::hir::{self, Node};
use crate::mir::interpret::{sign_extend, truncate};
use crate::ich::NodeIdHashingMode;
use crate::traits::{self, ObligationCause};
@ -615,32 +614,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability.
pub fn is_static(&self, def_id: DefId) -> Option<hir::Mutability> {
if let Some(node) = self.hir().get_if_local(def_id) {
match node {
Node::Item(&hir::Item {
node: hir::ItemKind::Static(_, mutbl, _), ..
}) => Some(mutbl),
Node::ForeignItem(&hir::ForeignItem {
node: hir::ForeignItemKind::Static(_, is_mutbl), ..
}) =>
Some(if is_mutbl {
hir::Mutability::MutMutable
} else {
hir::Mutability::MutImmutable
}),
_ => None
}
} else {
match self.describe_def(def_id) {
Some(Def::Static(_, is_mutbl)) =>
Some(if is_mutbl {
hir::Mutability::MutMutable
} else {
hir::Mutability::MutImmutable
}),
_ => None
}
}
self.static_mutability(def_id)
}
/// Expands the given impl trait type, stopping if the type is recursive.

View file

@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(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) }
describe_def => { cdata.get_def(def_id.index) }
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
lookup_stability => {

View file

@ -1163,6 +1163,16 @@ impl<'a, 'tcx> CrateMetadata {
}
}
crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
match self.entry(id).kind {
EntryKind::ImmStatic |
EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
EntryKind::MutStatic |
EntryKind::ForeignMutStatic => Some(hir::MutMutable),
_ => None,
}
}
pub fn fn_sig(&self,
id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)

View file

@ -78,6 +78,7 @@ pub fn provide(providers: &mut Providers<'_>) {
impl_trait_ref,
impl_polarity,
is_foreign_item,
static_mutability,
codegen_fn_attrs,
collect_mod_item_types,
..*providers
@ -2361,6 +2362,22 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool
}
}
fn static_mutability<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Option<hir::Mutability> {
match tcx.hir().get_if_local(def_id) {
Some(Node::Item(&hir::Item {
node: hir::ItemKind::Static(_, mutbl, _), ..
})) => Some(mutbl),
Some(Node::ForeignItem( &hir::ForeignItem {
node: hir::ForeignItemKind::Static(_, mutbl), ..
})) => Some(if mutbl { hir::MutMutable } else { hir::MutImmutable }),
Some(_) => None,
_ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
}
}
fn from_target_feature(
tcx: TyCtxt<'_, '_, '_>,
id: DefId,