Skip to content

Commit 867d626

Browse files
committed
unify the AST repr of type const and const RHS
1 parent 730a6b5 commit 867d626

28 files changed

Lines changed: 192 additions & 219 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,44 +3977,16 @@ pub struct ConstItem {
39773977
pub ident: Ident,
39783978
pub generics: Generics,
39793979
pub ty: Box<Ty>,
3980-
pub rhs_kind: ConstItemRhsKind,
3980+
pub body: Option<Box<Expr>>,
3981+
#[visitable(ignore)]
3982+
pub kind: ConstItemKind,
39813983
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
39823984
}
39833985

3984-
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3985-
pub enum ConstItemRhsKind {
3986-
Body { rhs: Option<Box<Expr>> },
3987-
TypeConst { rhs: Option<AnonConst> },
3988-
}
3989-
3990-
impl ConstItemRhsKind {
3991-
pub fn new_body(rhs: Box<Expr>) -> Self {
3992-
Self::Body { rhs: Some(rhs) }
3993-
}
3994-
3995-
pub fn span(&self) -> Option<Span> {
3996-
Some(self.expr()?.span)
3997-
}
3998-
3999-
pub fn expr(&self) -> Option<&Expr> {
4000-
match self {
4001-
Self::Body { rhs: Some(body) } => Some(&body),
4002-
Self::TypeConst { rhs: Some(anon) } => Some(&anon.value),
4003-
_ => None,
4004-
}
4005-
}
4006-
4007-
pub fn has_expr(&self) -> bool {
4008-
match self {
4009-
Self::Body { rhs: Some(_) } => true,
4010-
Self::TypeConst { rhs: Some(_) } => true,
4011-
_ => false,
4012-
}
4013-
}
4014-
4015-
pub fn is_type_const(&self) -> bool {
4016-
matches!(self, &Self::TypeConst { .. })
4017-
}
3986+
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq, Eq)]
3987+
pub enum ConstItemKind {
3988+
Body,
3989+
TypeConst,
40183990
}
40193991

40203992
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]

compiler/rustc_ast/src/visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ macro_rules! common_visitor_and_walkers {
427427
Const,
428428
ConstBlockItem,
429429
ConstItem,
430-
ConstItemRhsKind,
431430
Defaultness,
432431
Delegation,
433432
DelegationMac,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
270270
ident,
271271
generics,
272272
ty,
273-
rhs_kind,
273+
body,
274+
kind,
274275
define_opaque,
275276
}) => {
276277
let ident = self.lower_ident(*ident);
@@ -282,7 +283,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
282283
ty,
283284
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
284285
);
285-
let rhs = this.lower_const_item_rhs(rhs_kind, span);
286+
let rhs = this.lower_const_item_rhs(body, *kind, span);
286287
(ty, rhs)
287288
},
288289
);
@@ -918,7 +919,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
918919
ident,
919920
generics,
920921
ty,
921-
rhs_kind,
922+
body,
923+
kind,
922924
define_opaque,
923925
..
924926
}) => {
@@ -931,8 +933,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
931933
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
932934
);
933935
// Trait associated consts don't need an expression/body.
934-
let rhs = if rhs_kind.has_expr() {
935-
Some(this.lower_const_item_rhs(rhs_kind, i.span))
936+
let rhs = if body.is_some() {
937+
Some(this.lower_const_item_rhs(body, *kind, i.span))
936938
} else {
937939
None
938940
};
@@ -941,7 +943,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
941943
);
942944

943945
if define_opaque.is_some() {
944-
if rhs_kind.has_expr() {
946+
if body.is_some() {
945947
self.lower_define_opaque(hir_id, &define_opaque);
946948
} else {
947949
self.dcx().span_err(
@@ -951,7 +953,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
951953
}
952954
}
953955

954-
(*ident, generics, kind, rhs_kind.has_expr())
956+
(*ident, generics, kind, body.is_some())
955957
}
956958
AssocItemKind::Fn(Fn { sig, ident, generics, body: None, define_opaque, .. }) => {
957959
// FIXME(contracts): Deny contract here since it won't apply to
@@ -1180,7 +1182,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
11801182
ident,
11811183
generics,
11821184
ty,
1183-
rhs_kind,
1185+
body,
1186+
kind,
11841187
define_opaque,
11851188
..
11861189
}) => (
@@ -1194,7 +1197,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11941197
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
11951198
);
11961199
this.lower_define_opaque(hir_id, &define_opaque);
1197-
let rhs = this.lower_const_item_rhs(rhs_kind, i.span);
1200+
let rhs = this.lower_const_item_rhs(body, *kind, i.span);
11981201
hir::ImplItemKind::Const(ty, rhs)
11991202
},
12001203
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,20 +2660,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
26602660

26612661
fn lower_const_item_rhs(
26622662
&mut self,
2663-
rhs_kind: &ConstItemRhsKind,
2663+
body: &Option<Box<Expr>>,
2664+
kind: ConstItemKind,
26642665
span: Span,
26652666
) -> hir::ConstItemRhs<'hir> {
2666-
match rhs_kind {
2667-
ConstItemRhsKind::Body { rhs: Some(body) } => {
2668-
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2667+
match (body, kind) {
2668+
(body, ConstItemKind::Body) => {
2669+
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
26692670
}
2670-
ConstItemRhsKind::Body { rhs: None } => {
2671-
hir::ConstItemRhs::Body(self.lower_const_body(span, None))
2672-
}
2673-
ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
2674-
hir::ConstItemRhs::TypeConst(self.lower_anon_const_to_const_arg_and_alloc(anon))
2675-
}
2676-
ConstItemRhsKind::TypeConst { rhs: None } => {
2671+
(Some(body), ConstItemKind::TypeConst) => hir::ConstItemRhs::TypeConst(
2672+
self.arena.alloc(match self.can_lower_expr_to_const_arg_direct(&body) {
2673+
Ok(()) => self.lower_expr_to_const_arg_direct(&body, None),
2674+
Err(err) => err.emit(self),
2675+
}),
2676+
),
2677+
(None, ConstItemKind::TypeConst) => {
26772678
let const_arg = ConstArg {
26782679
hir_id: self.next_id(),
26792680
kind: hir::ConstArgKind::Error(

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,9 +1544,9 @@ impl Visitor<'_> for AstValidator<'_> {
15441544
visit::walk_item(this, item)
15451545
});
15461546
}
1547-
ItemKind::Const(ConstItem { defaultness, ident, rhs_kind, .. }) => {
1547+
ItemKind::Const(ConstItem { defaultness, ident, body, .. }) => {
15481548
self.check_defaultness(item.span, *defaultness, AllowDefault::No, AllowFinal::No);
1549-
if !rhs_kind.has_expr() {
1549+
if body.is_none() {
15501550
self.dcx().emit_err(diagnostics::ConstWithoutBody {
15511551
span: item.span,
15521552
replace_span: self.ending_semi_or_hi(item.span),
@@ -1941,8 +1941,8 @@ impl Visitor<'_> for AstValidator<'_> {
19411941

19421942
if let AssocCtxt::Impl { .. } = ctxt {
19431943
match &item.kind {
1944-
AssocItemKind::Const(ConstItem { rhs_kind, .. }) => {
1945-
if !rhs_kind.has_expr() {
1944+
AssocItemKind::Const(ConstItem { body, .. }) => {
1945+
if body.is_none() {
19461946
self.dcx().emit_err(diagnostics::AssocConstWithoutBody {
19471947
span: item.span,
19481948
replace_span: self.ending_semi_or_hi(item.span),

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
220220
self.check_impl_trait(ty, false)
221221
}
222222
ast::ItemKind::Const(ast::ConstItem {
223-
rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
224-
..
223+
kind: ast::ConstItemKind::TypeConst, ..
225224
}) => {
226225
// Make sure this is only allowed if the feature gate is enabled.
227226
// #![feature(min_generic_const_args)]
@@ -400,7 +399,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
400399
false
401400
}
402401
ast::AssocItemKind::Const(ast::ConstItem {
403-
rhs_kind: ast::ConstItemRhsKind::TypeConst { rhs },
402+
body,
403+
kind: ast::ConstItemKind::TypeConst,
404404
..
405405
}) => {
406406
// Make sure this is only allowed if the feature gate is enabled.
@@ -409,7 +409,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
409409
// Make sure associated `type const` defaults in traits are only allowed
410410
// if the feature gate is enabled.
411411
// #![feature(associated_type_defaults)]
412-
if ctxt == AssocCtxt::Trait && rhs.is_some() {
412+
if ctxt == AssocCtxt::Trait && body.is_some() {
413413
gate!(
414414
self,
415415
associated_type_defaults,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,16 @@ impl<'a> State<'a> {
229229
ident,
230230
generics,
231231
ty,
232-
rhs_kind,
232+
body,
233+
kind: _,
233234
define_opaque,
234235
}) => {
235236
self.print_item_const(
236237
*ident,
237238
None,
238239
generics,
239240
ty,
240-
rhs_kind.expr(),
241+
body.as_deref(),
241242
&item.vis,
242243
ast::Safety::Default,
243244
*defaultness,
@@ -617,15 +618,16 @@ impl<'a> State<'a> {
617618
ident,
618619
generics,
619620
ty,
620-
rhs_kind,
621+
body,
622+
kind: _,
621623
define_opaque,
622624
}) => {
623625
self.print_item_const(
624626
*ident,
625627
None,
626628
generics,
627629
ty,
628-
rhs_kind.expr(),
630+
body.as_deref(),
629631
vis,
630632
ast::Safety::Default,
631633
*defaultness,

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ pub(crate) fn expand(
4343

4444
// Generate anonymous constant serving as container for the allocator methods.
4545
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
46-
let const_body = ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts)));
47-
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
46+
let const_body = ecx.expr_block(ecx.block(span, stmts));
47+
let const_item = ecx.item_const(
48+
span,
49+
Ident::new(kw::Underscore, span),
50+
const_ty,
51+
Some(const_body),
52+
ast::ConstItemKind::Body,
53+
);
4854
let const_item = if is_stmt {
4955
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
5056
} else {

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ fn generate_default_impl(
335335
span,
336336
underscore,
337337
unit,
338-
ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts))),
338+
Some(ecx.expr_block(ecx.block(span, stmts))),
339+
ast::ConstItemKind::Body,
339340
)
340341
};
341342

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ pub(crate) fn expand(
5454

5555
// Generate anonymous constant serving as container for the allocator methods.
5656
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
57-
let const_body = ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts)));
58-
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
57+
let const_body = ecx.expr_block(ecx.block(span, stmts));
58+
let const_item = ecx.item_const(
59+
span,
60+
Ident::new(kw::Underscore, span),
61+
const_ty,
62+
Some(const_body),
63+
ast::ConstItemKind::Body,
64+
);
5965
let const_item = if is_stmt {
6066
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
6167
} else {

0 commit comments

Comments
 (0)