Skip to content

Commit 31a75a1

Browse files
committed
Auto merge of #60124 - petrochenkov:stanomut, r=eddyb
Remove mutability from `Def::Static` Querify `TyCtxt::is_static`. Use `Mutability` instead of bool in foreign statics in AST/HIR. cc #60110 r? @eddyb
2 parents 06a271a + 4eb94b4 commit 31a75a1

File tree

34 files changed

+93
-96
lines changed

34 files changed

+93
-96
lines changed

src/librustc/hir/def.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub enum Def<Id = hir::HirId> {
7373
Fn(DefId),
7474
Const(DefId),
7575
ConstParam(DefId),
76-
Static(DefId, bool /* is_mutbl */),
76+
Static(DefId),
7777
/// `DefId` refers to the struct or enum variant's constructor.
7878
Ctor(DefId, CtorOf, CtorKind),
7979
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
@@ -291,7 +291,7 @@ impl<Id> Def<Id> {
291291
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
292292
pub fn opt_def_id(&self) -> Option<DefId> {
293293
match *self {
294-
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
294+
Def::Fn(id) | Def::Mod(id) | Def::Static(id) |
295295
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
296296
Def::TyAlias(id) | Def::TraitAlias(id) |
297297
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
@@ -379,7 +379,7 @@ impl<Id> Def<Id> {
379379
match self {
380380
Def::Fn(id) => Def::Fn(id),
381381
Def::Mod(id) => Def::Mod(id),
382-
Def::Static(id, is_mutbl) => Def::Static(id, is_mutbl),
382+
Def::Static(id) => Def::Static(id),
383383
Def::Enum(id) => Def::Enum(id),
384384
Def::Variant(id) => Def::Variant(id),
385385
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3742,7 +3742,7 @@ impl<'a> LoweringContext<'a> {
37423742
}
37433743
ForeignItemKind::Static(ref t, m) => {
37443744
hir::ForeignItemKind::Static(
3745-
self.lower_ty(t, ImplTraitContext::disallowed()), m)
3745+
self.lower_ty(t, ImplTraitContext::disallowed()), self.lower_mutability(m))
37463746
}
37473747
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
37483748
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),

src/librustc/hir/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'hir> Map<'hir> {
322322
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
323323

324324
match item.node {
325-
ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
325+
ItemKind::Static(..) => Some(Def::Static(def_id())),
326326
ItemKind::Const(..) => Some(Def::Const(def_id())),
327327
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
328328
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
@@ -344,7 +344,7 @@ impl<'hir> Map<'hir> {
344344
let def_id = self.local_def_id_from_hir_id(item.hir_id);
345345
match item.node {
346346
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
347-
ForeignItemKind::Static(_, m) => Some(Def::Static(def_id, m)),
347+
ForeignItemKind::Static(..) => Some(Def::Static(def_id)),
348348
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
349349
}
350350
}

src/librustc/hir/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,9 +2405,8 @@ pub struct ForeignItem {
24052405
pub enum ForeignItemKind {
24062406
/// A foreign function.
24072407
Fn(P<FnDecl>, HirVec<Ident>, Generics),
2408-
/// A foreign static item (`static ext: u8`), with optional mutability
2409-
/// (the boolean is true when mutable).
2410-
Static(P<Ty>, bool),
2408+
/// A foreign static item (`static ext: u8`).
2409+
Static(P<Ty>, Mutability),
24112410
/// A foreign type.
24122411
Type,
24132412
}

src/librustc/hir/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl<'a> State<'a> {
466466
}
467467
hir::ForeignItemKind::Static(ref t, m) => {
468468
self.head(visibility_qualified(&item.vis, "static"))?;
469-
if m {
469+
if m == hir::MutMutable {
470470
self.word_space("mut")?;
471471
}
472472
self.print_ident(item.ident)?;

src/librustc/middle/mem_categorization.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
705705
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
706706
}
707707

708-
Def::Static(def_id, mutbl) => {
708+
Def::Static(def_id) => {
709709
// `#[thread_local]` statics may not outlive the current function, but
710710
// they also cannot be moved out of.
711711
let is_thread_local = self.tcx.get_attrs(def_id)[..]
@@ -723,7 +723,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
723723
hir_id,
724724
span,
725725
cat,
726-
mutbl: if mutbl { McDeclared } else { McImmutable},
726+
mutbl: match self.tcx.static_mutability(def_id).unwrap() {
727+
hir::MutImmutable => McImmutable,
728+
hir::MutMutable => McDeclared,
729+
},
727730
ty:expr_ty,
728731
note: NoteNone
729732
})

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: 9 additions & 30 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};
@@ -613,34 +612,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
613612
})
614613
}
615614

616-
/// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability.
617-
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-
}
615+
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
616+
pub fn is_static(&self, def_id: DefId) -> bool {
617+
self.static_mutability(def_id).is_some()
618+
}
619+
620+
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
621+
pub fn is_mutable_static(&self, def_id: DefId) -> bool {
622+
self.static_mutability(def_id) == Some(hir::MutMutable)
644623
}
645624

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

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
322322
self.get_fn(fn_instance)
323323
}
324324
Some(AllocKind::Static(def_id)) => {
325-
assert!(self.tcx.is_static(def_id).is_some());
325+
assert!(self.tcx.is_static(def_id));
326326
self.get_static(def_id)
327327
}
328328
None => bug!("missing allocation {:?}", ptr.alloc_id),

src/librustc_codegen_ssa/mono_item.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc::hir;
2-
use rustc::hir::def::Def;
32
use rustc::mir::mono::{Linkage, Visibility};
43
use rustc::ty::layout::HasTyCtxt;
54
use std::fmt;
@@ -19,17 +18,7 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
1918

2019
match *self.as_mono_item() {
2120
MonoItem::Static(def_id) => {
22-
let tcx = cx.tcx();
23-
let is_mutable = match tcx.describe_def(def_id) {
24-
Some(Def::Static(_, is_mutable)) => is_mutable,
25-
Some(other) => {
26-
bug!("Expected Def::Static, found {:?}", other)
27-
}
28-
None => {
29-
bug!("Expected Def::Static for {:?}, found nothing", def_id)
30-
}
31-
};
32-
cx.codegen_static(def_id, is_mutable);
21+
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
3322
}
3423
MonoItem::GlobalAsm(hir_id) => {
3524
let item = cx.tcx().hir().expect_item_by_hir_id(hir_id);

0 commit comments

Comments
 (0)