Skip to content

Commit c0a3752

Browse files
committed
XXX: Static
1 parent 300d74d commit c0a3752

File tree

16 files changed

+78
-50
lines changed

16 files changed

+78
-50
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,13 @@ pub struct TraitAlias {
28222822
pub bounds: GenericBounds,
28232823
}
28242824

2825+
#[derive(Clone, Encodable, Decodable, Debug)]
2826+
pub struct Static {
2827+
pub ty: P<Ty>,
2828+
pub mutbl: Mutability,
2829+
pub expr: Option<P<Expr>>,
2830+
}
2831+
28252832
#[derive(Clone, Encodable, Decodable, Debug)]
28262833
pub struct Const {
28272834
pub defaultness: Defaultness,
@@ -2842,7 +2849,7 @@ pub enum ItemKind {
28422849
/// A static item (`static`).
28432850
///
28442851
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
2845-
Static(P<Ty>, Mutability, Option<P<Expr>>),
2852+
Static(Box<Static>),
28462853
/// A constant item (`const`).
28472854
///
28482855
/// E.g., `const FOO: i32 = 42;`.
@@ -3011,7 +3018,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
30113018
#[derive(Clone, Encodable, Decodable, Debug)]
30123019
pub enum ForeignItemKind {
30133020
/// A foreign static item (`static FOO: u8`).
3014-
Static(P<Ty>, Mutability, Option<P<Expr>>),
3021+
Static(Box<Static>),
30153022
/// An foreign function.
30163023
Fn(Box<Fn>),
30173024
/// An foreign type.
@@ -3023,7 +3030,7 @@ pub enum ForeignItemKind {
30233030
impl From<ForeignItemKind> for ItemKind {
30243031
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
30253032
match foreign_item_kind {
3026-
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
3033+
ForeignItemKind::Static(static_) => ItemKind::Static(static_),
30273034
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
30283035
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
30293036
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -3036,7 +3043,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {
30363043

30373044
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
30383045
Ok(match item_kind {
3039-
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
3046+
ItemKind::Static(static_) => ForeignItemKind::Static(static_),
30403047
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
30413048
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
30423049
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
@@ -3059,8 +3066,8 @@ mod size_asserts {
30593066
static_assert_size!(Block, 48);
30603067
static_assert_size!(Expr, 104);
30613068
static_assert_size!(Fn, 192);
3062-
static_assert_size!(ForeignItem, 112);
3063-
static_assert_size!(ForeignItemKind, 24);
3069+
static_assert_size!(ForeignItem, 104);
3070+
static_assert_size!(ForeignItemKind, 16);
30643071
static_assert_size!(GenericBound, 88);
30653072
static_assert_size!(Generics, 72);
30663073
static_assert_size!(Impl, 200);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10101010
match kind {
10111011
ItemKind::ExternCrate(_orig_name) => {}
10121012
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
1013-
ItemKind::Static(ty, _, expr) => {
1013+
ItemKind::Static(box Static { ty, mutbl: _, expr }) => {
10141014
vis.visit_ty(ty);
10151015
visit_opt(expr, |expr| vis.visit_expr(expr));
10161016
}
@@ -1178,7 +1178,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
11781178
visitor.visit_vis(vis);
11791179
visit_attrs(attrs, visitor);
11801180
match kind {
1181-
ForeignItemKind::Static(ty, _, expr) => {
1181+
ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => {
11821182
visitor.visit_ty(ty);
11831183
visit_opt(expr, |expr| visitor.visit_expr(expr));
11841184
}

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
299299
match item.kind {
300300
ItemKind::ExternCrate(_) => {}
301301
ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
302-
ItemKind::Static(ref ty, _, ref expr)
302+
ItemKind::Static(box Static { ref ty, mutbl: _, ref expr })
303303
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
304304
visitor.visit_ty(ty);
305305
walk_list!(visitor, visit_expr, expr);
@@ -560,7 +560,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
560560
visitor.visit_ident(ident);
561561
walk_list!(visitor, visit_attribute, attrs);
562562
match kind {
563-
ForeignItemKind::Static(ty, _, expr) => {
563+
ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => {
564564
visitor.visit_ty(ty);
565565
walk_list!(visitor, visit_expr, expr);
566566
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
234234

235235
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
236236
}
237-
ItemKind::Static(ref t, m, ref e) => {
238-
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
239-
hir::ItemKind::Static(ty, m, body_id)
237+
ItemKind::Static(box Static { ref ty, mutbl, ref expr }) => {
238+
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
239+
hir::ItemKind::Static(ty, mutbl, body_id)
240240
}
241241
ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
242242
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
@@ -657,10 +657,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
657657

658658
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
659659
}
660-
ForeignItemKind::Static(ref t, m, _) => {
660+
ForeignItemKind::Static(box Static { ref ty, mutbl, .. }) => {
661661
let ty =
662-
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
663-
hir::ForeignItemKind::Static(ty, m)
662+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
663+
hir::ForeignItemKind::Static(ty, mutbl)
664664
}
665665
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
666666
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13341334
let msg = "free constant item without body";
13351335
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
13361336
}
1337-
ItemKind::Static(.., None) => {
1337+
ItemKind::Static(box Static { expr: None, .. }) => {
13381338
let msg = "free static item without body";
13391339
self.error_item_without_body(item.span, "static", msg, " = <expr>;");
13401340
}
@@ -1390,8 +1390,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13901390
self.check_foreign_ty_genericless(generics, where_clauses.0.1);
13911391
self.check_foreign_item_ascii_only(fi.ident);
13921392
}
1393-
ForeignItemKind::Static(_, _, body) => {
1394-
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1393+
ForeignItemKind::Static(box Static { expr, .. }) => {
1394+
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|e| e.span));
13951395
self.check_foreign_item_ascii_only(fi.ident);
13961396
}
13971397
ForeignItemKind::MacCall(..) => {}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl<'a> State<'a> {
2929
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
3030
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
3131
}
32-
ast::ForeignItemKind::Static(ty, mutbl, body) => {
33-
let def = ast::Defaultness::Final;
34-
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
32+
ast::ForeignItemKind::Static(box ast::Static { ty, mutbl, expr }) => {
33+
let defaultness = ast::Defaultness::Final;
34+
self.print_item_const(ident, Some(*mutbl), ty, expr.as_deref(), vis, defaultness);
3535
}
3636
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
3737
defaultness,
@@ -156,7 +156,7 @@ impl<'a> State<'a> {
156156
self.print_use_tree(tree);
157157
self.word(";");
158158
}
159-
ast::ItemKind::Static(ref ty, mutbl, ref expr) => {
159+
ast::ItemKind::Static(box ast::Static { ref ty, mutbl, ref expr }) => {
160160
let defaultness = ast::Defaultness::Final;
161161
self.print_item_const(
162162
item.ident,

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::expand::allocator::{
55
};
66
use rustc_ast::ptr::P;
77
use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
8-
use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
8+
use rustc_ast::{Fn, ItemKind, Mutability, Static, Stmt, Ty, TyKind, Unsafe};
99
use rustc_expand::base::{Annotatable, ExtCtxt};
1010
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1111
use rustc_span::Span;
@@ -28,12 +28,16 @@ pub fn expand(
2828
// FIXME - if we get deref patterns, use them to reduce duplication here
2929
let (item, is_stmt, ty_span) = match &item {
3030
Annotatable::Item(item) => match item.kind {
31-
ItemKind::Static(ref ty, ..) => (item, false, ecx.with_def_site_ctxt(ty.span)),
31+
ItemKind::Static(box Static { ref ty, .. }) => {
32+
(item, false, ecx.with_def_site_ctxt(ty.span))
33+
}
3234
_ => return not_static(),
3335
},
3436
Annotatable::Stmt(stmt) => match &stmt.kind {
3537
StmtKind::Item(item_) => match item_.kind {
36-
ItemKind::Static(ref ty, ..) => (item_, true, ecx.with_def_site_ctxt(ty.span)),
38+
ItemKind::Static(box Static { ref ty, .. }) => {
39+
(item_, true, ecx.with_def_site_ctxt(ty.span))
40+
}
3741
_ => return not_static(),
3842
},
3943
_ => return not_static(),

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ pub fn expand_test_or_bench(
325325
field("testfn", test_fn), // }
326326
],
327327
), // }
328-
)},
329-
),
330-
));
328+
),
329+
})),
330+
);
331331
test_const = test_const.map(|mut tc| {
332332
tc.vis.kind = ast::VisibilityKind::Public;
333333
tc

compiler/rustc_expand/src/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,12 @@ impl<'a> ExtCtxt<'a> {
592592
mutbl: ast::Mutability,
593593
expr: P<ast::Expr>,
594594
) -> P<ast::Item> {
595-
self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
595+
self.item(
596+
span,
597+
name,
598+
Vec::new(),
599+
ast::ItemKind::Static(Box::new(ast::Static { ty, mutbl, expr: Some(expr) })),
600+
)
596601
}
597602

598603
pub fn item_const(

compiler/rustc_lint/src/unused.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ trait UnusedDelimLint {
657657
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
658658
use ast::ItemKind::*;
659659

660-
if let Const(box ast::Const { expr: Some(expr), .. }) | Static(.., Some(expr)) = &item.kind
660+
if let Const(box ast::Const { expr: Some(expr), .. })
661+
| Static(box ast::Static { expr: Some(expr), .. }) = &item.kind
661662
{
662663
self.check_unused_delims_expr(
663664
cx,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ impl<'a> Parser<'a> {
225225
} else if self.is_static_global() {
226226
// STATIC ITEM
227227
self.bump(); // `static`
228-
let m = self.parse_mutability();
229-
let (ident, ty, expr) = self.parse_item_global(Some(m))?;
230-
(ident, ItemKind::Static(ty, m, expr))
228+
let mutbl = self.parse_mutability();
229+
let (ident, ty, expr) = self.parse_item_global(Some(mutbl))?;
230+
(ident, ItemKind::Static(Box::new(Static { ty, mutbl, expr })))
231231
} else if let Constness::Yes(const_span) = self.parse_constness() {
232232
// CONST ITEM
233233
if self.token.is_keyword(kw::Impl) {
@@ -822,7 +822,7 @@ impl<'a> Parser<'a> {
822822
let kind = match AssocItemKind::try_from(kind) {
823823
Ok(kind) => kind,
824824
Err(kind) => match kind {
825-
ItemKind::Static(ty, _, expr) => {
825+
ItemKind::Static(box Static { ty, expr, .. }) => {
826826
self.struct_span_err(span, "associated `static` items are not allowed")
827827
.emit();
828828
AssocItemKind::Const(Box::new(Const {
@@ -1064,7 +1064,11 @@ impl<'a> Parser<'a> {
10641064
Err(kind) => match kind {
10651065
ItemKind::Const(box Const { ty, expr, .. }) => {
10661066
self.error_on_foreign_const(span, ident);
1067-
ForeignItemKind::Static(ty, Mutability::Not, expr)
1067+
ForeignItemKind::Static(Box::new(Static {
1068+
ty,
1069+
mutbl: Mutability::Not,
1070+
expr,
1071+
}))
10681072
}
10691073
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
10701074
},

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
1717

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

714714
// These items live in the value namespace.
715-
ItemKind::Static(_, mt, _) => {
716-
let res = Res::Def(DefKind::Static(mt), def_id);
715+
ItemKind::Static(box Static { mutbl, .. }) => {
716+
let res = Res::Def(DefKind::Static(mutbl), def_id);
717717
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
718718
}
719719
ItemKind::Const(..) => {
@@ -918,7 +918,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
918918
let def_id = local_def_id.to_def_id();
919919
let (def_kind, ns) = match item.kind {
920920
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
921-
ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS),
921+
ForeignItemKind::Static(box Static { mutbl, .. }) => (DefKind::Static(mutbl), ValueNS),
922922
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
923923
ForeignItemKind::MacCall(_) => unreachable!(),
924924
};

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22352235
});
22362236
}
22372237

2238-
ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
2238+
ItemKind::Static(box Static { ref ty, ref expr, .. })
2239+
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
22392240
self.with_item_rib(|this| {
22402241
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
22412242
this.visit_ty(ty);

src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use clippy_utils::{meets_msrv, msrvs};
4-
use rustc_ast::ast::{Const, Item, ItemKind, Ty, TyKind};
4+
use rustc_ast::ast::{Const, Item, ItemKind, Static, Ty, TyKind};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_semver::RustcVersion;
@@ -107,7 +107,7 @@ impl EarlyLintPass for RedundantStaticLifetimes {
107107
// #2438)
108108
}
109109

110-
if let ItemKind::Static(ref ty, _, _) = item.kind {
110+
if let ItemKind::Static(box Static { ref ty, .. }) = item.kind {
111111
self.visit_type(ty, cx, "statics have by default a `'static` lifetime");
112112
}
113113
}

src/tools/clippy/clippy_utils/src/ast_utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
253253
match (l, r) {
254254
(ExternCrate(l), ExternCrate(r)) => l == r,
255255
(Use(l), Use(r)) => eq_use_tree(l, r),
256-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
256+
(
257+
Static(box ast::Static { ty: lt, mutbl: lm, expr: le }),
258+
Static(box ast::Static { ty: rt, mutbl: rm, expr: re })
259+
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
257260
(
258261
Const(box ast::Const { defaultness: ld, ty: lt, expr: le }),
259262
Const(box ast::Const { defaultness: rd, ty: rt, expr: re })
@@ -387,7 +390,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
387390
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
388391
use ForeignItemKind::*;
389392
match (l, r) {
390-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
393+
(
394+
Static(box ast::Static { ty: lt, mutbl: lm, expr: le }),
395+
Static(box ast::Static { ty: rt, mutbl: rm, expr: re })
396+
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
391397
(
392398
Fn(box ast::Fn {
393399
defaultness: ld,

src/tools/rustfmt/src/items.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,8 +1800,8 @@ pub(crate) struct StaticParts<'a> {
18001800
impl<'a> StaticParts<'a> {
18011801
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
18021802
let (defaultness, prefix, ty, mutability, expr) = match item.kind {
1803-
ast::ItemKind::Static(ref ty, mutability, ref expr) => {
1804-
(None, "static", ty, mutability, expr)
1803+
ast::ItemKind::Static(ref static_) => {
1804+
(None, "static", &static_.ty, static_.mutbl, &static_.expr)
18051805
}
18061806
ast::ItemKind::Const(ref const_) => {
18071807
(Some(const_.defaultness), "const", &const_.ty, ast::Mutability::Not, &const_.expr)
@@ -3204,11 +3204,11 @@ impl Rewrite for ast::ForeignItem {
32043204
.map(|(s, _, _)| format!("{};", s))
32053205
}
32063206
}
3207-
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
3207+
ast::ForeignItemKind::Static(ref static_) => {
32083208
// FIXME(#21): we're dropping potential comments in between the
32093209
// function kw here.
32103210
let vis = format_visibility(context, &self.vis);
3211-
let mut_str = format_mutability(mutability);
3211+
let mut_str = format_mutability(static_.mutbl);
32123212
let prefix = format!(
32133213
"{}static {}{}:",
32143214
vis,
@@ -3219,7 +3219,7 @@ impl Rewrite for ast::ForeignItem {
32193219
rewrite_assign_rhs(
32203220
context,
32213221
prefix,
3222-
&**ty,
3222+
&*static_.ty,
32233223
&RhsAssignKind::Ty,
32243224
shape.sub_width(1)?,
32253225
)

0 commit comments

Comments
 (0)