1
Fork 0

Auto merge of #77124 - spastorino:const-exprs-rfc-2920, r=oli-obk

Implement const expressions and patterns (RFC 2920)

cc `@ecstatic-morse` `@lcnr` `@oli-obk` `@petrochenkov`
This commit is contained in:
bors 2020-10-17 14:44:51 +00:00
commit 6af9846fcc
48 changed files with 301 additions and 36 deletions

View file

@ -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),
}
}

View file

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

View file

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

View file

@ -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 { .. }

View file

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

View file

@ -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);

View file

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