s/ConstantSource/ConstantKind/
This commit is contained in:
parent
11ddd22510
commit
c30c1be1e6
11 changed files with 43 additions and 51 deletions
|
@ -40,8 +40,8 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
|
||||||
let mut all_constants_ok = true;
|
let mut all_constants_ok = true;
|
||||||
for constant in &fx.mir.required_consts {
|
for constant in &fx.mir.required_consts {
|
||||||
let const_ = match fx.monomorphize(constant.literal) {
|
let const_ = match fx.monomorphize(constant.literal) {
|
||||||
ConstantSource::Ty(ct) => ct,
|
ConstantKind::Ty(ct) => ct,
|
||||||
ConstantSource::Val(..) => continue,
|
ConstantKind::Val(..) => continue,
|
||||||
};
|
};
|
||||||
match const_.val {
|
match const_.val {
|
||||||
ConstKind::Value(_) => {}
|
ConstKind::Value(_) => {}
|
||||||
|
@ -117,8 +117,8 @@ pub(crate) fn codegen_constant<'tcx>(
|
||||||
constant: &Constant<'tcx>,
|
constant: &Constant<'tcx>,
|
||||||
) -> CValue<'tcx> {
|
) -> CValue<'tcx> {
|
||||||
let const_ = match fx.monomorphize(constant.literal) {
|
let const_ = match fx.monomorphize(constant.literal) {
|
||||||
ConstantSource::Ty(ct) => ct,
|
ConstantKind::Ty(ct) => ct,
|
||||||
ConstantSource::Val(val, ty) => return codegen_const_value(fx, val, ty),
|
ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty),
|
||||||
};
|
};
|
||||||
let const_val = match const_.val {
|
let const_val = match const_.val {
|
||||||
ConstKind::Value(const_val) => const_val,
|
ConstKind::Value(const_val) => const_val,
|
||||||
|
@ -427,10 +427,10 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
|
||||||
match operand {
|
match operand {
|
||||||
Operand::Copy(_) | Operand::Move(_) => None,
|
Operand::Copy(_) | Operand::Move(_) => None,
|
||||||
Operand::Constant(const_) => match const_.literal {
|
Operand::Constant(const_) => match const_.literal {
|
||||||
ConstantSource::Ty(const_) => {
|
ConstantKind::Ty(const_) => {
|
||||||
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val.try_to_value()
|
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val.try_to_value()
|
||||||
}
|
}
|
||||||
ConstantSource::Val(val, _) => Some(val),
|
ConstantKind::Val(val, _) => Some(val),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||||
) -> Result<ConstValue<'tcx>, ErrorHandled> {
|
) -> Result<ConstValue<'tcx>, ErrorHandled> {
|
||||||
let ct = self.monomorphize(constant.literal);
|
let ct = self.monomorphize(constant.literal);
|
||||||
let ct = match ct {
|
let ct = match ct {
|
||||||
mir::ConstantSource::Ty(ct) => ct,
|
mir::ConstantKind::Ty(ct) => ct,
|
||||||
mir::ConstantSource::Val(val, _) => return Ok(val),
|
mir::ConstantKind::Val(val, _) => return Ok(val),
|
||||||
};
|
};
|
||||||
match ct.val {
|
match ct.val {
|
||||||
ty::ConstKind::Unevaluated(def, substs, promoted) => self
|
ty::ConstKind::Unevaluated(def, substs, promoted) => self
|
||||||
|
|
|
@ -381,14 +381,6 @@ impl<'tcx, Tag> Scalar<Tag> {
|
||||||
self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
|
self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn to_int(self) -> InterpResult<'tcx, ScalarInt> {
|
|
||||||
match self {
|
|
||||||
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
|
|
||||||
Scalar::Int(int) => Ok(int),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn assert_int(self) -> ScalarInt {
|
pub fn assert_int(self) -> ScalarInt {
|
||||||
self.to_int().expect("expected an int but got an abstract pointer")
|
self.to_int().expect("expected an int but got an abstract pointer")
|
||||||
|
|
|
@ -2033,7 +2033,7 @@ impl<'tcx> Operand<'tcx> {
|
||||||
Operand::Constant(box Constant {
|
Operand::Constant(box Constant {
|
||||||
span,
|
span,
|
||||||
user_ty: None,
|
user_ty: None,
|
||||||
literal: ConstantSource::Ty(ty::Const::zero_sized(tcx, ty)),
|
literal: ConstantKind::Ty(ty::Const::zero_sized(tcx, ty)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2064,7 +2064,7 @@ impl<'tcx> Operand<'tcx> {
|
||||||
Operand::Constant(box Constant {
|
Operand::Constant(box Constant {
|
||||||
span,
|
span,
|
||||||
user_ty: None,
|
user_ty: None,
|
||||||
literal: ConstantSource::Val(val.into(), ty),
|
literal: ConstantKind::Val(val.into(), ty),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2406,11 +2406,11 @@ pub struct Constant<'tcx> {
|
||||||
/// Needed for NLL to impose user-given type constraints.
|
/// Needed for NLL to impose user-given type constraints.
|
||||||
pub user_ty: Option<UserTypeAnnotationIndex>,
|
pub user_ty: Option<UserTypeAnnotationIndex>,
|
||||||
|
|
||||||
pub literal: ConstantSource<'tcx>,
|
pub literal: ConstantKind<'tcx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
|
#[derive(Clone, Copy, PartialEq, PartialOrd, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
|
||||||
pub enum ConstantSource<'tcx> {
|
pub enum ConstantKind<'tcx> {
|
||||||
/// This constant came from the type system
|
/// This constant came from the type system
|
||||||
Ty(&'tcx ty::Const<'tcx>),
|
Ty(&'tcx ty::Const<'tcx>),
|
||||||
/// This constant cannot go back into the type system, as it represents
|
/// This constant cannot go back into the type system, as it represents
|
||||||
|
@ -2436,33 +2436,33 @@ impl Constant<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'tcx ty::Const<'tcx>> for ConstantSource<'tcx> {
|
impl From<&'tcx ty::Const<'tcx>> for ConstantKind<'tcx> {
|
||||||
fn from(ct: &'tcx ty::Const<'tcx>) -> Self {
|
fn from(ct: &'tcx ty::Const<'tcx>) -> Self {
|
||||||
Self::Ty(ct)
|
Self::Ty(ct)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstantSource<'tcx> {
|
impl ConstantKind<'tcx> {
|
||||||
/// Returns `None` if the constant is not trivially safe for use in the type system.
|
/// Returns `None` if the constant is not trivially safe for use in the type system.
|
||||||
pub fn const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>> {
|
pub fn const_for_ty(&self) -> Option<&'tcx ty::Const<'tcx>> {
|
||||||
match self {
|
match self {
|
||||||
ConstantSource::Ty(c) => Some(c),
|
ConstantKind::Ty(c) => Some(c),
|
||||||
ConstantSource::Val(..) => None,
|
ConstantKind::Val(..) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ty(&self) -> Ty<'tcx> {
|
pub fn ty(&self) -> Ty<'tcx> {
|
||||||
match self {
|
match self {
|
||||||
ConstantSource::Ty(c) => c.ty,
|
ConstantKind::Ty(c) => c.ty,
|
||||||
ConstantSource::Val(_, ty) => ty,
|
ConstantKind::Val(_, ty) => ty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
|
pub fn try_to_value(self) -> Option<interpret::ConstValue<'tcx>> {
|
||||||
match self {
|
match self {
|
||||||
ConstantSource::Ty(c) => c.val.try_to_value(),
|
ConstantKind::Ty(c) => c.val.try_to_value(),
|
||||||
ConstantSource::Val(val, _) => Some(val),
|
ConstantKind::Val(val, _) => Some(val),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2709,8 +2709,8 @@ impl<'tcx> Display for Constant<'tcx> {
|
||||||
_ => write!(fmt, "const ")?,
|
_ => write!(fmt, "const ")?,
|
||||||
}
|
}
|
||||||
match self.literal {
|
match self.literal {
|
||||||
ConstantSource::Ty(c) => pretty_print_const(c, fmt, true),
|
ConstantKind::Ty(c) => pretty_print_const(c, fmt, true),
|
||||||
ConstantSource::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
|
ConstantKind::Val(val, ty) => pretty_print_const_value(val, ty, fmt, true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -347,18 +347,18 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> TypeFoldable<'tcx> for ConstantSource<'tcx> {
|
impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> {
|
||||||
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
|
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
|
||||||
match self {
|
match self {
|
||||||
ConstantSource::Ty(c) => ConstantSource::Ty(c.fold_with(folder)),
|
ConstantKind::Ty(c) => ConstantKind::Ty(c.fold_with(folder)),
|
||||||
ConstantSource::Val(v, t) => ConstantSource::Val(v, t.fold_with(folder)),
|
ConstantKind::Val(v, t) => ConstantKind::Val(v, t.fold_with(folder)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||||
match *self {
|
match *self {
|
||||||
ConstantSource::Ty(c) => c.visit_with(visitor),
|
ConstantKind::Ty(c) => c.visit_with(visitor),
|
||||||
ConstantSource::Val(_, t) => t.visit_with(visitor),
|
ConstantKind::Val(_, t) => t.visit_with(visitor),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -872,8 +872,8 @@ macro_rules! make_mir_visitor {
|
||||||
self.visit_span(span);
|
self.visit_span(span);
|
||||||
drop(user_ty); // no visit method for this
|
drop(user_ty); // no visit method for this
|
||||||
match literal {
|
match literal {
|
||||||
ConstantSource::Ty(ct) => self.visit_const(ct, location),
|
ConstantKind::Ty(ct) => self.visit_const(ct, location),
|
||||||
ConstantSource::Val(_, t) => self.visit_ty(t, TyContext::Location(location)),
|
ConstantKind::Val(_, t) => self.visit_ty(t, TyContext::Location(location)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -315,7 +315,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
|
||||||
} else {
|
} else {
|
||||||
let tcx = self.tcx();
|
let tcx = self.tcx();
|
||||||
let maybe_uneval = match constant.literal {
|
let maybe_uneval = match constant.literal {
|
||||||
ConstantSource::Ty(ct) => match ct.val {
|
ConstantKind::Ty(ct) => match ct.val {
|
||||||
ty::ConstKind::Unevaluated(def, substs, promoted) => {
|
ty::ConstKind::Unevaluated(def, substs, promoted) => {
|
||||||
Some((def, substs, promoted))
|
Some((def, substs, promoted))
|
||||||
}
|
}
|
||||||
|
|
|
@ -573,12 +573,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
|
|
||||||
crate fn mir_const_to_op(
|
crate fn mir_const_to_op(
|
||||||
&self,
|
&self,
|
||||||
val: &mir::ConstantSource<'tcx>,
|
val: &mir::ConstantKind<'tcx>,
|
||||||
layout: Option<TyAndLayout<'tcx>>,
|
layout: Option<TyAndLayout<'tcx>>,
|
||||||
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
||||||
match val {
|
match val {
|
||||||
mir::ConstantSource::Ty(ct) => self.const_to_op(ct, layout),
|
mir::ConstantKind::Ty(ct) => self.const_to_op(ct, layout),
|
||||||
mir::ConstantSource::Val(val, ty) => self.const_val_to_op(*val, ty, None),
|
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, ty, None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,9 @@ use rustc_middle::mir::visit::{
|
||||||
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
|
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
|
||||||
};
|
};
|
||||||
use rustc_middle::mir::{
|
use rustc_middle::mir::{
|
||||||
AssertKind, BasicBlock, BinOp, Body, ClearCrossCrate, Constant, ConstantSource, Local,
|
AssertKind, BasicBlock, BinOp, Body, ClearCrossCrate, Constant, ConstantKind, Local, LocalDecl,
|
||||||
LocalDecl, LocalKind, Location, Operand, Place, Rvalue, SourceInfo, SourceScope,
|
LocalKind, Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData,
|
||||||
SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
|
Statement, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::layout::{HasTyCtxt, LayoutError, TyAndLayout};
|
use rustc_middle::ty::layout::{HasTyCtxt, LayoutError, TyAndLayout};
|
||||||
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
||||||
|
@ -489,14 +489,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
let err = ConstEvalErr::new(&self.ecx, error, Some(c.span));
|
let err = ConstEvalErr::new(&self.ecx, error, Some(c.span));
|
||||||
if let Some(lint_root) = self.lint_root(source_info) {
|
if let Some(lint_root) = self.lint_root(source_info) {
|
||||||
let lint_only = match c.literal {
|
let lint_only = match c.literal {
|
||||||
ConstantSource::Ty(ct) => match ct.val {
|
ConstantKind::Ty(ct) => match ct.val {
|
||||||
// Promoteds must lint and not error as the user didn't ask for them
|
// Promoteds must lint and not error as the user didn't ask for them
|
||||||
ConstKind::Unevaluated(_, _, Some(_)) => true,
|
ConstKind::Unevaluated(_, _, Some(_)) => true,
|
||||||
// Out of backwards compatibility we cannot report hard errors in unused
|
// Out of backwards compatibility we cannot report hard errors in unused
|
||||||
// generic functions using associated constants of the generic parameters.
|
// generic functions using associated constants of the generic parameters.
|
||||||
_ => c.literal.needs_subst(),
|
_ => c.literal.needs_subst(),
|
||||||
},
|
},
|
||||||
ConstantSource::Val(_, ty) => ty.needs_subst(),
|
ConstantKind::Val(_, ty) => ty.needs_subst(),
|
||||||
};
|
};
|
||||||
if lint_only {
|
if lint_only {
|
||||||
// Out of backwards compatibility we cannot report hard errors in unused
|
// Out of backwards compatibility we cannot report hard errors in unused
|
||||||
|
@ -818,7 +818,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
) {
|
) {
|
||||||
if let Rvalue::Use(Operand::Constant(c)) = rval {
|
if let Rvalue::Use(Operand::Constant(c)) = rval {
|
||||||
match c.literal {
|
match c.literal {
|
||||||
ConstantSource::Ty(c) if matches!(c.val, ConstKind::Unevaluated(..)) => {}
|
ConstantKind::Ty(c) if matches!(c.val, ConstKind::Unevaluated(..)) => {}
|
||||||
_ => {
|
_ => {
|
||||||
trace!("skipping replace of Rvalue::Use({:?} because it is already a const", c);
|
trace!("skipping replace of Rvalue::Use({:?} because it is already a const", c);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -450,8 +450,8 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
|
||||||
self.push(&format!("+ user_ty: {:?}", user_ty));
|
self.push(&format!("+ user_ty: {:?}", user_ty));
|
||||||
}
|
}
|
||||||
match literal {
|
match literal {
|
||||||
ConstantSource::Ty(literal) => self.push(&format!("+ literal: {:?}", literal)),
|
ConstantKind::Ty(literal) => self.push(&format!("+ literal: {:?}", literal)),
|
||||||
ConstantSource::Val(val, ty) => {
|
ConstantKind::Val(val, ty) => {
|
||||||
self.push(&format!("+ literal: {:?}, {}", val, ty))
|
self.push(&format!("+ literal: {:?}, {}", val, ty))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -378,8 +378,8 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||||
Ok(self.locals[local])
|
Ok(self.locals[local])
|
||||||
}
|
}
|
||||||
mir::Operand::Constant(ct) => match ct.literal {
|
mir::Operand::Constant(ct) => match ct.literal {
|
||||||
mir::ConstantSource::Ty(ct) => Ok(self.add_node(Node::Leaf(ct), span)),
|
mir::ConstantKind::Ty(ct) => Ok(self.add_node(Node::Leaf(ct), span)),
|
||||||
mir::ConstantSource::Val(..) => self.error(Some(span), "unsupported constant")?,
|
mir::ConstantKind::Val(..) => self.error(Some(span), "unsupported constant")?,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue