Skip to content

Commit 422f099

Browse files
committed
mgca: Add ConstArg representation for const items
1 parent 5ea5bde commit 422f099

File tree

29 files changed

+470
-175
lines changed

29 files changed

+470
-175
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3754,10 +3754,29 @@ pub struct ConstItem {
37543754
pub ident: Ident,
37553755
pub generics: Generics,
37563756
pub ty: Box<Ty>,
3757-
pub expr: Option<Box<Expr>>,
3757+
pub body: Option<ConstItemRhs>,
37583758
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
37593759
}
37603760

3761+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3762+
pub enum ConstItemRhs {
3763+
TypeConst(AnonConst),
3764+
Body(Box<Expr>),
3765+
}
3766+
3767+
impl ConstItemRhs {
3768+
pub fn span(&self) -> Span {
3769+
self.expr().span
3770+
}
3771+
3772+
pub fn expr(&self) -> &Expr {
3773+
match self {
3774+
ConstItemRhs::TypeConst(anon_const) => &anon_const.value,
3775+
ConstItemRhs::Body(expr) => expr,
3776+
}
3777+
}
3778+
}
3779+
37613780
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
37623781
#[derive(Clone, Encodable, Decodable, Debug)]
37633782
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
@@ -172,37 +172,41 @@ impl<'hir> LoweringContext<'_, 'hir> {
172172
}
173173
ItemKind::Static(box ast::StaticItem {
174174
ident,
175-
ty: t,
175+
ty,
176176
safety: _,
177177
mutability: m,
178178
expr: e,
179179
define_opaque,
180180
}) => {
181181
let ident = self.lower_ident(*ident);
182-
let (ty, body_id) =
183-
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
182+
let ty =
183+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
184+
let body_id = self.lower_const_body(span, e.as_deref());
184185
self.lower_define_opaque(hir_id, define_opaque);
185186
hir::ItemKind::Static(*m, ident, ty, body_id)
186187
}
187188
ItemKind::Const(box ast::ConstItem {
188189
ident,
189190
generics,
190191
ty,
191-
expr,
192+
body,
192193
define_opaque,
193194
..
194195
}) => {
195196
let ident = self.lower_ident(*ident);
196-
let (generics, (ty, body_id)) = self.lower_generics(
197+
let (generics, (ty, body)) = self.lower_generics(
197198
generics,
198199
id,
199200
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
200201
|this| {
201-
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
202+
let ty = this
203+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
204+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), span);
205+
(ty, body)
202206
},
203207
);
204208
self.lower_define_opaque(hir_id, &define_opaque);
205-
hir::ItemKind::Const(ident, generics, ty, body_id)
209+
hir::ItemKind::Const(ident, generics, ty, body)
206210
}
207211
ItemKind::Fn(box Fn {
208212
sig: FnSig { decl, header, span: fn_sig_span },
@@ -463,17 +467,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
463467
}
464468
}
465469

466-
fn lower_const_item(
467-
&mut self,
468-
ty: &Ty,
469-
span: Span,
470-
body: Option<&Expr>,
471-
impl_trait_position: ImplTraitPosition,
472-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
473-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
474-
(ty, self.lower_const_body(span, body))
475-
}
476-
477470
#[instrument(level = "debug", skip(self))]
478471
fn lower_use_tree(
479472
&mut self,
@@ -808,7 +801,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
808801
ident,
809802
generics,
810803
ty,
811-
expr,
804+
body,
812805
define_opaque,
813806
..
814807
}) => {
@@ -819,14 +812,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
819812
|this| {
820813
let ty = this
821814
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
822-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
823-
815+
let body = body
816+
.as_ref()
817+
.map(|body| this.lower_const_item_rhs(attrs, Some(body), i.span));
824818
hir::TraitItemKind::Const(ty, body)
825819
},
826820
);
827821

828822
if define_opaque.is_some() {
829-
if expr.is_some() {
823+
if body.is_some() {
830824
self.lower_define_opaque(hir_id, &define_opaque);
831825
} else {
832826
self.dcx().span_err(
@@ -836,7 +830,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
836830
}
837831
}
838832

839-
(*ident, generics, kind, expr.is_some())
833+
(*ident, generics, kind, body.is_some())
840834
}
841835
AssocItemKind::Fn(box Fn {
842836
sig, ident, generics, body: None, define_opaque, ..
@@ -1021,7 +1015,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10211015
ident,
10221016
generics,
10231017
ty,
1024-
expr,
1018+
body,
10251019
define_opaque,
10261020
..
10271021
}) => (
@@ -1033,8 +1027,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10331027
|this| {
10341028
let ty = this
10351029
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1036-
let body = this.lower_const_body(i.span, expr.as_deref());
10371030
this.lower_define_opaque(hir_id, &define_opaque);
1031+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), i.span);
10381032
hir::ImplItemKind::Const(ty, body)
10391033
},
10401034
),

compiler/rustc_ast_lowering/src/lib.rs

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

2306+
fn lower_const_item_rhs(
2307+
&mut self,
2308+
attrs: &[hir::Attribute],
2309+
rhs: Option<&ConstItemRhs>,
2310+
span: Span,
2311+
) -> hir::ConstItemRhs<'hir> {
2312+
match rhs {
2313+
Some(ConstItemRhs::TypeConst(anon)) => {
2314+
hir::ConstItemRhs::TypeConst(self.lower_anon_const_to_const_arg(anon))
2315+
}
2316+
None if attr::contains_name(attrs, sym::type_const) => {
2317+
let const_arg = ConstArg {
2318+
hir_id: self.next_id(),
2319+
kind: hir::ConstArgKind::Error(
2320+
DUMMY_SP,
2321+
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
2322+
),
2323+
};
2324+
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
2325+
}
2326+
Some(ConstItemRhs::Body(body)) => {
2327+
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2328+
}
2329+
None => hir::ConstItemRhs::Body(self.lower_const_body(span, None)),
2330+
}
2331+
}
2332+
23062333
/// See [`hir::ConstArg`] for when to use this function vs
23072334
/// [`Self::lower_anon_const_to_anon_const`].
23082335
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
@@ -1236,9 +1236,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12361236
}
12371237
});
12381238
}
1239-
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1239+
ItemKind::Const(box ConstItem { defaultness, body, .. }) => {
12401240
self.check_defaultness(item.span, *defaultness);
1241-
if expr.is_none() {
1241+
if body.is_none() {
12421242
self.dcx().emit_err(errors::ConstWithoutBody {
12431243
span: item.span,
12441244
replace_span: self.ending_semi_or_hi(item.span),
@@ -1578,7 +1578,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15781578

15791579
if let AssocCtxt::Impl { .. } = ctxt {
15801580
match &item.kind {
1581-
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1581+
AssocItemKind::Const(box ConstItem { body: None, .. }) => {
15821582
self.dcx().emit_err(errors::AssocConstWithoutBody {
15831583
span: item.span,
15841584
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
@@ -213,15 +213,15 @@ impl<'a> State<'a> {
213213
ident,
214214
generics,
215215
ty,
216-
expr,
216+
body,
217217
define_opaque,
218218
}) => {
219219
self.print_item_const(
220220
*ident,
221221
None,
222222
generics,
223223
ty,
224-
expr.as_deref(),
224+
body.as_ref().map(|ct| ct.expr()),
225225
&item.vis,
226226
ast::Safety::Default,
227227
*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
@@ -42,7 +42,7 @@ pub(crate) fn expand(
4242

4343
// Generate anonymous constant serving as container for the allocator methods.
4444
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
45-
let const_body = ecx.expr_block(ecx.block(span, stmts));
45+
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
4646
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4747
let const_item = if is_stmt {
4848
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)