Skip to content

Commit 1be24d7

Browse files
committed
Auto merge of #125918 - oli-obk:const_block_ice, r=compiler-errors
Revert: create const block bodies in typeck via query feeding as per the discussion in #125806 (comment) It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile. reverts the const-block-specific parts of #124650 `@bors` rollup=never had a small perf impact previously fixes #125846 r? `@compiler-errors`
2 parents 468310e + 321d69d commit 1be24d7

File tree

44 files changed

+211
-189
lines changed

Some content is hidden

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

44 files changed

+211
-189
lines changed

compiler/rustc_ast/src/ast.rs

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

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

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
7575
let kind = match &e.kind {
7676
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
7777
ExprKind::ConstBlock(c) => {
78-
self.has_inline_consts = true;
79-
hir::ExprKind::ConstBlock(self.lower_expr(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)
8084
}
8185
ExprKind::Repeat(expr, count) => {
8286
let expr = self.lower_expr(expr);

compiler/rustc_ast_lowering/src/index.rs

+8
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_lowering/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ struct LoweringContext<'a, 'hir> {
9696

9797
/// Bodies inside the owner being lowered.
9898
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
99-
/// Whether there were inline consts that typeck will split out into bodies
100-
has_inline_consts: bool,
10199
/// Attributes inside the owner being lowered.
102100
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
103101
/// Collect items that were created by lowering the current owner.
@@ -160,7 +158,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
160158
item_local_id_counter: hir::ItemLocalId::ZERO,
161159
node_id_to_local_id: Default::default(),
162160
trait_map: Default::default(),
163-
has_inline_consts: false,
164161

165162
// Lowering state.
166163
catch_scope: None,
@@ -570,7 +567,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
570567

571568
let current_attrs = std::mem::take(&mut self.attrs);
572569
let current_bodies = std::mem::take(&mut self.bodies);
573-
let current_has_inline_consts = std::mem::take(&mut self.has_inline_consts);
574570
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
575571
let current_trait_map = std::mem::take(&mut self.trait_map);
576572
let current_owner =
@@ -597,7 +593,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
597593

598594
self.attrs = current_attrs;
599595
self.bodies = current_bodies;
600-
self.has_inline_consts = current_has_inline_consts;
601596
self.node_id_to_local_id = current_node_ids;
602597
self.trait_map = current_trait_map;
603598
self.current_hir_id_owner = current_owner;
@@ -634,7 +629,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
634629
let attrs = std::mem::take(&mut self.attrs);
635630
let mut bodies = std::mem::take(&mut self.bodies);
636631
let trait_map = std::mem::take(&mut self.trait_map);
637-
let has_inline_consts = std::mem::take(&mut self.has_inline_consts);
638632

639633
#[cfg(debug_assertions)]
640634
for (id, attrs) in attrs.iter() {
@@ -652,7 +646,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
652646
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
653647
let num_nodes = self.item_local_id_counter.as_usize();
654648
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
655-
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies, has_inline_consts };
649+
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
656650
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
657651

658652
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })

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

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

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,6 @@ pub struct OwnerNodes<'tcx> {
907907
pub nodes: IndexVec<ItemLocalId, ParentedNode<'tcx>>,
908908
/// Content of local bodies.
909909
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
910-
/// Whether the body contains inline constants that are created for the query system during typeck
911-
/// of the body.
912-
pub has_inline_consts: bool,
913910
}
914911

915912
impl<'tcx> OwnerNodes<'tcx> {
@@ -1626,6 +1623,14 @@ pub struct AnonConst {
16261623
pub span: Span,
16271624
}
16281625

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+
16291634
/// An expression.
16301635
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16311636
pub struct Expr<'hir> {
@@ -1912,7 +1917,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
19121917
#[derive(Debug, Clone, Copy, HashStable_Generic)]
19131918
pub enum ExprKind<'hir> {
19141919
/// Allow anonymous constants from an inline `const` block
1915-
ConstBlock(&'hir Expr<'hir>),
1920+
ConstBlock(ConstBlock),
19161921
/// An array (e.g., `[a, b, c, d]`).
19171922
Array(&'hir [Expr<'hir>]),
19181923
/// A function call.
@@ -3650,6 +3655,7 @@ pub enum Node<'hir> {
36503655
Variant(&'hir Variant<'hir>),
36513656
Field(&'hir FieldDef<'hir>),
36523657
AnonConst(&'hir AnonConst),
3658+
ConstBlock(&'hir ConstBlock),
36533659
Expr(&'hir Expr<'hir>),
36543660
ExprField(&'hir ExprField<'hir>),
36553661
Stmt(&'hir Stmt<'hir>),
@@ -3710,6 +3716,7 @@ impl<'hir> Node<'hir> {
37103716
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
37113717
Node::Param(..)
37123718
| Node::AnonConst(..)
3719+
| Node::ConstBlock(..)
37133720
| Node::Expr(..)
37143721
| Node::Stmt(..)
37153722
| Node::Block(..)
@@ -3807,6 +3814,7 @@ impl<'hir> Node<'hir> {
38073814
}
38083815

38093816
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
3817+
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
38103818

38113819
_ => None,
38123820
}
@@ -3875,6 +3883,7 @@ impl<'hir> Node<'hir> {
38753883
expect_variant, &'hir Variant<'hir>, Node::Variant(n), n;
38763884
expect_field, &'hir FieldDef<'hir>, Node::Field(n), n;
38773885
expect_anon_const, &'hir AnonConst, Node::AnonConst(n), n;
3886+
expect_inline_const, &'hir ConstBlock, Node::ConstBlock(n), n;
38783887
expect_expr, &'hir Expr<'hir>, Node::Expr(n), n;
38793888
expect_expr_field, &'hir ExprField<'hir>, Node::ExprField(n), n;
38803889
expect_stmt, &'hir Stmt<'hir>, Node::Stmt(n), n;

compiler/rustc_hir/src/intravisit.rs

+12-1
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/src/stable_hash_impls.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
9393
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
9494
// the body satisfies the condition of two nodes being different have different
9595
// `hash_stable` results.
96-
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _, has_inline_consts: _ } =
97-
*self;
96+
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
9897
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
9998
}
10099
}

compiler/rustc_hir_analysis/src/check/region.rs

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

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::Expr(&hir::Expr {
181-
kind: hir::ExprKind::Closure { .. } | hir::ExprKind::ConstBlock { .. },
182-
..
183-
}) => Some(tcx.typeck_root_def_id(def_id.to_def_id())),
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+
}
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::Expr(Expr { kind: ExprKind::ConstBlock(..), .. }) = node {
418+
if let Node::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

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

487487
Node::AnonConst(_) => anon_const_type_of(tcx, def_id),
488-
Node::Expr(&Expr { kind: ExprKind::ConstBlock(..), .. }) => {
488+
489+
Node::ConstBlock(_) => {
489490
let args = ty::GenericArgs::identity_for_item(tcx, def_id.to_def_id());
490491
args.as_inline_const().ty()
491492
}

compiler/rustc_hir_analysis/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ 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+
193197
// FIXME: Remove this when we implement creating `DefId`s
194198
// for anon constants during their parents' typeck.
195199
// Typeck all body owners in parallel will produce queries
@@ -201,10 +205,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
201205
}
202206
});
203207

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

+3-2
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),
@@ -1049,10 +1050,10 @@ impl<'a> State<'a> {
10491050
self.end()
10501051
}
10511052

1052-
fn print_inline_const(&mut self, constant: &hir::Expr<'_>) {
1053+
fn print_inline_const(&mut self, constant: &hir::ConstBlock) {
10531054
self.ibox(INDENT_UNIT);
10541055
self.word_space("const");
1055-
self.print_expr(constant);
1056+
self.ann.nested(self, Nested::Body(constant.body));
10561057
self.end()
10571058
}
10581059

compiler/rustc_hir_typeck/src/expr.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_errors::{
3131
use rustc_hir as hir;
3232
use rustc_hir::def::{CtorKind, DefKind, Res};
3333
use rustc_hir::def_id::DefId;
34+
use rustc_hir::intravisit::Visitor;
3435
use rustc_hir::lang_items::LangItem;
3536
use rustc_hir::{ExprKind, HirId, QPath};
3637
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _;
@@ -334,7 +335,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
334335
}
335336
ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
336337
ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
337-
ExprKind::ConstBlock(ref block) => self.check_expr_with_expectation(block, expected),
338+
ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
338339
ExprKind::Repeat(element, ref count) => {
339340
self.check_expr_repeat(element, count, expected, expr)
340341
}
@@ -1457,6 +1458,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14571458
}
14581459
}
14591460

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

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10511051
.take_while(|(_, node)| {
10521052
// look at parents until we find the first body owner
10531053
node.body_id().is_none()
1054-
&& !matches!(
1055-
node,
1056-
Node::Expr(Expr { kind: ExprKind::ConstBlock { .. }, .. })
1057-
)
10581054
})
10591055
.any(|(parent_id, _)| self.is_loop(parent_id));
10601056

0 commit comments

Comments
 (0)