Address review comments

This commit is contained in:
Oliver Scherer 2020-03-23 19:19:07 +01:00
parent 770be24ccd
commit 9bcd9fe674
6 changed files with 20 additions and 19 deletions

View file

@ -20,7 +20,7 @@ use polonius_engine::Atom;
use rustc_ast::ast::{self, Ident};
use rustc_data_structures::captures::Captures;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_index::vec::Idx;
use rustc_macros::HashStable;
use rustc_span::symbol::{kw, Symbol};
@ -2404,15 +2404,17 @@ static_assert_size!(Const<'_>, 48);
impl<'tcx> Const<'tcx> {
/// Literals and const generic parameters are eagerly converted to a constant, everything else
/// becomes `Unevaluated`.
pub fn from_hir_anon_const(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Ty<'tcx>) -> &'tcx Self {
debug!("Const::from_hir_anon_const(id={:?})", def_id);
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx Self {
debug!("Const::from_anon_const(id={:?})", def_id);
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let body_id = tcx.hir().body_owned_by(hir_id);
let expr = &tcx.hir().body(body_id).value;
let ty = tcx.type_of(def_id.to_def_id());
let lit_input = match expr.kind {
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => match expr.kind {
@ -2457,8 +2459,8 @@ impl<'tcx> Const<'tcx> {
ty::ConstKind::Param(ty::ParamConst::new(index, name))
}
_ => ty::ConstKind::Unevaluated(
def_id,
InternalSubsts::identity_for_item(tcx, def_id),
def_id.to_def_id(),
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
None,
),
};

View file

@ -406,8 +406,8 @@ fn make_mirror_unadjusted<'a, 'tcx>(
// Now comes the rote stuff:
hir::ExprKind::Repeat(ref v, ref count) => {
let count = cx.tcx.hir().local_def_id(count.hir_id);
let count = ty::Const::from_hir_anon_const(cx.tcx, count, cx.tcx.types.usize);
let count_def_id = cx.tcx.hir().local_def_id(count.hir_id).expect_local();
let count = ty::Const::from_anon_const(cx.tcx, count_def_id);
ExprKind::Repeat { value: v.to_ref(), count }
}

View file

@ -780,8 +780,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
}
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
let ct = tcx.hir().local_def_id(ct.value.hir_id);
ty::Const::from_hir_anon_const(tcx, ct, tcx.type_of(param.def_id)).into()
let ct_def_id = tcx.hir().local_def_id(ct.value.hir_id).expect_local();
ty::Const::from_anon_const(tcx, ct_def_id).into()
}
_ => unreachable!(),
},
@ -2765,8 +2765,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
.unwrap_or(tcx.types.err)
}
hir::TyKind::Array(ref ty, ref length) => {
let length = tcx.hir().local_def_id(length.hir_id);
let length = ty::Const::from_hir_anon_const(tcx, length, tcx.types.usize);
let length_def_id = tcx.hir().local_def_id(length.hir_id).expect_local();
let length = ty::Const::from_anon_const(tcx, length_def_id);
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length));
self.normalize_ty(ast_ty.span, array_ty)
}

View file

@ -1007,8 +1007,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_expr: &'tcx hir::Expr<'tcx>,
) -> Ty<'tcx> {
let tcx = self.tcx;
let count_def_id = tcx.hir().local_def_id(count.hir_id);
let count = self.to_const(count, tcx.type_of(count_def_id));
let count = self.to_const(count);
let uty = match expected {
ExpectHasType(uty) => match uty.kind {

View file

@ -331,7 +331,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
}
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => self.to_ty(ty).into(),
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
self.to_const(&ct.value, self.tcx.type_of(param.def_id)).into()
self.to_const(&ct.value).into()
}
_ => unreachable!(),
},

View file

@ -3279,9 +3279,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty
}
pub fn to_const(&self, ast_c: &hir::AnonConst, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> {
let c = self.tcx.hir().local_def_id(ast_c.hir_id);
ty::Const::from_hir_anon_const(self.tcx, c, ty)
pub fn to_const(&self, ast_c: &hir::AnonConst) -> &'tcx ty::Const<'tcx> {
let c = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local();
ty::Const::from_anon_const(self.tcx, c)
}
// If the type given by the user has free regions, save it for later, since
@ -5510,7 +5510,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.to_ty(ty).into()
}
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
self.to_const(&ct.value, self.tcx.type_of(param.def_id)).into()
self.to_const(&ct.value).into()
}
_ => unreachable!(),
},