Merge pull request #708 from tgross35/raskyld/master

fix(int): avoid infinite recursion on left shift #707
This commit is contained in:
Trevor Gross 2024-10-05 22:07:36 -04:00 committed by GitHub
commit a2d66e9869
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View file

@ -222,6 +222,10 @@ impl HInt for u128 {
fn widen_mul(self, rhs: Self) -> Self::D {
self.zero_widen_mul(rhs)
}
fn widen_hi(self) -> Self::D {
self.widen() << <Self as MinInt>::BITS
}
}
impl HInt for i128 {
@ -247,6 +251,10 @@ impl HInt for i128 {
fn widen_mul(self, rhs: Self) -> Self::D {
unimplemented!("signed i128 widening multiply is not used")
}
fn widen_hi(self) -> Self::D {
self.widen() << <Self as MinInt>::BITS
}
}
impl DInt for u256 {

View file

@ -313,15 +313,17 @@ pub(crate) trait HInt: Int {
/// Integer that is double the bit width of the integer this trait is implemented for
type D: DInt<H = Self> + MinInt;
// NB: some of the below methods could have default implementations (e.g. `widen_hi`), but for
// unknown reasons this can cause infinite recursion when optimizations are disabled. See
// <https://github.com/rust-lang/compiler-builtins/pull/707> for context.
/// Widens (using default extension) the integer to have double bit width
fn widen(self) -> Self::D;
/// Widens (zero extension only) the integer to have double bit width. This is needed to get
/// around problems with associated type bounds (such as `Int<Othersign: DInt>`) being unstable
fn zero_widen(self) -> Self::D;
/// Widens the integer to have double bit width and shifts the integer into the higher bits
fn widen_hi(self) -> Self::D {
self.widen() << <Self as MinInt>::BITS
}
fn widen_hi(self) -> Self::D;
/// Widening multiplication with zero widening. This cannot overflow.
fn zero_widen_mul(self, rhs: Self) -> Self::D;
/// Widening multiplication. This cannot overflow.
@ -364,6 +366,9 @@ macro_rules! impl_h_int {
fn widen_mul(self, rhs: Self) -> Self::D {
self.widen().wrapping_mul(rhs.widen())
}
fn widen_hi(self) -> Self::D {
(self as $X) << <Self as MinInt>::BITS
}
}
)*
};