Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 23, 2020
1 parent 770be24 commit 9bcd9fe
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/librustc/ty/sty.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
),
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir_build/hair/cx/expr.rs
Expand Up @@ -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 }
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -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!(),
},
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_typeck/check/expr.rs
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/confirm.rs
Expand Up @@ -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!(),
},
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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!(),
},
Expand Down

0 comments on commit 9bcd9fe

Please sign in to comment.