1
Fork 0

Lower inline const down to MIR

This commit is contained in:
Santiago Pastorino 2020-10-06 17:51:15 -03:00
parent 66e254314d
commit fe922e567f
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
12 changed files with 41 additions and 1 deletions

View file

@ -1361,6 +1361,7 @@ impl Expr<'_> {
pub fn precedence(&self) -> ExprPrecedence { pub fn precedence(&self) -> ExprPrecedence {
match self.kind { match self.kind {
ExprKind::Box(_) => ExprPrecedence::Box, ExprKind::Box(_) => ExprPrecedence::Box,
ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock,
ExprKind::Array(_) => ExprPrecedence::Array, ExprKind::Array(_) => ExprPrecedence::Array,
ExprKind::Call(..) => ExprPrecedence::Call, ExprKind::Call(..) => ExprPrecedence::Call,
ExprKind::MethodCall(..) => ExprPrecedence::MethodCall, ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
@ -1446,6 +1447,7 @@ impl Expr<'_> {
| ExprKind::LlvmInlineAsm(..) | ExprKind::LlvmInlineAsm(..)
| ExprKind::AssignOp(..) | ExprKind::AssignOp(..)
| ExprKind::Lit(_) | ExprKind::Lit(_)
| ExprKind::ConstBlock(..)
| ExprKind::Unary(..) | ExprKind::Unary(..)
| ExprKind::Box(..) | ExprKind::Box(..)
| ExprKind::AddrOf(..) | ExprKind::AddrOf(..)
@ -1501,6 +1503,8 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
pub enum ExprKind<'hir> { pub enum ExprKind<'hir> {
/// A `box x` expression. /// A `box x` expression.
Box(&'hir Expr<'hir>), Box(&'hir Expr<'hir>),
/// Allow anonymous constants from an inline `const` block
ConstBlock(AnonConst),
/// An array (e.g., `[a, b, c, d]`). /// An array (e.g., `[a, b, c, d]`).
Array(&'hir [Expr<'hir>]), Array(&'hir [Expr<'hir>]),
/// A function call. /// A function call.

View file

@ -1068,6 +1068,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
ExprKind::Array(subexpressions) => { ExprKind::Array(subexpressions) => {
walk_list!(visitor, visit_expr, subexpressions); walk_list!(visitor, visit_expr, subexpressions);
} }
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
ExprKind::Repeat(ref element, ref count) => { ExprKind::Repeat(ref element, ref count) => {
visitor.visit_expr(element); visitor.visit_expr(element);
visitor.visit_anon_const(count) visitor.visit_anon_const(count)

View file

@ -1135,6 +1135,15 @@ impl<'a> State<'a> {
self.end() 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) { fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) {
self.ibox(INDENT_UNIT); self.ibox(INDENT_UNIT);
self.s.word("["); self.s.word("[");
@ -1287,6 +1296,9 @@ impl<'a> State<'a> {
hir::ExprKind::Array(ref exprs) => { hir::ExprKind::Array(ref exprs) => {
self.print_expr_vec(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) => { hir::ExprKind::Repeat(ref element, ref count) => {
self.print_expr_repeat(&element, count); self.print_expr_repeat(&element, count);
} }

View file

@ -33,6 +33,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty, literal } Constant { span, user_ty, literal }
} }
ExprKind::StaticRef { literal, .. } => Constant { span, user_ty: None, 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), _ => span_bug!(span, "expression is not a valid constant {:?}", kind),
} }
} }

View file

@ -254,6 +254,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::Continue { .. } | ExprKind::Continue { .. }
| ExprKind::Return { .. } | ExprKind::Return { .. }
| ExprKind::Literal { .. } | ExprKind::Literal { .. }
| ExprKind::ConstBlock { .. }
| ExprKind::StaticRef { .. } | ExprKind::StaticRef { .. }
| ExprKind::InlineAsm { .. } | ExprKind::InlineAsm { .. }
| ExprKind::LlvmInlineAsm { .. } | ExprKind::LlvmInlineAsm { .. }

View file

@ -234,6 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
} }
ExprKind::Yield { .. } ExprKind::Yield { .. }
| ExprKind::Literal { .. } | ExprKind::Literal { .. }
| ExprKind::ConstBlock { .. }
| ExprKind::StaticRef { .. } | ExprKind::StaticRef { .. }
| ExprKind::Block { .. } | ExprKind::Block { .. }
| ExprKind::Match { .. } | ExprKind::Match { .. }

View file

@ -68,7 +68,9 @@ impl Category {
| ExprKind::ThreadLocalRef(_) | ExprKind::ThreadLocalRef(_)
| ExprKind::LlvmInlineAsm { .. } => Some(Category::Rvalue(RvalueFunc::AsRvalue)), | 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::Loop { .. }
| ExprKind::Block { .. } | ExprKind::Block { .. }

View file

@ -454,6 +454,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::Array { .. } | ExprKind::Array { .. }
| ExprKind::Tuple { .. } | ExprKind::Tuple { .. }
| ExprKind::Closure { .. } | ExprKind::Closure { .. }
| ExprKind::ConstBlock { .. }
| ExprKind::Literal { .. } | ExprKind::Literal { .. }
| ExprKind::ThreadLocalRef(_) | ExprKind::ThreadLocalRef(_)
| ExprKind::StaticRef { .. } => { | ExprKind::StaticRef { .. } => {

View file

@ -511,6 +511,12 @@ fn make_mirror_unadjusted<'a, 'tcx>(
inputs: asm.inputs_exprs.to_ref(), 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: // Now comes the rote stuff:
hir::ExprKind::Repeat(ref v, ref count) => { hir::ExprKind::Repeat(ref v, ref count) => {
let count_def_id = cx.tcx.hir().local_def_id(count.hir_id); let count_def_id = cx.tcx.hir().local_def_id(count.hir_id);

View file

@ -232,6 +232,9 @@ crate enum ExprKind<'tcx> {
Return { Return {
value: Option<ExprRef<'tcx>>, value: Option<ExprRef<'tcx>>,
}, },
ConstBlock {
value: &'tcx Const<'tcx>,
},
Repeat { Repeat {
value: ExprRef<'tcx>, value: ExprRef<'tcx>,
count: &'tcx Const<'tcx>, count: &'tcx Const<'tcx>,

View file

@ -856,6 +856,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
*self.lower_path(qpath, expr.hir_id, expr.span).kind *self.lower_path(qpath, expr.hir_id, expr.span).kind
} else { } else {
let (lit, neg) = match expr.kind { 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::Lit(ref lit) => (lit, false),
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => { hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => {
let lit = match expr.kind { let lit = match expr.kind {

View file

@ -432,6 +432,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
| hir::ExprKind::Break(..) | hir::ExprKind::Break(..)
| hir::ExprKind::Continue(_) | hir::ExprKind::Continue(_)
| hir::ExprKind::Lit(_) | hir::ExprKind::Lit(_)
| hir::ExprKind::ConstBlock(..)
| hir::ExprKind::Ret(..) | hir::ExprKind::Ret(..)
| hir::ExprKind::Block(..) | hir::ExprKind::Block(..)
| hir::ExprKind::Assign(..) | hir::ExprKind::Assign(..)
@ -1232,6 +1233,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
} }
hir::ExprKind::Lit(..) hir::ExprKind::Lit(..)
| hir::ExprKind::ConstBlock(..)
| hir::ExprKind::Err | hir::ExprKind::Err
| hir::ExprKind::Path(hir::QPath::TypeRelative(..)) | hir::ExprKind::Path(hir::QPath::TypeRelative(..))
| hir::ExprKind::Path(hir::QPath::LangItem(..)) => succ, | 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::Break(..)
| hir::ExprKind::Continue(..) | hir::ExprKind::Continue(..)
| hir::ExprKind::Lit(_) | hir::ExprKind::Lit(_)
| hir::ExprKind::ConstBlock(..)
| hir::ExprKind::Block(..) | hir::ExprKind::Block(..)
| hir::ExprKind::AddrOf(..) | hir::ExprKind::AddrOf(..)
| hir::ExprKind::Struct(..) | hir::ExprKind::Struct(..)