Skip to content

Commit

Permalink
Introduce query static_mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Apr 21, 2019
1 parent 4d9c6cd commit 286a469
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/librustc/query/mod.rs
Expand Up @@ -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> {
Expand Down
30 changes: 2 additions & 28 deletions src/librustc/ty/util.rs
@@ -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};
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -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 => {
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -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>)
Expand Down
17 changes: 17 additions & 0 deletions src/librustc_typeck/collect.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 286a469

Please sign in to comment.