1
Fork 0

do use ty::Const in patterns and abstract consts

This commit is contained in:
b-naber 2022-03-09 13:56:12 +01:00
parent b38077ea0b
commit ac60db231c
15 changed files with 105 additions and 127 deletions

View file

@ -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: mir::ConstantKind<'tcx>,
value: ty::Const<'tcx>,
},
Range(PatRange<'tcx>),
@ -691,8 +691,8 @@ pub enum PatKind<'tcx> {
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
pub struct PatRange<'tcx> {
pub lo: mir::ConstantKind<'tcx>,
pub hi: mir::ConstantKind<'tcx>,
pub lo: ty::Const<'tcx>,
pub hi: ty::Const<'tcx>,
pub end: RangeEnd,
}
@ -736,7 +736,11 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
Some(adt_def.variant(variant_index))
}
_ => self.ty.ty_adt_def().and_then(|adt| {
if !adt.is_enum() { Some(adt.non_enum_variant()) } else { None }
if !adt.is_enum() {
Some(adt.non_enum_variant())
} else {
None
}
}),
};

View file

@ -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(mir::ConstantKind<'tcx>),
Leaf(ty::Const<'tcx>),
Binop(mir::BinOp, NodeId, NodeId),
UnaryOp(mir::UnOp, NodeId),
FunctionCall(NodeId, &'tcx [NodeId]),

View file

@ -94,13 +94,8 @@ 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 } => {

View file

@ -76,7 +76,11 @@ static_assert_size!(ConstKind<'_>, 40);
impl<'tcx> ConstKind<'tcx> {
#[inline]
pub fn try_to_value(self) -> Option<ConstValue<'tcx>> {
if let ConstKind::Value(val) = self { Some(val) } else { None }
if let ConstKind::Value(val) = self {
Some(val)
} else {
None
}
}
#[inline]
@ -126,6 +130,7 @@ impl<'tcx> ConstKind<'tcx> {
#[inline]
/// Tries to evaluate the constant if it is `Unevaluated`. If that isn't possible or necessary
/// return `None`.
// FIXME(@lcnr): Completely rework the evaluation/normalization system for `ty::Const` once valtrees are merged.
pub fn try_eval(
self,
tcx: TyCtxt<'tcx>,