Skip to content

Commit ec74653

Browse files
committed
Split out ast::ItemKind::Const into its own struct
1 parent e382877 commit ec74653

File tree

14 files changed

+89
-61
lines changed

14 files changed

+89
-61
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,6 +2897,13 @@ pub struct Static {
28972897
pub expr: Option<P<Expr>>,
28982898
}
28992899

2900+
#[derive(Clone, Encodable, Decodable, Debug)]
2901+
pub struct ConstItem {
2902+
pub defaultness: Defaultness,
2903+
pub ty: P<Ty>,
2904+
pub expr: Option<P<Expr>>,
2905+
}
2906+
29002907
#[derive(Clone, Encodable, Decodable, Debug)]
29012908
pub enum ItemKind {
29022909
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
@@ -2914,7 +2921,7 @@ pub enum ItemKind {
29142921
/// A constant item (`const`).
29152922
///
29162923
/// E.g., `const FOO: i32 = 42;`.
2917-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
2924+
Const(ConstItem),
29182925
/// A function declaration (`fn`).
29192926
///
29202927
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -3030,7 +3037,7 @@ pub type AssocItem = Item<AssocItemKind>;
30303037
pub enum AssocItemKind {
30313038
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
30323039
/// If `def` is parsed, then the constant is provided, and otherwise required.
3033-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
3040+
Const(ConstItem),
30343041
/// An associated function.
30353042
Fn(Box<Fn>),
30363043
/// An associated type.
@@ -3042,7 +3049,7 @@ pub enum AssocItemKind {
30423049
impl AssocItemKind {
30433050
pub fn defaultness(&self) -> Defaultness {
30443051
match *self {
3045-
Self::Const(defaultness, ..)
3052+
Self::Const(ConstItem { defaultness, .. })
30463053
| Self::Fn(box Fn { defaultness, .. })
30473054
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
30483055
Self::MacCall(..) => Defaultness::Final,
@@ -3053,7 +3060,7 @@ impl AssocItemKind {
30533060
impl From<AssocItemKind> for ItemKind {
30543061
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
30553062
match assoc_item_kind {
3056-
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
3063+
AssocItemKind::Const(item) => ItemKind::Const(item),
30573064
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
30583065
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
30593066
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -3066,7 +3073,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
30663073

30673074
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
30683075
Ok(match item_kind {
3069-
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
3076+
ItemKind::Const(item) => AssocItemKind::Const(item),
30703077
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
30713078
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
30723079
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,10 +1034,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10341034
vis.visit_ty(ty);
10351035
visit_opt(expr, |expr| vis.visit_expr(expr));
10361036
}
1037-
ItemKind::Const(defaultness, ty, expr) => {
1038-
visit_defaultness(defaultness, vis);
1039-
vis.visit_ty(ty);
1040-
visit_opt(expr, |expr| vis.visit_expr(expr));
1037+
ItemKind::Const(item) => {
1038+
visit_const_item(item, vis);
10411039
}
10421040
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
10431041
visit_defaultness(defaultness, vis);
@@ -1120,10 +1118,8 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11201118
visitor.visit_vis(vis);
11211119
visit_attrs(attrs, visitor);
11221120
match kind {
1123-
AssocItemKind::Const(defaultness, ty, expr) => {
1124-
visit_defaultness(defaultness, visitor);
1125-
visitor.visit_ty(ty);
1126-
visit_opt(expr, |expr| visitor.visit_expr(expr));
1121+
AssocItemKind::Const(item) => {
1122+
visit_const_item(item, visitor);
11271123
}
11281124
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
11291125
visit_defaultness(defaultness, visitor);
@@ -1153,6 +1149,15 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11531149
smallvec![item]
11541150
}
11551151

1152+
fn visit_const_item<T: MutVisitor>(
1153+
ConstItem { defaultness, ty, expr }: &mut ConstItem,
1154+
visitor: &mut T,
1155+
) {
1156+
visit_defaultness(defaultness, visitor);
1157+
visitor.visit_ty(ty);
1158+
visit_opt(expr, |expr| visitor.visit_expr(expr));
1159+
}
1160+
11561161
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
11571162
let FnHeader { unsafety, asyncness, constness, ext: _ } = header;
11581163
visit_constness(constness, vis);

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ 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(Static { ty: typ, mutability: _, expr })
309-
| ItemKind::Const(_, typ, expr) => {
310-
visitor.visit_ty(typ);
308+
ItemKind::Static(Static { ty, mutability: _, expr })
309+
| ItemKind::Const(ConstItem { ty, expr, .. }) => {
310+
visitor.visit_ty(ty);
311311
walk_list!(visitor, visit_expr, expr);
312312
}
313313
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
@@ -675,7 +675,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
675675
visitor.visit_ident(ident);
676676
walk_list!(visitor, visit_attribute, attrs);
677677
match kind {
678-
AssocItemKind::Const(_, ty, expr) => {
678+
AssocItemKind::Const(ConstItem { ty, expr, .. }) => {
679679
visitor.visit_ty(ty);
680680
walk_list!(visitor, visit_expr, expr);
681681
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
233233
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
234234
hir::ItemKind::Static(ty, *m, body_id)
235235
}
236-
ItemKind::Const(_, t, e) => {
237-
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
236+
ItemKind::Const(ast::ConstItem { ty, expr, .. }) => {
237+
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
238238
hir::ItemKind::Const(ty, body_id)
239239
}
240240
ItemKind::Fn(box Fn {
@@ -708,10 +708,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
708708
let trait_item_def_id = hir_id.expect_owner();
709709

710710
let (generics, kind, has_default) = match &i.kind {
711-
AssocItemKind::Const(_, ty, default) => {
711+
AssocItemKind::Const(ConstItem { ty, expr, .. }) => {
712712
let ty =
713713
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
714-
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
714+
let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
715715
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
716716
}
717717
AssocItemKind::Fn(box Fn { sig, generics, body: None, .. }) => {
@@ -809,7 +809,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
809809
self.lower_attrs(hir_id, &i.attrs);
810810

811811
let (generics, kind) = match &i.kind {
812-
AssocItemKind::Const(_, ty, expr) => {
812+
AssocItemKind::Const(ConstItem { ty, expr, .. }) => {
813813
let ty =
814814
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
815815
(

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
983983
self.err_handler().emit_err(errors::FieldlessUnion { span: item.span });
984984
}
985985
}
986-
ItemKind::Const(def, .., None) => {
987-
self.check_defaultness(item.span, *def);
986+
ItemKind::Const(ConstItem { defaultness, expr: None, .. }) => {
987+
self.check_defaultness(item.span, *defaultness);
988988
self.session.emit_err(errors::ConstWithoutBody {
989989
span: item.span,
990990
replace_span: self.ending_semi_or_hi(item.span),
@@ -1259,13 +1259,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12591259

12601260
if ctxt == AssocCtxt::Impl {
12611261
match &item.kind {
1262-
AssocItemKind::Const(_, _, body) => {
1263-
if body.is_none() {
1264-
self.session.emit_err(errors::AssocConstWithoutBody {
1265-
span: item.span,
1266-
replace_span: self.ending_semi_or_hi(item.span),
1267-
});
1268-
}
1262+
AssocItemKind::Const(ConstItem { expr: None, .. }) => {
1263+
self.session.emit_err(errors::AssocConstWithoutBody {
1264+
span: item.span,
1265+
replace_span: self.ending_semi_or_hi(item.span),
1266+
});
12691267
}
12701268
AssocItemKind::Fn(box Fn { body, .. }) => {
12711269
if body.is_none() {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,15 @@ impl<'a> State<'a> {
168168
def,
169169
);
170170
}
171-
ast::ItemKind::Const(def, ty, body) => {
172-
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, *def);
171+
ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr }) => {
172+
self.print_item_const(
173+
item.ident,
174+
None,
175+
ty,
176+
expr.as_deref(),
177+
&item.vis,
178+
*defaultness,
179+
);
173180
}
174181
ast::ItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
175182
self.print_fn_full(
@@ -508,8 +515,8 @@ impl<'a> State<'a> {
508515
ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
509516
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
510517
}
511-
ast::AssocItemKind::Const(def, ty, body) => {
512-
self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
518+
ast::AssocItemKind::Const(ast::ConstItem { defaultness, ty, expr }) => {
519+
self.print_item_const(ident, None, ty, expr.as_deref(), vis, *defaultness);
513520
}
514521
ast::AssocItemKind::Type(box ast::TyAlias {
515522
defaultness,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ pub fn expand_test_or_bench(
264264
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp),
265265
],
266266
// const $ident: test::TestDescAndFn =
267-
ast::ItemKind::Const(
268-
ast::Defaultness::Final,
269-
cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
267+
ast::ItemKind::Const(ast::ConstItem {
268+
defaultness: ast::Defaultness::Final,
269+
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
270270
// test::TestDescAndFn {
271-
Some(
271+
expr: Some(
272272
cx.expr_struct(
273273
sp,
274274
test_path("TestDescAndFn"),
@@ -361,7 +361,7 @@ pub fn expand_test_or_bench(
361361
],
362362
), // }
363363
),
364-
),
364+
}),
365365
);
366366
test_const = test_const.map(|mut tc| {
367367
tc.vis.kind = ast::VisibilityKind::Public;

compiler/rustc_expand/src/build.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,13 @@ impl<'a> ExtCtxt<'a> {
638638
ty: P<ast::Ty>,
639639
expr: P<ast::Expr>,
640640
) -> P<ast::Item> {
641-
let def = ast::Defaultness::Final;
642-
self.item(span, name, AttrVec::new(), ast::ItemKind::Const(def, ty, Some(expr)))
641+
let defaultness = ast::Defaultness::Final;
642+
self.item(
643+
span,
644+
name,
645+
AttrVec::new(),
646+
ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr: Some(expr) }),
647+
)
643648
}
644649

645650
// Builds `#[name]`.

compiler/rustc_lint/src/unused.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,9 @@ 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(ast::Static { expr: Some(expr), .. }) = &item.kind {
808+
if let Const(ast::ConstItem { expr: Some(expr), .. })
809+
| Static(ast::Static { expr: Some(expr), .. }) = &item.kind
810+
{
809811
self.check_unused_delims_expr(
810812
cx,
811813
expr,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a> Parser<'a> {
237237
} else {
238238
self.recover_const_mut(const_span);
239239
let (ident, ty, expr) = self.parse_item_global(None)?;
240-
(ident, ItemKind::Const(def_(), ty, expr))
240+
(ident, ItemKind::Const(ConstItem { defaultness: def_(), ty, expr }))
241241
}
242242
} else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
243243
// TRAIT ITEM
@@ -863,9 +863,13 @@ impl<'a> Parser<'a> {
863863
let kind = match AssocItemKind::try_from(kind) {
864864
Ok(kind) => kind,
865865
Err(kind) => match kind {
866-
ItemKind::Static(Static { ty: a, mutability: _, expr: b }) => {
866+
ItemKind::Static(Static { ty, mutability: _, expr }) => {
867867
self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span });
868-
AssocItemKind::Const(Defaultness::Final, a, b)
868+
AssocItemKind::Const(ConstItem {
869+
defaultness: Defaultness::Final,
870+
ty,
871+
expr,
872+
})
869873
}
870874
_ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
871875
},
@@ -1115,12 +1119,12 @@ impl<'a> Parser<'a> {
11151119
let kind = match ForeignItemKind::try_from(kind) {
11161120
Ok(kind) => kind,
11171121
Err(kind) => match kind {
1118-
ItemKind::Const(_, a, b) => {
1122+
ItemKind::Const(ConstItem { ty, expr, .. }) => {
11191123
self.sess.emit_err(errors::ExternItemCannotBeConst {
11201124
ident_span: ident.span,
11211125
const_span: span.with_hi(ident.span.lo()),
11221126
});
1123-
ForeignItemKind::Static(a, Mutability::Not, b)
1127+
ForeignItemKind::Static(ty, Mutability::Not, expr)
11241128
}
11251129
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
11261130
},

compiler/rustc_resolve/src/late.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23472347
}
23482348

23492349
ItemKind::Static(ast::Static { ref ty, ref expr, .. })
2350-
| ItemKind::Const(_, ref ty, ref expr) => {
2350+
| ItemKind::Const(ast::ConstItem { ref ty, ref expr, .. }) => {
23512351
self.with_static_rib(|this| {
23522352
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
23532353
this.visit_ty(ty);
@@ -2625,11 +2625,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26252625
for item in trait_items {
26262626
self.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id));
26272627
match &item.kind {
2628-
AssocItemKind::Const(_, ty, default) => {
2628+
AssocItemKind::Const(ast::ConstItem { ty, expr, .. }) => {
26292629
self.visit_ty(ty);
26302630
// Only impose the restrictions of `ConstRibKind` for an
26312631
// actual constant expression in a provided default.
2632-
if let Some(expr) = default {
2632+
if let Some(expr) = expr {
26332633
// We allow arbitrary const expressions inside of associated consts,
26342634
// even if they are potentially not const evaluatable.
26352635
//
@@ -2800,7 +2800,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28002800
use crate::ResolutionError::*;
28012801
self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis)));
28022802
match &item.kind {
2803-
AssocItemKind::Const(_, ty, default) => {
2803+
AssocItemKind::Const(ast::ConstItem { ty, expr, .. }) => {
28042804
debug!("resolve_implementation AssocItemKind::Const");
28052805
// If this is a trait impl, ensure the const
28062806
// exists in trait
@@ -2815,7 +2815,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28152815
);
28162816

28172817
self.visit_ty(ty);
2818-
if let Some(expr) = default {
2818+
if let Some(expr) = expr {
28192819
// We allow arbitrary const expressions inside of associated consts,
28202820
// even if they are potentially not const evaluatable.
28212821
//

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, Static};
4+
use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static, ConstItem};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -100,7 +100,7 @@ impl EarlyLintPass for RedundantStaticLifetimes {
100100
}
101101

102102
if !item.span.from_expansion() {
103-
if let ItemKind::Const(_, ref var_type, _) = item.kind {
103+
if let ItemKind::Const(ConstItem { ty: ref var_type, .. }) = item.kind {
104104
Self::visit_type(var_type, cx, "constants have by default a `'static` lifetime");
105105
// Don't check associated consts because `'static` cannot be elided on those (issue
106106
// #2438)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
287287
(ExternCrate(l), ExternCrate(r)) => l == r,
288288
(Use(l), Use(r)) => eq_use_tree(l, r),
289289
(Static(ast::Static{ ty: lt, mutability: lm, expr: le}), Static(ast::Static { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
290-
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
290+
(Const(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { defaultness: rd, ty: rt, expr: re} )) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
291291
(
292292
Fn(box ast::Fn {
293293
defaultness: ld,
@@ -451,7 +451,7 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
451451
pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
452452
use AssocItemKind::*;
453453
match (l, r) {
454-
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
454+
(Const(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { defaultness: rd, ty: rt, expr: re})) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
455455
(
456456
Fn(box ast::Fn {
457457
defaultness: ld,

0 commit comments

Comments
 (0)