Skip to content

Split {ast,hir,...}::Static(Mutability) into Static and StaticMut. #60110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use syntax_pos::Span;

#[derive(Copy, Clone, PartialEq)]
pub(crate) enum Target {
// FIXME(eddyb) replace most of this with `DefKind`.
ExternCrate,
Use,
Static,
StaticMut,
Const,
Fn,
Closure,
Expand All @@ -43,6 +45,7 @@ impl Display for Target {
Target::ExternCrate => "extern crate",
Target::Use => "use",
Target::Static => "static item",
Target::StaticMut => "mutable static item",
Target::Const => "constant item",
Target::Fn => "function",
Target::Closure => "closure",
Expand All @@ -69,6 +72,7 @@ impl Target {
hir::ItemKind::ExternCrate(..) => Target::ExternCrate,
hir::ItemKind::Use(..) => Target::Use,
hir::ItemKind::Static(..) => Target::Static,
hir::ItemKind::StaticMut(..) => Target::StaticMut,
hir::ItemKind::Const(..) => Target::Const,
hir::ItemKind::Fn(..) => Target::Fn,
hir::ItemKind::Mod(..) => Target::Mod,
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ pub enum Def<Id = hir::HirId> {
Fn(DefId),
Const(DefId),
ConstParam(DefId),
Static(DefId, bool /* is_mutbl */),
Static(DefId),
StaticMut(DefId),
/// `DefId` refers to the struct or enum variant's constructor.
Ctor(DefId, CtorOf, CtorKind),
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
Expand Down Expand Up @@ -291,7 +292,7 @@ impl<Id> Def<Id> {
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
pub fn opt_def_id(&self) -> Option<DefId> {
match *self {
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
Def::Fn(id) | Def::Mod(id) | Def::Static(id) | Def::StaticMut(id) |
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
Def::TyAlias(id) | Def::TraitAlias(id) |
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
Expand Down Expand Up @@ -329,6 +330,7 @@ impl<Id> Def<Id> {
Def::Fn(..) => "function",
Def::Mod(..) => "module",
Def::Static(..) => "static",
Def::StaticMut(..) => "mutable static",
Def::Enum(..) => "enum",
Def::Variant(..) => "variant",
Def::Ctor(_, CtorOf::Variant, CtorKind::Fn) => "tuple variant",
Expand Down Expand Up @@ -379,7 +381,8 @@ impl<Id> Def<Id> {
match self {
Def::Fn(id) => Def::Fn(id),
Def::Mod(id) => Def::Mod(id),
Def::Static(id, is_mutbl) => Def::Static(id, is_mutbl),
Def::Static(id) => Def::Static(id),
Def::StaticMut(id) => Def::StaticMut(id),
Def::Enum(id) => Def::Enum(id),
Def::Variant(id) => Def::Variant(id),
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
Expand Down
8 changes: 5 additions & 3 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
ItemKind::Use(ref path, _) => {
visitor.visit_use(path, item.hir_id);
}
ItemKind::Static(ref typ, _, body) |
ItemKind::Const(ref typ, body) => {
ItemKind::Const(ref typ, body)
| ItemKind::Static(ref typ, body)
| ItemKind::StaticMut(ref typ, body) => {
visitor.visit_id(item.hir_id);
visitor.visit_ty(typ);
visitor.visit_nested_body(body);
Expand Down Expand Up @@ -725,7 +726,8 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
visitor.visit_ident(param_name);
}
}
ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
ForeignItemKind::Static(ref typ)
| ForeignItemKind::StaticMut(ref typ) => visitor.visit_ty(typ),
ForeignItemKind::Type => (),
}

Expand Down
37 changes: 24 additions & 13 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3031,7 +3031,7 @@ impl<'a> LoweringContext<'a> {

self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
}
ItemKind::Static(ref t, m, ref e) => {
ItemKind::Static(ref t, ref e) => {
let value = self.lower_body(None, |this| this.lower_expr(e));
hir::ItemKind::Static(
self.lower_ty(
Expand All @@ -3042,7 +3042,20 @@ impl<'a> LoweringContext<'a> {
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
}
),
self.lower_mutability(m),
value,
)
}
ItemKind::StaticMut(ref t, ref e) => {
let value = self.lower_body(None, |this| this.lower_expr(e));
hir::ItemKind::StaticMut(
self.lower_ty(
t,
if self.sess.features_untracked().impl_trait_in_bindings {
ImplTraitContext::Existential(None)
} else {
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
}
),
value,
)
}
Expand Down Expand Up @@ -3634,15 +3647,9 @@ impl<'a> LoweringContext<'a> {
ItemKind::MacroDef(..) => SmallVec::new(),
ItemKind::Fn(..) |
ItemKind::Impl(.., None, _, _) => smallvec![i.id],
ItemKind::Static(ref ty, ..) => {
let mut ids = smallvec![i.id];
if self.sess.features_untracked().impl_trait_in_bindings {
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
visitor.visit_ty(ty);
}
ids
},
ItemKind::Const(ref ty, ..) => {
ItemKind::Const(ref ty, _)
| ItemKind::Static(ref ty, _)
| ItemKind::StaticMut(ref ty, _) => {
let mut ids = smallvec![i.id];
if self.sess.features_untracked().impl_trait_in_bindings {
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
Expand Down Expand Up @@ -3740,9 +3747,13 @@ impl<'a> LoweringContext<'a> {

hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
}
ForeignItemKind::Static(ref t, m) => {
ForeignItemKind::Static(ref t) => {
hir::ForeignItemKind::Static(
self.lower_ty(t, ImplTraitContext::disallowed()), m)
self.lower_ty(t, ImplTraitContext::disallowed()))
}
ForeignItemKind::StaticMut(ref t) => {
hir::ForeignItemKind::StaticMut(
self.lower_ty(t, ImplTraitContext::disallowed()))
}
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
)
}
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
DefPathData::ValueNs(i.ident.as_interned_str()),
ItemKind::Const(..)
| ItemKind::Static(..)
| ItemKind::StaticMut(..)
| ItemKind::Fn(..) => DefPathData::ValueNs(i.ident.as_interned_str()),
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
ItemKind::GlobalAsm(..) => DefPathData::Misc,
Expand Down
17 changes: 12 additions & 5 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl<'hir> Entry<'hir> {
Node::Item(item) => {
match item.node {
ItemKind::Const(_, body) |
ItemKind::Static(.., body) |
ItemKind::Static(_, body) |
ItemKind::StaticMut(_, body) |
ItemKind::Fn(_, _, _, body) => Some(body),
_ => None,
}
Expand Down Expand Up @@ -322,7 +323,8 @@ impl<'hir> Map<'hir> {
let def_id = || self.local_def_id_from_hir_id(item.hir_id);

match item.node {
ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
ItemKind::Static(..) => Some(Def::Static(def_id())),
ItemKind::StaticMut(..) => Some(Def::StaticMut(def_id())),
ItemKind::Const(..) => Some(Def::Const(def_id())),
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
Expand All @@ -344,7 +346,8 @@ impl<'hir> Map<'hir> {
let def_id = self.local_def_id_from_hir_id(item.hir_id);
match item.node {
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
ForeignItemKind::Static(_, m) => Some(Def::Static(def_id, m)),
ForeignItemKind::Static(_) => Some(Def::Static(def_id)),
ForeignItemKind::StaticMut(_) => Some(Def::StaticMut(def_id)),
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
}
}
Expand Down Expand Up @@ -527,8 +530,11 @@ impl<'hir> Map<'hir> {
Node::ImplItem(&ImplItem { node: ImplItemKind::Method(..), .. }) => {
BodyOwnerKind::Fn
}
Node::Item(&Item { node: ItemKind::Static(_, m, _), .. }) => {
BodyOwnerKind::Static(m)
Node::Item(&Item { node: ItemKind::Static(..), .. }) => {
BodyOwnerKind::Static
}
Node::Item(&Item { node: ItemKind::StaticMut(..), .. }) => {
BodyOwnerKind::StaticMut
}
Node::Expr(&Expr { node: ExprKind::Closure(..), .. }) => {
BodyOwnerKind::Closure
Expand Down Expand Up @@ -1368,6 +1374,7 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "use",
ItemKind::Static(..) => "static",
ItemKind::StaticMut(..) => "static mut",
ItemKind::Const(..) => "const",
ItemKind::Fn(..) => "fn",
ItemKind::Mod(..) => "mod",
Expand Down
28 changes: 21 additions & 7 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,14 +1318,19 @@ pub enum BodyOwnerKind {
Const,

/// Initializer of a `static` item.
Static(Mutability),
Static,

/// Initializer of a `static mut` item.
StaticMut,
}

impl BodyOwnerKind {
pub fn is_fn_or_closure(self) -> bool {
match self {
BodyOwnerKind::Fn | BodyOwnerKind::Closure => true,
BodyOwnerKind::Const | BodyOwnerKind::Static(_) => false,
BodyOwnerKind::Const
| BodyOwnerKind::Static
| BodyOwnerKind::StaticMut => false,
}
}
}
Expand Down Expand Up @@ -1393,7 +1398,11 @@ impl Expr {
match self.node {
ExprKind::Path(QPath::Resolved(_, ref path)) => {
match path.def {
Def::Local(..) | Def::Upvar(..) | Def::Static(..) | Def::Err => true,
Def::Local(..)
| Def::Upvar(..)
| Def::Static(..)
| Def::StaticMut(..)
| Def::Err => true,
_ => false,
}
}
Expand Down Expand Up @@ -2266,7 +2275,9 @@ pub enum ItemKind {
Use(P<Path>, UseKind),

/// A `static` item
Static(P<Ty>, Mutability, BodyId),
Static(P<Ty>, BodyId),
/// A `static mut` item
StaticMut(P<Ty>, BodyId),
/// A `const` item
Const(P<Ty>, BodyId),
/// A function declaration
Expand Down Expand Up @@ -2308,6 +2319,7 @@ impl ItemKind {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "use",
ItemKind::Static(..) => "static item",
ItemKind::StaticMut(..) => "mutable static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
Expand Down Expand Up @@ -2405,9 +2417,10 @@ pub struct ForeignItem {
pub enum ForeignItemKind {
/// A foreign function.
Fn(P<FnDecl>, HirVec<Ident>, Generics),
/// A foreign static item (`static ext: u8`), with optional mutability
/// (the boolean is true when mutable).
Static(P<Ty>, bool),
/// A foreign static item (`static ext: u8`).
Static(P<Ty>),
/// A foreign mutable static item (`static mut ext: u8`).
StaticMut(P<Ty>),
/// A foreign type.
Type,
}
Expand All @@ -2417,6 +2430,7 @@ impl ForeignItemKind {
match *self {
ForeignItemKind::Fn(..) => "foreign function",
ForeignItemKind::Static(..) => "foreign static item",
ForeignItemKind::StaticMut(..) => "foreign mutable static item",
ForeignItemKind::Type => "foreign type",
}
}
Expand Down
34 changes: 26 additions & 8 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,18 @@ impl<'a> State<'a> {
self.s.word(";")?;
self.end() // end the outer fn box
}
hir::ForeignItemKind::Static(ref t, m) => {
hir::ForeignItemKind::Static(ref t) => {
self.head(visibility_qualified(&item.vis, "static"))?;
if m {
self.word_space("mut")?;
}
self.print_ident(item.ident)?;
self.word_space(":")?;
self.print_type(&t)?;
self.s.word(";")?;
self.end()?; // end the head-ibox
self.end() // end the outer cbox
}
hir::ForeignItemKind::StaticMut(ref t) => {
self.head(visibility_qualified(&item.vis, "static"))?;
self.word_space("mut")?;
self.print_ident(item.ident)?;
self.word_space(":")?;
self.print_type(&t)?;
Expand Down Expand Up @@ -562,11 +569,22 @@ impl<'a> State<'a> {
self.end()?; // end inner head-block
self.end()?; // end outer head-block
}
hir::ItemKind::Static(ref ty, m, expr) => {
hir::ItemKind::Static(ref ty, expr) => {
self.head(visibility_qualified(&item.vis, "static"))?;
if m == hir::MutMutable {
self.word_space("mut")?;
}
self.print_ident(item.ident)?;
self.word_space(":")?;
self.print_type(&ty)?;
self.s.space()?;
self.end()?; // end the head-ibox

self.word_space("=")?;
self.ann.nested(self, Nested::Body(expr))?;
self.s.word(";")?;
self.end()?; // end the outer cbox
}
hir::ItemKind::StaticMut(ref ty, expr) => {
self.head(visibility_qualified(&item.vis, "static"))?;
self.word_space("mut")?;
self.print_ident(item.ident)?;
self.word_space(":")?;
self.print_type(&ty)?;
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
fn should_warn_about_item(&mut self, item: &hir::Item) -> bool {
let should_warn = match item.node {
hir::ItemKind::Static(..)
| hir::ItemKind::StaticMut(..)
| hir::ItemKind::Const(..)
| hir::ItemKind::Fn(..)
| hir::ItemKind::Ty(..)
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
}

Def::Static(def_id, mutbl) => {
Def::Static(def_id)
| Def::StaticMut(def_id) => {
// `#[thread_local]` statics may not outlive the current function, but
// they also cannot be moved out of.
let is_thread_local = self.tcx.get_attrs(def_id)[..]
Expand All @@ -727,7 +728,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
hir_id,
span,
cat,
mutbl: if mutbl { McDeclared } else { McImmutable},
mutbl: if let Def::StaticMut(_) = def { McDeclared } else { McImmutable},
ty:expr_ty,
note: NoteNone
})
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
hir::ItemKind::Existential(..) |
hir::ItemKind::Ty(..) |
hir::ItemKind::Static(..) |
hir::ItemKind::StaticMut(..) |
hir::ItemKind::Mod(..) |
hir::ItemKind::ForeignMod(..) |
hir::ItemKind::Impl(..) |
Expand Down
Loading