From 5363f350887b1e5b5dd21a86f88c8af9d7fea6da Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:25 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_middle/src/ty/consts.rs | 66 ++++++++++------------------------ 1 file changed, 18 insertions(+), 48 deletions(-) (limited to 'compiler/rustc_middle/src/ty/consts.rs') diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index f998e6083..c2be08e49 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,8 +1,6 @@ use crate::mir::interpret::LitToConstInput; -use crate::mir::ConstantKind; -use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; +use crate::ty::{self, DefIdTree, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; use rustc_data_structures::intern::Interned; -use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_macros::HashStable; @@ -77,13 +75,13 @@ impl<'tcx> Const<'tcx> { match Self::try_eval_lit_or_param(tcx, ty, expr) { Some(v) => v, - None => tcx.mk_const(ty::ConstS { - kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { + None => tcx.mk_const( + ty::UnevaluatedConst { def: def.to_global(), substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()), - }), + }, ty, - }), + ), } } @@ -132,27 +130,16 @@ impl<'tcx> Const<'tcx> { ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let item_id = tcx.hir().get_parent_node(hir_id); - let item_def_id = tcx.hir().local_def_id(item_id); - let generics = tcx.generics_of(item_def_id.to_def_id()); + let item_def_id = tcx.parent(def_id); + let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; - let name = tcx.hir().name(hir_id); - Some(tcx.mk_const(ty::ConstS { - kind: ty::ConstKind::Param(ty::ParamConst::new(index, name)), - ty, - })) + let name = tcx.item_name(def_id); + Some(tcx.mk_const(ty::ParamConst::new(index, name), ty)) } _ => None, } } - /// Interns the given value as a constant. - #[inline] - pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self { - tcx.mk_const(ConstS { kind: ConstKind::Value(val), ty }) - } - /// Panics if self.kind != ty::ConstKind::Value pub fn to_valtree(self) -> ty::ValTree<'tcx> { match self.kind() { @@ -161,11 +148,6 @@ impl<'tcx> Const<'tcx> { } } - pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self { - let valtree = ty::ValTree::from_scalar_int(i); - Self::from_value(tcx, valtree, ty) - } - #[inline] /// Creates a constant with the given integer value and interns it. pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Self { @@ -173,14 +155,16 @@ impl<'tcx> Const<'tcx> { .layout_of(ty) .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e)) .size; - Self::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap(), ty.value) + tcx.mk_const( + ty::ValTree::from_scalar_int(ScalarInt::try_from_uint(bits, size).unwrap()), + ty.value, + ) } #[inline] /// Creates an interned zst constant. pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self { - let valtree = ty::ValTree::zst(); - Self::from_value(tcx, valtree, ty) + tcx.mk_const(ty::ValTree::zst(), ty) } #[inline] @@ -227,8 +211,8 @@ impl<'tcx> Const<'tcx> { pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> { if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) { match val { - Ok(val) => Const::from_value(tcx, val, self.ty()), - Err(ErrorGuaranteed { .. }) => tcx.const_error(self.ty()), + Ok(val) => tcx.mk_const(val, self.ty()), + Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar), } } else { // Either the constant isn't evaluatable or ValTree creation failed. @@ -236,20 +220,6 @@ impl<'tcx> Const<'tcx> { } } - #[inline] - /// Tries to evaluate the constant if it is `Unevaluated` and creates a ConstValue if the - /// evaluation succeeds. If it doesn't succeed, returns the unevaluated constant. - pub fn eval_for_mir(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> ConstantKind<'tcx> { - if let Some(val) = self.kind().try_eval_for_mir(tcx, param_env) { - match val { - Ok(const_val) => ConstantKind::from_value(const_val, self.ty()), - Err(ErrorGuaranteed { .. }) => ConstantKind::Ty(tcx.const_error(self.ty())), - } - } else { - ConstantKind::Ty(self) - } - } - #[inline] /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type. pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 { @@ -272,9 +242,9 @@ impl<'tcx> Const<'tcx> { pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> { let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) { hir::Node::GenericParam(hir::GenericParam { - kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) }, + kind: hir::GenericParamKind::Const { default: Some(ac), .. }, .. - }) => tcx.hir().local_def_id(ac.hir_id), + }) => ac.def_id, _ => span_bug!( tcx.def_span(def_id), "`const_param_default` expected a generic parameter with a constant" -- cgit v1.2.3