Typeck inline consts

This commit is contained in:
Santiago Pastorino 2020-10-06 17:52:32 -03:00
parent fe922e567f
commit 85b5ce2643
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
5 changed files with 27 additions and 2 deletions

View File

@ -286,6 +286,7 @@ fn check_expr_kind(
}
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)
}

View File

@ -111,6 +111,7 @@
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);

View File

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

View File

@ -258,7 +258,10 @@ pub fn walk_expr(&mut self, expr: &hir::Expr<'_>) {
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);

View File

@ -370,6 +370,7 @@ fn cat_expr_adjusted_with<F>(
| hir::ExprKind::Loop(..)
| hir::ExprKind::Match(..)
| hir::ExprKind::Lit(..)
| hir::ExprKind::ConstBlock(..)
| hir::ExprKind::Break(..)
| hir::ExprKind::Continue(..)
| hir::ExprKind::Struct(..)