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:
commit
6af9846fcc
48 changed files with 301 additions and 36 deletions
|
@ -286,6 +286,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
}
|
||||
ExprKind::DropTemps(ref e) => self.check_expr_with_expectation(e, expected),
|
||||
ExprKind::Array(ref args) => self.check_expr_array(args, expected, expr),
|
||||
ExprKind::ConstBlock(ref anon_const) => self.to_const(anon_const).ty,
|
||||
ExprKind::Repeat(ref element, ref count) => {
|
||||
self.check_expr_repeat(element, count, expected, expr)
|
||||
}
|
||||
|
|
|
@ -111,6 +111,7 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
|||
use rustc_hir::{HirIdMap, Node};
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_index::vec::Idx;
|
||||
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
||||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
|
||||
use rustc_middle::ty::query::Providers;
|
||||
use rustc_middle::ty::subst::GenericArgKind;
|
||||
|
@ -528,7 +529,20 @@ fn typeck_with_fallback<'tcx>(
|
|||
hir::TyKind::Infer => Some(AstConv::ast_ty_to_ty(&fcx, ty)),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(fallback);
|
||||
.unwrap_or_else(|| match tcx.hir().get(id) {
|
||||
Node::AnonConst(_) => match tcx.hir().get(tcx.hir().get_parent_node(id)) {
|
||||
Node::Expr(&hir::Expr {
|
||||
kind: hir::ExprKind::ConstBlock(ref anon_const),
|
||||
..
|
||||
}) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
|
||||
kind: TypeVariableOriginKind::TypeInference,
|
||||
span,
|
||||
}),
|
||||
_ => fallback(),
|
||||
},
|
||||
_ => fallback(),
|
||||
});
|
||||
|
||||
let expected_type = fcx.normalize_associated_types_in(body.value.span, &expected_type);
|
||||
fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);
|
||||
|
||||
|
|
|
@ -309,6 +309,12 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
tcx.types.usize
|
||||
}
|
||||
|
||||
Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. })
|
||||
if anon_const.hir_id == hir_id =>
|
||||
{
|
||||
tcx.typeck(def_id).node_type(anon_const.hir_id)
|
||||
}
|
||||
|
||||
Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx
|
||||
.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id())
|
||||
.repr
|
||||
|
|
|
@ -258,7 +258,10 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
|||
self.consume_exprs(&ia.inputs_exprs);
|
||||
}
|
||||
|
||||
hir::ExprKind::Continue(..) | hir::ExprKind::Lit(..) | hir::ExprKind::Err => {}
|
||||
hir::ExprKind::Continue(..)
|
||||
| hir::ExprKind::Lit(..)
|
||||
| hir::ExprKind::ConstBlock(..)
|
||||
| hir::ExprKind::Err => {}
|
||||
|
||||
hir::ExprKind::Loop(ref blk, _, _) => {
|
||||
self.walk_block(blk);
|
||||
|
|
|
@ -370,6 +370,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
|
|||
| hir::ExprKind::Loop(..)
|
||||
| hir::ExprKind::Match(..)
|
||||
| hir::ExprKind::Lit(..)
|
||||
| hir::ExprKind::ConstBlock(..)
|
||||
| hir::ExprKind::Break(..)
|
||||
| hir::ExprKind::Continue(..)
|
||||
| hir::ExprKind::Struct(..)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue