Skip to content

Commit b08a557

Browse files
committed
rust-analyzer guided enum variant structification
1 parent 35d06f9 commit b08a557

File tree

16 files changed

+36
-25
lines changed

16 files changed

+36
-25
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,9 @@ pub struct Fn {
28902890
pub body: Option<P<Block>>,
28912891
}
28922892

2893+
#[derive(Clone, Encodable, Decodable, Debug)]
2894+
pub struct Static(pub P<Ty>, pub Mutability, pub Option<P<Expr>>);
2895+
28932896
#[derive(Clone, Encodable, Decodable, Debug)]
28942897
pub enum ItemKind {
28952898
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
@@ -2903,7 +2906,7 @@ pub enum ItemKind {
29032906
/// A static item (`static`).
29042907
///
29052908
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
2906-
Static(P<Ty>, Mutability, Option<P<Expr>>),
2909+
Static(Static),
29072910
/// A constant item (`const`).
29082911
///
29092912
/// E.g., `const FOO: i32 = 42;`.
@@ -2975,7 +2978,7 @@ impl ItemKind {
29752978
match self {
29762979
ItemKind::ExternCrate(..) => "extern crate",
29772980
ItemKind::Use(..) => "`use` import",
2978-
ItemKind::Static(..) => "static item",
2981+
ItemKind::Static(Static(..)) => "static item",
29792982
ItemKind::Const(..) => "constant item",
29802983
ItemKind::Fn(..) => "function",
29812984
ItemKind::Mod(..) => "module",
@@ -3084,7 +3087,7 @@ pub enum ForeignItemKind {
30843087
impl From<ForeignItemKind> for ItemKind {
30853088
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
30863089
match foreign_item_kind {
3087-
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
3090+
ForeignItemKind::Static(a, b, c) => ItemKind::Static(Static(a, b, c)),
30883091
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
30893092
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
30903093
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -3097,7 +3100,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {
30973100

30983101
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
30993102
Ok(match item_kind {
3100-
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
3103+
ItemKind::Static(Static(a, b, c)) => ForeignItemKind::Static(a, b, c),
31013104
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
31023105
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
31033106
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
//! a `MutVisitor` renaming item names in a module will miss all of those
88
//! that are created by the expansion of a macro.
99
10-
use crate::ast::*;
1110
use crate::ptr::P;
1211
use crate::token::{self, Token};
1312
use crate::tokenstream::*;
13+
use crate::{ast::*, Static};
1414

1515
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1616
use rustc_data_structures::sync::Lrc;
@@ -1030,7 +1030,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10301030
match kind {
10311031
ItemKind::ExternCrate(_orig_name) => {}
10321032
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
1033-
ItemKind::Static(ty, _, expr) => {
1033+
ItemKind::Static(Static(ty, _, expr)) => {
10341034
vis.visit_ty(ty);
10351035
visit_opt(expr, |expr| vis.visit_expr(expr));
10361036
}

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! instance, a walker looking for item names in a module will miss all of
1414
//! those that are created by the expansion of a macro.
1515
16-
use crate::ast::*;
16+
use crate::{ast::*, Static};
1717

1818
use rustc_span::symbol::Ident;
1919
use rustc_span::Span;
@@ -305,7 +305,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
305305
match &item.kind {
306306
ItemKind::ExternCrate(_) => {}
307307
ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
308-
ItemKind::Static(typ, _, expr) | ItemKind::Const(_, typ, expr) => {
308+
ItemKind::Static(Static(typ, _, expr)) | ItemKind::Const(_, typ, expr) => {
309309
visitor.visit_ty(typ);
310310
walk_list!(visitor, visit_expr, expr);
311311
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
229229

230230
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
231231
}
232-
ItemKind::Static(t, m, e) => {
232+
ItemKind::Static(ast::Static(t, m, e)) => {
233233
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
234234
hir::ItemKind::Static(ty, *m, body_id)
235235
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use itertools::{Either, Itertools};
1010
use rustc_ast::ptr::P;
1111
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
12-
use rustc_ast::walk_list;
1312
use rustc_ast::*;
13+
use rustc_ast::{walk_list, Static};
1414
use rustc_ast_pretty::pprust::{self, State};
1515
use rustc_data_structures::fx::FxIndexMap;
1616
use rustc_macros::Subdiagnostic;
@@ -990,7 +990,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
990990
replace_span: self.ending_semi_or_hi(item.span),
991991
});
992992
}
993-
ItemKind::Static(.., None) => {
993+
ItemKind::Static(Static(.., None)) => {
994994
self.session.emit_err(errors::StaticWithoutBody {
995995
span: item.span,
996996
replace_span: self.ending_semi_or_hi(item.span),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::pp::Breaks::Inconsistent;
22
use crate::pprust::state::delimited::IterDelimited;
33
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
44

5+
use ast::Static;
56
use rustc_ast as ast;
67
use rustc_ast::GenericBound;
78
use rustc_ast::ModKind;
@@ -156,7 +157,7 @@ impl<'a> State<'a> {
156157
self.print_use_tree(tree);
157158
self.word(";");
158159
}
159-
ast::ItemKind::Static(ty, mutbl, body) => {
160+
ast::ItemKind::Static(Static(ty, mutbl, body)) => {
160161
let def = ast::Defaultness::Final;
161162
self.print_item_const(
162163
item.ident,

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ pub fn expand(
2525
// FIXME - if we get deref patterns, use them to reduce duplication here
2626
let (item, is_stmt, ty_span) =
2727
if let Annotatable::Item(item) = &item
28-
&& let ItemKind::Static(ty, ..) = &item.kind
28+
&& let ItemKind::Static(ast::Static(ty, ..)) = &item.kind
2929
{
3030
(item, false, ecx.with_def_site_ctxt(ty.span))
3131
} else if let Annotatable::Stmt(stmt) = &item
3232
&& let StmtKind::Item(item) = &stmt.kind
33-
&& let ItemKind::Static(ty, ..) = &item.kind
33+
&& let ItemKind::Static(ast::Static(ty, ..)) = &item.kind
3434
{
3535
(item, true, ecx.with_def_site_ctxt(ty.span))
3636
} else {

compiler/rustc_expand/src/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,12 @@ impl<'a> ExtCtxt<'a> {
623623
mutbl: ast::Mutability,
624624
expr: P<ast::Expr>,
625625
) -> P<ast::Item> {
626-
self.item(span, name, AttrVec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
626+
self.item(
627+
span,
628+
name,
629+
AttrVec::new(),
630+
ast::ItemKind::Static(ast::Static(ty, mutbl, Some(expr))),
631+
)
627632
}
628633

629634
pub fn item_const(

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use crate::{
4343
types::{transparent_newtype_field, CItemKind},
4444
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
4545
};
46+
use ast::Static;
4647
use hir::IsAsync;
4748
use rustc_ast::attr;
4849
use rustc_ast::tokenstream::{TokenStream, TokenTree};
@@ -370,7 +371,7 @@ impl EarlyLintPass for UnsafeCode {
370371
}
371372
}
372373

373-
ast::ItemKind::Static(..) => {
374+
ast::ItemKind::Static(Static(..)) => {
374375
if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) {
375376
self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleStatic);
376377
}

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ trait UnusedDelimLint {
805805
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
806806
use ast::ItemKind::*;
807807

808-
if let Const(.., Some(expr)) | Static(.., Some(expr)) = &item.kind {
808+
if let Const(.., Some(expr)) | Static(ast::Static(.., Some(expr))) = &item.kind {
809809
self.check_unused_delims_expr(
810810
cx,
811811
expr,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::errors;
33
use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
44
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
55
use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken};
6+
use ast::Static;
67
use rustc_ast::ast::*;
78
use rustc_ast::ptr::P;
89
use rustc_ast::token::{self, Delimiter, TokenKind};
@@ -227,7 +228,7 @@ impl<'a> Parser<'a> {
227228
self.bump(); // `static`
228229
let m = self.parse_mutability();
229230
let (ident, ty, expr) = self.parse_item_global(Some(m))?;
230-
(ident, ItemKind::Static(ty, m, expr))
231+
(ident, ItemKind::Static(Static(ty, m, expr)))
231232
} else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) {
232233
// CONST ITEM
233234
if self.token.is_keyword(kw::Impl) {
@@ -862,7 +863,7 @@ impl<'a> Parser<'a> {
862863
let kind = match AssocItemKind::try_from(kind) {
863864
Ok(kind) => kind,
864865
Err(kind) => match kind {
865-
ItemKind::Static(a, _, b) => {
866+
ItemKind::Static(Static(a, _, b)) => {
866867
self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span });
867868
AssocItemKind::Const(Defaultness::Final, a, b)
868869
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
688688
}
689689

690690
// These items live in the value namespace.
691-
ItemKind::Static(_, mt, _) => {
691+
ItemKind::Static(ast::Static(_, mt, _)) => {
692692
let res = Res::Def(DefKind::Static(mt), def_id);
693693
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
694694
}

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23462346
});
23472347
}
23482348

2349-
ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => {
2349+
ItemKind::Static(ast::Static(ref ty, _, ref expr)) | ItemKind::Const(_, ref ty, ref expr) => {
23502350
self.with_static_rib(|this| {
23512351
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
23522352
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::msrvs::{self, Msrv};
33
use clippy_utils::source::snippet;
4-
use rustc_ast::ast::{Item, ItemKind, Ty, TyKind};
4+
use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -106,7 +106,7 @@ impl EarlyLintPass for RedundantStaticLifetimes {
106106
// #2438)
107107
}
108108

109-
if let ItemKind::Static(ref var_type, _, _) = item.kind {
109+
if let ItemKind::Static(Static(ref var_type, _, _)) = item.kind {
110110
Self::visit_type(var_type, cx, "statics have by default a `'static` lifetime");
111111
}
112112
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
286286
match (l, r) {
287287
(ExternCrate(l), ExternCrate(r)) => l == r,
288288
(Use(l), Use(r)) => eq_use_tree(l, r),
289-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
289+
(Static(ast::Static(lt, lm, le)), Static(ast::Static(rt, rm, re))) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
290290
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
291291
(
292292
Fn(box ast::Fn {

src/tools/rustfmt/src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ pub(crate) struct StaticParts<'a> {
18051805
impl<'a> StaticParts<'a> {
18061806
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
18071807
let (defaultness, prefix, ty, mutability, expr) = match item.kind {
1808-
ast::ItemKind::Static(ref ty, mutability, ref expr) => {
1808+
ast::ItemKind::Static(ast::Static(ref ty, mutability, ref expr)) => {
18091809
(None, "static", ty, mutability, expr)
18101810
}
18111811
ast::ItemKind::Const(defaultness, ref ty, ref expr) => {

0 commit comments

Comments
 (0)