Add Ty to ConstKind::Value

This commit is contained in:
Boxy 2024-06-03 02:34:35 +01:00
parent 58feec9b85
commit a9702a6668
30 changed files with 180 additions and 186 deletions

View File

@ -1201,6 +1201,7 @@ macro_rules! require_simd {
.expect_const()
.eval(tcx, ty::ParamEnv::reveal_all(), span)
.unwrap()
.1
.unwrap_branch();
let n = idx.len() as u64;

View File

@ -693,42 +693,46 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
ty::ConstKind::Param(param) => {
write!(output, "{}", param.name)
}
// THISPR
_ => match { todo!() as Ty<'tcx> }.kind() {
ty::Int(ity) => {
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
write!(output, "{val}")
}
ty::Uint(_) => {
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
write!(output, "{val}")
}
ty::Bool => {
let val = ct.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap();
write!(output, "{val}")
}
_ => {
// If we cannot evaluate the constant to a known type, we fall back
// to emitting a stable hash value of the constant. This isn't very pretty
// but we get a deterministic, virtually unique value for the constant.
//
// Let's only emit 64 bits of the hash value. That should be plenty for
// avoiding collisions and will make the emitted type names shorter.
let hash_short = tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP).unwrap();
hcx.while_hashing_spans(false, |hcx| ct.hash_stable(hcx, &mut hasher));
hasher.finish::<Hash64>()
});
ty::ConstKind::Value(ty, _) => {
match ty.kind() {
ty::Int(ity) => {
// FIXME: directly extract the bits from a valtree instead of evaluating an
// alreay evaluated `Const` in order to get the bits.
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
write!(output, "{val}")
}
ty::Uint(_) => {
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
write!(output, "{val}")
}
ty::Bool => {
let val = ct.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap();
write!(output, "{val}")
}
_ => {
// If we cannot evaluate the constant to a known type, we fall back
// to emitting a stable hash value of the constant. This isn't very pretty
// but we get a deterministic, virtually unique value for the constant.
//
// Let's only emit 64 bits of the hash value. That should be plenty for
// avoiding collisions and will make the emitted type names shorter.
let hash_short = tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP).unwrap();
hcx.while_hashing_spans(false, |hcx| ct.hash_stable(hcx, &mut hasher));
hasher.finish::<Hash64>()
});
if cpp_like_debuginfo(tcx) {
write!(output, "CONST${hash_short:x}")
} else {
write!(output, "{{CONST#{hash_short:x}}}")
if cpp_like_debuginfo(tcx) {
write!(output, "CONST${hash_short:x}")
} else {
write!(output, "{{CONST#{hash_short:x}}}")
}
}
}
},
}
_ => bug!("Invalid `Const` during codegen: {:?}", ct),
}
.unwrap();
}

View File

@ -43,7 +43,7 @@ pub fn eval_unevaluated_mir_constant_to_valtree(
mir::Const::Ty(c) => match c.kind() {
// A constant that came from a const generic but was then used as an argument to old-style
// simd_shuffle (passing as argument instead of as a generic param).
rustc_type_ir::ConstKind::Value(valtree) => return Ok(Some(valtree)),
rustc_type_ir::ConstKind::Value(_, valtree) => return Ok(Some(valtree)),
other => span_bug!(constant.span, "{other:#?}"),
},
// We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate

View File

@ -360,7 +360,7 @@ pub fn in_operand<'tcx, Q, F>(
Const::Ty(ct)
if matches!(
ct.kind(),
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) | ty::ConstKind::Value(_)
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) | ty::ConstKind::Value(_, _)
) =>
{
None

View File

@ -2409,7 +2409,8 @@ fn check_array_pat_len(
min_len: u64,
) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
let len = match len.eval(self.tcx, self.param_env, span) {
Ok(val) => val
// FIXME(BoxyUwU): Assert the `Ty` is a `usize`?
Ok((_, val)) => val
.try_to_scalar()
.and_then(|scalar| scalar.try_to_int().ok())
.and_then(|int| int.try_to_target_usize(self.tcx).ok()),

View File

@ -176,7 +176,7 @@ fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
}
ty::ConstKind::Param(_)
| ty::ConstKind::Value(_)
| ty::ConstKind::Value(_, _)
| ty::ConstKind::Unevaluated(..)
| ty::ConstKind::Expr(..)
| ty::ConstKind::Error(_) => ct.super_fold_with(self),

View File

@ -1299,7 +1299,7 @@ pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
| ty::ConstKind::Bound(_, _)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Value(_)
| ty::ConstKind::Value(_, _)
| ty::ConstKind::Error(_)
| ty::ConstKind::Expr(_) => ct,
}
@ -1514,8 +1514,11 @@ pub fn try_const_eval_resolve(
span: Span,
) -> Result<ty::Const<'tcx>, ErrorHandled> {
match self.const_eval_resolve(param_env, unevaluated, span) {
// THISPR
Ok(Some(val)) => Ok(ty::Const::new_value(self.tcx, val, todo!())),
Ok(Some(val)) => Ok(ty::Const::new_value(
self.tcx,
val,
self.tcx.type_of(unevaluated.def).instantiate(self.tcx, unevaluated.args),
)),
Ok(None) => {
let tcx = self.tcx;
let def_id = unevaluated.def;

View File

@ -249,7 +249,7 @@ pub fn ty(&self) -> Ty<'tcx> {
pub fn is_required_const(&self) -> bool {
match self {
Const::Ty(c) => match c.kind() {
ty::ConstKind::Value(_) => false, // already a value, cannot error
ty::ConstKind::Value(_, _) => false, // already a value, cannot error
_ => true,
},
Const::Val(..) => false, // already a value, cannot error
@ -261,9 +261,7 @@ pub fn is_required_const(&self) -> bool {
pub fn try_to_scalar(self) -> Option<Scalar> {
match self {
Const::Ty(c) => match c.kind() {
// THISPR
// if c.ty().is_primitive()
ty::ConstKind::Value(valtree) if todo!() => {
ty::ConstKind::Value(ty, valtree) if ty.is_primitive() => {
// A valtree of a type where leaves directly represent the scalar const value.
// Just checking whether it is a leaf is insufficient as e.g. references are leafs
// but the leaf value is the value they point to, not the reference itself!
@ -282,9 +280,9 @@ pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
match self {
Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
Const::Ty(c) => match c.kind() {
// THISPR
// if c.ty().is_primitive()
ty::ConstKind::Value(valtree) if todo!() => Some(valtree.unwrap_leaf()),
ty::ConstKind::Value(ty, valtree) if ty.is_primitive() => {
Some(valtree.unwrap_leaf())
}
_ => None,
},
_ => None,
@ -312,8 +310,8 @@ pub fn eval(
Const::Ty(c) => {
// We want to consistently have a "clean" value for type system constants (i.e., no
// data hidden in the padding), so we always go through a valtree here.
let val = c.eval(tcx, param_env, span)?;
Ok(tcx.valtree_to_const_val((self.ty(), val)))
let (ty, val) = c.eval(tcx, param_env, span)?;
Ok(tcx.valtree_to_const_val((ty, val)))
}
Const::Unevaluated(uneval, _) => {
// FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
@ -341,17 +339,16 @@ pub fn try_eval_scalar(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<Scalar> {
match self {
// THISPR
// c.ty().is_primitive()
Const::Ty(c) if todo!() => {
// Avoid the `valtree_to_const_val` query. Can only be done on primitive types that
// are valtree leaves, and *not* on references. (References should return the
// pointer here, which valtrees don't represent.)
let val = c.eval(tcx, param_env, DUMMY_SP).ok()?;
Some(val.unwrap_leaf().into())
}
_ => self.eval(tcx, param_env, DUMMY_SP).ok()?.try_to_scalar(),
if let Const::Ty(c) = self
&& let ty::ConstKind::Value(ty, val) = c.kind()
&& ty.is_primitive()
{
// Avoid the `valtree_to_const_val` query. Can only be done on primitive types that
// are valtree leaves, and *not* on references. (References should return the
// pointer here, which valtrees don't represent.)
Some(val.unwrap_leaf().into())
} else {
self.eval(tcx, param_env, DUMMY_SP).ok()?.try_to_scalar()
}
}
@ -446,11 +443,10 @@ pub fn from_scalar(_tcx: TyCtxt<'tcx>, s: Scalar, ty: Ty<'tcx>) -> Self {
pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
match c.kind() {
ty::ConstKind::Value(valtree) => {
ty::ConstKind::Value(ty, valtree) => {
// Make sure that if `c` is normalized, then the return value is normalized.
// THISPR
let const_val = tcx.valtree_to_const_val((todo!(), valtree));
Self::Val(const_val, todo!())
let const_val = tcx.valtree_to_const_val((ty, valtree));
Self::Val(const_val, ty)
}
_ => Self::Ty(c),
}
@ -469,9 +465,7 @@ pub fn is_deterministic(&self) -> bool {
// A valtree may be a reference. Valtree references correspond to a
// different allocation each time they are evaluated. Valtrees for primitive
// types are fine though.
// THISPR
// c.ty().is_primitive()
ty::ConstKind::Value(_) => todo!(),
ty::ConstKind::Value(ty, _) => ty.is_primitive(),
ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false,
// This can happen if evaluation of a constant failed. The result does not matter
// much since compilation is doomed.

View File

@ -1318,7 +1318,7 @@ fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _location: Location)
ty::ConstKind::Unevaluated(uv) => {
format!("ty::Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,)
}
ty::ConstKind::Value(val) => format!("ty::Valtree({})", fmt_valtree(&val)),
ty::ConstKind::Value(_, val) => format!("ty::Valtree({})", fmt_valtree(&val)),
// No `ty::` prefix since we also use this to represent errors from `mir::Unevaluated`.
ty::ConstKind::Error(_) => "Error".to_string(),
// These variants shouldn't exist in the MIR.

View File

@ -24,7 +24,7 @@
pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
#[cfg(target_pointer_width = "64")]
rustc_data_structures::static_assert_size!(ConstKind<'_>, 24);
rustc_data_structures::static_assert_size!(ConstKind<'_>, 32);
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
#[rustc_pass_by_value]
@ -168,10 +168,6 @@ fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::Bound
fn new_unevaluated(interner: TyCtxt<'tcx>, uv: ty::UnevaluatedConst<'tcx>) -> Self {
Const::new_unevaluated(interner, uv)
}
fn ty(self) -> Ty<'tcx> {
self.ty()
}
}
impl<'tcx> Const<'tcx> {
@ -311,7 +307,7 @@ pub fn eval(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
span: Span,
) -> Result<ValTree<'tcx>, ErrorHandled> {
) -> Result<(Ty<'tcx>, ValTree<'tcx>), ErrorHandled> {
assert!(!self.has_escaping_bound_vars(), "escaping vars in {self:?}");
match self.kind() {
ConstKind::Unevaluated(unevaluated) => {
@ -329,9 +325,9 @@ pub fn eval(
);
return Err(e.into());
};
Ok(c)
Ok((tcx.type_of(unevaluated.def).instantiate(tcx, unevaluated.args), c))
}
ConstKind::Value(val) => Ok(val),
ConstKind::Value(ty, val) => Ok((ty, val)),
ConstKind::Error(g) => Err(g.into()),
ConstKind::Param(_)
| ConstKind::Infer(_)
@ -345,7 +341,7 @@ pub fn eval(
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, DUMMY_SP) {
Ok(val) => Self::new_value(tcx, val, self.ty_for_ctfe(tcx).unwrap()),
Ok((ty, val)) => Self::new_value(tcx, val, ty),
Err(ErrorHandled::Reported(r, _span)) => Self::new_error(tcx, r.into()),
Err(ErrorHandled::TooGeneric(_span)) => self,
}
@ -356,8 +352,10 @@ pub fn try_eval_scalar(
self,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<Scalar> {
self.eval(tcx, param_env, DUMMY_SP).ok()?.try_to_scalar()
) -> Option<(Ty<'tcx>, Scalar)> {
let (ty, val) = self.eval(tcx, param_env, DUMMY_SP).ok()?;
let val = val.try_to_scalar()?;
Some((ty, val))
}
#[inline]
@ -368,8 +366,10 @@ pub fn try_eval_scalar_int(
self,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
) -> Option<ScalarInt> {
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
) -> Option<(Ty<'tcx>, ScalarInt)> {
let (ty, scalar) = self.try_eval_scalar(tcx, param_env)?;
let val = scalar.try_to_int().ok()?;
Some((ty, val))
}
#[inline]
@ -377,15 +377,10 @@ pub fn try_eval_scalar_int(
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
/// contains const generic parameters or pointers).
pub fn try_eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u128> {
let int = self.try_eval_scalar_int(tcx, param_env)?;
let size = tcx
.layout_of(
param_env.with_reveal_all_normalized(tcx).and(self.ty_for_ctfe(tcx).unwrap()),
)
.ok()?
.size;
let (ty, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
// if `ty` does not depend on generic parameters, use an empty param_env
int.try_to_bits(size).ok()
scalar.try_to_bits(size).ok()
}
#[inline]
@ -401,12 +396,14 @@ pub fn try_eval_target_usize(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
) -> Option<u64> {
self.try_eval_scalar_int(tcx, param_env)?.try_to_target_usize(tcx).ok()
let (_, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
scalar.try_to_target_usize(tcx).ok()
}
#[inline]
pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<bool> {
self.try_eval_scalar_int(tcx, param_env)?.try_into().ok()
let (_, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
scalar.try_into().ok()
}
#[inline]
@ -419,7 +416,7 @@ pub fn eval_target_usize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) ->
/// Panics if self.kind != ty::ConstKind::Value
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
match self.kind() {
ty::ConstKind::Value(valtree) => valtree,
ty::ConstKind::Value(_, valtree) => valtree,
_ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
}
}
@ -427,7 +424,7 @@ pub fn to_valtree(self) -> ty::ValTree<'tcx> {
/// Attempts to convert to a `ValTree`
pub fn try_to_valtree(self) -> Option<ty::ValTree<'tcx>> {
match self.kind() {
ty::ConstKind::Value(valtree) => Some(valtree),
ty::ConstKind::Value(_, valtree) => Some(valtree),
_ => None,
}
}

View File

@ -601,9 +601,15 @@ fn new(
};
CommonConsts {
unit: mk_const(ty::ConstKind::Value(ty::ValTree::zst())),
true_: mk_const(ty::ConstKind::Value(ty::ValTree::Leaf(ty::ScalarInt::TRUE))),
false_: mk_const(ty::ConstKind::Value(ty::ValTree::Leaf(ty::ScalarInt::FALSE))),
unit: mk_const(ty::ConstKind::Value(types.unit, ty::ValTree::zst())),
true_: mk_const(ty::ConstKind::Value(
types.bool,
ty::ValTree::Leaf(ty::ScalarInt::TRUE),
)),
false_: mk_const(ty::ConstKind::Value(
types.bool,
ty::ValTree::Leaf(ty::ScalarInt::FALSE),
)),
}
}
}

View File

@ -337,7 +337,7 @@ pub fn consts_may_unify(self, obligation_ct: ty::Const<'_>, impl_ct: ty::Const<'
| ty::ConstKind::Error(_) => {
return true;
}
ty::ConstKind::Value(impl_val) => impl_val,
ty::ConstKind::Value(_, impl_val) => impl_val,
ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
bug!("unexpected impl arg: {:?}", impl_ct)
}
@ -357,7 +357,7 @@ pub fn consts_may_unify(self, obligation_ct: ty::Const<'_>, impl_ct: ty::Const<'
ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) | ty::ConstKind::Error(_) => {
true
}
ty::ConstKind::Value(obl_val) => obl_val == impl_val,
ty::ConstKind::Value(_, obl_val) => obl_val == impl_val,
ty::ConstKind::Infer(_) => true,

View File

@ -372,7 +372,7 @@ fn add_const_kind(&mut self, c: &ty::ConstKind<'_>) {
self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
}
ty::ConstKind::Value(_) => {}
ty::ConstKind::Value(ty, _) => self.add_ty(ty),
ty::ConstKind::Expr(e) => self.add_args(e.args()),
ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
}

View File

@ -1511,8 +1511,8 @@ macro_rules! print_underscore {
_ => print_underscore!(),
},
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
ty::ConstKind::Value(value) => {
return self.pretty_print_const_valtree(value, todo!(), print_ty);
ty::ConstKind::Value(ty, value) => {
return self.pretty_print_const_valtree(value, ty, print_ty);
}
ty::ConstKind::Bound(debruijn, bound_var) => {
@ -1666,7 +1666,7 @@ fn pretty_print_const_scalar_ptr(
ty::Ref(_, inner, _) => {
if let ty::Array(elem, len) = inner.kind() {
if let ty::Uint(ty::UintTy::U8) = elem.kind() {
if let ty::ConstKind::Value(ty::ValTree::Leaf(int)) = len.kind() {
if let ty::ConstKind::Value(_, ty::ValTree::Leaf(int)) = len.kind() {
match self.tcx().try_get_global_alloc(prov.alloc_id()) {
Some(GlobalAlloc::Memory(alloc)) => {
let len = int.assert_bits(self.tcx().data_layout.pointer_size);

View File

@ -646,7 +646,7 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>(
true
}
(ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => a_val == b_val,
(ty::ConstKind::Value(_, a_val), ty::ConstKind::Value(_, b_val)) => a_val == b_val,
// While this is slightly incorrect, it shouldn't matter for `min_const_generics`
// and is the better alternative to waiting until `generic_const_exprs` can

View File

@ -201,18 +201,16 @@ fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
// If this is a value, we spend some effort to make it look nice.
if let ConstKind::Value(_) = this.data.kind() {
if let ConstKind::Value(_, _) = this.data.kind() {
return ty::tls::with(move |tcx| {
// Somehow trying to lift the valtree results in lifetime errors, so we lift the
// entire constant.
let lifted = tcx.lift(*this.data).unwrap();
let ConstKind::Value(valtree) = lifted.kind() else {
let ConstKind::Value(ty, valtree) = lifted.kind() else {
bug!("we checked that this is a valtree")
};
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
// THISPR
todo!();
// cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?;
cx.pretty_print_const_valtree(valtree, ty, /*print_ty*/ true)?;
f.write_str(&cx.into_buffer())
});
}
@ -652,7 +650,9 @@ fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
}
ConstKind::Placeholder(p) => ConstKind::Placeholder(p.try_fold_with(folder)?),
ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?),
ConstKind::Value(t, v) => {
ConstKind::Value(t.try_fold_with(folder)?, v.try_fold_with(folder)?)
}
ConstKind::Error(e) => ConstKind::Error(e.try_fold_with(folder)?),
ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
};
@ -671,7 +671,10 @@ fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::
}
ConstKind::Placeholder(p) => p.visit_with(visitor),
ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
ConstKind::Value(v) => v.visit_with(visitor),
ConstKind::Value(t, v) => {
try_visit!(t.visit_with(visitor));
v.visit_with(visitor)
}
ConstKind::Error(e) => e.visit_with(visitor),
ConstKind::Expr(e) => e.visit_with(visitor),
}

View File

@ -217,9 +217,10 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
| ty::ConstKind::Param(_)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Value(_)
| ty::ConstKind::Error(_) => {}
ty::ConstKind::Value(ty, _) => stack.push(ty.into()),
ty::ConstKind::Expr(expr) => stack.extend(expr.args().iter().rev()),
ty::ConstKind::Unevaluated(ct) => {
stack.extend(ct.args.iter().rev());

View File

@ -101,7 +101,7 @@ fn to_pat(&mut self, cv: mir::Const<'tcx>) -> Box<Pat<'tcx>> {
// level of indirection can be eliminated
let have_valtree =
matches!(cv, mir::Const::Ty(c) if matches!(c.kind(), ty::ConstKind::Value(_)));
matches!(cv, mir::Const::Ty(c) if matches!(c.kind(), ty::ConstKind::Value(_, _)));
let inlined_const_as_pat = match cv {
mir::Const::Ty(c) => match c.kind() {
ty::ConstKind::Param(_)
@ -113,8 +113,8 @@ fn to_pat(&mut self, cv: mir::Const<'tcx>) -> Box<Pat<'tcx>> {
| ty::ConstKind::Expr(_) => {
span_bug!(self.span, "unexpected const in `to_pat`: {:?}", c.kind())
}
ty::ConstKind::Value(valtree) => {
self.recur(valtree, cv.ty()).unwrap_or_else(|_: FallbackToOpaqueConst| {
ty::ConstKind::Value(ty, valtree) => {
self.recur(valtree, ty).unwrap_or_else(|_: FallbackToOpaqueConst| {
Box::new(Pat {
span: self.span,
ty: cv.ty(),

View File

@ -416,7 +416,7 @@ fn fold_const(&mut self, c: I::Const) -> I::Const {
// FIXME: See comment above -- we could fold the region separately or something.
ty::ConstKind::Bound(_, _)
| ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Value(_)
| ty::ConstKind::Value(_, _)
| ty::ConstKind::Error(_)
| ty::ConstKind::Expr(_) => return c.super_fold_with(self),
};

View File

@ -116,12 +116,10 @@ fn encode_const<'tcx>(
}
// Literal arguments
ty::ConstKind::Value(..) => {
ty::ConstKind::Value(ct_ty, ..) => {
// L<element-type>[n]<element-value>E as literal argument
// Element type
// THISPR
let ct_ty: Ty<'tcx> = todo!();
s.push_str(&encode_ty(tcx, ct_ty, dict, options));
// The only allowed types of const values are bool, u8, u16, u32,

View File

@ -415,7 +415,7 @@ pub fn mir_const_from_ty_const<'tcx>(
ty: Ty<'tcx>,
) -> stable_mir::ty::MirConst {
let kind = match ty_const.kind() {
ty::Value(val) => {
ty::Value(ty, val) => {
let val = match val {
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
ty::ValTree::Branch(branch) => {
@ -456,7 +456,7 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let kind = match self.kind() {
ty::Value(val) => {
ty::Value(ty, val) => {
let val = match val {
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
ty::ValTree::Branch(branch) => {
@ -464,9 +464,7 @@ fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
}
};
// THISPR
let ty = todo!();
// let ty = tables.tcx.lift(c.ty()).unwrap();
let ty = tables.tcx.lift(ty).unwrap();
let const_val = tables.tcx.valtree_to_const_val((ty, val));
if matches!(const_val, mir::ConstValue::ZeroSized) {
stable_mir::ty::TyConstKind::ZSTValue(ty.stable(tables))

View File

@ -269,21 +269,16 @@ fn print_dyn_existential(
}
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
// THISPR
let ct_ty: Ty<'tcx> = todo!();
// only print integers
match (ct.kind(), ct_ty.kind()) {
(ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => {
match ct.kind() {
ty::ConstKind::Value(ty, ty::ValTree::Leaf(scalar)) if ty.is_integral() => {
// The `pretty_print_const` formatting depends on -Zverbose-internals
// flag, so we cannot reuse it here.
let signed = matches!(ct_ty.kind(), ty::Int(_));
let signed = matches!(ty.kind(), ty::Int(_));
write!(
self,
"{:#?}",
// THISPR
// ct.ty().is_ptr_sized_integral()
ty::ConstInt::new(scalar, signed, todo!())
ty::ConstInt::new(scalar, signed, ty.is_ptr_sized_integral())
)?;
}
_ => self.write_str("_")?,

View File

@ -541,8 +541,8 @@ fn print_dyn_existential(
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
// We only mangle a typed value if the const can be evaluated.
let ct = ct.normalize(self.tcx, ty::ParamEnv::reveal_all());
match ct.kind() {
ty::ConstKind::Value(_) => {}
let (ct_ty, valtree) = match ct.kind() {
ty::ConstKind::Value(ty, val) => (ty, val),
// Placeholders (should be demangled as `_`).
// NOTE(eddyb) despite `Unevaluated` having a `DefId` (and therefore
@ -559,7 +559,7 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
self.push("p");
return Ok(());
}
}
};
if let Some(&i) = self.consts.get(&ct) {
self.print_backref(i)?;
@ -567,18 +567,15 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
}
let start = self.out.len();
// THISPR
let ty: Ty<'tcx> = todo!();
// let ty = ct.ty();
match ty.kind() {
match ct_ty.kind() {
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
ty.print(self)?;
ct_ty.print(self)?;
let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all());
// Negative integer values are mangled using `n` as a "sign prefix".
if let ty::Int(ity) = ty.kind() {
if let ty::Int(ity) = ct_ty.kind() {
let val =
Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
if val < 0 {
@ -600,38 +597,32 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
match inner_ty.kind() {
ty::Str if mutbl.is_not() => {
match ct.kind() {
ty::ConstKind::Value(valtree) => {
let slice =
valtree.try_to_raw_bytes(self.tcx(), ty).unwrap_or_else(|| {
bug!(
"expected to get raw bytes from valtree {:?} for type {:}",
valtree, ty
)
});
let s = std::str::from_utf8(slice)
.expect("non utf8 str from MIR interpreter");
let slice =
valtree.try_to_raw_bytes(self.tcx(), ct_ty).unwrap_or_else(|| {
bug!(
"expected to get raw bytes from valtree {:?} for type {:}",
valtree,
ct_ty
)
});
let s =
std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
self.push("e");
self.push("e");
// FIXME(eddyb) use a specialized hex-encoding loop.
for byte in s.bytes() {
let _ = write!(self.out, "{byte:02x}");
}
self.push("_");
}
_ => {
bug!("symbol_names: unsupported `&str` constant: {:?}", ct);
}
// FIXME(eddyb) use a specialized hex-encoding loop.
for byte in s.bytes() {
let _ = write!(self.out, "{byte:02x}");
}
self.push("_");
}
_ => {
let pointee_ty =
ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
// FIXME(const_generics): add an assert that we only do this for valtrees.
let dereferenced_const = self.tcx.mk_ct_from_kind(ct.kind());
let pointee_ty = ct_ty
.builtin_deref(true)
.expect("tried to dereference on non-ptr type");
let dereferenced_const =
ty::Const::new_value(self.tcx, valtree, pointee_ty);
dereferenced_const.print(self)?;
}
}
@ -649,7 +640,7 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
Ok(())
};
match *ty.kind() {
match *ct_ty.kind() {
ty::Array(..) | ty::Slice(_) => {
self.push("A");
print_field_list(self)?;
@ -698,7 +689,7 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
}
}
_ => {
bug!("symbol_names: unsupported constant of type `{}` ({:?})", ty, ct);
bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct_ty, ct);
}
}

View File

@ -1040,8 +1040,13 @@ pub(super) fn try_const_eval_resolve(
) -> Option<ty::Const<'tcx>> {
use rustc_middle::mir::interpret::ErrorHandled;
match self.infcx.const_eval_resolve(param_env, unevaluated, DUMMY_SP) {
// THISPR
Ok(Some(val)) => Some(ty::Const::new_value(self.interner(), val, todo!())),
Ok(Some(val)) => Some(ty::Const::new_value(
self.interner(),
val,
self.interner()
.type_of(unevaluated.def)
.instantiate(self.interner(), unevaluated.args),
)),
Ok(None) | Err(ErrorHandled::TooGeneric(_)) => None,
Err(ErrorHandled::Reported(e, _)) => {
Some(ty::Const::new_error(self.interner(), e.into()))

View File

@ -178,7 +178,9 @@ fn compute_const_evaluatable_goal(
ty::ConstKind::Infer(_) => {
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
ty::ConstKind::Placeholder(_) | ty::ConstKind::Value(_) | ty::ConstKind::Error(_) => {
ty::ConstKind::Placeholder(_)
| ty::ConstKind::Value(_, _)
| ty::ConstKind::Error(_) => {
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}
// We can freely ICE here as:

View File

@ -765,8 +765,7 @@ fn evaluate_nested_obligations(
unevaluated,
obligation.cause.span,
) {
// THISPR
Ok(Some(valtree)) => Ok(ty::Const::new_value(selcx.tcx(),valtree, todo!())),
Ok(Some(valtree)) => Ok(ty::Const::new_value(selcx.tcx(),valtree, self.tcx.type_of(unevaluated.def).instantiate(self.tcx, unevaluated.args))),
Ok(None) => {
let tcx = self.tcx;
let reported =

View File

@ -34,7 +34,7 @@ pub fn is_const_evaluatable<'tcx>(
ty::ConstKind::Param(_)
| ty::ConstKind::Bound(_, _)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Value(_)
| ty::ConstKind::Value(_, _)
| ty::ConstKind::Error(_) => return Ok(()),
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
};

View File

@ -135,7 +135,7 @@ pub fn from_const<'tcx>(
use rustc_middle::ty::ScalarInt;
use rustc_span::symbol::sym;
let Ok(cv) = c.eval(tcx, param_env, DUMMY_SP) else {
let Ok((ty, cv)) = c.eval(tcx, param_env, DUMMY_SP) else {
return Some(Self {
alignment: true,
lifetimes: true,
@ -144,8 +144,7 @@ pub fn from_const<'tcx>(
});
};
let assume_def_id = tcx.get_lang_items(()).get(LangItem::TransmuteOpts)?;
let adt_def = tcx.adt_def(assume_def_id);
let adt_def = ty.ty_adt_def()?;
assert_eq!(
tcx.require_lang_item(LangItem::TransmuteOpts, None),

View File

@ -23,7 +23,7 @@ fn destructure_const<'tcx>(
tcx: TyCtxt<'tcx>,
const_: ty::Const<'tcx>,
) -> ty::DestructuredConst<'tcx> {
let ty::ConstKind::Value(valtree) = const_.kind() else {
let ty::ConstKind::Value(ct_ty, valtree) = const_.kind() else {
bug!("cannot destructure constant {:?}", const_)
};
@ -32,9 +32,6 @@ fn destructure_const<'tcx>(
_ => bug!("cannot destructure constant {:?}", const_),
};
// THISPR
let ct_ty: Ty<'tcx> = todo!();
let (fields, variant) = match ct_ty.kind() {
ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
// construct the consts for the elements of the array/slice

View File

@ -32,7 +32,7 @@ pub enum ConstKind<I: Interner> {
Unevaluated(ty::UnevaluatedConst<I>),
/// Used to hold computed value.
Value(I::ValueConst),
Value(I::Ty, I::ValueConst),
/// A placeholder for a const which could not be computed; this is
/// propagated to avoid useless error messages.
@ -51,7 +51,7 @@ fn eq(&self, other: &Self) -> bool {
(Bound(l0, l1), Bound(r0, r1)) => l0 == r0 && l1 == r1,
(Placeholder(l0), Placeholder(r0)) => l0 == r0,
(Unevaluated(l0), Unevaluated(r0)) => l0 == r0,
(Value(l0), Value(r0)) => l0 == r0,
(Value(l0, l1), Value(r0, r1)) => l0 == r0 && l1 == r1,
(Error(l0), Error(r0)) => l0 == r0,
(Expr(l0), Expr(r0)) => l0 == r0,
_ => false,
@ -80,7 +80,7 @@ fn fmt<Infcx: InferCtxtLike<Interner = I>>(
Unevaluated(uv) => {
write!(f, "{:?}", &this.wrap(uv))
}
Value(valtree) => write!(f, "{valtree:?}"),
Value(ty, valtree) => write!(f, "({valtree:?}: {:?})", &this.wrap(ty)),
Error(_) => write!(f, "{{const error}}"),
Expr(expr) => write!(f, "{:?}", &this.wrap(expr)),
}