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

+1-1
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

+1-1
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

+1-1
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

+1-8
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

-8
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

+3-2
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

+1-1
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

+1-13
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

+1-12
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

+5-2
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 {}",

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
177177
}
178178
}
179179
}
180-
Node::ConstBlock(_)
181-
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
182-
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
183-
}
180+
Node::Expr(&hir::Expr {
181+
kind: hir::ExprKind::Closure { .. } | hir::ExprKind::ConstBlock { .. },
182+
..
183+
}) => Some(tcx.typeck_root_def_id(def_id.to_def_id())),
184184
Node::Item(item) => match item.kind {
185185
ItemKind::OpaqueTy(&hir::OpaqueTy {
186186
origin:
@@ -415,7 +415,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
415415
}
416416

417417
// provide junk type parameter defs for const blocks.
418-
if let Node::ConstBlock(_) = node {
418+
if let Node::Expr(Expr { kind: ExprKind::ConstBlock(..), .. }) = node {
419419
own_params.push(ty::GenericParamDef {
420420
index: next_index(),
421421
name: Symbol::intern("<const_ty>"),

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
484484
}
485485

486486
Node::AnonConst(_) => anon_const_type_of(tcx, def_id),
487-
488-
Node::ConstBlock(_) => {
487+
Node::Expr(&Expr { kind: ExprKind::ConstBlock(..), .. }) => {
489488
let args = ty::GenericArgs::identity_for_item(tcx, def_id.to_def_id());
490489
args.as_inline_const().ty()
491490
}

compiler/rustc_hir_analysis/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
190190
}
191191
});
192192

193-
// Freeze definitions as we don't add new ones at this point. This improves performance by
194-
// allowing lock-free access to them.
195-
tcx.untracked().definitions.freeze();
196-
197193
// FIXME: Remove this when we implement creating `DefId`s
198194
// for anon constants during their parents' typeck.
199195
// Typeck all body owners in parallel will produce queries
@@ -205,6 +201,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
205201
}
206202
});
207203

204+
// Freeze definitions as we don't add new ones at this point. This improves performance by
205+
// allowing lock-free access to them.
206+
tcx.untracked().definitions.freeze();
207+
208208
tcx.ensure().check_unused_traits(());
209209
}
210210

compiler/rustc_hir_pretty/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ 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),
8887
Node::Expr(a) => self.print_expr(a),
8988
Node::ExprField(a) => self.print_expr_field(a),
9089
Node::Stmt(a) => self.print_stmt(a),
@@ -1049,10 +1048,10 @@ impl<'a> State<'a> {
10491048
self.end()
10501049
}
10511050

1052-
fn print_inline_const(&mut self, constant: &hir::ConstBlock) {
1051+
fn print_inline_const(&mut self, constant: &hir::Expr<'_>) {
10531052
self.ibox(INDENT_UNIT);
10541053
self.word_space("const");
1055-
self.ann.nested(self, Nested::Body(constant.body));
1054+
self.print_expr(constant);
10561055
self.end()
10571056
}
10581057

compiler/rustc_hir_typeck/src/expr.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_errors::{
3232
use rustc_hir as hir;
3333
use rustc_hir::def::{CtorKind, DefKind, Res};
3434
use rustc_hir::def_id::DefId;
35-
use rustc_hir::intravisit::Visitor;
3635
use rustc_hir::lang_items::LangItem;
3736
use rustc_hir::{ExprKind, HirId, QPath};
3837
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _;
@@ -336,7 +335,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
336335
}
337336
ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
338337
ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
339-
ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
338+
ExprKind::ConstBlock(ref block) => self.check_expr_with_expectation(block, expected),
340339
ExprKind::Repeat(element, ref count) => {
341340
self.check_expr_repeat(element, count, expected, expr)
342341
}
@@ -1460,24 +1459,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14601459
}
14611460
}
14621461

1463-
fn check_expr_const_block(
1464-
&self,
1465-
block: &'tcx hir::ConstBlock,
1466-
expected: Expectation<'tcx>,
1467-
) -> Ty<'tcx> {
1468-
let body = self.tcx.hir().body(block.body);
1469-
1470-
// Create a new function context.
1471-
let def_id = block.def_id;
1472-
let fcx = FnCtxt::new(self, self.param_env, def_id);
1473-
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
1474-
1475-
let ty = fcx.check_expr_with_expectation(body.value, expected);
1476-
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized);
1477-
fcx.write_ty(block.hir_id, ty);
1478-
ty
1479-
}
1480-
14811462
fn check_expr_repeat(
14821463
&self,
14831464
element: &'tcx hir::Expr<'tcx>,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551055
.take_while(|(_, node)| {
10561056
// look at parents until we find the first body owner
10571057
node.body_id().is_none()
1058+
&& !matches!(
1059+
node,
1060+
Node::Expr(Expr { kind: ExprKind::ConstBlock { .. }, .. })
1061+
)
10581062
})
10591063
.any(|(parent_id, _)| self.is_loop(parent_id));
10601064

compiler/rustc_hir_typeck/src/upvar.rs

-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> {
149149
self.visit_body(body);
150150
self.fcx.analyze_closure(expr.hir_id, expr.span, body_id, body, capture_clause);
151151
}
152-
hir::ExprKind::ConstBlock(anon_const) => {
153-
let body = self.fcx.tcx.hir().body(anon_const.body);
154-
self.visit_body(body);
155-
}
156152
_ => {}
157153
}
158154

compiler/rustc_hir_typeck/src/writeback.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// generic parameters.
44

55
use crate::FnCtxt;
6+
use hir::def::DefKind;
67
use rustc_data_structures::unord::ExtendUnord;
78
use rustc_errors::{ErrorGuaranteed, StashKey};
89
use rustc_hir as hir;
@@ -16,7 +17,7 @@ use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
1617
use rustc_middle::ty::visit::TypeVisitableExt;
1718
use rustc_middle::ty::TypeSuperFoldable;
1819
use rustc_middle::ty::{self, Ty, TyCtxt};
19-
use rustc_span::symbol::sym;
20+
use rustc_span::symbol::{kw, sym};
2021
use rustc_span::Span;
2122
use rustc_trait_selection::solve;
2223
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
@@ -295,11 +296,11 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
295296
hir::ExprKind::Field(..) | hir::ExprKind::OffsetOf(..) => {
296297
self.visit_field_id(e.hir_id);
297298
}
298-
hir::ExprKind::ConstBlock(anon_const) => {
299-
self.visit_node_id(e.span, anon_const.hir_id);
300-
301-
let body = self.tcx().hir().body(anon_const.body);
302-
self.visit_body(body);
299+
hir::ExprKind::ConstBlock(_) => {
300+
let feed = self.tcx().create_def(self.fcx.body_id, kw::Empty, DefKind::InlineConst);
301+
feed.def_span(e.span);
302+
feed.local_def_id_to_hir_id(e.hir_id);
303+
self.typeck_results.inline_consts.insert(e.hir_id.local_id, feed.def_id());
303304
}
304305
_ => {}
305306
}

0 commit comments

Comments
 (0)