Skip to content

Commit fe922e5

Browse files
committed
Lower inline const down to MIR
1 parent 66e2543 commit fe922e5

File tree

12 files changed

+41
-1
lines changed

12 files changed

+41
-1
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ impl Expr<'_> {
13611361
pub fn precedence(&self) -> ExprPrecedence {
13621362
match self.kind {
13631363
ExprKind::Box(_) => ExprPrecedence::Box,
1364+
ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock,
13641365
ExprKind::Array(_) => ExprPrecedence::Array,
13651366
ExprKind::Call(..) => ExprPrecedence::Call,
13661367
ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
@@ -1446,6 +1447,7 @@ impl Expr<'_> {
14461447
| ExprKind::LlvmInlineAsm(..)
14471448
| ExprKind::AssignOp(..)
14481449
| ExprKind::Lit(_)
1450+
| ExprKind::ConstBlock(..)
14491451
| ExprKind::Unary(..)
14501452
| ExprKind::Box(..)
14511453
| ExprKind::AddrOf(..)
@@ -1501,6 +1503,8 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
15011503
pub enum ExprKind<'hir> {
15021504
/// A `box x` expression.
15031505
Box(&'hir Expr<'hir>),
1506+
/// Allow anonymous constants from an inline `const` block
1507+
ConstBlock(AnonConst),
15041508
/// An array (e.g., `[a, b, c, d]`).
15051509
Array(&'hir [Expr<'hir>]),
15061510
/// A function call.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
10681068
ExprKind::Array(subexpressions) => {
10691069
walk_list!(visitor, visit_expr, subexpressions);
10701070
}
1071+
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
10711072
ExprKind::Repeat(ref element, ref count) => {
10721073
visitor.visit_expr(element);
10731074
visitor.visit_anon_const(count)

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,15 @@ impl<'a> State<'a> {
11351135
self.end()
11361136
}
11371137

1138+
fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) {
1139+
self.ibox(INDENT_UNIT);
1140+
self.s.word_space("const");
1141+
self.s.word("{");
1142+
self.print_anon_const(anon_const);
1143+
self.s.word("}");
1144+
self.end()
1145+
}
1146+
11381147
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) {
11391148
self.ibox(INDENT_UNIT);
11401149
self.s.word("[");
@@ -1287,6 +1296,9 @@ impl<'a> State<'a> {
12871296
hir::ExprKind::Array(ref exprs) => {
12881297
self.print_expr_vec(exprs);
12891298
}
1299+
hir::ExprKind::ConstBlock(ref anon_const) => {
1300+
self.print_expr_anon_const(anon_const);
1301+
}
12901302
hir::ExprKind::Repeat(ref element, ref count) => {
12911303
self.print_expr_repeat(&element, count);
12921304
}

compiler/rustc_mir_build/src/build/expr/as_constant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3333
Constant { span, user_ty, literal }
3434
}
3535
ExprKind::StaticRef { literal, .. } => Constant { span, user_ty: None, literal },
36+
ExprKind::ConstBlock { value } => Constant { span, user_ty: None, literal: value },
3637
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
3738
}
3839
}

compiler/rustc_mir_build/src/build/expr/as_place.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
254254
| ExprKind::Continue { .. }
255255
| ExprKind::Return { .. }
256256
| ExprKind::Literal { .. }
257+
| ExprKind::ConstBlock { .. }
257258
| ExprKind::StaticRef { .. }
258259
| ExprKind::InlineAsm { .. }
259260
| ExprKind::LlvmInlineAsm { .. }

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
234234
}
235235
ExprKind::Yield { .. }
236236
| ExprKind::Literal { .. }
237+
| ExprKind::ConstBlock { .. }
237238
| ExprKind::StaticRef { .. }
238239
| ExprKind::Block { .. }
239240
| ExprKind::Match { .. }

compiler/rustc_mir_build/src/build/expr/category.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl Category {
6868
| ExprKind::ThreadLocalRef(_)
6969
| ExprKind::LlvmInlineAsm { .. } => Some(Category::Rvalue(RvalueFunc::AsRvalue)),
7070

71-
ExprKind::Literal { .. } | ExprKind::StaticRef { .. } => Some(Category::Constant),
71+
ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } | ExprKind::StaticRef { .. } => {
72+
Some(Category::Constant)
73+
}
7274

7375
ExprKind::Loop { .. }
7476
| ExprKind::Block { .. }

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
454454
| ExprKind::Array { .. }
455455
| ExprKind::Tuple { .. }
456456
| ExprKind::Closure { .. }
457+
| ExprKind::ConstBlock { .. }
457458
| ExprKind::Literal { .. }
458459
| ExprKind::ThreadLocalRef(_)
459460
| ExprKind::StaticRef { .. } => {

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,12 @@ fn make_mirror_unadjusted<'a, 'tcx>(
511511
inputs: asm.inputs_exprs.to_ref(),
512512
},
513513

514+
hir::ExprKind::ConstBlock(ref anon_const) => {
515+
let anon_const_def_id = cx.tcx.hir().local_def_id(anon_const.hir_id);
516+
let value = ty::Const::from_anon_const(cx.tcx, anon_const_def_id);
517+
518+
ExprKind::ConstBlock { value }
519+
}
514520
// Now comes the rote stuff:
515521
hir::ExprKind::Repeat(ref v, ref count) => {
516522
let count_def_id = cx.tcx.hir().local_def_id(count.hir_id);

compiler/rustc_mir_build/src/thir/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ crate enum ExprKind<'tcx> {
232232
Return {
233233
value: Option<ExprRef<'tcx>>,
234234
},
235+
ConstBlock {
236+
value: &'tcx Const<'tcx>,
237+
},
235238
Repeat {
236239
value: ExprRef<'tcx>,
237240
count: &'tcx Const<'tcx>,

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
856856
*self.lower_path(qpath, expr.hir_id, expr.span).kind
857857
} else {
858858
let (lit, neg) = match expr.kind {
859+
hir::ExprKind::ConstBlock(ref anon_const) => {
860+
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
861+
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
862+
return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind;
863+
}
859864
hir::ExprKind::Lit(ref lit) => (lit, false),
860865
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => {
861866
let lit = match expr.kind {

compiler/rustc_passes/src/liveness.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
432432
| hir::ExprKind::Break(..)
433433
| hir::ExprKind::Continue(_)
434434
| hir::ExprKind::Lit(_)
435+
| hir::ExprKind::ConstBlock(..)
435436
| hir::ExprKind::Ret(..)
436437
| hir::ExprKind::Block(..)
437438
| hir::ExprKind::Assign(..)
@@ -1232,6 +1233,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12321233
}
12331234

12341235
hir::ExprKind::Lit(..)
1236+
| hir::ExprKind::ConstBlock(..)
12351237
| hir::ExprKind::Err
12361238
| hir::ExprKind::Path(hir::QPath::TypeRelative(..))
12371239
| hir::ExprKind::Path(hir::QPath::LangItem(..)) => succ,
@@ -1478,6 +1480,7 @@ fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr<'tcx>) {
14781480
| hir::ExprKind::Break(..)
14791481
| hir::ExprKind::Continue(..)
14801482
| hir::ExprKind::Lit(_)
1483+
| hir::ExprKind::ConstBlock(..)
14811484
| hir::ExprKind::Block(..)
14821485
| hir::ExprKind::AddrOf(..)
14831486
| hir::ExprKind::Struct(..)

0 commit comments

Comments
 (0)