Skip to content

Commit ddc5f9b

Browse files
committed
Create const block DefIds in typeck instead of ast lowering
1 parent e5cba17 commit ddc5f9b

File tree

39 files changed

+162
-189
lines changed

39 files changed

+162
-189
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ pub enum ExprKind {
13931393
/// An array (e.g, `[a, b, c, d]`).
13941394
Array(ThinVec<P<Expr>>),
13951395
/// Allow anonymous constants from an inline `const` block
1396-
ConstBlock(AnonConst),
1396+
ConstBlock(P<Expr>),
13971397
/// A function call
13981398
///
13991399
/// 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
@@ -1411,7 +1411,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14111411
match kind {
14121412
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
14131413
ExprKind::ConstBlock(anon_const) => {
1414-
vis.visit_anon_const(anon_const);
1414+
vis.visit_expr(anon_const);
14151415
}
14161416
ExprKind::Repeat(expr, count) => {
14171417
vis.visit_expr(expr);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
951951
ExprKind::Array(subexpressions) => {
952952
walk_list!(visitor, visit_expr, subexpressions);
953953
}
954-
ExprKind::ConstBlock(anon_const) => try_visit!(visitor.visit_anon_const(anon_const)),
954+
ExprKind::ConstBlock(anon_const) => try_visit!(visitor.visit_expr(anon_const)),
955955
ExprKind::Repeat(element, count) => {
956956
try_visit!(visitor.visit_expr(element));
957957
try_visit!(visitor.visit_anon_const(count));

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,7 @@ 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) => {
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-
}
77+
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_expr(c)),
8578
ExprKind::Repeat(expr, count) => {
8679
let expr = self.lower_expr(expr);
8780
let count = self.lower_array_length(count);

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,6 @@ 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-
247239
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
248240
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
249241

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,9 @@ impl<'a> State<'a> {
380380
ast::ExprKind::Array(exprs) => {
381381
self.print_expr_vec(exprs);
382382
}
383-
ast::ExprKind::ConstBlock(anon_const) => {
384-
self.print_expr_anon_const(anon_const, attrs);
383+
ast::ExprKind::ConstBlock(expr) => {
384+
self.word_space("const");
385+
self.print_expr(expr, FixupContext::default());
385386
}
386387
ast::ExprKind::Repeat(element, count) => {
387388
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,7 +38,6 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3838
match node {
3939
hir::Node::Ctor(_)
4040
| hir::Node::AnonConst(_)
41-
| hir::Node::ConstBlock(_)
4241
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => {
4342
hir::Constness::Const
4443
}
@@ -57,6 +56,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
5756
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
5857
}
5958
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: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,14 +1592,6 @@ pub struct AnonConst {
15921592
pub span: Span,
15931593
}
15941594

1595-
/// An inline constant expression `const { something }`.
1596-
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1597-
pub struct ConstBlock {
1598-
pub hir_id: HirId,
1599-
pub def_id: LocalDefId,
1600-
pub body: BodyId,
1601-
}
1602-
16031595
/// An expression.
16041596
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16051597
pub struct Expr<'hir> {
@@ -1886,7 +1878,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
18861878
#[derive(Debug, Clone, Copy, HashStable_Generic)]
18871879
pub enum ExprKind<'hir> {
18881880
/// Allow anonymous constants from an inline `const` block
1889-
ConstBlock(ConstBlock),
1881+
ConstBlock(&'hir Expr<'hir>),
18901882
/// An array (e.g., `[a, b, c, d]`).
18911883
Array(&'hir [Expr<'hir>]),
18921884
/// A function call.
@@ -3609,7 +3601,6 @@ pub enum Node<'hir> {
36093601
Variant(&'hir Variant<'hir>),
36103602
Field(&'hir FieldDef<'hir>),
36113603
AnonConst(&'hir AnonConst),
3612-
ConstBlock(&'hir ConstBlock),
36133604
Expr(&'hir Expr<'hir>),
36143605
ExprField(&'hir ExprField<'hir>),
36153606
Stmt(&'hir Stmt<'hir>),
@@ -3670,7 +3661,6 @@ impl<'hir> Node<'hir> {
36703661
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
36713662
Node::Param(..)
36723663
| Node::AnonConst(..)
3673-
| Node::ConstBlock(..)
36743664
| Node::Expr(..)
36753665
| Node::Stmt(..)
36763666
| Node::Block(..)
@@ -3768,7 +3758,6 @@ impl<'hir> Node<'hir> {
37683758
}
37693759

37703760
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
3771-
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
37723761

37733762
_ => None,
37743763
}
@@ -3837,7 +3826,6 @@ impl<'hir> Node<'hir> {
38373826
expect_variant, &'hir Variant<'hir>, Node::Variant(n), n;
38383827
expect_field, &'hir FieldDef<'hir>, Node::Field(n), n;
38393828
expect_anon_const, &'hir AnonConst, Node::AnonConst(n), n;
3840-
expect_inline_const, &'hir ConstBlock, Node::ConstBlock(n), n;
38413829
expect_expr, &'hir Expr<'hir>, Node::Expr(n), n;
38423830
expect_expr_field, &'hir ExprField<'hir>, Node::ExprField(n), n;
38433831
expect_stmt, &'hir Stmt<'hir>, Node::Stmt(n), n;

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,6 @@ 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-
}
350347
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
351348
walk_expr(self, ex)
352349
}
@@ -716,22 +713,14 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
716713
visitor.visit_nested_body(constant.body)
717714
}
718715

719-
pub fn walk_inline_const<'v, V: Visitor<'v>>(
720-
visitor: &mut V,
721-
constant: &'v ConstBlock,
722-
) -> V::Result {
723-
try_visit!(visitor.visit_id(constant.hir_id));
724-
visitor.visit_nested_body(constant.body)
725-
}
726-
727716
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
728717
try_visit!(visitor.visit_id(expression.hir_id));
729718
match expression.kind {
730719
ExprKind::Array(subexpressions) => {
731720
walk_list!(visitor, visit_expr, subexpressions);
732721
}
733722
ExprKind::ConstBlock(ref const_block) => {
734-
try_visit!(visitor.visit_inline_const(const_block))
723+
try_visit!(visitor.visit_expr(const_block))
735724
}
736725
ExprKind::Repeat(ref element, ref count) => {
737726
try_visit!(visitor.visit_expr(element));

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,14 @@ 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, .. })
411-
| hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) => {
410+
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
412411
let body = visitor.tcx.hir().body(body);
413412
visitor.visit_body(body);
414413
}
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+
}),
415418
hir::ExprKind::AssignOp(_, left_expr, right_expr) => {
416419
debug!(
417420
"resolve_expr - enabling pessimistic_yield, was previously {}",

0 commit comments

Comments
 (0)