Deal with fallout

This commit is contained in:
Boxy 2023-07-04 15:41:45 +01:00
parent 8ac3ffe834
commit 62174bfe72
17 changed files with 47 additions and 29 deletions

View File

@ -706,7 +706,6 @@ fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
let times = fx let times = fx
.monomorphize(times) .monomorphize(times)
.eval(fx.tcx, ParamEnv::reveal_all()) .eval(fx.tcx, ParamEnv::reveal_all())
.kind()
.try_to_bits(fx.tcx.data_layout.pointer_size) .try_to_bits(fx.tcx.data_layout.pointer_size)
.unwrap(); .unwrap();
if operand.layout().size.bytes() == 0 { if operand.layout().size.bytes() == 0 {

View File

@ -2329,7 +2329,7 @@ pub fn try_to_bool(self) -> Option<bool> {
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
match self { match self {
Self::Ty(c) => { Self::Ty(c) => {
if let Some(val) = c.kind().try_eval_for_mir(tcx, param_env) { if let Some(val) = c.try_eval_for_mir(tcx, param_env) {
match val { match val {
Ok(val) => Self::Val(val, c.ty()), Ok(val) => Self::Val(val, c.ty()),
Err(guar) => Self::Ty(ty::Const::new_error(tcx, guar, self.ty())), Err(guar) => Self::Ty(ty::Const::new_error(tcx, guar, self.ty())),
@ -2867,7 +2867,7 @@ fn pretty_print_const_value<'tcx>(
} }
} }
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => { (ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
let n = n.kind().try_to_bits(tcx.data_layout.pointer_size).unwrap(); let n = n.try_to_bits(tcx.data_layout.pointer_size).unwrap();
// cast is ok because we already checked for pointer size (32 or 64 bit) above // cast is ok because we already checked for pointer size (32 or 64 bit) above
let range = AllocRange { start: offset, size: Size::from_bytes(n) }; let range = AllocRange { start: offset, size: Size::from_bytes(n) };
let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap(); let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();

View File

@ -54,7 +54,7 @@ pub fn ty(self) -> Ty<'tcx> {
#[inline] #[inline]
pub fn kind(self) -> ConstKind<'tcx> { pub fn kind(self) -> ConstKind<'tcx> {
self.0.kind self.0.kind.clone()
} }
#[inline] #[inline]

View File

@ -1,7 +1,6 @@
use super::Const; use super::Const;
use crate::mir; use crate::mir;
use crate::ty::abstract_const::CastKind; use crate::ty::abstract_const::CastKind;
use crate::ty::sty::ConstKind;
use crate::ty::subst::SubstsRef; use crate::ty::subst::SubstsRef;
use crate::ty::{self, List, Ty}; use crate::ty::{self, List, Ty};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -36,12 +35,6 @@ pub fn new(def: DefId, substs: SubstsRef<'tcx>) -> UnevaluatedConst<'tcx> {
} }
} }
impl<'tcx> From<ty::ConstVid<'tcx>> for ConstKind<'tcx> {
fn from(const_vid: ty::ConstVid<'tcx>) -> Self {
InferConst::Var(const_vid).into()
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)] #[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
pub enum Expr<'tcx> { pub enum Expr<'tcx> {
@ -55,7 +48,7 @@ pub enum Expr<'tcx> {
static_assert_size!(Expr<'_>, 24); static_assert_size!(Expr<'_>, 24);
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(ConstKind<'_>, 32); static_assert_size!(super::ConstKind<'_>, 32);
/// An inference variable for a const, for use in const generics. /// An inference variable for a const, for use in const generics.
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)] #[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]

View File

@ -170,7 +170,7 @@ fn subst_opt(self, tcx: TyCtxt<'tcx>, substs: ty::SubstsRef<'tcx>) -> Option<Sel
match self { match self {
Self::ConstIsZero(c) => { Self::ConstIsZero(c) => {
let c = ty::EarlyBinder::bind(c).subst(tcx, substs); let c = ty::EarlyBinder::bind(c).subst(tcx, substs);
let pred = match c.kind().try_to_target_usize(tcx) { let pred = match c.try_to_target_usize(tcx) {
Some(0) => Self::True, Some(0) => Self::True,
Some(1..) => Self::False, Some(1..) => Self::False,
None => Self::ConstIsZero(c), None => Self::ConstIsZero(c),

View File

@ -197,7 +197,7 @@ fn inhabited_predicate_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> InhabitedP
// If we can evaluate the array length before having a `ParamEnv`, then // If we can evaluate the array length before having a `ParamEnv`, then
// we can simplify the predicate. This is an optimization. // we can simplify the predicate. This is an optimization.
Array(ty, len) => match len.kind().try_to_target_usize(tcx) { Array(ty, len) => match len.try_to_target_usize(tcx) {
Some(0) => InhabitedPredicate::True, Some(0) => InhabitedPredicate::True,
Some(1..) => ty.inhabited_predicate(tcx), Some(1..) => ty.inhabited_predicate(tcx),
None => ty.inhabited_predicate(tcx).or(tcx, InhabitedPredicate::ConstIsZero(len)), None => ty.inhabited_predicate(tcx).or(tcx, InhabitedPredicate::ConstIsZero(len)),

View File

@ -66,6 +66,10 @@
pub use crate::ty::diagnostics::*; pub use crate::ty::diagnostics::*;
pub use rustc_type_ir::AliasKind::*; pub use rustc_type_ir::AliasKind::*;
pub use rustc_type_ir::ConstKind::{
Bound as BoundCt, Error as ErrorCt, Expr as ExprCt, Infer as InferCt, Param as ParamCt,
Placeholder as PlaceholderCt, Unevaluated, Value,
};
pub use rustc_type_ir::DynKind::*; pub use rustc_type_ir::DynKind::*;
pub use rustc_type_ir::InferTy::*; pub use rustc_type_ir::InferTy::*;
pub use rustc_type_ir::RegionKind::*; pub use rustc_type_ir::RegionKind::*;

View File

@ -11,6 +11,7 @@
use rustc_hir::def::Namespace; use rustc_hir::def::Namespace;
use rustc_index::{Idx, IndexVec}; use rustc_index::{Idx, IndexVec};
use rustc_target::abi::TyAndLayout; use rustc_target::abi::TyAndLayout;
use rustc_type_ir::ConstKind;
use std::fmt; use std::fmt;
use std::ops::ControlFlow; use std::ops::ControlFlow;
@ -710,7 +711,18 @@ fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
folder: &mut F, folder: &mut F,
) -> Result<Self, F::Error> { ) -> Result<Self, F::Error> {
let ty = self.ty().try_fold_with(folder)?; let ty = self.ty().try_fold_with(folder)?;
let kind = self.kind().try_fold_with(folder)?; let kind = match self.kind() {
ConstKind::Param(p) => ConstKind::Param(p.try_fold_with(folder)?),
ConstKind::Infer(i) => ConstKind::Infer(i.try_fold_with(folder)?),
ConstKind::Bound(d, b) => {
ConstKind::Bound(d.try_fold_with(folder)?, b.try_fold_with(folder)?)
}
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::Error(e) => ConstKind::Error(e.try_fold_with(folder)?),
ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
};
if ty != self.ty() || kind != self.kind() { if ty != self.ty() || kind != self.kind() {
Ok(folder.interner().mk_ct_from_kind(kind, ty)) Ok(folder.interner().mk_ct_from_kind(kind, ty))
} else { } else {
@ -725,7 +737,19 @@ fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(
visitor: &mut V, visitor: &mut V,
) -> ControlFlow<V::BreakTy> { ) -> ControlFlow<V::BreakTy> {
self.ty().visit_with(visitor)?; self.ty().visit_with(visitor)?;
self.kind().visit_with(visitor) match self.kind() {
ConstKind::Param(p) => p.visit_with(visitor),
ConstKind::Infer(i) => i.visit_with(visitor),
ConstKind::Bound(d, b) => {
d.visit_with(visitor)?;
b.visit_with(visitor)
}
ConstKind::Placeholder(p) => p.visit_with(visitor),
ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
ConstKind::Value(v) => v.visit_with(visitor),
ConstKind::Error(e) => e.visit_with(visitor),
ConstKind::Expr(e) => e.visit_with(visitor),
}
} }
} }

View File

@ -1306,7 +1306,7 @@ pub fn needs_drop_components<'tcx>(
ty::Array(elem_ty, size) => { ty::Array(elem_ty, size) => {
match needs_drop_components(*elem_ty, target_layout) { match needs_drop_components(*elem_ty, target_layout) {
Ok(v) if v.is_empty() => Ok(v), Ok(v) if v.is_empty() => Ok(v),
res => match size.kind().try_to_bits(target_layout.pointer_size) { res => match size.try_to_bits(target_layout.pointer_size) {
// Arrays of size zero don't need drop, even if their element // Arrays of size zero don't need drop, even if their element
// type does. // type does.
Some(0) => Ok(SmallVec::new()), Some(0) => Ok(SmallVec::new()),

View File

@ -230,7 +230,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
self.write_str("[")?; self.write_str("[")?;
self = self.print_type(ty)?; self = self.print_type(ty)?;
self.write_str("; ")?; self.write_str("; ")?;
if let Some(size) = size.kind().try_to_bits(self.tcx().data_layout.pointer_size) { if let Some(size) = size.try_to_bits(self.tcx().data_layout.pointer_size) {
write!(self, "{size}")? write!(self, "{size}")?
} else if let ty::ConstKind::Param(param) = size.kind() { } else if let ty::ConstKind::Param(param) = size.kind() {
self = param.print(self)? self = param.print(self)?

View File

@ -112,7 +112,7 @@ fn push_unsigned_value<T: Display>(s: &mut String, value: T) {
let _ = write!(s, "{value}"); let _ = write!(s, "{value}");
} }
if let Some(scalar_int) = c.kind().try_to_scalar_int() { if let Some(scalar_int) = c.try_to_scalar_int() {
let signed = c.ty().is_signed(); let signed = c.ty().is_signed();
match scalar_int.size().bits() { match scalar_int.size().bits() {
8 if signed => push_signed_value(&mut s, scalar_int.try_to_i8().unwrap(), 0), 8 if signed => push_signed_value(&mut s, scalar_int.try_to_i8().unwrap(), 0),
@ -504,8 +504,7 @@ fn encode_ty<'tcx>(
let _ = write!( let _ = write!(
s, s,
"{}", "{}",
&len.kind() &len.try_to_scalar()
.try_to_scalar()
.unwrap() .unwrap()
.to_u64() .to_u64()
.unwrap_or_else(|_| panic!("failed to convert length to u64")) .unwrap_or_else(|_| panic!("failed to convert length to u64"))
@ -815,7 +814,6 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
ty::Array(ty0, len) => { ty::Array(ty0, len) => {
let len = len let len = len
.kind()
.try_to_scalar() .try_to_scalar()
.unwrap() .unwrap()
.to_u64() .to_u64()

View File

@ -651,7 +651,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error
.builtin_deref(true) .builtin_deref(true)
.expect("tried to dereference on non-ptr type") .expect("tried to dereference on non-ptr type")
.ty; .ty;
// FIXME: add an assert that we only do this for valtrees. // FIXME(const_generics): add an assert that we only do this for valtrees.
let dereferenced_const = self.tcx.mk_ct_from_kind(ct.kind(), pointee_ty); let dereferenced_const = self.tcx.mk_ct_from_kind(ct.kind(), pointee_ty);
self = dereferenced_const.print(self)?; self = dereferenced_const.print(self)?;
} }

View File

@ -278,7 +278,7 @@ fn on_unimplemented_note(
// Arrays give us `[]`, `[{ty}; _]` and `[{ty}; N]` // Arrays give us `[]`, `[{ty}; _]` and `[{ty}; N]`
if let ty::Array(aty, len) = self_ty.kind() { if let ty::Array(aty, len) = self_ty.kind() {
flags.push((sym::_Self, Some("[]".to_string()))); flags.push((sym::_Self, Some("[]".to_string())));
let len = len.kind().try_to_value().and_then(|v| v.try_to_target_usize(self.tcx)); let len = len.try_to_value().and_then(|v| v.try_to_target_usize(self.tcx));
flags.push((sym::_Self, Some(format!("[{}; _]", aty)))); flags.push((sym::_Self, Some(format!("[{}; _]", aty))));
if let Some(n) = len { if let Some(n) = len {
flags.push((sym::_Self, Some(format!("[{}; {}]", aty, n)))); flags.push((sym::_Self, Some(format!("[{}; {}]", aty, n))));

View File

@ -528,7 +528,7 @@ fn process_obligation(
debug!("equating consts:\nc1= {:?}\nc2= {:?}", c1, c2); debug!("equating consts:\nc1= {:?}\nc2= {:?}", c1, c2);
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use ty::ConstKind::Unevaluated; use ty::Unevaluated;
match (c1.kind(), c2.kind()) { match (c1.kind(), c2.kind()) {
(Unevaluated(a), Unevaluated(b)) (Unevaluated(a), Unevaluated(b))
if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst => if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst =>

View File

@ -897,7 +897,7 @@ fn evaluate_predicate_recursively<'o>(
); );
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use ty::ConstKind::Unevaluated; use ty::Unevaluated;
match (c1.kind(), c2.kind()) { match (c1.kind(), c2.kind()) {
(Unevaluated(a), Unevaluated(b)) (Unevaluated(a), Unevaluated(b))
if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst => if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst =>

View File

@ -689,7 +689,7 @@ pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::ConstantKind<'t
mir::ConstantKind::Val(ConstValue::ByRef { alloc, offset: _ }, _) => match result.ty().kind() { mir::ConstantKind::Val(ConstValue::ByRef { alloc, offset: _ }, _) => match result.ty().kind() {
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)), ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
ty::Array(sub_type, len) => match sub_type.kind() { ty::Array(sub_type, len) => match sub_type.kind() {
ty::Float(FloatTy::F32) => match len.kind().try_to_target_usize(lcx.tcx) { ty::Float(FloatTy::F32) => match len.try_to_target_usize(lcx.tcx) {
Some(len) => alloc Some(len) => alloc
.inner() .inner()
.inspect_with_uninit_and_ptr_outside_interpreter(0..(4 * usize::try_from(len).unwrap())) .inspect_with_uninit_and_ptr_outside_interpreter(0..(4 * usize::try_from(len).unwrap()))
@ -700,7 +700,7 @@ pub fn miri_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::ConstantKind<'t
.map(Constant::Vec), .map(Constant::Vec),
_ => None, _ => None,
}, },
ty::Float(FloatTy::F64) => match len.kind().try_to_target_usize(lcx.tcx) { ty::Float(FloatTy::F64) => match len.try_to_target_usize(lcx.tcx) {
Some(len) => alloc Some(len) => alloc
.inner() .inner()
.inspect_with_uninit_and_ptr_outside_interpreter(0..(8 * usize::try_from(len).unwrap())) .inspect_with_uninit_and_ptr_outside_interpreter(0..(8 * usize::try_from(len).unwrap()))

View File

@ -2,7 +2,7 @@
| User Type Annotations | User Type Annotations
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} | 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Unevaluated([], DefId(0:8 ~ issue_99325[22bb]::main::{constant#1})) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} | 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), substs: [] } }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
| |
fn main() -> () { fn main() -> () {
let mut _0: (); let mut _0: ();