|
| 1 | +// Regression test for #52873. We used to ICE due to unexpected |
| 2 | +// overflows when checking for "blanket impl inclusion". |
| 3 | + |
| 4 | +use std::marker::PhantomData; |
| 5 | +use std::cmp::Ordering; |
| 6 | +use std::ops::{Add, Mul}; |
| 7 | + |
| 8 | +pub type True = B1; |
| 9 | +pub type False = B0; |
| 10 | +pub type U0 = UTerm; |
| 11 | +pub type U1 = UInt<UTerm, B1>; |
| 12 | + |
| 13 | +pub trait NonZero {} |
| 14 | + |
| 15 | +pub trait Bit { |
| 16 | +} |
| 17 | + |
| 18 | +pub trait Unsigned { |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)] |
| 22 | +pub struct B0; |
| 23 | + |
| 24 | +impl B0 { |
| 25 | + #[inline] |
| 26 | + pub fn new() -> B0 { |
| 27 | + B0 |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)] |
| 32 | +pub struct B1; |
| 33 | + |
| 34 | +impl B1 { |
| 35 | + #[inline] |
| 36 | + pub fn new() -> B1 { |
| 37 | + B1 |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Bit for B0 { |
| 42 | +} |
| 43 | + |
| 44 | +impl Bit for B1 { |
| 45 | +} |
| 46 | + |
| 47 | +impl NonZero for B1 {} |
| 48 | + |
| 49 | +pub trait PrivatePow<Y, N> { |
| 50 | + type Output; |
| 51 | +} |
| 52 | +pub type PrivatePowOut<A, Y, N> = <A as PrivatePow<Y, N>>::Output; |
| 53 | + |
| 54 | +pub type Add1<A> = <A as Add<::B1>>::Output; |
| 55 | +pub type Prod<A, B> = <A as Mul<B>>::Output; |
| 56 | +pub type Square<A> = <A as Mul>::Output; |
| 57 | +pub type Sum<A, B> = <A as Add<B>>::Output; |
| 58 | + |
| 59 | +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)] |
| 60 | +pub struct UTerm; |
| 61 | + |
| 62 | +impl UTerm { |
| 63 | + #[inline] |
| 64 | + pub fn new() -> UTerm { |
| 65 | + UTerm |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Unsigned for UTerm { |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)] |
| 73 | +pub struct UInt<U, B> { |
| 74 | + _marker: PhantomData<(U, B)>, |
| 75 | +} |
| 76 | + |
| 77 | +impl<U: Unsigned, B: Bit> UInt<U, B> { |
| 78 | + #[inline] |
| 79 | + pub fn new() -> UInt<U, B> { |
| 80 | + UInt { |
| 81 | + _marker: PhantomData, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<U: Unsigned, B: Bit> Unsigned for UInt<U, B> { |
| 87 | +} |
| 88 | + |
| 89 | +impl<U: Unsigned, B: Bit> NonZero for UInt<U, B> {} |
| 90 | + |
| 91 | +impl Add<B0> for UTerm { |
| 92 | + type Output = UTerm; |
| 93 | + fn add(self, _: B0) -> Self::Output { |
| 94 | + UTerm |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<U: Unsigned, B: Bit> Add<B0> for UInt<U, B> { |
| 99 | + type Output = UInt<U, B>; |
| 100 | + fn add(self, _: B0) -> Self::Output { |
| 101 | + UInt::new() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl<U: Unsigned> Add<U> for UTerm { |
| 106 | + type Output = U; |
| 107 | + fn add(self, _: U) -> Self::Output { |
| 108 | + unsafe { ::std::mem::uninitialized() } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<U: Unsigned, B: Bit> Mul<B0> for UInt<U, B> { |
| 113 | + type Output = UTerm; |
| 114 | + fn mul(self, _: B0) -> Self::Output { |
| 115 | + UTerm |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<U: Unsigned, B: Bit> Mul<B1> for UInt<U, B> { |
| 120 | + type Output = UInt<U, B>; |
| 121 | + fn mul(self, _: B1) -> Self::Output { |
| 122 | + UInt::new() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<U: Unsigned> Mul<U> for UTerm { |
| 127 | + type Output = UTerm; |
| 128 | + fn mul(self, _: U) -> Self::Output { |
| 129 | + UTerm |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<Ul: Unsigned, B: Bit, Ur: Unsigned> Mul<UInt<Ur, B>> for UInt<Ul, B0> |
| 134 | +where |
| 135 | + Ul: Mul<UInt<Ur, B>>, |
| 136 | +{ |
| 137 | + type Output = UInt<Prod<Ul, UInt<Ur, B>>, B0>; |
| 138 | + fn mul(self, _: UInt<Ur, B>) -> Self::Output { |
| 139 | + unsafe { ::std::mem::uninitialized() } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +pub trait Pow<Exp> { |
| 144 | + type Output; |
| 145 | +} |
| 146 | + |
| 147 | +impl<X: Unsigned, N: Unsigned> Pow<N> for X |
| 148 | +where |
| 149 | + X: PrivatePow<U1, N>, |
| 150 | +{ |
| 151 | + type Output = PrivatePowOut<X, U1, N>; |
| 152 | +} |
| 153 | + |
| 154 | +impl<Y: Unsigned, X: Unsigned> PrivatePow<Y, U0> for X { |
| 155 | + type Output = Y; |
| 156 | +} |
| 157 | + |
| 158 | +impl<Y: Unsigned, X: Unsigned> PrivatePow<Y, U1> for X |
| 159 | +where |
| 160 | + X: Mul<Y>, |
| 161 | +{ |
| 162 | + type Output = Prod<X, Y>; |
| 163 | +} |
| 164 | + |
| 165 | +impl<Y: Unsigned, U: Unsigned, B: Bit, X: Unsigned> PrivatePow<Y, UInt<UInt<U, B>, B0>> for X |
| 166 | +where |
| 167 | + X: Mul, |
| 168 | + Square<X>: PrivatePow<Y, UInt<U, B>>, |
| 169 | +{ |
| 170 | + type Output = PrivatePowOut<Square<X>, Y, UInt<U, B>>; |
| 171 | +} |
0 commit comments