From fe922e567fd9768221e8092413f9492998a67451 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 6 Oct 2020 17:51:15 -0300 Subject: [PATCH] Lower inline const down to MIR --- compiler/rustc_hir/src/hir.rs | 4 ++++ compiler/rustc_hir/src/intravisit.rs | 1 + compiler/rustc_hir_pretty/src/lib.rs | 12 ++++++++++++ .../rustc_mir_build/src/build/expr/as_constant.rs | 1 + compiler/rustc_mir_build/src/build/expr/as_place.rs | 1 + compiler/rustc_mir_build/src/build/expr/as_rvalue.rs | 1 + compiler/rustc_mir_build/src/build/expr/category.rs | 4 +++- compiler/rustc_mir_build/src/build/expr/into.rs | 1 + compiler/rustc_mir_build/src/thir/cx/expr.rs | 6 ++++++ compiler/rustc_mir_build/src/thir/mod.rs | 3 +++ compiler/rustc_mir_build/src/thir/pattern/mod.rs | 5 +++++ compiler/rustc_passes/src/liveness.rs | 3 +++ 12 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 52727e3a6194..0ebed5c34806 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1361,6 +1361,7 @@ impl Expr<'_> { pub fn precedence(&self) -> ExprPrecedence { match self.kind { ExprKind::Box(_) => ExprPrecedence::Box, + ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock, ExprKind::Array(_) => ExprPrecedence::Array, ExprKind::Call(..) => ExprPrecedence::Call, ExprKind::MethodCall(..) => ExprPrecedence::MethodCall, @@ -1446,6 +1447,7 @@ impl Expr<'_> { | ExprKind::LlvmInlineAsm(..) | ExprKind::AssignOp(..) | ExprKind::Lit(_) + | ExprKind::ConstBlock(..) | ExprKind::Unary(..) | ExprKind::Box(..) | ExprKind::AddrOf(..) @@ -1501,6 +1503,8 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool { pub enum ExprKind<'hir> { /// A `box x` expression. Box(&'hir Expr<'hir>), + /// Allow anonymous constants from an inline `const` block + ConstBlock(AnonConst), /// An array (e.g., `[a, b, c, d]`). Array(&'hir [Expr<'hir>]), /// A function call. diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 820d664c07d9..35615af0fc7d 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -1068,6 +1068,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) ExprKind::Array(subexpressions) => { walk_list!(visitor, visit_expr, subexpressions); } + ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const), ExprKind::Repeat(ref element, ref count) => { visitor.visit_expr(element); visitor.visit_anon_const(count) diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 57a38adc1691..4686b4989ae5 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1135,6 +1135,15 @@ impl<'a> State<'a> { self.end() } + fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) { + self.ibox(INDENT_UNIT); + self.s.word_space("const"); + self.s.word("{"); + self.print_anon_const(anon_const); + self.s.word("}"); + self.end() + } + fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) { self.ibox(INDENT_UNIT); self.s.word("["); @@ -1287,6 +1296,9 @@ impl<'a> State<'a> { hir::ExprKind::Array(ref exprs) => { self.print_expr_vec(exprs); } + hir::ExprKind::ConstBlock(ref anon_const) => { + self.print_expr_anon_const(anon_const); + } hir::ExprKind::Repeat(ref element, ref count) => { self.print_expr_repeat(&element, count); } diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 244a70f83b03..3a36ad590c50 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -33,6 +33,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Constant { span, user_ty, literal } } ExprKind::StaticRef { literal, .. } => Constant { span, user_ty: None, literal }, + ExprKind::ConstBlock { value } => Constant { span, user_ty: None, literal: value }, _ => span_bug!(span, "expression is not a valid constant {:?}", kind), } } diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 39dbb6dd3ff9..443025c4f845 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -254,6 +254,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | ExprKind::Continue { .. } | ExprKind::Return { .. } | ExprKind::Literal { .. } + | ExprKind::ConstBlock { .. } | ExprKind::StaticRef { .. } | ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 9c5fddc6b77c..4033cc6cf46c 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -234,6 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } ExprKind::Yield { .. } | ExprKind::Literal { .. } + | ExprKind::ConstBlock { .. } | ExprKind::StaticRef { .. } | ExprKind::Block { .. } | ExprKind::Match { .. } diff --git a/compiler/rustc_mir_build/src/build/expr/category.rs b/compiler/rustc_mir_build/src/build/expr/category.rs index 9cabd186d846..ac5cf187aa01 100644 --- a/compiler/rustc_mir_build/src/build/expr/category.rs +++ b/compiler/rustc_mir_build/src/build/expr/category.rs @@ -68,7 +68,9 @@ impl Category { | ExprKind::ThreadLocalRef(_) | ExprKind::LlvmInlineAsm { .. } => Some(Category::Rvalue(RvalueFunc::AsRvalue)), - ExprKind::Literal { .. } | ExprKind::StaticRef { .. } => Some(Category::Constant), + ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } | ExprKind::StaticRef { .. } => { + Some(Category::Constant) + } ExprKind::Loop { .. } | ExprKind::Block { .. } diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index a12c22fb850e..a268b0b46ff5 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -454,6 +454,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | ExprKind::Array { .. } | ExprKind::Tuple { .. } | ExprKind::Closure { .. } + | ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } | ExprKind::ThreadLocalRef(_) | ExprKind::StaticRef { .. } => { diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 13e69474cfb9..731bd954246c 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -511,6 +511,12 @@ fn make_mirror_unadjusted<'a, 'tcx>( inputs: asm.inputs_exprs.to_ref(), }, + hir::ExprKind::ConstBlock(ref anon_const) => { + let anon_const_def_id = cx.tcx.hir().local_def_id(anon_const.hir_id); + let value = ty::Const::from_anon_const(cx.tcx, anon_const_def_id); + + ExprKind::ConstBlock { value } + } // Now comes the rote stuff: hir::ExprKind::Repeat(ref v, ref count) => { let count_def_id = cx.tcx.hir().local_def_id(count.hir_id); diff --git a/compiler/rustc_mir_build/src/thir/mod.rs b/compiler/rustc_mir_build/src/thir/mod.rs index 4d57fd5c64f8..f2a2ef0d8f2b 100644 --- a/compiler/rustc_mir_build/src/thir/mod.rs +++ b/compiler/rustc_mir_build/src/thir/mod.rs @@ -232,6 +232,9 @@ crate enum ExprKind<'tcx> { Return { value: Option>, }, + ConstBlock { + value: &'tcx Const<'tcx>, + }, Repeat { value: ExprRef<'tcx>, count: &'tcx Const<'tcx>, diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index d46e9a98825a..b05ddb3b4645 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -856,6 +856,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { *self.lower_path(qpath, expr.hir_id, expr.span).kind } else { let (lit, neg) = match expr.kind { + hir::ExprKind::ConstBlock(ref anon_const) => { + let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); + let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id); + return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind; + } hir::ExprKind::Lit(ref lit) => (lit, false), hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => { let lit = match expr.kind { diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index e8b97d7dc7d5..ae810b9e79a5 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -432,6 +432,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { | hir::ExprKind::Break(..) | hir::ExprKind::Continue(_) | hir::ExprKind::Lit(_) + | hir::ExprKind::ConstBlock(..) | hir::ExprKind::Ret(..) | hir::ExprKind::Block(..) | hir::ExprKind::Assign(..) @@ -1232,6 +1233,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { } hir::ExprKind::Lit(..) + | hir::ExprKind::ConstBlock(..) | hir::ExprKind::Err | hir::ExprKind::Path(hir::QPath::TypeRelative(..)) | hir::ExprKind::Path(hir::QPath::LangItem(..)) => succ, @@ -1478,6 +1480,7 @@ fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr<'tcx>) { | hir::ExprKind::Break(..) | hir::ExprKind::Continue(..) | hir::ExprKind::Lit(_) + | hir::ExprKind::ConstBlock(..) | hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) | hir::ExprKind::Struct(..)