Skip to content

Commit

Permalink
XXX: Static
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Aug 10, 2022
1 parent 300d74d commit c0a3752
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 50 deletions.
19 changes: 13 additions & 6 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,13 @@ pub struct TraitAlias {
pub bounds: GenericBounds,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Static {
pub ty: P<Ty>,
pub mutbl: Mutability,
pub expr: Option<P<Expr>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Const {
pub defaultness: Defaultness,
Expand All @@ -2842,7 +2849,7 @@ pub enum ItemKind {
/// A static item (`static`).
///
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
Static(P<Ty>, Mutability, Option<P<Expr>>),
Static(Box<Static>),
/// A constant item (`const`).
///
/// E.g., `const FOO: i32 = 42;`.
Expand Down Expand Up @@ -3011,7 +3018,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`).
Static(P<Ty>, Mutability, Option<P<Expr>>),
Static(Box<Static>),
/// An foreign function.
Fn(Box<Fn>),
/// An foreign type.
Expand All @@ -3023,7 +3030,7 @@ pub enum ForeignItemKind {
impl From<ForeignItemKind> for ItemKind {
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
match foreign_item_kind {
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
ForeignItemKind::Static(static_) => ItemKind::Static(static_),
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
Expand All @@ -3036,7 +3043,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {

fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
Ok(match item_kind {
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
ItemKind::Static(static_) => ForeignItemKind::Static(static_),
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
Expand All @@ -3059,8 +3066,8 @@ mod size_asserts {
static_assert_size!(Block, 48);
static_assert_size!(Expr, 104);
static_assert_size!(Fn, 192);
static_assert_size!(ForeignItem, 112);
static_assert_size!(ForeignItemKind, 24);
static_assert_size!(ForeignItem, 104);
static_assert_size!(ForeignItemKind, 16);
static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 72);
static_assert_size!(Impl, 200);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
match kind {
ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
ItemKind::Static(ty, _, expr) => {
ItemKind::Static(box Static { ty, mutbl: _, expr }) => {
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
Expand Down Expand Up @@ -1178,7 +1178,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
ForeignItemKind::Static(ty, _, expr) => {
ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => {
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
match item.kind {
ItemKind::ExternCrate(_) => {}
ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
ItemKind::Static(ref ty, _, ref expr)
ItemKind::Static(box Static { ref ty, mutbl: _, ref expr })
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
Expand Down Expand Up @@ -560,7 +560,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
ForeignItemKind::Static(ty, _, expr) => {
ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ impl<'hir> LoweringContext<'_, 'hir> {

self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
}
ItemKind::Static(ref t, m, ref e) => {
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
hir::ItemKind::Static(ty, m, body_id)
ItemKind::Static(box Static { ref ty, mutbl, ref expr }) => {
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
hir::ItemKind::Static(ty, mutbl, body_id)
}
ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
Expand Down Expand Up @@ -657,10 +657,10 @@ impl<'hir> LoweringContext<'_, 'hir> {

hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
}
ForeignItemKind::Static(ref t, m, _) => {
ForeignItemKind::Static(box Static { ref ty, mutbl, .. }) => {
let ty =
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ForeignItemKind::Static(ty, m)
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ForeignItemKind::Static(ty, mutbl)
}
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
let msg = "free constant item without body";
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
}
ItemKind::Static(.., None) => {
ItemKind::Static(box Static { expr: None, .. }) => {
let msg = "free static item without body";
self.error_item_without_body(item.span, "static", msg, " = <expr>;");
}
Expand Down Expand Up @@ -1390,8 +1390,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.check_foreign_ty_genericless(generics, where_clauses.0.1);
self.check_foreign_item_ascii_only(fi.ident);
}
ForeignItemKind::Static(_, _, body) => {
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
ForeignItemKind::Static(box Static { expr, .. }) => {
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|e| e.span));
self.check_foreign_item_ascii_only(fi.ident);
}
ForeignItemKind::MacCall(..) => {}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl<'a> State<'a> {
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
}
ast::ForeignItemKind::Static(ty, mutbl, body) => {
let def = ast::Defaultness::Final;
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
ast::ForeignItemKind::Static(box ast::Static { ty, mutbl, expr }) => {
let defaultness = ast::Defaultness::Final;
self.print_item_const(ident, Some(*mutbl), ty, expr.as_deref(), vis, defaultness);
}
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
defaultness,
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<'a> State<'a> {
self.print_use_tree(tree);
self.word(";");
}
ast::ItemKind::Static(ref ty, mutbl, ref expr) => {
ast::ItemKind::Static(box ast::Static { ref ty, mutbl, ref expr }) => {
let defaultness = ast::Defaultness::Final;
self.print_item_const(
item.ident,
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_ast::expand::allocator::{
};
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
use rustc_ast::{Fn, ItemKind, Mutability, Static, Stmt, Ty, TyKind, Unsafe};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
Expand All @@ -28,12 +28,16 @@ pub fn expand(
// FIXME - if we get deref patterns, use them to reduce duplication here
let (item, is_stmt, ty_span) = match &item {
Annotatable::Item(item) => match item.kind {
ItemKind::Static(ref ty, ..) => (item, false, ecx.with_def_site_ctxt(ty.span)),
ItemKind::Static(box Static { ref ty, .. }) => {
(item, false, ecx.with_def_site_ctxt(ty.span))
}
_ => return not_static(),
},
Annotatable::Stmt(stmt) => match &stmt.kind {
StmtKind::Item(item_) => match item_.kind {
ItemKind::Static(ref ty, ..) => (item_, true, ecx.with_def_site_ctxt(ty.span)),
ItemKind::Static(box Static { ref ty, .. }) => {
(item_, true, ecx.with_def_site_ctxt(ty.span))
}
_ => return not_static(),
},
_ => return not_static(),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ pub fn expand_test_or_bench(
field("testfn", test_fn), // }
],
), // }
)},
),
));
),
})),
);
test_const = test_const.map(|mut tc| {
tc.vis.kind = ast::VisibilityKind::Public;
tc
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,12 @@ impl<'a> ExtCtxt<'a> {
mutbl: ast::Mutability,
expr: P<ast::Expr>,
) -> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
self.item(
span,
name,
Vec::new(),
ast::ItemKind::Static(Box::new(ast::Static { ty, mutbl, expr: Some(expr) })),
)
}

pub fn item_const(
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ trait UnusedDelimLint {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
use ast::ItemKind::*;

if let Const(box ast::Const { expr: Some(expr), .. }) | Static(.., Some(expr)) = &item.kind
if let Const(box ast::Const { expr: Some(expr), .. })
| Static(box ast::Static { expr: Some(expr), .. }) = &item.kind
{
self.check_unused_delims_expr(
cx,
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ impl<'a> Parser<'a> {
} else if self.is_static_global() {
// STATIC ITEM
self.bump(); // `static`
let m = self.parse_mutability();
let (ident, ty, expr) = self.parse_item_global(Some(m))?;
(ident, ItemKind::Static(ty, m, expr))
let mutbl = self.parse_mutability();
let (ident, ty, expr) = self.parse_item_global(Some(mutbl))?;
(ident, ItemKind::Static(Box::new(Static { ty, mutbl, expr })))
} else if let Constness::Yes(const_span) = self.parse_constness() {
// CONST ITEM
if self.token.is_keyword(kw::Impl) {
Expand Down Expand Up @@ -822,7 +822,7 @@ impl<'a> Parser<'a> {
let kind = match AssocItemKind::try_from(kind) {
Ok(kind) => kind,
Err(kind) => match kind {
ItemKind::Static(ty, _, expr) => {
ItemKind::Static(box Static { ty, expr, .. }) => {
self.struct_span_err(span, "associated `static` items are not allowed")
.emit();
AssocItemKind::Const(Box::new(Const {
Expand Down Expand Up @@ -1064,7 +1064,11 @@ impl<'a> Parser<'a> {
Err(kind) => match kind {
ItemKind::Const(box Const { ty, expr, .. }) => {
self.error_on_foreign_const(span, ident);
ForeignItemKind::Static(ty, Mutability::Not, expr)
ForeignItemKind::Static(Box::new(Static {
ty,
mutbl: Mutability::Not,
expr,
}))
}
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
},
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};

use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, Static, StmtKind};
use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId, Struct};
use rustc_attr as attr;
use rustc_data_structures::sync::Lrc;
Expand Down Expand Up @@ -712,8 +712,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

// These items live in the value namespace.
ItemKind::Static(_, mt, _) => {
let res = Res::Def(DefKind::Static(mt), def_id);
ItemKind::Static(box Static { mutbl, .. }) => {
let res = Res::Def(DefKind::Static(mutbl), def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Const(..) => {
Expand Down Expand Up @@ -918,7 +918,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let def_id = local_def_id.to_def_id();
let (def_kind, ns) = match item.kind {
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS),
ForeignItemKind::Static(box Static { mutbl, .. }) => (DefKind::Static(mutbl), ValueNS),
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
ForeignItemKind::MacCall(_) => unreachable!(),
};
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
});
}

ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
ItemKind::Static(box Static { ref ty, ref expr, .. })
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
self.with_item_rib(|this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
this.visit_ty(ty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{meets_msrv, msrvs};
use rustc_ast::ast::{Const, Item, ItemKind, Ty, TyKind};
use rustc_ast::ast::{Const, Item, ItemKind, Static, Ty, TyKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_semver::RustcVersion;
Expand Down Expand Up @@ -107,7 +107,7 @@ impl EarlyLintPass for RedundantStaticLifetimes {
// #2438)
}

if let ItemKind::Static(ref ty, _, _) = item.kind {
if let ItemKind::Static(box Static { ref ty, .. }) = item.kind {
self.visit_type(ty, cx, "statics have by default a `'static` lifetime");
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/tools/clippy/clippy_utils/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
match (l, r) {
(ExternCrate(l), ExternCrate(r)) => l == r,
(Use(l), Use(r)) => eq_use_tree(l, r),
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(
Static(box ast::Static { ty: lt, mutbl: lm, expr: le }),
Static(box ast::Static { ty: rt, mutbl: rm, expr: re })
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(
Const(box ast::Const { defaultness: ld, ty: lt, expr: le }),
Const(box ast::Const { defaultness: rd, ty: rt, expr: re })
Expand Down Expand Up @@ -387,7 +390,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
use ForeignItemKind::*;
match (l, r) {
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(
Static(box ast::Static { ty: lt, mutbl: lm, expr: le }),
Static(box ast::Static { ty: rt, mutbl: rm, expr: re })
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(
Fn(box ast::Fn {
defaultness: ld,
Expand Down
10 changes: 5 additions & 5 deletions src/tools/rustfmt/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,8 +1800,8 @@ pub(crate) struct StaticParts<'a> {
impl<'a> StaticParts<'a> {
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
let (defaultness, prefix, ty, mutability, expr) = match item.kind {
ast::ItemKind::Static(ref ty, mutability, ref expr) => {
(None, "static", ty, mutability, expr)
ast::ItemKind::Static(ref static_) => {
(None, "static", &static_.ty, static_.mutbl, &static_.expr)
}
ast::ItemKind::Const(ref const_) => {
(Some(const_.defaultness), "const", &const_.ty, ast::Mutability::Not, &const_.expr)
Expand Down Expand Up @@ -3204,11 +3204,11 @@ impl Rewrite for ast::ForeignItem {
.map(|(s, _, _)| format!("{};", s))
}
}
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
ast::ForeignItemKind::Static(ref static_) => {
// FIXME(#21): we're dropping potential comments in between the
// function kw here.
let vis = format_visibility(context, &self.vis);
let mut_str = format_mutability(mutability);
let mut_str = format_mutability(static_.mutbl);
let prefix = format!(
"{}static {}{}:",
vis,
Expand All @@ -3219,7 +3219,7 @@ impl Rewrite for ast::ForeignItem {
rewrite_assign_rhs(
context,
prefix,
&**ty,
&*static_.ty,
&RhsAssignKind::Ty,
shape.sub_width(1)?,
)
Expand Down

0 comments on commit c0a3752

Please sign in to comment.