Separate AnonConst from ConstBlock in HIR.
This commit is contained in:
parent
794249d768
commit
ca4d0d4c24
19 changed files with 111 additions and 68 deletions
|
@ -1675,6 +1675,14 @@ pub struct AnonConst {
|
|||
pub body: BodyId,
|
||||
}
|
||||
|
||||
/// An inline constant expression `const { something }`.
|
||||
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
||||
pub struct ConstBlock {
|
||||
pub hir_id: HirId,
|
||||
pub def_id: LocalDefId,
|
||||
pub body: BodyId,
|
||||
}
|
||||
|
||||
/// An expression.
|
||||
#[derive(Debug, Clone, Copy, HashStable_Generic)]
|
||||
pub struct Expr<'hir> {
|
||||
|
@ -1922,7 +1930,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
|
|||
#[derive(Debug, Clone, Copy, HashStable_Generic)]
|
||||
pub enum ExprKind<'hir> {
|
||||
/// Allow anonymous constants from an inline `const` block
|
||||
ConstBlock(AnonConst),
|
||||
ConstBlock(ConstBlock),
|
||||
/// An array (e.g., `[a, b, c, d]`).
|
||||
Array(&'hir [Expr<'hir>]),
|
||||
/// A function call.
|
||||
|
@ -3641,6 +3649,7 @@ pub enum Node<'hir> {
|
|||
Variant(&'hir Variant<'hir>),
|
||||
Field(&'hir FieldDef<'hir>),
|
||||
AnonConst(&'hir AnonConst),
|
||||
ConstBlock(&'hir ConstBlock),
|
||||
Expr(&'hir Expr<'hir>),
|
||||
ExprField(&'hir ExprField<'hir>),
|
||||
Stmt(&'hir Stmt<'hir>),
|
||||
|
@ -3695,6 +3704,7 @@ impl<'hir> Node<'hir> {
|
|||
Node::TypeBinding(b) => Some(b.ident),
|
||||
Node::Param(..)
|
||||
| Node::AnonConst(..)
|
||||
| Node::ConstBlock(..)
|
||||
| Node::Expr(..)
|
||||
| Node::Stmt(..)
|
||||
| Node::Block(..)
|
||||
|
@ -3758,7 +3768,7 @@ impl<'hir> Node<'hir> {
|
|||
})
|
||||
| Node::Expr(Expr {
|
||||
kind:
|
||||
ExprKind::ConstBlock(AnonConst { body, .. })
|
||||
ExprKind::ConstBlock(ConstBlock { body, .. })
|
||||
| ExprKind::Closure(Closure { body, .. })
|
||||
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
|
||||
..
|
||||
|
@ -3878,6 +3888,13 @@ impl<'hir> Node<'hir> {
|
|||
this
|
||||
}
|
||||
|
||||
/// Expect a [`Node::ConstBlock`] or panic.
|
||||
#[track_caller]
|
||||
pub fn expect_inline_const(self) -> &'hir ConstBlock {
|
||||
let Node::ConstBlock(this) = self else { self.expect_failed("an inline constant") };
|
||||
this
|
||||
}
|
||||
|
||||
/// Expect a [`Node::Expr`] or panic.
|
||||
#[track_caller]
|
||||
pub fn expect_expr(self) -> &'hir Expr<'hir> {
|
||||
|
|
|
@ -335,6 +335,9 @@ pub trait Visitor<'v>: Sized {
|
|||
fn visit_anon_const(&mut self, c: &'v AnonConst) {
|
||||
walk_anon_const(self, c)
|
||||
}
|
||||
fn visit_inline_const(&mut self, c: &'v ConstBlock) {
|
||||
walk_inline_const(self, c)
|
||||
}
|
||||
fn visit_expr(&mut self, ex: &'v Expr<'v>) {
|
||||
walk_expr(self, ex)
|
||||
}
|
||||
|
@ -679,13 +682,18 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
|
|||
visitor.visit_nested_body(constant.body);
|
||||
}
|
||||
|
||||
pub fn walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock) {
|
||||
visitor.visit_id(constant.hir_id);
|
||||
visitor.visit_nested_body(constant.body);
|
||||
}
|
||||
|
||||
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
|
||||
visitor.visit_id(expression.hir_id);
|
||||
match expression.kind {
|
||||
ExprKind::Array(subexpressions) => {
|
||||
walk_list!(visitor, visit_expr, subexpressions);
|
||||
}
|
||||
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
|
||||
ExprKind::ConstBlock(ref const_block) => visitor.visit_inline_const(const_block),
|
||||
ExprKind::Repeat(ref element, ref count) => {
|
||||
visitor.visit_expr(element);
|
||||
visitor.visit_array_length(count)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue