Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cbee17d

Browse files
committed
Revert "Create const block DefIds in typeck instead of ast lowering"
This reverts commit ddc5f9b.
1 parent 92c54db commit cbee17d

File tree

39 files changed

+190
-168
lines changed

39 files changed

+190
-168
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ pub enum ExprKind {
13921392
/// An array (e.g, `[a, b, c, d]`).
13931393
Array(ThinVec<P<Expr>>),
13941394
/// Allow anonymous constants from an inline `const` block
1395-
ConstBlock(P<Expr>),
1395+
ConstBlock(AnonConst),
13961396
/// A function call
13971397
///
13981398
/// The first field resolves to the function itself,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14171417
match kind {
14181418
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
14191419
ExprKind::ConstBlock(anon_const) => {
1420-
vis.visit_expr(anon_const);
1420+
vis.visit_anon_const(anon_const);
14211421
}
14221422
ExprKind::Repeat(expr, count) => {
14231423
vis.visit_expr(expr);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
959959
ExprKind::Array(subexpressions) => {
960960
walk_list!(visitor, visit_expr, subexpressions);
961961
}
962-
ExprKind::ConstBlock(anon_const) => try_visit!(visitor.visit_expr(anon_const)),
962+
ExprKind::ConstBlock(anon_const) => try_visit!(visitor.visit_anon_const(anon_const)),
963963
ExprKind::Repeat(element, count) => {
964964
try_visit!(visitor.visit_expr(element));
965965
try_visit!(visitor.visit_anon_const(count));

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
7474

7575
let kind = match &e.kind {
7676
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
77-
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_expr(c)),
77+
ExprKind::ConstBlock(c) => {
78+
let c = self.with_new_scopes(c.value.span, |this| hir::ConstBlock {
79+
def_id: this.local_def_id(c.id),
80+
hir_id: this.lower_node_id(c.id),
81+
body: this.lower_const_body(c.value.span, Some(&c.value)),
82+
});
83+
hir::ExprKind::ConstBlock(c)
84+
}
7885
ExprKind::Repeat(expr, count) => {
7986
let expr = self.lower_expr(expr);
8087
let count = self.lower_array_length(count);

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
236236
});
237237
}
238238

239+
fn visit_inline_const(&mut self, constant: &'hir ConstBlock) {
240+
self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant));
241+
242+
self.with_parent(constant.hir_id, |this| {
243+
intravisit::walk_inline_const(this, constant);
244+
});
245+
}
246+
239247
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
240248
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
241249

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,8 @@ impl<'a> State<'a> {
380380
ast::ExprKind::Array(exprs) => {
381381
self.print_expr_vec(exprs);
382382
}
383-
ast::ExprKind::ConstBlock(expr) => {
384-
self.word_space("const");
385-
self.print_expr(expr, FixupContext::default());
383+
ast::ExprKind::ConstBlock(anon_const) => {
384+
self.print_expr_anon_const(anon_const, attrs);
386385
}
387386
ast::ExprKind::Repeat(element, count) => {
388387
self.print_expr_repeat(element, count);

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3838
match node {
3939
hir::Node::Ctor(_)
4040
| hir::Node::AnonConst(_)
41+
| hir::Node::ConstBlock(_)
4142
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => {
4243
hir::Constness::Const
4344
}
@@ -56,7 +57,6 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
5657
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
5758
}
5859
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
59-
hir::Node::Expr(e) if let hir::ExprKind::ConstBlock(_) = e.kind => hir::Constness::Const,
6060
_ => {
6161
if let Some(fn_kind) = node.fn_kind() {
6262
if fn_kind.constness() == hir::Constness::Const {

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,14 @@ pub struct AnonConst {
16231623
pub span: Span,
16241624
}
16251625

1626+
/// An inline constant expression `const { something }`.
1627+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1628+
pub struct ConstBlock {
1629+
pub hir_id: HirId,
1630+
pub def_id: LocalDefId,
1631+
pub body: BodyId,
1632+
}
1633+
16261634
/// An expression.
16271635
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16281636
pub struct Expr<'hir> {
@@ -1909,7 +1917,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
19091917
#[derive(Debug, Clone, Copy, HashStable_Generic)]
19101918
pub enum ExprKind<'hir> {
19111919
/// Allow anonymous constants from an inline `const` block
1912-
ConstBlock(&'hir Expr<'hir>),
1920+
ConstBlock(ConstBlock),
19131921
/// An array (e.g., `[a, b, c, d]`).
19141922
Array(&'hir [Expr<'hir>]),
19151923
/// 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>),
@@ -3701,6 +3710,7 @@ impl<'hir> Node<'hir> {
37013710
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
37023711
Node::Param(..)
37033712
| Node::AnonConst(..)
3713+
| Node::ConstBlock(..)
37043714
| Node::Expr(..)
37053715
| Node::Stmt(..)
37063716
| Node::Block(..)
@@ -3798,6 +3808,7 @@ impl<'hir> Node<'hir> {
37983808
}
37993809

38003810
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
3811+
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
38013812

38023813
_ => None,
38033814
}
@@ -3866,6 +3877,7 @@ impl<'hir> Node<'hir> {
38663877
expect_variant, &'hir Variant<'hir>, Node::Variant(n), n;
38673878
expect_field, &'hir FieldDef<'hir>, Node::Field(n), n;
38683879
expect_anon_const, &'hir AnonConst, Node::AnonConst(n), n;
3880+
expect_inline_const, &'hir ConstBlock, Node::ConstBlock(n), n;
38693881
expect_expr, &'hir Expr<'hir>, Node::Expr(n), n;
38703882
expect_expr_field, &'hir ExprField<'hir>, Node::ExprField(n), n;
38713883
expect_stmt, &'hir Stmt<'hir>, Node::Stmt(n), n;

compiler/rustc_hir/src/intravisit.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ pub trait Visitor<'v>: Sized {
344344
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
345345
walk_anon_const(self, c)
346346
}
347+
fn visit_inline_const(&mut self, c: &'v ConstBlock) -> Self::Result {
348+
walk_inline_const(self, c)
349+
}
347350
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
348351
walk_expr(self, ex)
349352
}
@@ -718,14 +721,22 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
718721
visitor.visit_nested_body(constant.body)
719722
}
720723

724+
pub fn walk_inline_const<'v, V: Visitor<'v>>(
725+
visitor: &mut V,
726+
constant: &'v ConstBlock,
727+
) -> V::Result {
728+
try_visit!(visitor.visit_id(constant.hir_id));
729+
visitor.visit_nested_body(constant.body)
730+
}
731+
721732
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
722733
try_visit!(visitor.visit_id(expression.hir_id));
723734
match expression.kind {
724735
ExprKind::Array(subexpressions) => {
725736
walk_list!(visitor, visit_expr, subexpressions);
726737
}
727738
ExprKind::ConstBlock(ref const_block) => {
728-
try_visit!(visitor.visit_expr(const_block))
739+
try_visit!(visitor.visit_inline_const(const_block))
729740
}
730741
ExprKind::Repeat(ref element, ref count) => {
731742
try_visit!(visitor.visit_expr(element));

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,11 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
407407
match expr.kind {
408408
// Manually recurse over closures and inline consts, because they are the only
409409
// case of nested bodies that share the parent environment.
410-
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
410+
hir::ExprKind::Closure(&hir::Closure { body, .. })
411+
| hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) => {
411412
let body = visitor.tcx.hir().body(body);
412413
visitor.visit_body(body);
413414
}
414-
hir::ExprKind::ConstBlock(expr) => visitor.enter_body(expr.hir_id, |this| {
415-
this.cx.var_parent = None;
416-
resolve_local(this, None, Some(expr));
417-
}),
418415
hir::ExprKind::AssignOp(_, left_expr, right_expr) => {
419416
debug!(
420417
"resolve_expr - enabling pessimistic_yield, was previously {}",

0 commit comments

Comments
 (0)