move Constness into TraitPredicate
This commit is contained in:
parent
04c9901a08
commit
32390a0df6
49 changed files with 157 additions and 124 deletions
|
@ -2169,7 +2169,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
let generic_predicates = self.super_predicates_of(trait_did);
|
||||
|
||||
for (predicate, _) in generic_predicates.predicates {
|
||||
if let ty::PredicateKind::Trait(data, _) = predicate.kind().skip_binder() {
|
||||
if let ty::PredicateKind::Trait(data) = predicate.kind().skip_binder() {
|
||||
if set.insert(data.def_id()) {
|
||||
stack.push(data.def_id());
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ impl<T> ExpectedFound<T> {
|
|||
#[derive(Clone, Debug, TypeFoldable)]
|
||||
pub enum TypeError<'tcx> {
|
||||
Mismatch,
|
||||
ConstnessMismatch(ExpectedFound<hir::Constness>),
|
||||
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
|
||||
AbiMismatch(ExpectedFound<abi::Abi>),
|
||||
Mutability,
|
||||
|
@ -106,6 +107,9 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
|
|||
CyclicTy(_) => write!(f, "cyclic type of infinite size"),
|
||||
CyclicConst(_) => write!(f, "encountered a self-referencing constant"),
|
||||
Mismatch => write!(f, "types differ"),
|
||||
ConstnessMismatch(values) => {
|
||||
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
|
||||
}
|
||||
UnsafetyMismatch(values) => {
|
||||
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
|
||||
}
|
||||
|
@ -213,9 +217,11 @@ impl<'tcx> TypeError<'tcx> {
|
|||
pub fn must_include_note(&self) -> bool {
|
||||
use self::TypeError::*;
|
||||
match self {
|
||||
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | Mismatch | AbiMismatch(_)
|
||||
| FixedArraySize(_) | ArgumentSorts(..) | Sorts(_) | IntMismatch(_)
|
||||
| FloatMismatch(_) | VariadicMismatch(_) | TargetFeatureCast(_) => false,
|
||||
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | ConstnessMismatch(_)
|
||||
| Mismatch | AbiMismatch(_) | FixedArraySize(_) | ArgumentSorts(..) | Sorts(_)
|
||||
| IntMismatch(_) | FloatMismatch(_) | VariadicMismatch(_) | TargetFeatureCast(_) => {
|
||||
false
|
||||
}
|
||||
|
||||
Mutability
|
||||
| ArgumentMutability(_)
|
||||
|
|
|
@ -216,7 +216,7 @@ impl FlagComputation {
|
|||
|
||||
fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) {
|
||||
match atom {
|
||||
ty::PredicateKind::Trait(trait_pred, _constness) => {
|
||||
ty::PredicateKind::Trait(trait_pred) => {
|
||||
self.add_substs(trait_pred.trait_ref.substs);
|
||||
}
|
||||
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
|
||||
|
|
|
@ -456,7 +456,7 @@ pub enum PredicateKind<'tcx> {
|
|||
/// A trait predicate will have `Constness::Const` if it originates
|
||||
/// from a bound on a `const fn` without the `?const` opt-out (e.g.,
|
||||
/// `const fn foobar<Foo: Bar>() {}`).
|
||||
Trait(TraitPredicate<'tcx>, Constness),
|
||||
Trait(TraitPredicate<'tcx>),
|
||||
|
||||
/// `where 'a: 'b`
|
||||
RegionOutlives(RegionOutlivesPredicate<'tcx>),
|
||||
|
@ -612,6 +612,11 @@ impl<'tcx> Predicate<'tcx> {
|
|||
#[derive(HashStable, TypeFoldable)]
|
||||
pub struct TraitPredicate<'tcx> {
|
||||
pub trait_ref: TraitRef<'tcx>,
|
||||
|
||||
/// A trait predicate will have `Constness::Const` if it originates
|
||||
/// from a bound on a `const fn` without the `?const` opt-out (e.g.,
|
||||
/// `const fn foobar<Foo: Bar>() {}`).
|
||||
pub constness: hir::Constness,
|
||||
}
|
||||
|
||||
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
|
||||
|
@ -745,8 +750,11 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> {
|
|||
|
||||
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
|
||||
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
|
||||
PredicateKind::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
|
||||
.to_predicate(tcx)
|
||||
PredicateKind::Trait(ty::TraitPredicate {
|
||||
trait_ref: self.value,
|
||||
constness: self.constness,
|
||||
})
|
||||
.to_predicate(tcx)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -754,15 +762,15 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
|
|||
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
|
||||
self.value
|
||||
.map_bound(|trait_ref| {
|
||||
PredicateKind::Trait(ty::TraitPredicate { trait_ref }, self.constness)
|
||||
PredicateKind::Trait(ty::TraitPredicate { trait_ref, constness: self.constness })
|
||||
})
|
||||
.to_predicate(tcx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
|
||||
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
|
||||
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
|
||||
self.value.map_bound(|value| PredicateKind::Trait(value, self.constness)).to_predicate(tcx)
|
||||
self.map_bound(PredicateKind::Trait).to_predicate(tcx)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -788,8 +796,8 @@ impl<'tcx> Predicate<'tcx> {
|
|||
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
|
||||
let predicate = self.kind();
|
||||
match predicate.skip_binder() {
|
||||
PredicateKind::Trait(t, constness) => {
|
||||
Some(ConstnessAnd { constness, value: predicate.rebind(t.trait_ref) })
|
||||
PredicateKind::Trait(t) => {
|
||||
Some(ConstnessAnd { constness: t.constness, value: predicate.rebind(t.trait_ref) })
|
||||
}
|
||||
PredicateKind::Projection(..)
|
||||
| PredicateKind::Subtype(..)
|
||||
|
|
|
@ -630,7 +630,7 @@ pub trait PrettyPrinter<'tcx>:
|
|||
for (predicate, _) in bounds {
|
||||
let predicate = predicate.subst(self.tcx(), substs);
|
||||
let bound_predicate = predicate.kind();
|
||||
if let ty::PredicateKind::Trait(pred, _) = bound_predicate.skip_binder() {
|
||||
if let ty::PredicateKind::Trait(pred) = bound_predicate.skip_binder() {
|
||||
let trait_ref = bound_predicate.rebind(pred.trait_ref);
|
||||
// Don't print +Sized, but rather +?Sized if absent.
|
||||
if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() {
|
||||
|
@ -2264,10 +2264,7 @@ define_print_and_forward_display! {
|
|||
|
||||
ty::PredicateKind<'tcx> {
|
||||
match *self {
|
||||
ty::PredicateKind::Trait(ref data, constness) => {
|
||||
if let hir::Constness::Const = constness {
|
||||
p!("const ");
|
||||
}
|
||||
ty::PredicateKind::Trait(ref data) => {
|
||||
p!(print(data))
|
||||
}
|
||||
ty::PredicateKind::Subtype(predicate) => p!(print(predicate)),
|
||||
|
|
|
@ -200,6 +200,20 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Relate<'tcx> for ast::Constness {
|
||||
fn relate<R: TypeRelation<'tcx>>(
|
||||
relation: &mut R,
|
||||
a: ast::Constness,
|
||||
b: ast::Constness,
|
||||
) -> RelateResult<'tcx, ast::Constness> {
|
||||
if a != b {
|
||||
Err(TypeError::ConstnessMismatch(expected_found(relation, a, b)))
|
||||
} else {
|
||||
Ok(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Relate<'tcx> for ast::Unsafety {
|
||||
fn relate<R: TypeRelation<'tcx>>(
|
||||
relation: &mut R,
|
||||
|
@ -767,7 +781,10 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
|
|||
a: ty::TraitPredicate<'tcx>,
|
||||
b: ty::TraitPredicate<'tcx>,
|
||||
) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> {
|
||||
Ok(ty::TraitPredicate { trait_ref: relation.relate(a.trait_ref, b.trait_ref)? })
|
||||
Ok(ty::TraitPredicate {
|
||||
trait_ref: relation.relate(a.trait_ref, b.trait_ref)?,
|
||||
constness: relation.relate(a.constness, b.constness)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -155,6 +155,9 @@ impl fmt::Debug for ty::ParamConst {
|
|||
|
||||
impl fmt::Debug for ty::TraitPredicate<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let hir::Constness::Const = self.constness {
|
||||
write!(f, "const ")?;
|
||||
}
|
||||
write!(f, "TraitPredicate({:?})", self.trait_ref)
|
||||
}
|
||||
}
|
||||
|
@ -174,12 +177,7 @@ impl fmt::Debug for ty::Predicate<'tcx> {
|
|||
impl fmt::Debug for ty::PredicateKind<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
ty::PredicateKind::Trait(ref a, constness) => {
|
||||
if let hir::Constness::Const = constness {
|
||||
write!(f, "const ")?;
|
||||
}
|
||||
a.fmt(f)
|
||||
}
|
||||
ty::PredicateKind::Trait(ref a) => a.fmt(f),
|
||||
ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
|
||||
ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
|
||||
ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
|
||||
|
@ -366,7 +364,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
|
|||
impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
|
||||
type Lifted = ty::TraitPredicate<'tcx>;
|
||||
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
|
||||
tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
|
||||
tcx.lift(self.trait_ref)
|
||||
.map(|trait_ref| ty::TraitPredicate { trait_ref, constness: self.constness })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -419,9 +418,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
|
|||
type Lifted = ty::PredicateKind<'tcx>;
|
||||
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
|
||||
match self {
|
||||
ty::PredicateKind::Trait(data, constness) => {
|
||||
tcx.lift(data).map(|data| ty::PredicateKind::Trait(data, constness))
|
||||
}
|
||||
ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
|
||||
ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
|
||||
ty::PredicateKind::RegionOutlives(data) => {
|
||||
tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
|
||||
|
@ -584,6 +581,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
|
|||
|
||||
Some(match self {
|
||||
Mismatch => Mismatch,
|
||||
ConstnessMismatch(x) => ConstnessMismatch(x),
|
||||
UnsafetyMismatch(x) => UnsafetyMismatch(x),
|
||||
AbiMismatch(x) => AbiMismatch(x),
|
||||
Mutability => Mutability,
|
||||
|
|
|
@ -876,7 +876,10 @@ impl<'tcx> PolyTraitRef<'tcx> {
|
|||
}
|
||||
|
||||
pub fn to_poly_trait_predicate(&self) -> ty::PolyTraitPredicate<'tcx> {
|
||||
self.map_bound(|trait_ref| ty::TraitPredicate { trait_ref })
|
||||
self.map_bound(|trait_ref| ty::TraitPredicate {
|
||||
trait_ref,
|
||||
constness: hir::Constness::NotConst,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue