change thir to use mir::ConstantKind instead of ty::Const
This commit is contained in:
parent
9d843171d1
commit
b38077ea0b
29 changed files with 534 additions and 112 deletions
|
@ -2327,7 +2327,6 @@ dependencies = [
|
|||
"compiletest_rs",
|
||||
"env_logger 0.9.0",
|
||||
"getrandom 0.2.0",
|
||||
"hex 0.4.2",
|
||||
"libc",
|
||||
"log",
|
||||
"measureme 9.1.2",
|
||||
|
|
|
@ -172,6 +172,40 @@ pub(crate) fn try_destructure_const<'tcx>(
|
|||
Ok(mir::DestructuredConst { variant, fields })
|
||||
}
|
||||
|
||||
pub(crate) fn destructure_mir_constant<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
val: mir::ConstantKind<'tcx>,
|
||||
) -> mir::DestructuredMirConstant<'tcx> {
|
||||
trace!("destructure_const: {:?}", val);
|
||||
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
||||
let op = ecx.mir_const_to_op(&val, None).unwrap();
|
||||
|
||||
// We go to `usize` as we cannot allocate anything bigger anyway.
|
||||
let (field_count, variant, down) = match val.ty().kind() {
|
||||
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
|
||||
ty::Adt(def, _) if def.variants.is_empty() => {
|
||||
return mir::DestructuredMirConstant { variant: None, fields: &[] };
|
||||
}
|
||||
ty::Adt(def, _) => {
|
||||
let variant = ecx.read_discriminant(&op).unwrap().1;
|
||||
let down = ecx.operand_downcast(&op, variant).unwrap();
|
||||
(def.variants[variant].fields.len(), Some(variant), down)
|
||||
}
|
||||
ty::Tuple(substs) => (substs.len(), None, op),
|
||||
_ => bug!("cannot destructure constant {:?}", val),
|
||||
};
|
||||
|
||||
let fields_iter = (0..field_count).map(|i| {
|
||||
let field_op = ecx.operand_field(&down, i).unwrap();
|
||||
let val = op_to_const(&ecx, &field_op);
|
||||
mir::ConstantKind::Val(val, field_op.layout.ty)
|
||||
});
|
||||
let fields = tcx.arena.alloc_from_iter(fields_iter);
|
||||
|
||||
mir::DestructuredMirConstant { variant, fields }
|
||||
}
|
||||
|
||||
pub(crate) fn deref_const<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
|
@ -207,3 +241,39 @@ pub(crate) fn deref_const<'tcx>(
|
|||
|
||||
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
pub(crate) fn deref_mir_constant<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
val: mir::ConstantKind<'tcx>,
|
||||
) -> mir::ConstantKind<'tcx> {
|
||||
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
||||
let op = ecx.mir_const_to_op(&val, None).unwrap();
|
||||
let mplace = ecx.deref_operand(&op).unwrap();
|
||||
if let Some(alloc_id) = mplace.ptr.provenance {
|
||||
assert_eq!(
|
||||
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().mutability,
|
||||
Mutability::Not,
|
||||
"deref_const cannot be used with mutable allocations as \
|
||||
that could allow pattern matching to observe mutable statics",
|
||||
);
|
||||
}
|
||||
|
||||
let ty = match mplace.meta {
|
||||
MemPlaceMeta::None => mplace.layout.ty,
|
||||
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
|
||||
// In case of unsized types, figure out the real type behind.
|
||||
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
|
||||
ty::Str => bug!("there's no sized equivalent of a `str`"),
|
||||
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
|
||||
_ => bug!(
|
||||
"type {} should not have metadata, but had {:?}",
|
||||
mplace.layout.ty,
|
||||
mplace.meta
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
mir::ConstantKind::Val(op_to_const(&ecx, &mplace.into()), ty)
|
||||
}
|
||||
|
|
|
@ -45,6 +45,10 @@ pub fn provide(providers: &mut Providers) {
|
|||
let (param_env, value) = param_env_and_value.into_parts();
|
||||
const_eval::try_destructure_const(tcx, param_env, value).ok()
|
||||
};
|
||||
providers.destructure_mir_constant = |tcx, param_env_and_value| {
|
||||
let (param_env, value) = param_env_and_value.into_parts();
|
||||
const_eval::destructure_mir_constant(tcx, param_env, value)
|
||||
};
|
||||
providers.const_to_valtree = |tcx, param_env_and_value| {
|
||||
let (param_env, raw) = param_env_and_value.into_parts();
|
||||
const_eval::const_to_valtree(tcx, param_env, raw)
|
||||
|
@ -53,4 +57,8 @@ pub fn provide(providers: &mut Providers) {
|
|||
let (param_env, value) = param_env_and_value.into_parts();
|
||||
const_eval::deref_const(tcx, param_env, value)
|
||||
};
|
||||
providers.deref_mir_constant = |tcx, param_env_and_value| {
|
||||
let (param_env, value) = param_env_and_value.into_parts();
|
||||
const_eval::deref_mir_constant(tcx, param_env, value)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,13 +9,13 @@ use crate::ty::adjustment::PointerCast;
|
|||
use crate::ty::codec::{TyDecoder, TyEncoder};
|
||||
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
|
||||
use crate::ty::print::{FmtPrinter, Printer};
|
||||
use crate::ty::subst::{Subst, SubstsRef};
|
||||
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef};
|
||||
use crate::ty::{self, List, Ty, TyCtxt};
|
||||
use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex};
|
||||
|
||||
use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir::def::{CtorKind, Namespace};
|
||||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
|
||||
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
|
||||
use rustc_hir::{self, GeneratorKind};
|
||||
use rustc_hir::{self as hir, HirId};
|
||||
use rustc_session::Session;
|
||||
|
@ -2664,6 +2664,16 @@ impl<'tcx> ConstantKind<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn try_val(&self) -> Option<ConstValue<'tcx>> {
|
||||
match self {
|
||||
ConstantKind::Ty(c) => match c.val() {
|
||||
ty::ConstKind::Value(v) => Some(v),
|
||||
_ => None,
|
||||
},
|
||||
ConstantKind::Val(v, _) => Some(*v),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
|
||||
match self {
|
||||
|
@ -2692,6 +2702,32 @@ impl<'tcx> ConstantKind<'tcx> {
|
|||
self.try_to_scalar_int()?.try_into().ok()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
|
||||
match self {
|
||||
Self::Ty(c) => {
|
||||
// FIXME Need to use a different evaluation function that directly returns a `ConstValue`
|
||||
// if evaluation succeeds and does not create a ValTree first
|
||||
if let Some(val) = c.val().try_eval(tcx, param_env) {
|
||||
match val {
|
||||
Ok(val) => Self::Val(val, c.ty()),
|
||||
Err(ErrorReported) => Self::Ty(tcx.const_error(self.ty())),
|
||||
}
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
Self::Val(_, _) => 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: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
|
||||
self.try_eval_bits(tcx, param_env, ty)
|
||||
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn try_eval_bits(
|
||||
&self,
|
||||
|
@ -2726,25 +2762,181 @@ impl<'tcx> ConstantKind<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_bits(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
bits: u128,
|
||||
param_env_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
|
||||
) -> Self {
|
||||
let size = tcx
|
||||
.layout_of(param_env_ty)
|
||||
.unwrap_or_else(|e| {
|
||||
bug!("could not compute layout for {:?}: {:?}", param_env_ty.value, e)
|
||||
})
|
||||
.size;
|
||||
let cv = ConstValue::Scalar(Scalar::from_uint(bits, size));
|
||||
|
||||
Self::Val(cv, param_env_ty.value)
|
||||
}
|
||||
|
||||
pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
|
||||
let cv = ConstValue::from_bool(v);
|
||||
Self::Val(cv, tcx.types.bool)
|
||||
}
|
||||
|
||||
pub fn from_zero_sized(ty: Ty<'tcx>) -> Self {
|
||||
pub fn zero_sized(ty: Ty<'tcx>) -> Self {
|
||||
let cv = ConstValue::Scalar(Scalar::ZST);
|
||||
Self::Val(cv, ty)
|
||||
}
|
||||
|
||||
pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
|
||||
let ty = tcx.types.usize;
|
||||
let size = tcx
|
||||
.layout_of(ty::ParamEnv::empty().and(ty))
|
||||
.unwrap_or_else(|e| bug!("could not compute layout for {:?}: {:?}", ty, e))
|
||||
.size;
|
||||
let cv = ConstValue::Scalar(Scalar::from_uint(n as u128, size));
|
||||
Self::from_bits(tcx, n as u128, ty::ParamEnv::empty().and(ty))
|
||||
}
|
||||
|
||||
Self::Val(cv, ty)
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
pub fn try_eval_lit_or_param(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
expr: &'tcx hir::Expr<'tcx>,
|
||||
) -> Option<Self> {
|
||||
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
|
||||
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
|
||||
let expr = match &expr.kind {
|
||||
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
|
||||
block.expr.as_ref().unwrap()
|
||||
}
|
||||
_ => expr,
|
||||
};
|
||||
|
||||
let lit_input = match expr.kind {
|
||||
hir::ExprKind::Lit(ref lit) => {
|
||||
Some(interpret::LitToConstInput { lit: &lit.node, ty, neg: false })
|
||||
}
|
||||
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
|
||||
hir::ExprKind::Lit(ref lit) => {
|
||||
Some(interpret::LitToConstInput { lit: &lit.node, ty, neg: true })
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(lit_input) = lit_input {
|
||||
// If an error occurred, ignore that it's a literal and leave reporting the error up to
|
||||
// mir.
|
||||
match tcx.at(expr.span).lit_to_mir_constant(lit_input) {
|
||||
Ok(c) => return Some(c),
|
||||
Err(e) => {
|
||||
tcx.sess.delay_span_bug(
|
||||
expr.span,
|
||||
&format!("Const::from_anon_const: couldn't lit_to_const {:?}", e),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
|
||||
match expr.kind {
|
||||
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 index = generics.param_def_id_to_index[&def_id];
|
||||
let name = tcx.hir().name(hir_id);
|
||||
let ty_const = tcx.mk_const(ty::ConstS {
|
||||
val: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
|
||||
ty,
|
||||
});
|
||||
|
||||
Some(Self::Ty(ty_const))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
pub fn from_inline_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
||||
|
||||
let body_id = match tcx.hir().get(hir_id) {
|
||||
hir::Node::AnonConst(ac) => ac.body,
|
||||
_ => span_bug!(
|
||||
tcx.def_span(def_id.to_def_id()),
|
||||
"from_inline_const can only process anonymous constants"
|
||||
),
|
||||
};
|
||||
|
||||
let expr = &tcx.hir().body(body_id).value;
|
||||
|
||||
let ty = tcx.typeck(def_id).node_type(hir_id);
|
||||
|
||||
let ret = match Self::try_eval_lit_or_param(tcx, ty, expr) {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id());
|
||||
let parent_substs =
|
||||
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
|
||||
let substs = ty::InlineConstSubsts::new(
|
||||
tcx,
|
||||
ty::InlineConstSubstsParts { parent_substs, ty },
|
||||
)
|
||||
.substs;
|
||||
let ty_const = tcx.mk_const(ty::ConstS {
|
||||
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
|
||||
def: ty::WithOptConstParam::unknown(def_id).to_global(),
|
||||
substs,
|
||||
promoted: None,
|
||||
}),
|
||||
ty,
|
||||
});
|
||||
|
||||
Self::Ty(ty_const)
|
||||
}
|
||||
};
|
||||
debug_assert!(!ret.has_free_regions());
|
||||
ret
|
||||
}
|
||||
|
||||
/// Literals are converted to `ConstantKindVal`, const generic parameters are eagerly
|
||||
/// converted to a constant, everything else becomes `Unevaluated`.
|
||||
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
|
||||
Self::from_opt_const_arg_anon_const(tcx, ty::WithOptConstParam::unknown(def_id))
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
fn from_opt_const_arg_anon_const(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def: ty::WithOptConstParam<LocalDefId>,
|
||||
) -> Self {
|
||||
let body_id = match tcx.hir().get_by_def_id(def.did) {
|
||||
hir::Node::AnonConst(ac) => ac.body,
|
||||
_ => span_bug!(
|
||||
tcx.def_span(def.did.to_def_id()),
|
||||
"from_anon_const can only process anonymous constants"
|
||||
),
|
||||
};
|
||||
|
||||
let expr = &tcx.hir().body(body_id).value;
|
||||
debug!(?expr);
|
||||
|
||||
let ty = tcx.type_of(def.def_id_for_type_of());
|
||||
|
||||
match Self::try_eval_lit_or_param(tcx, ty, expr) {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let ty_const = tcx.mk_const(ty::ConstS {
|
||||
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
|
||||
def: def.to_global(),
|
||||
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
|
||||
promoted: None,
|
||||
}),
|
||||
ty,
|
||||
});
|
||||
Self::Ty(ty_const)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Values computed by queries that use MIR.
|
||||
|
||||
use crate::mir::{Body, Promoted};
|
||||
use crate::mir::{self, Body, Promoted};
|
||||
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
|
||||
use rustc_data_structures::stable_map::FxHashMap;
|
||||
use rustc_data_structures::vec_map::VecMap;
|
||||
|
@ -421,6 +421,13 @@ pub struct DestructuredConst<'tcx> {
|
|||
pub fields: &'tcx [ty::Const<'tcx>],
|
||||
}
|
||||
|
||||
/// The constituent parts of an ADT or array.
|
||||
#[derive(Copy, Clone, Debug, HashStable)]
|
||||
pub struct DestructuredMirConstant<'tcx> {
|
||||
pub variant: Option<VariantIdx>,
|
||||
pub fields: &'tcx [mir::ConstantKind<'tcx>],
|
||||
}
|
||||
|
||||
/// Coverage information summarized from a MIR if instrumented for source code coverage (see
|
||||
/// compiler option `-Cinstrument-coverage`). This information is generated by the
|
||||
/// `InstrumentCoverage` MIR pass and can be retrieved via the `coverageinfo` query.
|
||||
|
|
|
@ -940,6 +940,13 @@ rustc_queries! {
|
|||
remap_env_constness
|
||||
}
|
||||
|
||||
/// Destructure an `mir::ConstantKind` ADT or array into its variant index
|
||||
/// and its field values.
|
||||
query destructure_mir_constant(key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>) -> mir::DestructuredMirConstant<'tcx> {
|
||||
desc { "destructure mir constant"}
|
||||
remap_env_constness
|
||||
}
|
||||
|
||||
/// Dereference a constant reference or raw pointer and turn the result into a constant
|
||||
/// again.
|
||||
query deref_const(
|
||||
|
@ -949,6 +956,15 @@ rustc_queries! {
|
|||
remap_env_constness
|
||||
}
|
||||
|
||||
/// Dereference a constant reference or raw pointer and turn the result into a constant
|
||||
/// again.
|
||||
query deref_mir_constant(
|
||||
key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
|
||||
) -> mir::ConstantKind<'tcx> {
|
||||
desc { "deref constant" }
|
||||
remap_env_constness
|
||||
}
|
||||
|
||||
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
|
||||
desc { "get a &core::panic::Location referring to a span" }
|
||||
}
|
||||
|
@ -960,6 +976,10 @@ rustc_queries! {
|
|||
desc { "converting literal to const" }
|
||||
}
|
||||
|
||||
query lit_to_mir_constant(key: LitToConstInput<'tcx>) -> Result<mir::ConstantKind<'tcx>, LitToConstError> {
|
||||
desc { "converting literal to mir constant" }
|
||||
}
|
||||
|
||||
query check_match(key: DefId) {
|
||||
desc { |tcx| "match-checking `{}`", tcx.def_path_str(key) }
|
||||
cache_on_disk_if { key.is_local() }
|
||||
|
|
|
@ -19,11 +19,11 @@ use rustc_middle::infer::canonical::Canonical;
|
|||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::interpret::AllocId;
|
||||
use rustc_middle::mir::{
|
||||
BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp, UserTypeProjection,
|
||||
self, BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp, UserTypeProjection,
|
||||
};
|
||||
use rustc_middle::ty::adjustment::PointerCast;
|
||||
use rustc_middle::ty::subst::SubstsRef;
|
||||
use rustc_middle::ty::{self, AdtDef, Const, Ty, UpvarSubsts, UserType};
|
||||
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts, UserType};
|
||||
use rustc_middle::ty::{
|
||||
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
|
||||
};
|
||||
|
@ -193,7 +193,7 @@ pub enum StmtKind<'tcx> {
|
|||
|
||||
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
||||
rustc_data_structures::static_assert_size!(Expr<'_>, 104);
|
||||
rustc_data_structures::static_assert_size!(Expr<'_>, 144);
|
||||
|
||||
/// A THIR expression.
|
||||
#[derive(Debug, HashStable)]
|
||||
|
@ -375,7 +375,7 @@ pub enum ExprKind<'tcx> {
|
|||
/// An array literal constructed from one repeated element, e.g. `[1; 5]`.
|
||||
Repeat {
|
||||
value: ExprId,
|
||||
count: Const<'tcx>,
|
||||
count: ty::Const<'tcx>,
|
||||
},
|
||||
/// An array, e.g. `[a, b, c, d]`.
|
||||
Array {
|
||||
|
@ -522,7 +522,7 @@ pub enum InlineAsmOperand<'tcx> {
|
|||
out_expr: Option<ExprId>,
|
||||
},
|
||||
Const {
|
||||
value: Const<'tcx>,
|
||||
value: mir::ConstantKind<'tcx>,
|
||||
span: Span,
|
||||
},
|
||||
SymFn {
|
||||
|
@ -661,7 +661,7 @@ pub enum PatKind<'tcx> {
|
|||
/// * Opaque constants, that must not be matched structurally. So anything that does not derive
|
||||
/// `PartialEq` and `Eq`.
|
||||
Constant {
|
||||
value: ty::Const<'tcx>,
|
||||
value: mir::ConstantKind<'tcx>,
|
||||
},
|
||||
|
||||
Range(PatRange<'tcx>),
|
||||
|
@ -691,8 +691,8 @@ pub enum PatKind<'tcx> {
|
|||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
|
||||
pub struct PatRange<'tcx> {
|
||||
pub lo: ty::Const<'tcx>,
|
||||
pub hi: ty::Const<'tcx>,
|
||||
pub lo: mir::ConstantKind<'tcx>,
|
||||
pub hi: mir::ConstantKind<'tcx>,
|
||||
pub end: RangeEnd,
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ pub enum CastKind {
|
|||
/// A node of an `AbstractConst`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum Node<'tcx> {
|
||||
Leaf(ty::Const<'tcx>),
|
||||
Leaf(mir::ConstantKind<'tcx>),
|
||||
Binop(mir::BinOp, NodeId, NodeId),
|
||||
UnaryOp(mir::UnOp, NodeId),
|
||||
FunctionCall(NodeId, &'tcx [NodeId]),
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use super::{
|
||||
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
|
||||
};
|
||||
use crate::mir::ConstantKind;
|
||||
use crate::ty::Const;
|
||||
|
||||
pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
||||
fn thir(&self) -> &'a Thir<'tcx>;
|
||||
|
@ -24,6 +26,8 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
|||
fn visit_pat(&mut self, pat: &Pat<'tcx>) {
|
||||
walk_pat(self, pat);
|
||||
}
|
||||
|
||||
fn visit_constant(&mut self, _constant: ConstantKind<'tcx>) {}
|
||||
}
|
||||
|
||||
pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) {
|
||||
|
@ -90,8 +94,13 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
|
|||
visitor.visit_expr(&visitor.thir()[value])
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
ConstBlock { did: _, substs: _ } => {}
|
||||
Repeat { value, count: _ } => {
|
||||
=======
|
||||
ConstBlock { value } => visitor.visit_constant(value),
|
||||
Repeat { value, count } => {
|
||||
>>>>>>> 6064f16d846 (change thir to use mir::ConstantKind instead of ty::Const)
|
||||
visitor.visit_expr(&visitor.thir()[value]);
|
||||
}
|
||||
Array { ref fields } | Tuple { ref fields } => {
|
||||
|
|
|
@ -126,7 +126,7 @@ impl<'tcx> ConstKind<'tcx> {
|
|||
#[inline]
|
||||
/// Tries to evaluate the constant if it is `Unevaluated`. If that isn't possible or necessary
|
||||
/// return `None`.
|
||||
pub(super) fn try_eval(
|
||||
pub fn try_eval(
|
||||
self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ParamEnv<'tcx>,
|
||||
|
|
|
@ -662,8 +662,8 @@ impl<'tcx> TypeFolder<'tcx> for OpaqueTypeExpander<'tcx> {
|
|||
impl<'tcx> Ty<'tcx> {
|
||||
/// Returns the maximum value for the given numeric type (including `char`s)
|
||||
/// or returns `None` if the type is not numeric.
|
||||
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<Const<'tcx>> {
|
||||
let val = match self.kind() {
|
||||
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<u128> {
|
||||
match self.kind() {
|
||||
ty::Int(_) | ty::Uint(_) => {
|
||||
let (size, signed) = int_size_and_signed(tcx, self);
|
||||
let val =
|
||||
|
@ -676,14 +676,13 @@ impl<'tcx> Ty<'tcx> {
|
|||
ty::FloatTy::F64 => rustc_apfloat::ieee::Double::INFINITY.to_bits(),
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the minimum value for the given numeric type (including `char`s)
|
||||
/// or returns `None` if the type is not numeric.
|
||||
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<Const<'tcx>> {
|
||||
let val = match self.kind() {
|
||||
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<u128> {
|
||||
match self.kind() {
|
||||
ty::Int(_) | ty::Uint(_) => {
|
||||
let (size, signed) = int_size_and_signed(tcx, self);
|
||||
let val = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 };
|
||||
|
@ -695,8 +694,7 @@ impl<'tcx> Ty<'tcx> {
|
|||
ty::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(),
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether values of this type `T` are *moved* or *copied*
|
||||
|
|
|
@ -73,7 +73,7 @@ impl<'tcx> CFG<'tcx> {
|
|||
Rvalue::Use(Operand::Constant(Box::new(Constant {
|
||||
span: source_info.span,
|
||||
user_ty: None,
|
||||
literal: ConstantKind::from_zero_sized(tcx.types.unit),
|
||||
literal: ConstantKind::zero_sized(tcx.types.unit),
|
||||
}))),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -85,6 +85,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
|
||||
Constant { span, user_ty: None, literal }
|
||||
}
|
||||
ExprKind::ConstBlock { value } => {
|
||||
Constant { span: span, user_ty: None, literal: value }
|
||||
}
|
||||
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -322,7 +322,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
block.and(Rvalue::Use(Operand::Constant(Box::new(Constant {
|
||||
span: expr_span,
|
||||
user_ty: None,
|
||||
literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit).into(),
|
||||
literal: ConstantKind::zero_sized(this.tcx.types.unit),
|
||||
}))))
|
||||
}
|
||||
ExprKind::Yield { .. }
|
||||
|
@ -552,7 +552,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
||||
let param_ty = ty::ParamEnv::empty().and(ty);
|
||||
let size = self.tcx.layout_of(param_ty).unwrap().size;
|
||||
let literal = ty::Const::from_bits(self.tcx, size.unsigned_int_max(), param_ty);
|
||||
let literal = ConstantKind::from_bits(self.tcx, size.unsigned_int_max(), param_ty);
|
||||
|
||||
self.literal_operand(span, literal)
|
||||
}
|
||||
|
@ -563,7 +563,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
let param_ty = ty::ParamEnv::empty().and(ty);
|
||||
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
|
||||
let n = 1 << (bits - 1);
|
||||
let literal = ty::Const::from_bits(self.tcx, n, param_ty);
|
||||
let literal = ConstantKind::from_bits(self.tcx, n, param_ty);
|
||||
|
||||
self.literal_operand(span, literal)
|
||||
}
|
||||
|
|
|
@ -964,13 +964,13 @@ enum TestKind<'tcx> {
|
|||
///
|
||||
/// For `bool` we always generate two edges, one for `true` and one for
|
||||
/// `false`.
|
||||
options: FxIndexMap<ty::Const<'tcx>, u128>,
|
||||
options: FxIndexMap<ConstantKind<'tcx>, u128>,
|
||||
},
|
||||
|
||||
/// Test for equality with value, possibly after an unsizing coercion to
|
||||
/// `ty`,
|
||||
Eq {
|
||||
value: ty::Const<'tcx>,
|
||||
value: ConstantKind<'tcx>,
|
||||
// Integer types are handled by `SwitchInt`, and constants with ADT
|
||||
// types are converted back into patterns, so this can only be `&str`,
|
||||
// `&[T]`, `f32` or `f64`.
|
||||
|
|
|
@ -228,9 +228,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
_ => (None, 0),
|
||||
};
|
||||
if let Some((min, max, sz)) = range {
|
||||
if let (Some(lo), Some(hi)) =
|
||||
(lo.val().try_to_bits(sz), hi.val().try_to_bits(sz))
|
||||
{
|
||||
if let (Some(lo), Some(hi)) = (lo.try_to_bits(sz), hi.try_to_bits(sz)) {
|
||||
// We want to compare ranges numerically, but the order of the bitwise
|
||||
// representation of signed integers does not match their numeric order.
|
||||
// Thus, to correct the ordering, we need to shift the range of signed
|
||||
|
|
|
@ -86,7 +86,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
test_place: &PlaceBuilder<'tcx>,
|
||||
candidate: &Candidate<'pat, 'tcx>,
|
||||
switch_ty: Ty<'tcx>,
|
||||
options: &mut FxIndexMap<ty::Const<'tcx>, u128>,
|
||||
options: &mut FxIndexMap<ConstantKind<'tcx>, u128>,
|
||||
) -> bool {
|
||||
let Some(match_pair) = candidate.match_pairs.iter().find(|mp| mp.place == *test_place) else {
|
||||
return false;
|
||||
|
@ -366,7 +366,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
block: BasicBlock,
|
||||
make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
|
||||
source_info: SourceInfo,
|
||||
value: ty::Const<'tcx>,
|
||||
value: ConstantKind<'tcx>,
|
||||
place: Place<'tcx>,
|
||||
mut ty: Ty<'tcx>,
|
||||
) {
|
||||
|
@ -760,7 +760,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
span_bug!(match_pair.pattern.span, "simplifyable pattern found: {:?}", match_pair.pattern)
|
||||
}
|
||||
|
||||
fn const_range_contains(&self, range: PatRange<'tcx>, value: ty::Const<'tcx>) -> Option<bool> {
|
||||
fn const_range_contains(
|
||||
&self,
|
||||
range: PatRange<'tcx>,
|
||||
value: ConstantKind<'tcx>,
|
||||
) -> Option<bool> {
|
||||
use std::cmp::Ordering::*;
|
||||
|
||||
let tcx = self.tcx;
|
||||
|
@ -777,7 +781,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
fn values_not_contained_in_range(
|
||||
&self,
|
||||
range: PatRange<'tcx>,
|
||||
options: &FxIndexMap<ty::Const<'tcx>, u128>,
|
||||
options: &FxIndexMap<ConstantKind<'tcx>, u128>,
|
||||
) -> Option<bool> {
|
||||
for &val in options.keys() {
|
||||
if self.const_range_contains(range, val)? {
|
||||
|
@ -823,7 +827,7 @@ fn trait_method<'tcx>(
|
|||
method_name: Symbol,
|
||||
self_ty: Ty<'tcx>,
|
||||
params: &[GenericArg<'tcx>],
|
||||
) -> ty::Const<'tcx> {
|
||||
) -> ConstantKind<'tcx> {
|
||||
let substs = tcx.mk_substs_trait(self_ty, params);
|
||||
|
||||
// The unhygienic comparison here is acceptable because this is only
|
||||
|
@ -836,5 +840,6 @@ fn trait_method<'tcx>(
|
|||
|
||||
let method_ty = tcx.type_of(item.def_id);
|
||||
let method_ty = method_ty.subst(tcx, substs);
|
||||
ty::Const::zero_sized(tcx, method_ty)
|
||||
|
||||
ConstantKind::zero_sized(method_ty)
|
||||
}
|
||||
|
|
|
@ -25,8 +25,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
|
||||
/// Convenience function for creating a literal operand, one
|
||||
/// without any user type annotation.
|
||||
crate fn literal_operand(&mut self, span: Span, literal: ty::Const<'tcx>) -> Operand<'tcx> {
|
||||
let literal = literal.into();
|
||||
crate fn literal_operand(&mut self, span: Span, literal: ConstantKind<'tcx>) -> Operand<'tcx> {
|
||||
let constant = Box::new(Constant { span, user_ty: None, literal });
|
||||
Operand::Constant(constant)
|
||||
}
|
||||
|
@ -34,7 +33,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
// Returns a zero literal operand for the appropriate type, works for
|
||||
// bool, char and integers.
|
||||
crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
||||
let literal = ty::Const::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
|
||||
let literal = ConstantKind::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
|
||||
|
||||
self.literal_operand(span, literal)
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ use rustc_middle::ty::query::Providers;
|
|||
pub fn provide(providers: &mut Providers) {
|
||||
providers.check_match = thir::pattern::check_match;
|
||||
providers.lit_to_const = thir::constant::lit_to_const;
|
||||
providers.lit_to_mir_constant = thir::constant::lit_to_mir_constant;
|
||||
providers.mir_built = build::mir_built;
|
||||
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
|
||||
providers.thir_check_unsafety_for_const_arg = check_unsafety::thir_check_unsafety_for_const_arg;
|
||||
|
|
|
@ -3,6 +3,7 @@ use rustc_ast as ast;
|
|||
use rustc_middle::mir::interpret::{
|
||||
Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
|
||||
};
|
||||
use rustc_middle::mir::ConstantKind;
|
||||
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
|
||||
use rustc_span::symbol::Symbol;
|
||||
use rustc_target::abi::Size;
|
||||
|
|
|
@ -8,7 +8,7 @@ use rustc_middle::hir::place::Place as HirPlace;
|
|||
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
|
||||
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::{BinOp, BorrowKind, Field, UnOp};
|
||||
use rustc_middle::mir::{self, BinOp, BorrowKind, Field, UnOp};
|
||||
use rustc_middle::thir::*;
|
||||
use rustc_middle::ty::adjustment::{
|
||||
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
|
||||
|
@ -491,7 +491,8 @@ impl<'tcx> Cx<'tcx> {
|
|||
hir::InlineAsmOperand::Const { ref anon_const } => {
|
||||
let anon_const_def_id =
|
||||
self.tcx.hir().local_def_id(anon_const.hir_id);
|
||||
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
|
||||
let value =
|
||||
mir::ConstantKind::from_anon_const(self.tcx, anon_const_def_id);
|
||||
let span = self.tcx.hir().span(anon_const.hir_id);
|
||||
|
||||
InlineAsmOperand::Const { value, span }
|
||||
|
|
|
@ -12,6 +12,8 @@ use rustc_hir::def_id::{DefId, LocalDefId};
|
|||
use rustc_hir::HirId;
|
||||
use rustc_hir::Node;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
|
||||
use rustc_middle::mir::ConstantKind;
|
||||
use rustc_middle::thir::*;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_span::Span;
|
||||
|
@ -75,6 +77,24 @@ impl<'tcx> Cx<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
crate fn const_eval_literal(
|
||||
&mut self,
|
||||
lit: &'tcx ast::LitKind,
|
||||
ty: Ty<'tcx>,
|
||||
sp: Span,
|
||||
neg: bool,
|
||||
) -> ConstantKind<'tcx> {
|
||||
match self.tcx.at(sp).lit_to_mir_constant(LitToConstInput { lit, ty, neg }) {
|
||||
Ok(c) => c,
|
||||
Err(LitToConstError::Reported) => {
|
||||
// create a dummy value and continue compiling
|
||||
ConstantKind::Ty(self.tcx.const_error(ty))
|
||||
}
|
||||
Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
|
||||
}
|
||||
}
|
||||
|
||||
crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
|
||||
let p = match self.tcx.hir().get(p.hir_id) {
|
||||
Node::Pat(p) | Node::Binding(p) => p,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use rustc_hir as hir;
|
||||
use rustc_index::vec::Idx;
|
||||
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
|
||||
use rustc_middle::mir::Field;
|
||||
use rustc_middle::mir::{self, Field};
|
||||
use rustc_middle::thir::{FieldPat, Pat, PatKind};
|
||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
|
||||
|
@ -22,7 +22,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
#[instrument(level = "debug", skip(self))]
|
||||
pub(super) fn const_to_pat(
|
||||
&self,
|
||||
cv: ty::Const<'tcx>,
|
||||
cv: mir::ConstantKind<'tcx>,
|
||||
id: hir::HirId,
|
||||
span: Span,
|
||||
mir_structural_match_violation: bool,
|
||||
|
@ -152,7 +152,11 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
ty.is_structural_eq_shallow(self.infcx.tcx)
|
||||
}
|
||||
|
||||
fn to_pat(&mut self, cv: ty::Const<'tcx>, mir_structural_match_violation: bool) -> Pat<'tcx> {
|
||||
fn to_pat(
|
||||
&mut self,
|
||||
cv: mir::ConstantKind<'tcx>,
|
||||
mir_structural_match_violation: bool,
|
||||
) -> Pat<'tcx> {
|
||||
trace!(self.treat_byte_string_as_slice);
|
||||
// This method is just a wrapper handling a validity check; the heavy lifting is
|
||||
// performed by the recursive `recur` method, which is not meant to be
|
||||
|
@ -246,7 +250,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
|
||||
fn field_pats(
|
||||
&self,
|
||||
vals: impl Iterator<Item = ty::Const<'tcx>>,
|
||||
vals: impl Iterator<Item = mir::ConstantKind<'tcx>>,
|
||||
) -> Result<Vec<FieldPat<'tcx>>, FallbackToConstRef> {
|
||||
vals.enumerate()
|
||||
.map(|(idx, val)| {
|
||||
|
@ -259,7 +263,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
// Recursive helper for `to_pat`; invoke that (instead of calling this directly).
|
||||
fn recur(
|
||||
&self,
|
||||
cv: ty::Const<'tcx>,
|
||||
cv: mir::ConstantKind<'tcx>,
|
||||
mir_structural_match_violation: bool,
|
||||
) -> Result<Pat<'tcx>, FallbackToConstRef> {
|
||||
let id = self.id;
|
||||
|
@ -365,7 +369,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
PatKind::Wild
|
||||
}
|
||||
ty::Adt(adt_def, substs) if adt_def.is_enum() => {
|
||||
let destructured = tcx.destructure_const(param_env.and(cv));
|
||||
let destructured = tcx.destructure_mir_constant(param_env.and(cv));
|
||||
PatKind::Variant {
|
||||
adt_def: *adt_def,
|
||||
substs,
|
||||
|
@ -376,12 +380,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
ty::Tuple(_) | ty::Adt(_, _) => {
|
||||
let destructured = tcx.destructure_const(param_env.and(cv));
|
||||
let destructured = tcx.destructure_mir_constant(param_env.and(cv));
|
||||
PatKind::Leaf { subpatterns: self.field_pats(destructured.fields.iter().copied())? }
|
||||
}
|
||||
ty::Array(..) => PatKind::Array {
|
||||
prefix: tcx
|
||||
.destructure_const(param_env.and(cv))
|
||||
.destructure_mir_constant(param_env.and(cv))
|
||||
.fields
|
||||
.iter()
|
||||
.map(|val| self.recur(*val, false))
|
||||
|
@ -412,12 +416,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
// arrays.
|
||||
ty::Array(..) if !self.treat_byte_string_as_slice => {
|
||||
let old = self.behind_reference.replace(true);
|
||||
let array = tcx.deref_const(self.param_env.and(cv));
|
||||
let array = tcx.deref_mir_constant(self.param_env.and(cv));
|
||||
let val = PatKind::Deref {
|
||||
subpattern: Pat {
|
||||
kind: Box::new(PatKind::Array {
|
||||
prefix: tcx
|
||||
.destructure_const(param_env.and(array))
|
||||
.destructure_mir_constant(param_env.and(array))
|
||||
.fields
|
||||
.iter()
|
||||
.map(|val| self.recur(*val, false))
|
||||
|
@ -438,12 +442,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
// pattern.
|
||||
ty::Slice(elem_ty) => {
|
||||
let old = self.behind_reference.replace(true);
|
||||
let array = tcx.deref_const(self.param_env.and(cv));
|
||||
let array = tcx.deref_mir_constant(self.param_env.and(cv));
|
||||
let val = PatKind::Deref {
|
||||
subpattern: Pat {
|
||||
kind: Box::new(PatKind::Slice {
|
||||
prefix: tcx
|
||||
.destructure_const(param_env.and(array))
|
||||
.destructure_mir_constant(param_env.and(array))
|
||||
.fields
|
||||
.iter()
|
||||
.map(|val| self.recur(*val, false))
|
||||
|
@ -512,7 +516,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
|||
// we fall back to a const pattern. If we do not do this, we may end up with
|
||||
// a !structural-match constant that is not of reference type, which makes it
|
||||
// very hard to invoke `PartialEq::eq` on it as a fallback.
|
||||
let val = match self.recur(tcx.deref_const(self.param_env.and(cv)), false) {
|
||||
let val = match self.recur(tcx.deref_mir_constant(self.param_env.and(cv)), false) {
|
||||
Ok(subpattern) => PatKind::Deref { subpattern },
|
||||
Err(_) => PatKind::Constant { value: cv },
|
||||
};
|
||||
|
|
|
@ -52,10 +52,10 @@ use rustc_data_structures::captures::Captures;
|
|||
use rustc_index::vec::Idx;
|
||||
|
||||
use rustc_hir::{HirId, RangeEnd};
|
||||
use rustc_middle::mir::Field;
|
||||
use rustc_middle::mir::{self, Field};
|
||||
use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange};
|
||||
use rustc_middle::ty::layout::IntegerExt;
|
||||
use rustc_middle::ty::{self, Const, Ty, TyCtxt, VariantDef};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, VariantDef};
|
||||
use rustc_middle::{middle::stability::EvalResult, mir::interpret::ConstValue};
|
||||
use rustc_session::lint;
|
||||
use rustc_span::{Span, DUMMY_SP};
|
||||
|
@ -136,12 +136,13 @@ impl IntRange {
|
|||
fn from_const<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
value: Const<'tcx>,
|
||||
value: mir::ConstantKind<'tcx>,
|
||||
) -> Option<IntRange> {
|
||||
let ty = value.ty();
|
||||
if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, ty) {
|
||||
let val = (|| {
|
||||
if let ty::ConstKind::Value(ConstValue::Scalar(scalar)) = value.val() {
|
||||
match value {
|
||||
mir::ConstantKind::Val(ConstValue::Scalar(scalar), _) => {
|
||||
// For this specific pattern we can skip a lot of effort and go
|
||||
// straight to the result, after doing a bit of checking. (We
|
||||
// could remove this branch and just fall through, which
|
||||
|
@ -150,6 +151,15 @@ impl IntRange {
|
|||
return Some(bits);
|
||||
}
|
||||
}
|
||||
mir::ConstantKind::Ty(c) => match c.val() {
|
||||
ty::ConstKind::Value(_) => bug!(
|
||||
"encountered ConstValue in mir::ConstantKind::Ty, whereas this is expected to be in ConstantKind::Val"
|
||||
),
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// This is a more general form of the previous case.
|
||||
value.try_eval_bits(tcx, param_env, ty)
|
||||
})()?;
|
||||
|
@ -234,8 +244,8 @@ impl IntRange {
|
|||
let (lo, hi) = (lo ^ bias, hi ^ bias);
|
||||
|
||||
let env = ty::ParamEnv::empty().and(ty);
|
||||
let lo_const = ty::Const::from_bits(tcx, lo, env);
|
||||
let hi_const = ty::Const::from_bits(tcx, hi, env);
|
||||
let lo_const = mir::ConstantKind::from_bits(tcx, lo, env);
|
||||
let hi_const = mir::ConstantKind::from_bits(tcx, hi, env);
|
||||
|
||||
let kind = if lo == hi {
|
||||
PatKind::Constant { value: lo_const }
|
||||
|
@ -630,9 +640,9 @@ pub(super) enum Constructor<'tcx> {
|
|||
/// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
|
||||
IntRange(IntRange),
|
||||
/// Ranges of floating-point literal values (`2.0..=5.2`).
|
||||
FloatRange(ty::Const<'tcx>, ty::Const<'tcx>, RangeEnd),
|
||||
FloatRange(mir::ConstantKind<'tcx>, mir::ConstantKind<'tcx>, RangeEnd),
|
||||
/// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
|
||||
Str(ty::Const<'tcx>),
|
||||
Str(mir::ConstantKind<'tcx>),
|
||||
/// Array and slice patterns.
|
||||
Slice(Slice),
|
||||
/// Constants that must not be matched structurally. They are treated as black
|
||||
|
|
|
@ -17,7 +17,7 @@ use rustc_hir::RangeEnd;
|
|||
use rustc_index::vec::Idx;
|
||||
use rustc_middle::mir::interpret::{get_slice_bytes, ConstValue};
|
||||
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
|
||||
use rustc_middle::mir::UserTypeProjection;
|
||||
use rustc_middle::mir::{self, UserTypeProjection};
|
||||
use rustc_middle::mir::{BorrowKind, Field, Mutability};
|
||||
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, Pat, PatKind, PatRange, PatTyProj};
|
||||
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
|
||||
|
@ -121,8 +121,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
fn lower_pattern_range(
|
||||
&mut self,
|
||||
ty: Ty<'tcx>,
|
||||
lo: ty::Const<'tcx>,
|
||||
hi: ty::Const<'tcx>,
|
||||
lo: mir::ConstantKind<'tcx>,
|
||||
hi: mir::ConstantKind<'tcx>,
|
||||
end: RangeEnd,
|
||||
span: Span,
|
||||
) -> PatKind<'tcx> {
|
||||
|
@ -177,16 +177,24 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
ty: Ty<'tcx>,
|
||||
lo: Option<&PatKind<'tcx>>,
|
||||
hi: Option<&PatKind<'tcx>>,
|
||||
) -> Option<(ty::Const<'tcx>, ty::Const<'tcx>)> {
|
||||
) -> Option<(mir::ConstantKind<'tcx>, mir::ConstantKind<'tcx>)> {
|
||||
match (lo, hi) {
|
||||
(Some(PatKind::Constant { value: lo }), Some(PatKind::Constant { value: hi })) => {
|
||||
Some((*lo, *hi))
|
||||
}
|
||||
(Some(PatKind::Constant { value: lo }), None) => {
|
||||
Some((*lo, ty.numeric_max_val(self.tcx)?))
|
||||
let hi = ty.numeric_max_val(self.tcx)?;
|
||||
Some((
|
||||
*lo,
|
||||
mir::ConstantKind::from_bits(self.tcx, hi, ty::ParamEnv::empty().and(ty)),
|
||||
))
|
||||
}
|
||||
(None, Some(PatKind::Constant { value: hi })) => {
|
||||
Some((ty.numeric_min_val(self.tcx)?, *hi))
|
||||
let lo = ty.numeric_min_val(self.tcx)?;
|
||||
Some((
|
||||
mir::ConstantKind::from_bits(self.tcx, lo, ty::ParamEnv::empty().and(ty)),
|
||||
*hi,
|
||||
))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -488,9 +496,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
|
||||
match self.tcx.const_eval_instance(param_env_reveal_all, instance, Some(span)) {
|
||||
Ok(value) => {
|
||||
let const_ =
|
||||
ty::Const::from_value(self.tcx, value, self.typeck_results.node_type(id));
|
||||
|
||||
let const_ = mir::ConstantKind::Val(value, ty);
|
||||
let pattern = self.const_to_pat(const_, id, span, mir_structural_match_violation);
|
||||
|
||||
if !is_associated_const {
|
||||
|
@ -539,25 +545,30 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
span: Span,
|
||||
) -> PatKind<'tcx> {
|
||||
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
|
||||
let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
|
||||
let value = mir::ConstantKind::from_inline_const(self.tcx, anon_const_def_id);
|
||||
|
||||
// Evaluate early like we do in `lower_path`.
|
||||
let value = value.eval(self.tcx, self.param_env);
|
||||
|
||||
match value.val() {
|
||||
match value {
|
||||
mir::ConstantKind::Ty(c) => {
|
||||
match c.val() {
|
||||
ConstKind::Param(_) => {
|
||||
self.errors.push(PatternError::ConstParamInPattern(span));
|
||||
return PatKind::Wild;
|
||||
}
|
||||
ConstKind::Unevaluated(_) => {
|
||||
// If we land here it means the const can't be evaluated because it's `TooGeneric`.
|
||||
self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter");
|
||||
self.tcx
|
||||
.sess
|
||||
.span_err(span, "constant pattern depends on a generic parameter");
|
||||
return PatKind::Wild;
|
||||
}
|
||||
_ => (),
|
||||
_ => bug!("Expected either ConstKind::Param or ConstKind::Unevaluated"),
|
||||
}
|
||||
}
|
||||
mir::ConstantKind::Val(_, _) => *self.const_to_pat(value, id, span, false).kind,
|
||||
}
|
||||
|
||||
*self.const_to_pat(value, id, span, false).kind
|
||||
}
|
||||
|
||||
/// Converts literals, paths and negation of literals to patterns.
|
||||
|
@ -584,8 +595,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
|
||||
let lit_input =
|
||||
LitToConstInput { lit: &lit.node, ty: self.typeck_results.expr_ty(expr), neg };
|
||||
match self.tcx.at(expr.span).lit_to_const(lit_input) {
|
||||
Ok(val) => *self.const_to_pat(val, expr.hir_id, lit.span, false).kind,
|
||||
match self.tcx.at(expr.span).lit_to_mir_constant(lit_input) {
|
||||
Ok(constant) => *self.const_to_pat(constant, expr.hir_id, lit.span, false).kind,
|
||||
Err(LitToConstError::Reported) => PatKind::Wild,
|
||||
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
|
||||
}
|
||||
|
@ -739,15 +750,14 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
crate fn compare_const_vals<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
a: ty::Const<'tcx>,
|
||||
b: ty::Const<'tcx>,
|
||||
a: mir::ConstantKind<'tcx>,
|
||||
b: mir::ConstantKind<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Option<Ordering> {
|
||||
trace!("compare_const_vals: {:?}, {:?}", a, b);
|
||||
|
||||
let from_bool = |v: bool| v.then_some(Ordering::Equal);
|
||||
|
||||
let fallback = || from_bool(a == b);
|
||||
|
@ -759,7 +769,8 @@ crate fn compare_const_vals<'tcx>(
|
|||
|
||||
// Early return for equal constants (so e.g. references to ZSTs can be compared, even if they
|
||||
// are just integer addresses).
|
||||
if a.val() == b.val() {
|
||||
// FIXME This might be wrong
|
||||
if a == b {
|
||||
return from_bool(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ use rustc_hir::{AssocItemKind, HirIdSet, Node, PatKind};
|
|||
use rustc_middle::bug;
|
||||
use rustc_middle::hir::nested_filter;
|
||||
use rustc_middle::middle::privacy::{AccessLevel, AccessLevels};
|
||||
use rustc_middle::mir::ConstantKind;
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::thir::abstract_const::Node as ACNode;
|
||||
use rustc_middle::ty::fold::TypeVisitor;
|
||||
|
@ -156,7 +157,10 @@ where
|
|||
ct: AbstractConst<'tcx>,
|
||||
) -> ControlFlow<V::BreakTy> {
|
||||
const_evaluatable::walk_abstract_const(tcx, ct, |node| match node.root(tcx) {
|
||||
ACNode::Leaf(leaf) => self.visit_const(leaf),
|
||||
ACNode::Leaf(leaf) => match leaf {
|
||||
ConstantKind::Ty(c) => self.visit_const(c),
|
||||
ConstantKind::Val(_, ty) => self.visit_ty(ty),
|
||||
},
|
||||
ACNode::Cast(_, _, ty) => self.visit_ty(ty),
|
||||
ACNode::Binop(..) | ACNode::UnaryOp(..) | ACNode::FunctionCall(_, _) => {
|
||||
ControlFlow::CONTINUE
|
||||
|
@ -284,7 +288,7 @@ where
|
|||
fn visit_const(&mut self, c: Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||
self.visit_ty(c.ty())?;
|
||||
let tcx = self.def_id_visitor.tcx();
|
||||
if let Ok(Some(ct)) = AbstractConst::from_const(tcx, c) {
|
||||
if let Ok(Some(ct)) = AbstractConst::from_constant(tcx, ConstantKind::Ty(c)) {
|
||||
self.visit_abstract_const_expr(tcx, ct)?;
|
||||
}
|
||||
ControlFlow::CONTINUE
|
||||
|
|
|
@ -37,9 +37,14 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
|
|||
param_env: ty::ParamEnv<'tcx>,
|
||||
span: Span,
|
||||
) -> Result<(), NotConstEvaluatable> {
|
||||
<<<<<<< HEAD
|
||||
let tcx = infcx.tcx;
|
||||
|
||||
if tcx.features().generic_const_exprs {
|
||||
=======
|
||||
if infcx.tcx.features().generic_const_exprs {
|
||||
let tcx = infcx.tcx;
|
||||
>>>>>>> 6064f16d846 (change thir to use mir::ConstantKind instead of ty::Const)
|
||||
match AbstractConst::new(tcx, uv)? {
|
||||
// We are looking at a generic abstract constant.
|
||||
Some(ct) => {
|
||||
|
@ -244,7 +249,7 @@ impl<'tcx> AbstractConst<'tcx> {
|
|||
Ok(inner.map(|inner| AbstractConst { inner, substs: uv.substs }))
|
||||
}
|
||||
|
||||
pub fn from_const(
|
||||
pub fn from_constant(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: ty::Const<'tcx>,
|
||||
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
|
||||
|
@ -376,6 +381,10 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
|||
visit::walk_pat(self, pat);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_constant(&mut self, ct: mir::ConstantKind<'tcx>) {
|
||||
self.is_poly |= ct.has_param_types_or_consts();
|
||||
}
|
||||
}
|
||||
|
||||
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
||||
|
@ -414,10 +423,10 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
|||
self.recurse_build(self.body_id)?;
|
||||
|
||||
for n in self.nodes.iter() {
|
||||
if let Node::Leaf(ty::Const(Interned(
|
||||
if let Node::Leaf(mir::ConstantKind::Ty(ty::Const(Interned(
|
||||
ty::ConstS { val: ty::ConstKind::Unevaluated(ct), ty: _ },
|
||||
_,
|
||||
))) = n
|
||||
)))) = n
|
||||
{
|
||||
// `AbstractConst`s should not contain any promoteds as they require references which
|
||||
// are not allowed.
|
||||
|
@ -637,6 +646,7 @@ pub(super) fn thir_abstract_const<'tcx>(
|
|||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
pub(super) fn try_unify_abstract_consts<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
(a, b): (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>),
|
||||
|
@ -806,3 +816,51 @@ impl<'tcx> ConstUnifyCtxt<'tcx> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Think I need these changes
|
||||
=======
|
||||
match (a_ct, b_ct) {
|
||||
(mir::ConstantKind::Ty(a_ct), mir::ConstantKind::Ty(b_ct)) => {
|
||||
match (a_ct.val(), b_ct.val()) {
|
||||
// We can just unify errors with everything to reduce the amount of
|
||||
// emitted errors here.
|
||||
(ty::ConstKind::Error(_), _) | (_, ty::ConstKind::Error(_)) => true,
|
||||
(ty::ConstKind::Param(a_param), ty::ConstKind::Param(b_param)) => {
|
||||
a_param == b_param
|
||||
}
|
||||
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
|
||||
a_val == b_val
|
||||
}
|
||||
|
||||
// If we have `fn a<const N: usize>() -> [u8; N + 1]` and `fn b<const M: usize>() -> [u8; 1 + M]`
|
||||
// we do not want to use `assert_eq!(a(), b())` to infer that `N` and `M` have to be `1`. This
|
||||
// means that we only allow inference variables if they are equal.
|
||||
(ty::ConstKind::Infer(a_val), ty::ConstKind::Infer(b_val)) => {
|
||||
a_val == b_val
|
||||
}
|
||||
// We expand generic anonymous constants at the start of this function, so this
|
||||
// branch should only be taking when dealing with associated constants, at
|
||||
// which point directly comparing them seems like the desired behavior.
|
||||
//
|
||||
// FIXME(generic_const_exprs): This isn't actually the case.
|
||||
// We also take this branch for concrete anonymous constants and
|
||||
// expand generic anonymous constants with concrete substs.
|
||||
(ty::ConstKind::Unevaluated(a_uv), ty::ConstKind::Unevaluated(b_uv)) => {
|
||||
a_uv == b_uv
|
||||
}
|
||||
// FIXME(generic_const_exprs): We may want to either actually try
|
||||
// to evaluate `a_ct` and `b_ct` if they are are fully concrete or something like
|
||||
// this, for now we just return false here.
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
(mir::ConstantKind::Val(a_val, a_ty), mir::ConstantKind::Val(b_val, b_ty)) => {
|
||||
a_val == b_val && a_ty == b_ty
|
||||
}
|
||||
_ => {
|
||||
// FIXME Can it happen that we need to compare ConstantKind::Ty(ConstKind::Value)
|
||||
// with a ConstantKind::Val and vice versa?
|
||||
false
|
||||
>>>>>>> 6064f16d846 (change thir to use mir::ConstantKind instead of ty::Const)
|
||||
|
||||
*/
|
|
@ -407,7 +407,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
|
|||
let pred =
|
||||
ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
|
||||
ProcessResult::Changed(mk_pending(vec![
|
||||
obligation.with(pred.to_predicate(self.selcx.tcx())),
|
||||
obligation.with(pred.to_predicate(self.selcx.tcx()))
|
||||
]))
|
||||
}
|
||||
ty::PredicateKind::TypeWellFormedFromEnv(..) => {
|
||||
|
|
|
@ -17,6 +17,7 @@ use crate::traits::{self, Obligation, ObligationCause};
|
|||
use rustc_errors::FatalError;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::mir::ConstantKind;
|
||||
use rustc_middle::ty::subst::{GenericArg, InternalSubsts, Subst};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
|
||||
use rustc_middle::ty::{Predicate, ToPredicate};
|
||||
|
@ -836,7 +837,10 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
|
|||
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, uv.shrink()) {
|
||||
const_evaluatable::walk_abstract_const(self.tcx, ct, |node| {
|
||||
match node.root(self.tcx) {
|
||||
Node::Leaf(leaf) => self.visit_const(leaf),
|
||||
Node::Leaf(leaf) => match leaf {
|
||||
ConstantKind::Ty(c) => self.visit_const(c),
|
||||
ConstantKind::Val(_, ty) => self.visit_ty(ty),
|
||||
},
|
||||
Node::Cast(_, _, ty) => self.visit_ty(ty),
|
||||
Node::Binop(..) | Node::UnaryOp(..) | Node::FunctionCall(_, _) => {
|
||||
ControlFlow::CONTINUE
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue