Skip to content

Commit 286a469

Browse files
committed
Introduce query static_mutability
1 parent 4d9c6cd commit 286a469

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

src/librustc/query/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ rustc_queries! {
238238
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
239239
query is_foreign_item(_: DefId) -> bool {}
240240

241+
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
242+
query static_mutability(_: DefId) -> Option<hir::Mutability> {}
243+
241244
/// Get a map with the variance of every item; use `item_variance`
242245
/// instead.
243246
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {

src/librustc/ty/util.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
3-
use crate::hir::def::Def;
3+
use crate::hir;
44
use crate::hir::def_id::DefId;
55
use crate::hir::map::DefPathData;
6-
use crate::hir::{self, Node};
76
use crate::mir::interpret::{sign_extend, truncate};
87
use crate::ich::NodeIdHashingMode;
98
use crate::traits::{self, ObligationCause};
@@ -615,32 +614,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
615614

616615
/// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability.
617616
pub fn is_static(&self, def_id: DefId) -> Option<hir::Mutability> {
618-
if let Some(node) = self.hir().get_if_local(def_id) {
619-
match node {
620-
Node::Item(&hir::Item {
621-
node: hir::ItemKind::Static(_, mutbl, _), ..
622-
}) => Some(mutbl),
623-
Node::ForeignItem(&hir::ForeignItem {
624-
node: hir::ForeignItemKind::Static(_, is_mutbl), ..
625-
}) =>
626-
Some(if is_mutbl {
627-
hir::Mutability::MutMutable
628-
} else {
629-
hir::Mutability::MutImmutable
630-
}),
631-
_ => None
632-
}
633-
} else {
634-
match self.describe_def(def_id) {
635-
Some(Def::Static(_, is_mutbl)) =>
636-
Some(if is_mutbl {
637-
hir::Mutability::MutMutable
638-
} else {
639-
hir::Mutability::MutImmutable
640-
}),
641-
_ => None
642-
}
643-
}
617+
self.static_mutability(def_id)
644618
}
645619

646620
/// Expands the given impl trait type, stopping if the type is recursive.

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
137137
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
138138
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
139139
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
140+
static_mutability => { cdata.static_mutability(def_id.index) }
140141
describe_def => { cdata.get_def(def_id.index) }
141142
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
142143
lookup_stability => {

src/librustc_metadata/decoder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,16 @@ impl<'a, 'tcx> CrateMetadata {
11631163
}
11641164
}
11651165

1166+
crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
1167+
match self.entry(id).kind {
1168+
EntryKind::ImmStatic |
1169+
EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
1170+
EntryKind::MutStatic |
1171+
EntryKind::ForeignMutStatic => Some(hir::MutMutable),
1172+
_ => None,
1173+
}
1174+
}
1175+
11661176
pub fn fn_sig(&self,
11671177
id: DefIndex,
11681178
tcx: TyCtxt<'a, 'tcx, 'tcx>)

src/librustc_typeck/collect.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn provide(providers: &mut Providers<'_>) {
7878
impl_trait_ref,
7979
impl_polarity,
8080
is_foreign_item,
81+
static_mutability,
8182
codegen_fn_attrs,
8283
collect_mod_item_types,
8384
..*providers
@@ -2361,6 +2362,22 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool
23612362
}
23622363
}
23632364

2365+
fn static_mutability<'a, 'tcx>(
2366+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2367+
def_id: DefId,
2368+
) -> Option<hir::Mutability> {
2369+
match tcx.hir().get_if_local(def_id) {
2370+
Some(Node::Item(&hir::Item {
2371+
node: hir::ItemKind::Static(_, mutbl, _), ..
2372+
})) => Some(mutbl),
2373+
Some(Node::ForeignItem( &hir::ForeignItem {
2374+
node: hir::ForeignItemKind::Static(_, mutbl), ..
2375+
})) => Some(if mutbl { hir::MutMutable } else { hir::MutImmutable }),
2376+
Some(_) => None,
2377+
_ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
2378+
}
2379+
}
2380+
23642381
fn from_target_feature(
23652382
tcx: TyCtxt<'_, '_, '_>,
23662383
id: DefId,

0 commit comments

Comments
 (0)