Skip to content

Commit 0515aa5

Browse files
committed
mgca: Add ConstArg representation for const items
1 parent 838684b commit 0515aa5

File tree

26 files changed

+278
-141
lines changed

26 files changed

+278
-141
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3746,10 +3746,29 @@ pub struct ConstItem {
37463746
pub ident: Ident,
37473747
pub generics: Generics,
37483748
pub ty: Box<Ty>,
3749-
pub expr: Option<Box<Expr>>,
3749+
pub body: Option<ConstItemRhs>,
37503750
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
37513751
}
37523752

3753+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3754+
pub enum ConstItemRhs {
3755+
TypeConst(AnonConst),
3756+
Body(Box<Expr>),
3757+
}
3758+
3759+
impl ConstItemRhs {
3760+
pub fn span(&self) -> Span {
3761+
self.expr().span
3762+
}
3763+
3764+
pub fn expr(&self) -> &Expr {
3765+
match self {
3766+
ConstItemRhs::TypeConst(anon_const) => &anon_const.value,
3767+
ConstItemRhs::Body(expr) => expr,
3768+
}
3769+
}
3770+
}
3771+
37533772
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
37543773
#[derive(Clone, Encodable, Decodable, Debug)]
37553774
pub enum ItemKind {

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ macro_rules! common_visitor_and_walkers {
422422
Closure,
423423
Const,
424424
ConstItem,
425+
ConstItemRhs,
425426
Defaultness,
426427
Delegation,
427428
DelegationMac,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,41 @@ impl<'hir> LoweringContext<'_, 'hir> {
170170
}
171171
ItemKind::Static(box ast::StaticItem {
172172
ident,
173-
ty: t,
173+
ty,
174174
safety: _,
175175
mutability: m,
176176
expr: e,
177177
define_opaque,
178178
}) => {
179179
let ident = self.lower_ident(*ident);
180-
let (ty, body_id) =
181-
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
180+
let ty =
181+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
182+
let body_id = self.lower_const_body(span, e.as_deref());
182183
self.lower_define_opaque(hir_id, define_opaque);
183184
hir::ItemKind::Static(*m, ident, ty, body_id)
184185
}
185186
ItemKind::Const(box ast::ConstItem {
186187
ident,
187188
generics,
188189
ty,
189-
expr,
190+
body,
190191
define_opaque,
191192
..
192193
}) => {
193194
let ident = self.lower_ident(*ident);
194-
let (generics, (ty, body_id)) = self.lower_generics(
195+
let (generics, (ty, body)) = self.lower_generics(
195196
generics,
196197
id,
197198
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
198199
|this| {
199-
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
200+
let ty = this
201+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
202+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), span);
203+
(ty, body)
200204
},
201205
);
202206
self.lower_define_opaque(hir_id, &define_opaque);
203-
hir::ItemKind::Const(ident, generics, ty, body_id)
207+
hir::ItemKind::Const(ident, generics, ty, body)
204208
}
205209
ItemKind::Fn(box Fn {
206210
sig: FnSig { decl, header, span: fn_sig_span },
@@ -462,17 +466,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
462466
}
463467
}
464468

465-
fn lower_const_item(
466-
&mut self,
467-
ty: &Ty,
468-
span: Span,
469-
body: Option<&Expr>,
470-
impl_trait_position: ImplTraitPosition,
471-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
472-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
473-
(ty, self.lower_const_body(span, body))
474-
}
475-
476469
#[instrument(level = "debug", skip(self))]
477470
fn lower_use_tree(
478471
&mut self,
@@ -807,7 +800,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
807800
ident,
808801
generics,
809802
ty,
810-
expr,
803+
body,
811804
define_opaque,
812805
..
813806
}) => {
@@ -818,14 +811,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
818811
|this| {
819812
let ty = this
820813
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
821-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
822-
814+
let body = body
815+
.as_ref()
816+
.map(|body| this.lower_const_item_rhs(attrs, Some(body), i.span));
823817
hir::TraitItemKind::Const(ty, body)
824818
},
825819
);
826820

827821
if define_opaque.is_some() {
828-
if expr.is_some() {
822+
if body.is_some() {
829823
self.lower_define_opaque(hir_id, &define_opaque);
830824
} else {
831825
self.dcx().span_err(
@@ -835,7 +829,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
835829
}
836830
}
837831

838-
(*ident, generics, kind, expr.is_some())
832+
(*ident, generics, kind, body.is_some())
839833
}
840834
AssocItemKind::Fn(box Fn {
841835
sig, ident, generics, body: None, define_opaque, ..
@@ -1025,7 +1019,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10251019
ident,
10261020
generics,
10271021
ty,
1028-
expr,
1022+
body,
10291023
define_opaque,
10301024
..
10311025
}) => (
@@ -1037,8 +1031,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10371031
|this| {
10381032
let ty = this
10391033
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1040-
let body = this.lower_const_body(i.span, expr.as_deref());
10411034
this.lower_define_opaque(hir_id, &define_opaque);
1035+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), i.span);
10421036
hir::ImplItemKind::Const(ty, body)
10431037
},
10441038
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23302330
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
23312331
}
23322332

2333+
fn lower_const_item_rhs(
2334+
&mut self,
2335+
attrs: &[hir::Attribute],
2336+
rhs: Option<&ConstItemRhs>,
2337+
span: Span,
2338+
) -> hir::ConstItemRhs<'hir> {
2339+
match rhs {
2340+
Some(ConstItemRhs::TypeConst(anon)) => {
2341+
hir::ConstItemRhs::TypeConst(self.lower_anon_const_to_const_arg(anon))
2342+
}
2343+
None if attr::contains_name(attrs, sym::type_const) => {
2344+
let const_arg = ConstArg {
2345+
hir_id: self.next_id(),
2346+
kind: hir::ConstArgKind::Error(
2347+
DUMMY_SP,
2348+
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
2349+
),
2350+
};
2351+
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
2352+
}
2353+
Some(ConstItemRhs::Body(body)) => {
2354+
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2355+
}
2356+
None => hir::ConstItemRhs::Body(self.lower_const_body(span, None)),
2357+
}
2358+
}
2359+
23332360
/// See [`hir::ConstArg`] for when to use this function vs
23342361
/// [`Self::lower_anon_const_to_anon_const`].
23352362
fn lower_anon_const_to_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,9 +1239,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12391239
}
12401240
});
12411241
}
1242-
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1242+
ItemKind::Const(box ConstItem { defaultness, body, .. }) => {
12431243
self.check_defaultness(item.span, *defaultness);
1244-
if expr.is_none() {
1244+
if body.is_none() {
12451245
self.dcx().emit_err(errors::ConstWithoutBody {
12461246
span: item.span,
12471247
replace_span: self.ending_semi_or_hi(item.span),
@@ -1581,7 +1581,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15811581

15821582
if let AssocCtxt::Impl { .. } = ctxt {
15831583
match &item.kind {
1584-
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1584+
AssocItemKind::Const(box ConstItem { body: None, .. }) => {
15851585
self.dcx().emit_err(errors::AssocConstWithoutBody {
15861586
span: item.span,
15871587
replace_span: self.ending_semi_or_hi(item.span),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,15 @@ impl<'a> State<'a> {
210210
ident,
211211
generics,
212212
ty,
213-
expr,
213+
body,
214214
define_opaque,
215215
}) => {
216216
self.print_item_const(
217217
*ident,
218218
None,
219219
generics,
220220
ty,
221-
expr.as_deref(),
221+
body.as_ref().map(|ct| ct.expr()),
222222
&item.vis,
223223
ast::Safety::Default,
224224
*defaultness,
@@ -566,15 +566,15 @@ impl<'a> State<'a> {
566566
ident,
567567
generics,
568568
ty,
569-
expr,
569+
body,
570570
define_opaque,
571571
}) => {
572572
self.print_item_const(
573573
*ident,
574574
None,
575575
generics,
576576
ty,
577-
expr.as_deref(),
577+
body.as_ref().map(|ct| ct.expr()),
578578
vis,
579579
ast::Safety::Default,
580580
*defaultness,

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ 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 = ecx.expr_block(ecx.block(span, stmts));
46+
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
4747
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4848
let const_item = if is_stmt {
4949
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn expand(
4747

4848
// Generate anonymous constant serving as container for the allocator methods.
4949
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
50-
let const_body = ecx.expr_block(ecx.block(span, stmts));
50+
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
5151
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
5252
let const_item = if is_stmt {
5353
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
385385
cx.attr_nested_word(sym::allow, sym::deprecated, span),
386386
]);
387387

388-
let block = cx.expr_block(
388+
let block = ast::ConstItemRhs::Body(cx.expr_block(
389389
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
390-
);
390+
));
391391

392392
let anon_constant = cx.item_const(
393393
span,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub(crate) fn expand_test_or_bench(
289289
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
290290
define_opaque: None,
291291
// test::TestDescAndFn {
292-
expr: Some(
292+
body: Some(ast::ConstItemRhs::Body(
293293
cx.expr_struct(
294294
sp,
295295
test_path("TestDescAndFn"),
@@ -371,7 +371,7 @@ pub(crate) fn expand_test_or_bench(
371371
field("testfn", test_fn), // }
372372
],
373373
), // }
374-
),
374+
)),
375375
}
376376
.into(),
377377
),

0 commit comments

Comments
 (0)