Skip to content

Commit 9e1c088

Browse files
camelidBoxyUwU
andcommitted
mgca: Add ConstArg representation for const items
* Centralize const item lowering logic * Add dummy NodeId to AST const item for mgca lowering * wip on using ConstArg for const item bodies * wip on fixing const normalization ICEs * Extract helper function for normalizing free/assoc terms * more wip on fixing ICEs * Recurse through const items in instance resolution * Fix more ICEs * Use AnonConst as RHS of const items instead of side channel hack * Implement type_of for anon consts on RHS of const items * Fix yet more ICEs * wip on fixing more ICEs (this might make it worse) * Fix clippy and rustfmt * Address some review comments * Inherit generics correctly for anon const as const item RHS * Lower const item RHS to const path even on stable * wip on moving const RHS special-casing * intern + write to place * allow anon consts with expected types with generics Co-authored-by: Boxy <[email protected]>
1 parent 283db70 commit 9e1c088

File tree

48 files changed

+521
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+521
-386
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3617,7 +3617,7 @@ pub struct ConstItem {
36173617
pub ident: Ident,
36183618
pub generics: Generics,
36193619
pub ty: P<Ty>,
3620-
pub expr: Option<P<Expr>>,
3620+
pub body: Option<P<AnonConst>>,
36213621
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
36223622
}
36233623

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,12 +1307,12 @@ impl WalkItemKind for AssocItemKind {
13071307
}
13081308

13091309
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
1310-
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
1310+
let ConstItem { defaultness, ident, generics, ty, body, define_opaque } = item;
13111311
visit_defaultness(vis, defaultness);
13121312
vis.visit_ident(ident);
13131313
vis.visit_generics(generics);
13141314
vis.visit_ty(ty);
1315-
visit_opt(expr, |expr| vis.visit_expr(expr));
1315+
visit_opt(body, |body| vis.visit_anon_const(body));
13161316
walk_define_opaques(vis, define_opaque);
13171317
}
13181318

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,13 @@ impl WalkItemKind for ItemKind {
448448
ident,
449449
generics,
450450
ty,
451-
expr,
451+
body,
452452
define_opaque,
453453
}) => {
454454
try_visit!(visitor.visit_ident(ident));
455455
try_visit!(visitor.visit_generics(generics));
456456
try_visit!(visitor.visit_ty(ty));
457-
visit_opt!(visitor, visit_expr, expr);
457+
visit_opt!(visitor, visit_anon_const, body);
458458
try_visit!(walk_define_opaques(visitor, define_opaque));
459459
}
460460
ItemKind::Fn(func) => {
@@ -1044,13 +1044,13 @@ impl WalkItemKind for AssocItemKind {
10441044
ident,
10451045
generics,
10461046
ty,
1047-
expr,
1047+
body,
10481048
define_opaque,
10491049
}) => {
10501050
try_visit!(visitor.visit_ident(ident));
10511051
try_visit!(visitor.visit_generics(generics));
10521052
try_visit!(visitor.visit_ty(ty));
1053-
visit_opt!(visitor, visit_expr, expr);
1053+
visit_opt!(visitor, visit_anon_const, body);
10541054
try_visit!(walk_define_opaques(visitor, define_opaque));
10551055
}
10561056
AssocItemKind::Fn(func) => {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 18 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(ident, ty, *m, 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_anon_const_to_const_arg(body.as_deref().unwrap());
203+
(ty, body)
200204
},
201205
);
202206
self.lower_define_opaque(hir_id, &define_opaque);
203-
hir::ItemKind::Const(ident, ty, generics, body_id)
207+
hir::ItemKind::Const(ident, ty, generics, body)
204208
}
205209
ItemKind::Fn(box Fn {
206210
sig: FnSig { decl, header, span: fn_sig_span },
@@ -480,17 +484,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
480484
}
481485
}
482486

483-
fn lower_const_item(
484-
&mut self,
485-
ty: &Ty,
486-
span: Span,
487-
body: Option<&Expr>,
488-
impl_trait_position: ImplTraitPosition,
489-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
490-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
491-
(ty, self.lower_const_body(span, body))
492-
}
493-
494487
#[instrument(level = "debug", skip(self))]
495488
fn lower_use_tree(
496489
&mut self,
@@ -786,7 +779,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
786779
ident,
787780
generics,
788781
ty,
789-
expr,
782+
body,
790783
define_opaque,
791784
..
792785
}) => {
@@ -797,14 +790,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
797790
|this| {
798791
let ty = this
799792
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
800-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
801-
793+
let body =
794+
body.as_deref().map(|body| this.lower_anon_const_to_const_arg(body));
802795
hir::TraitItemKind::Const(ty, body)
803796
},
804797
);
805798

806799
if define_opaque.is_some() {
807-
if expr.is_some() {
800+
if body.is_some() {
808801
self.lower_define_opaque(hir_id, &define_opaque);
809802
} else {
810803
self.dcx().span_err(
@@ -814,7 +807,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
814807
}
815808
}
816809

817-
(*ident, generics, kind, expr.is_some())
810+
(*ident, generics, kind, body.is_some())
818811
}
819812
AssocItemKind::Fn(box Fn {
820813
sig, ident, generics, body: None, define_opaque, ..
@@ -978,7 +971,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
978971
ident,
979972
generics,
980973
ty,
981-
expr,
974+
body,
982975
define_opaque,
983976
..
984977
}) => (
@@ -990,8 +983,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
990983
|this| {
991984
let ty = this
992985
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
993-
let body = this.lower_const_body(i.span, expr.as_deref());
994986
this.lower_define_opaque(hir_id, &define_opaque);
987+
let body = this.lower_anon_const_to_const_arg(body.as_deref().unwrap());
995988
hir::ImplItemKind::Const(ty, body)
996989
},
997990
),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10841084
_ => visit::walk_item(self, item),
10851085
}
10861086
}
1087-
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1087+
ItemKind::Const(box ConstItem { defaultness, body, .. }) => {
10881088
self.check_defaultness(item.span, *defaultness);
1089-
if expr.is_none() {
1089+
if body.is_none() {
10901090
self.dcx().emit_err(errors::ConstWithoutBody {
10911091
span: item.span,
10921092
replace_span: self.ending_semi_or_hi(item.span),
@@ -1436,7 +1436,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14361436

14371437
if let AssocCtxt::Impl { .. } = ctxt {
14381438
match &item.kind {
1439-
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1439+
AssocItemKind::Const(box ConstItem { body: None, .. }) => {
14401440
self.dcx().emit_err(errors::AssocConstWithoutBody {
14411441
span: item.span,
14421442
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
@@ -214,15 +214,15 @@ impl<'a> State<'a> {
214214
ident,
215215
generics,
216216
ty,
217-
expr,
217+
body,
218218
define_opaque,
219219
}) => {
220220
self.print_item_const(
221221
*ident,
222222
None,
223223
generics,
224224
ty,
225-
expr.as_deref(),
225+
body.as_deref().map(|ct| &*ct.value),
226226
&item.vis,
227227
ast::Safety::Default,
228228
*defaultness,
@@ -563,15 +563,15 @@ impl<'a> State<'a> {
563563
ident,
564564
generics,
565565
ty,
566-
expr,
566+
body,
567567
define_opaque,
568568
}) => {
569569
self.print_item_const(
570570
*ident,
571571
None,
572572
generics,
573573
ty,
574-
expr.as_deref(),
574+
body.as_deref().map(|ct| &*ct.value),
575575
vis,
576576
ast::Safety::Default,
577577
*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 = ecx.anon_const_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(P(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
@@ -48,7 +48,7 @@ pub(crate) fn expand(
4848

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

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
379379
i
380380
});
381381

382-
let block = cx.expr_block(
382+
let block = cx.anon_const_block(
383383
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
384384
);
385385

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ pub(crate) fn expand_test_or_bench(
294294
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
295295
define_opaque: None,
296296
// test::TestDescAndFn {
297-
expr: Some(
298-
cx.expr_struct(
297+
body: Some(P(ast::AnonConst {
298+
id: ast::DUMMY_NODE_ID,
299+
value: cx.expr_struct(
299300
sp,
300301
test_path("TestDescAndFn"),
301302
thin_vec![
@@ -376,7 +377,7 @@ pub(crate) fn expand_test_or_bench(
376377
field("testfn", test_fn), // }
377378
],
378379
), // }
379-
),
380+
})),
380381
}
381382
.into(),
382383
),

0 commit comments

Comments
 (0)