Skip to content

Commit 6c9b7d6

Browse files
Rollup merge of #109609 - cjgillot:split-anon-const, r=BoxyUwU
Separate AnonConst from ConstBlock in HIR. Their behaviours are different enough to justify having separate nodes.
2 parents dd5d7c7 + ca4d0d4 commit 6c9b7d6

File tree

19 files changed

+111
-68
lines changed

19 files changed

+111
-68
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
7171

7272
let kind = match &e.kind {
7373
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
74-
ExprKind::ConstBlock(anon_const) => {
75-
let anon_const = self.lower_anon_const(anon_const);
76-
hir::ExprKind::ConstBlock(anon_const)
74+
ExprKind::ConstBlock(c) => {
75+
let c = self.with_new_scopes(|this| hir::ConstBlock {
76+
def_id: this.local_def_id(c.id),
77+
hir_id: this.lower_node_id(c.id),
78+
body: this.lower_const_body(c.value.span, Some(&c.value)),
79+
});
80+
hir::ExprKind::ConstBlock(c)
7781
}
7882
ExprKind::Repeat(expr, count) => {
7983
let expr = self.lower_expr(expr);

compiler/rustc_ast_lowering/src/index.rs

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
223223
});
224224
}
225225

226+
fn visit_inline_const(&mut self, constant: &'hir ConstBlock) {
227+
self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant));
228+
229+
self.with_parent(constant.hir_id, |this| {
230+
intravisit::walk_inline_const(this, constant);
231+
});
232+
}
233+
226234
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
227235
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
228236

compiler/rustc_hir/src/hir.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,14 @@ pub struct AnonConst {
16751675
pub body: BodyId,
16761676
}
16771677

1678+
/// An inline constant expression `const { something }`.
1679+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1680+
pub struct ConstBlock {
1681+
pub hir_id: HirId,
1682+
pub def_id: LocalDefId,
1683+
pub body: BodyId,
1684+
}
1685+
16781686
/// An expression.
16791687
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16801688
pub struct Expr<'hir> {
@@ -1922,7 +1930,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
19221930
#[derive(Debug, Clone, Copy, HashStable_Generic)]
19231931
pub enum ExprKind<'hir> {
19241932
/// Allow anonymous constants from an inline `const` block
1925-
ConstBlock(AnonConst),
1933+
ConstBlock(ConstBlock),
19261934
/// An array (e.g., `[a, b, c, d]`).
19271935
Array(&'hir [Expr<'hir>]),
19281936
/// A function call.
@@ -3641,6 +3649,7 @@ pub enum Node<'hir> {
36413649
Variant(&'hir Variant<'hir>),
36423650
Field(&'hir FieldDef<'hir>),
36433651
AnonConst(&'hir AnonConst),
3652+
ConstBlock(&'hir ConstBlock),
36443653
Expr(&'hir Expr<'hir>),
36453654
ExprField(&'hir ExprField<'hir>),
36463655
Stmt(&'hir Stmt<'hir>),
@@ -3695,6 +3704,7 @@ impl<'hir> Node<'hir> {
36953704
Node::TypeBinding(b) => Some(b.ident),
36963705
Node::Param(..)
36973706
| Node::AnonConst(..)
3707+
| Node::ConstBlock(..)
36983708
| Node::Expr(..)
36993709
| Node::Stmt(..)
37003710
| Node::Block(..)
@@ -3758,7 +3768,7 @@ impl<'hir> Node<'hir> {
37583768
})
37593769
| Node::Expr(Expr {
37603770
kind:
3761-
ExprKind::ConstBlock(AnonConst { body, .. })
3771+
ExprKind::ConstBlock(ConstBlock { body, .. })
37623772
| ExprKind::Closure(Closure { body, .. })
37633773
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
37643774
..
@@ -3878,6 +3888,13 @@ impl<'hir> Node<'hir> {
38783888
this
38793889
}
38803890

3891+
/// Expect a [`Node::ConstBlock`] or panic.
3892+
#[track_caller]
3893+
pub fn expect_inline_const(self) -> &'hir ConstBlock {
3894+
let Node::ConstBlock(this) = self else { self.expect_failed("an inline constant") };
3895+
this
3896+
}
3897+
38813898
/// Expect a [`Node::Expr`] or panic.
38823899
#[track_caller]
38833900
pub fn expect_expr(self) -> &'hir Expr<'hir> {

compiler/rustc_hir/src/intravisit.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ pub trait Visitor<'v>: Sized {
335335
fn visit_anon_const(&mut self, c: &'v AnonConst) {
336336
walk_anon_const(self, c)
337337
}
338+
fn visit_inline_const(&mut self, c: &'v ConstBlock) {
339+
walk_inline_const(self, c)
340+
}
338341
fn visit_expr(&mut self, ex: &'v Expr<'v>) {
339342
walk_expr(self, ex)
340343
}
@@ -679,13 +682,18 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
679682
visitor.visit_nested_body(constant.body);
680683
}
681684

685+
pub fn walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock) {
686+
visitor.visit_id(constant.hir_id);
687+
visitor.visit_nested_body(constant.body);
688+
}
689+
682690
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
683691
visitor.visit_id(expression.hir_id);
684692
match expression.kind {
685693
ExprKind::Array(subexpressions) => {
686694
walk_list!(visitor, visit_expr, subexpressions);
687695
}
688-
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
696+
ExprKind::ConstBlock(ref const_block) => visitor.visit_inline_const(const_block),
689697
ExprKind::Repeat(ref element, ref count) => {
690698
visitor.visit_expr(element);
691699
visitor.visit_array_length(count)

compiler/rustc_hir_analysis/src/check/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
392392
// Manually recurse over closures and inline consts, because they are the only
393393
// case of nested bodies that share the parent environment.
394394
hir::ExprKind::Closure(&hir::Closure { body, .. })
395-
| hir::ExprKind::ConstBlock(hir::AnonConst { body, .. }) => {
395+
| hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) => {
396396
let body = visitor.tcx.hir().body(body);
397397
visitor.visit_body(body);
398398
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
123123
{
124124
Some(parent_def_id.to_def_id())
125125
}
126-
Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => {
127-
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
128-
}
129126
// Exclude `GlobalAsm` here which cannot have generics.
130127
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
131128
if asm.operands.iter().any(|(op, _op_sp)| match op {
@@ -142,7 +139,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
142139
}
143140
}
144141
}
145-
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
142+
Node::ConstBlock(_)
143+
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
146144
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
147145
}
148146
Node::Item(item) => match item.kind {
@@ -339,17 +337,14 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
339337
}
340338

341339
// provide junk type parameter defs for const blocks.
342-
if let Node::AnonConst(_) = node {
343-
let parent_node = tcx.hir().get_parent(hir_id);
344-
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
345-
params.push(ty::GenericParamDef {
346-
index: next_index(),
347-
name: Symbol::intern("<const_ty>"),
348-
def_id: def_id.to_def_id(),
349-
pure_wrt_drop: false,
350-
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
351-
});
352-
}
340+
if let Node::ConstBlock(_) = node {
341+
params.push(ty::GenericParamDef {
342+
index: next_index(),
343+
name: Symbol::intern("<const_ty>"),
344+
def_id: def_id.to_def_id(),
345+
pure_wrt_drop: false,
346+
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
347+
});
353348
}
354349

355350
let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
3434
Node::Ty(&Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => {
3535
return tcx.typeck(def_id).node_type(e.hir_id)
3636
}
37-
Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. })
38-
if anon_const.hir_id == hir_id =>
39-
{
40-
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
41-
return substs.as_inline_const().ty()
42-
}
4337
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
4438
| Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. })
4539
if asm.operands.iter().any(|(op, _op_sp)| match op {
@@ -487,6 +481,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
487481

488482
Node::AnonConst(_) => anon_const_type_of(tcx, def_id),
489483

484+
Node::ConstBlock(_) => {
485+
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
486+
substs.as_inline_const().ty()
487+
}
488+
490489
Node::GenericParam(param) => match &param.kind {
491490
GenericParamKind::Type { default: Some(ty), .. }
492491
| GenericParamKind::Const { ty, .. } => icx.to_ty(ty),

compiler/rustc_hir_pretty/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl<'a> State<'a> {
8484
Node::ImplItem(a) => self.print_impl_item(a),
8585
Node::Variant(a) => self.print_variant(a),
8686
Node::AnonConst(a) => self.print_anon_const(a),
87+
Node::ConstBlock(a) => self.print_inline_const(a),
8788
Node::Expr(a) => self.print_expr(a),
8889
Node::ExprField(a) => self.print_expr_field(&a),
8990
Node::Stmt(a) => self.print_stmt(a),
@@ -1095,10 +1096,10 @@ impl<'a> State<'a> {
10951096
self.end()
10961097
}
10971098

1098-
fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) {
1099+
fn print_inline_const(&mut self, constant: &hir::ConstBlock) {
10991100
self.ibox(INDENT_UNIT);
11001101
self.word_space("const");
1101-
self.print_anon_const(anon_const);
1102+
self.ann.nested(self, Nested::Body(constant.body));
11021103
self.end()
11031104
}
11041105

@@ -1370,7 +1371,7 @@ impl<'a> State<'a> {
13701371
self.print_expr_vec(exprs);
13711372
}
13721373
hir::ExprKind::ConstBlock(ref anon_const) => {
1373-
self.print_expr_anon_const(anon_const);
1374+
self.print_inline_const(anon_const);
13741375
}
13751376
hir::ExprKind::Repeat(element, ref count) => {
13761377
self.print_expr_repeat(element, count);

compiler/rustc_hir_typeck/src/expr.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
348348
}
349349
ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
350350
ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
351-
ExprKind::ConstBlock(ref anon_const) => {
352-
self.check_expr_const_block(anon_const, expected, expr)
353-
}
351+
ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected, expr),
354352
ExprKind::Repeat(element, ref count) => {
355353
self.check_expr_repeat(element, count, expected, expr)
356354
}
@@ -1368,20 +1366,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13681366

13691367
fn check_expr_const_block(
13701368
&self,
1371-
anon_const: &'tcx hir::AnonConst,
1369+
block: &'tcx hir::ConstBlock,
13721370
expected: Expectation<'tcx>,
13731371
_expr: &'tcx hir::Expr<'tcx>,
13741372
) -> Ty<'tcx> {
1375-
let body = self.tcx.hir().body(anon_const.body);
1373+
let body = self.tcx.hir().body(block.body);
13761374

13771375
// Create a new function context.
1378-
let def_id = anon_const.def_id;
1376+
let def_id = block.def_id;
13791377
let fcx = FnCtxt::new(self, self.param_env.with_const(), def_id);
13801378
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
13811379

13821380
let ty = fcx.check_expr_with_expectation(&body.value, expected);
13831381
fcx.require_type_is_sized(ty, body.value.span, traits::ConstSized);
1384-
fcx.write_ty(anon_const.hir_id, ty);
1382+
fcx.write_ty(block.hir_id, ty);
13851383
ty
13861384
}
13871385

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
270270
| hir::Node::Variant(..)
271271
| hir::Node::Field(..)
272272
| hir::Node::AnonConst(..)
273+
| hir::Node::ConstBlock(..)
273274
| hir::Node::Stmt(..)
274275
| hir::Node::PathSegment(..)
275276
| hir::Node::Ty(..)

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11911191

11921192
fn should_encode_const(def_kind: DefKind) -> bool {
11931193
match def_kind {
1194-
DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => true,
1194+
DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true,
11951195

11961196
DefKind::Struct
11971197
| DefKind::Union
@@ -1210,7 +1210,6 @@ fn should_encode_const(def_kind: DefKind) -> bool {
12101210
| DefKind::Closure
12111211
| DefKind::Generator
12121212
| DefKind::ConstParam
1213-
| DefKind::InlineConst
12141213
| DefKind::AssocTy
12151214
| DefKind::TyParam
12161215
| DefKind::Trait

compiler/rustc_middle/src/hir/map/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> {
4444
}
4545

4646
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
47+
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
4748

4849
_ => None,
4950
}
@@ -240,15 +241,8 @@ impl<'hir> Map<'hir> {
240241
None => bug!("constructor node without a constructor"),
241242
}
242243
}
243-
Node::AnonConst(_) => {
244-
let inline = match self.find_parent(hir_id) {
245-
Some(Node::Expr(&Expr {
246-
kind: ExprKind::ConstBlock(ref anon_const), ..
247-
})) if anon_const.hir_id == hir_id => true,
248-
_ => false,
249-
};
250-
if inline { DefKind::InlineConst } else { DefKind::AnonConst }
251-
}
244+
Node::AnonConst(_) => DefKind::AnonConst,
245+
Node::ConstBlock(_) => DefKind::InlineConst,
252246
Node::Field(_) => DefKind::Field,
253247
Node::Expr(expr) => match expr.kind {
254248
ExprKind::Closure(Closure { movability: None, .. }) => DefKind::Closure,
@@ -1060,6 +1054,7 @@ impl<'hir> Map<'hir> {
10601054
Node::Variant(variant) => variant.span,
10611055
Node::Field(field) => field.span,
10621056
Node::AnonConst(constant) => self.body(constant.body).value.span,
1057+
Node::ConstBlock(constant) => self.body(constant.body).value.span,
10631058
Node::Expr(expr) => expr.span,
10641059
Node::ExprField(field) => field.span,
10651060
Node::Stmt(stmt) => stmt.span,
@@ -1289,6 +1284,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12891284
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
12901285
}
12911286
Some(Node::AnonConst(_)) => node_str("const"),
1287+
Some(Node::ConstBlock(_)) => node_str("const"),
12921288
Some(Node::Expr(_)) => node_str("expr"),
12931289
Some(Node::ExprField(_)) => node_str("expr field"),
12941290
Some(Node::Stmt(_)) => node_str("stmt"),
@@ -1434,6 +1430,11 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
14341430
intravisit::walk_anon_const(self, c)
14351431
}
14361432

1433+
fn visit_inline_const(&mut self, c: &'hir ConstBlock) {
1434+
self.body_owners.push(c.def_id);
1435+
intravisit::walk_inline_const(self, c)
1436+
}
1437+
14371438
fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
14381439
if let ExprKind::Closure(closure) = ex.kind {
14391440
self.body_owners.push(closure.def_id);

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ fn construct_const<'a, 'tcx>(
556556
span,
557557
..
558558
}) => (*span, ty.span),
559-
Node::AnonConst(_) => {
559+
Node::AnonConst(_) | Node::ConstBlock(_) => {
560560
let span = tcx.def_span(def);
561561
(span, span)
562562
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -582,22 +582,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
582582
/// Converts inline const patterns.
583583
fn lower_inline_const(
584584
&mut self,
585-
anon_const: &'tcx hir::AnonConst,
585+
block: &'tcx hir::ConstBlock,
586586
id: hir::HirId,
587587
span: Span,
588588
) -> PatKind<'tcx> {
589589
let tcx = self.tcx;
590-
let def_id = anon_const.def_id;
591-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
592-
let body_id = match tcx.hir().get(hir_id) {
593-
hir::Node::AnonConst(ac) => ac.body,
594-
_ => span_bug!(
595-
tcx.def_span(def_id.to_def_id()),
596-
"from_inline_const can only process anonymous constants"
597-
),
598-
};
590+
let def_id = block.def_id;
591+
let body_id = block.body;
599592
let expr = &tcx.hir().body(body_id).value;
600-
let ty = tcx.typeck(def_id).node_type(hir_id);
593+
let ty = tcx.typeck(def_id).node_type(block.hir_id);
601594

602595
// Special case inline consts that are just literals. This is solely
603596
// a performance optimization, as we could also just go through the regular

compiler/rustc_mir_transform/src/check_unsafety.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,8 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
421421
intravisit::walk_block(self, block);
422422
}
423423

424-
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
425-
if matches!(self.tcx.def_kind(c.def_id), DefKind::InlineConst) {
426-
self.visit_body(self.tcx.hir().body(c.body))
427-
}
424+
fn visit_inline_const(&mut self, c: &'tcx hir::ConstBlock) {
425+
self.visit_body(self.tcx.hir().body(c.body))
428426
}
429427

430428
fn visit_fn(

0 commit comments

Comments
 (0)