Skip to content

Commit 300d74d

Browse files
committed
XXX: Const/Constness
1 parent e71f89d commit 300d74d

File tree

21 files changed

+173
-133
lines changed

21 files changed

+173
-133
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ impl Async {
23462346

23472347
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
23482348
#[derive(HashStable_Generic)]
2349-
pub enum Const {
2349+
pub enum Constness {
23502350
Yes(Span),
23512351
No,
23522352
}
@@ -2713,7 +2713,7 @@ impl Extern {
27132713
pub struct FnHeader {
27142714
pub unsafety: Unsafe,
27152715
pub asyncness: Async,
2716-
pub constness: Const,
2716+
pub constness: Constness,
27172717
pub ext: Extern,
27182718
}
27192719

@@ -2723,7 +2723,7 @@ impl FnHeader {
27232723
let Self { unsafety, asyncness, constness, ext } = self;
27242724
matches!(unsafety, Unsafe::Yes(_))
27252725
|| asyncness.is_async()
2726-
|| matches!(constness, Const::Yes(_))
2726+
|| matches!(constness, Constness::Yes(_))
27272727
|| !matches!(ext, Extern::None)
27282728
}
27292729
}
@@ -2733,7 +2733,7 @@ impl Default for FnHeader {
27332733
FnHeader {
27342734
unsafety: Unsafe::No,
27352735
asyncness: Async::No,
2736-
constness: Const::No,
2736+
constness: Constness::No,
27372737
ext: Extern::None,
27382738
}
27392739
}
@@ -2788,7 +2788,7 @@ pub struct Impl {
27882788
pub defaultness: Defaultness,
27892789
pub unsafety: Unsafe,
27902790
pub generics: Generics,
2791-
pub constness: Const,
2791+
pub constness: Constness,
27922792
pub polarity: ImplPolarity,
27932793
/// The trait being implemented, if any.
27942794
pub of_trait: Option<TraitRef>,
@@ -2822,6 +2822,13 @@ pub struct TraitAlias {
28222822
pub bounds: GenericBounds,
28232823
}
28242824

2825+
#[derive(Clone, Encodable, Decodable, Debug)]
2826+
pub struct Const {
2827+
pub defaultness: Defaultness,
2828+
pub ty: P<Ty>,
2829+
pub expr: Option<P<Expr>>,
2830+
}
2831+
28252832
#[derive(Clone, Encodable, Decodable, Debug)]
28262833
pub enum ItemKind {
28272834
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
@@ -2839,7 +2846,7 @@ pub enum ItemKind {
28392846
/// A constant item (`const`).
28402847
///
28412848
/// E.g., `const FOO: i32 = 42;`.
2842-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
2849+
Const(Box<Const>),
28432850
/// A function declaration (`fn`).
28442851
///
28452852
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -2955,7 +2962,7 @@ pub type AssocItem = Item<AssocItemKind>;
29552962
pub enum AssocItemKind {
29562963
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
29572964
/// If `def` is parsed, then the constant is provided, and otherwise required.
2958-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
2965+
Const(Box<Const>),
29592966
/// An associated function.
29602967
Fn(Box<Fn>),
29612968
/// An associated type.
@@ -2967,7 +2974,7 @@ pub enum AssocItemKind {
29672974
impl AssocItemKind {
29682975
pub fn defaultness(&self) -> Defaultness {
29692976
match *self {
2970-
Self::Const(defaultness, ..)
2977+
Self::Const(box Const { defaultness, .. })
29712978
| Self::Fn(box Fn { defaultness, .. })
29722979
| Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness,
29732980
Self::MacCall(..) => Defaultness::Final,
@@ -2978,7 +2985,7 @@ impl AssocItemKind {
29782985
impl From<AssocItemKind> for ItemKind {
29792986
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
29802987
match assoc_item_kind {
2981-
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
2988+
AssocItemKind::Const(const_) => ItemKind::Const(const_),
29822989
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
29832990
AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
29842991
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -2991,7 +2998,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
29912998

29922999
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
29933000
Ok(match item_kind {
2994-
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
3001+
ItemKind::Const(const_) => AssocItemKind::Const(const_),
29953002
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
29963003
ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind),
29973004
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
@@ -3046,8 +3053,8 @@ mod size_asserts {
30463053
use super::*;
30473054
use rustc_data_structures::static_assert_size;
30483055
// These are in alphabetical order, which is easy to maintain.
3049-
static_assert_size!(AssocItem, 120);
3050-
static_assert_size!(AssocItemKind, 32);
3056+
static_assert_size!(AssocItem, 104);
3057+
static_assert_size!(AssocItemKind, 16);
30513058
static_assert_size!(Attribute, 152);
30523059
static_assert_size!(Block, 48);
30533060
static_assert_size!(Expr, 104);
@@ -3057,8 +3064,8 @@ mod size_asserts {
30573064
static_assert_size!(GenericBound, 88);
30583065
static_assert_size!(Generics, 72);
30593066
static_assert_size!(Impl, 200);
3060-
static_assert_size!(Item, 120);
3061-
static_assert_size!(ItemKind, 32);
3067+
static_assert_size!(Item, 112);
3068+
static_assert_size!(ItemKind, 24);
30623069
static_assert_size!(Lit, 48);
30633070
static_assert_size!(Pat, 120);
30643071
static_assert_size!(Path, 40);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,10 @@ pub fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
822822
}
823823

824824
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
825-
pub fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
825+
pub fn visit_constness<T: MutVisitor>(constness: &mut Constness, vis: &mut T) {
826826
match constness {
827-
Const::Yes(span) => vis.visit_span(span),
828-
Const::No => {}
827+
Constness::Yes(span) => vis.visit_span(span),
828+
Constness::No => {}
829829
}
830830
}
831831

@@ -1014,7 +1014,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10141014
vis.visit_ty(ty);
10151015
visit_opt(expr, |expr| vis.visit_expr(expr));
10161016
}
1017-
ItemKind::Const(defaultness, ty, expr) => {
1017+
ItemKind::Const(box Const { defaultness, ty, expr }) => {
10181018
visit_defaultness(defaultness, vis);
10191019
vis.visit_ty(ty);
10201020
visit_opt(expr, |expr| vis.visit_expr(expr));
@@ -1101,7 +1101,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11011101
visitor.visit_vis(vis);
11021102
visit_attrs(attrs, visitor);
11031103
match kind {
1104-
AssocItemKind::Const(defaultness, ty, expr) => {
1104+
AssocItemKind::Const(box Const { defaultness, ty, expr }) => {
11051105
visit_defaultness(defaultness, visitor);
11061106
visitor.visit_ty(ty);
11071107
visit_opt(expr, |expr| visitor.visit_expr(expr));

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,9 @@ 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 typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
303-
visitor.visit_ty(typ);
302+
ItemKind::Static(ref ty, _, ref expr)
303+
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
304+
visitor.visit_ty(ty);
304305
walk_list!(visitor, visit_expr, expr);
305306
}
306307
ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
@@ -677,7 +678,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
677678
visitor.visit_ident(ident);
678679
walk_list!(visitor, visit_attribute, attrs);
679680
match kind {
680-
AssocItemKind::Const(_, ty, expr) => {
681+
AssocItemKind::Const(box Const { ty, expr, .. }) => {
681682
visitor.visit_ty(ty);
682683
walk_list!(visitor, visit_expr, expr);
683684
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
238238
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
239239
hir::ItemKind::Static(ty, m, body_id)
240240
}
241-
ItemKind::Const(_, ref t, ref e) => {
242-
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
241+
ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
242+
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
243243
hir::ItemKind::Const(ty, body_id)
244244
}
245245
ItemKind::Fn(box Fn {
@@ -753,9 +753,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
753753
let trait_item_def_id = hir_id.expect_owner();
754754

755755
let (generics, kind, has_default) = match i.kind {
756-
AssocItemKind::Const(_, ref ty, ref default) => {
756+
AssocItemKind::Const(box Const { ref ty, ref expr, .. }) => {
757757
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
758-
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
758+
let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
759759
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
760760
}
761761
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
@@ -849,7 +849,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
849849
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
850850

851851
let (generics, kind) = match &i.kind {
852-
AssocItemKind::Const(_, ty, expr) => {
852+
AssocItemKind::Const(box Const { ty, expr, .. }) => {
853853
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
854854
(
855855
hir::Generics::empty(),
@@ -1271,10 +1271,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
12711271
}
12721272
}
12731273

1274-
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1274+
fn lower_constness(&mut self, c: Constness) -> hir::Constness {
12751275
match c {
1276-
Const::Yes(_) => hir::Constness::Const,
1277-
Const::No => hir::Constness::NotConst,
1276+
Constness::Yes(_) => hir::Constness::Const,
1277+
Constness::No => hir::Constness::NotConst,
12781278
}
12791279
}
12801280

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ impl<'a> AstValidator<'a> {
7676
fn with_in_trait_impl(
7777
&mut self,
7878
is_in: bool,
79-
constness: Option<Const>,
79+
constness: Option<Constness>,
8080
f: impl FnOnce(&mut Self),
8181
) {
8282
let old = mem::replace(&mut self.in_trait_impl, is_in);
83-
let old_const =
84-
mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_))));
83+
let old_const = mem::replace(
84+
&mut self.in_const_trait_impl,
85+
matches!(constness, Some(Constness::Yes(_))),
86+
);
8587
f(self);
8688
self.in_trait_impl = old;
8789
self.in_const_trait_impl = old_const;
@@ -324,8 +326,8 @@ impl<'a> AstValidator<'a> {
324326
}
325327
}
326328

327-
fn check_trait_fn_not_const(&self, constness: Const) {
328-
if let Const::Yes(span) = constness {
329+
fn check_trait_fn_not_const(&self, constness: Constness) {
330+
if let Constness::Yes(span) = constness {
329331
struct_span_err!(
330332
self.session,
331333
span,
@@ -1136,7 +1138,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11361138

11371139
this.visit_vis(&item.vis);
11381140
this.visit_ident(item.ident);
1139-
if let Const::Yes(_) = constness {
1141+
if let Constness::Yes(_) = constness {
11401142
this.with_tilde_const_allowed(|this| this.visit_generics(generics));
11411143
} else {
11421144
this.visit_generics(generics);
@@ -1183,7 +1185,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11831185
.note("only trait implementations may be annotated with `default`")
11841186
.emit();
11851187
}
1186-
if let Const::Yes(span) = constness {
1188+
if let Constness::Yes(span) = constness {
11871189
error(span, "`const`")
11881190
.note("only trait implementations may be annotated with `const`")
11891191
.emit();
@@ -1327,8 +1329,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13271329
_ => {}
13281330
}
13291331
}
1330-
ItemKind::Const(def, .., None) => {
1331-
self.check_defaultness(item.span, def);
1332+
ItemKind::Const(box Const { defaultness, expr: None, .. }) => {
1333+
self.check_defaultness(item.span, defaultness);
13321334
let msg = "free constant item without body";
13331335
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
13341336
}
@@ -1559,7 +1561,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15591561

15601562
// Functions cannot both be `const async`
15611563
if let Some(FnHeader {
1562-
constness: Const::Yes(cspan),
1564+
constness: Constness::Yes(cspan),
15631565
asyncness: Async::Yes { span: aspan, .. },
15641566
..
15651567
}) = fk.header()
@@ -1628,7 +1630,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16281630
}
16291631

16301632
let tilde_const_allowed =
1631-
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
1633+
matches!(fk.header(), Some(FnHeader { constness: Constness::Yes(_), .. }))
16321634
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));
16331635

16341636
self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span));
@@ -1645,8 +1647,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16451647

16461648
if ctxt == AssocCtxt::Impl {
16471649
match &item.kind {
1648-
AssocItemKind::Const(_, _, body) => {
1649-
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
1650+
AssocItemKind::Const(box Const { expr, .. }) => {
1651+
self.check_impl_item_provided(item.span, expr, "constant", " = <expr>;");
16501652
}
16511653
AssocItemKind::Fn(box Fn { body, .. }) => {
16521654
self.check_impl_item_provided(item.span, body, "function", " { <body> }");
@@ -1701,7 +1703,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
17011703
AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. })
17021704
if self.in_const_trait_impl
17031705
|| ctxt == AssocCtxt::Trait
1704-
|| matches!(sig.header.constness, Const::Yes(_)) =>
1706+
|| matches!(sig.header.constness, Constness::Yes(_)) =>
17051707
{
17061708
self.visit_vis(&item.vis);
17071709
self.visit_ident(item.ident);

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ struct PostExpansionVisitor<'a> {
5858
}
5959

6060
impl<'a> PostExpansionVisitor<'a> {
61-
fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
61+
fn check_abi(&self, abi: ast::StrLit, constness: ast::Constness) {
6262
let ast::StrLit { symbol_unescaped, span, .. } = abi;
6363

64-
if let ast::Const::Yes(_) = constness {
64+
if let ast::Constness::Yes(_) = constness {
6565
match symbol_unescaped {
6666
// Stable
6767
sym::Rust | sym::C => {}
@@ -284,7 +284,7 @@ impl<'a> PostExpansionVisitor<'a> {
284284
}
285285
}
286286

287-
fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
287+
fn check_extern(&self, ext: ast::Extern, constness: ast::Constness) {
288288
if let ast::Extern::Explicit(abi, _) = ext {
289289
self.check_abi(abi, constness);
290290
}
@@ -433,7 +433,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
433433
match i.kind {
434434
ast::ItemKind::ForeignMod(ref foreign_module) => {
435435
if let Some(abi) = foreign_module.abi {
436-
self.check_abi(abi, ast::Const::No);
436+
self.check_abi(abi, ast::Constness::No);
437437
}
438438
}
439439

@@ -557,7 +557,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
557557
match ty.kind {
558558
ast::TyKind::BareFn(ref bare_fn_ty) => {
559559
// Function pointers cannot be `const`
560-
self.check_extern(bare_fn_ty.ext, ast::Const::No);
560+
self.check_extern(bare_fn_ty.ext, ast::Constness::No);
561561
}
562562
ast::TyKind::Never => {
563563
gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,10 +1754,10 @@ impl<'a> State<'a> {
17541754
}
17551755
}
17561756

1757-
pub(crate) fn print_constness(&mut self, s: ast::Const) {
1757+
pub(crate) fn print_constness(&mut self, s: ast::Constness) {
17581758
match s {
1759-
ast::Const::No => {}
1760-
ast::Const::Yes(_) => self.word_nbsp("const"),
1759+
ast::Constness::No => {}
1760+
ast::Constness::Yes(_) => self.word_nbsp("const"),
17611761
}
17621762
}
17631763

0 commit comments

Comments
 (0)