Skip to content

Commit 7717a30

Browse files
committed
Auto merge of #124650 - oli-obk:pattern_types_syntax, r=nnethercote
Create const block DefIds in typeck instead of ast lowering this is a prerequisite for cleaning up pattern types and the range pattern HIR nodes in general. Right now they contain expressions, but they are supposed to only contain constants. In order to generate the anonymous constants lazily during typeck, we need to support generating new items with bodies during typeck in general. Transforming const blocks was the simplest change I could find to allow us to do that (everything else is much more invasive if we haven't already done it for const blocks).
2 parents f989d2f + ac7e836 commit 7717a30

File tree

44 files changed

+263
-277
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

+263
-277
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

+2-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ 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-
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)
78+
self.has_inline_consts = true;
79+
hir::ExprKind::ConstBlock(self.lower_expr(c))
8480
}
8581
ExprKind::Repeat(expr, count) => {
8682
let expr = self.lower_expr(expr);

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ 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,
99101
/// Attributes inside the owner being lowered.
100102
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
101103
/// Collect items that were created by lowering the current owner.
@@ -158,6 +160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
158160
item_local_id_counter: hir::ItemLocalId::ZERO,
159161
node_id_to_local_id: Default::default(),
160162
trait_map: Default::default(),
163+
has_inline_consts: false,
161164

162165
// Lowering state.
163166
catch_scope: None,
@@ -567,6 +570,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
567570

568571
let current_attrs = std::mem::take(&mut self.attrs);
569572
let current_bodies = std::mem::take(&mut self.bodies);
573+
let current_has_inline_consts = std::mem::take(&mut self.has_inline_consts);
570574
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
571575
let current_trait_map = std::mem::take(&mut self.trait_map);
572576
let current_owner =
@@ -593,6 +597,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
593597

594598
self.attrs = current_attrs;
595599
self.bodies = current_bodies;
600+
self.has_inline_consts = current_has_inline_consts;
596601
self.node_id_to_local_id = current_node_ids;
597602
self.trait_map = current_trait_map;
598603
self.current_hir_id_owner = current_owner;
@@ -629,6 +634,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
629634
let attrs = std::mem::take(&mut self.attrs);
630635
let mut bodies = std::mem::take(&mut self.bodies);
631636
let trait_map = std::mem::take(&mut self.trait_map);
637+
let has_inline_consts = std::mem::take(&mut self.has_inline_consts);
632638

633639
#[cfg(debug_assertions)]
634640
for (id, attrs) in attrs.iter() {
@@ -646,7 +652,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
646652
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
647653
let num_nodes = self.item_local_id_counter.as_usize();
648654
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
649-
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
655+
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies, has_inline_consts };
650656
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
651657

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

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_const_eval/src/const_eval/machine.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::ops::ControlFlow;
66
use rustc_ast::Mutability;
77
use rustc_data_structures::fx::FxIndexMap;
88
use rustc_data_structures::fx::IndexEntry;
9-
use rustc_hir::def::DefKind;
109
use rustc_hir::def_id::DefId;
1110
use rustc_hir::def_id::LocalDefId;
1211
use rustc_hir::LangItem;
@@ -392,18 +391,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeInterpreter<'tcx> {
392391
instance: ty::InstanceDef<'tcx>,
393392
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
394393
match instance {
395-
ty::InstanceDef::Item(def) => {
396-
if ecx.tcx.is_ctfe_mir_available(def) {
397-
Ok(ecx.tcx.mir_for_ctfe(def))
398-
} else if ecx.tcx.def_kind(def) == DefKind::AssocConst {
399-
ecx.tcx.dcx().bug("This is likely a const item that is missing from its impl");
400-
} else {
401-
// `find_mir_or_eval_fn` checks that this is a const fn before even calling us,
402-
// so this should be unreachable.
403-
let path = ecx.tcx.def_path_str(def);
404-
bug!("trying to call extern function `{path}` at compile-time");
405-
}
406-
}
394+
ty::InstanceDef::Item(def) => Ok(ecx.tcx.mir_for_ctfe(def)),
407395
_ => Ok(ecx.tcx.instance_mir(instance)),
408396
}
409397
}

compiler/rustc_hir/src/hir.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,9 @@ pub struct OwnerNodes<'tcx> {
876876
pub nodes: IndexVec<ItemLocalId, ParentedNode<'tcx>>,
877877
/// Content of local bodies.
878878
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
879+
/// Whether the body contains inline constants that are created for the query system during typeck
880+
/// of the body.
881+
pub has_inline_consts: bool,
879882
}
880883

881884
impl<'tcx> OwnerNodes<'tcx> {
@@ -1592,14 +1595,6 @@ pub struct AnonConst {
15921595
pub span: Span,
15931596
}
15941597

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-
16031598
/// An expression.
16041599
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16051600
pub struct Expr<'hir> {
@@ -1886,7 +1881,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
18861881
#[derive(Debug, Clone, Copy, HashStable_Generic)]
18871882
pub enum ExprKind<'hir> {
18881883
/// Allow anonymous constants from an inline `const` block
1889-
ConstBlock(ConstBlock),
1884+
ConstBlock(&'hir Expr<'hir>),
18901885
/// An array (e.g., `[a, b, c, d]`).
18911886
Array(&'hir [Expr<'hir>]),
18921887
/// A function call.
@@ -3609,7 +3604,6 @@ pub enum Node<'hir> {
36093604
Variant(&'hir Variant<'hir>),
36103605
Field(&'hir FieldDef<'hir>),
36113606
AnonConst(&'hir AnonConst),
3612-
ConstBlock(&'hir ConstBlock),
36133607
Expr(&'hir Expr<'hir>),
36143608
ExprField(&'hir ExprField<'hir>),
36153609
Stmt(&'hir Stmt<'hir>),
@@ -3670,7 +3664,6 @@ impl<'hir> Node<'hir> {
36703664
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
36713665
Node::Param(..)
36723666
| Node::AnonConst(..)
3673-
| Node::ConstBlock(..)
36743667
| Node::Expr(..)
36753668
| Node::Stmt(..)
36763669
| Node::Block(..)
@@ -3768,7 +3761,6 @@ impl<'hir> Node<'hir> {
37683761
}
37693762

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

37733765
_ => None,
37743766
}
@@ -3837,7 +3829,6 @@ impl<'hir> Node<'hir> {
38373829
expect_variant, &'hir Variant<'hir>, Node::Variant(n), n;
38383830
expect_field, &'hir FieldDef<'hir>, Node::Field(n), n;
38393831
expect_anon_const, &'hir AnonConst, Node::AnonConst(n), n;
3840-
expect_inline_const, &'hir ConstBlock, Node::ConstBlock(n), n;
38413832
expect_expr, &'hir Expr<'hir>, Node::Expr(n), n;
38423833
expect_expr_field, &'hir ExprField<'hir>, Node::ExprField(n), n;
38433834
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/src/stable_hash_impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ 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: _ } = *self;
96+
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _, has_inline_consts: _ } =
97+
*self;
9798
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
9899
}
99100
}

0 commit comments

Comments
 (0)