-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #123175 - Nilstrieb:debug-strict-overflow, r=wesleywiser
Add add/sub methods that only panic with debug assertions to rustc This mitigates the perf impact of enabling overflow checks on rustc. The change to use overflow checks will be done in a later PR. For rust-lang/compiler-team#724, based on data gathered in #119440.
- Loading branch information
Showing
6 changed files
with
109 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// This would belong to `rustc_data_structures`, but `rustc_serialize` needs it too. | ||
|
||
/// Addition, but only overflow checked when `cfg(debug_assertions)` is set | ||
/// instead of respecting `-Coverflow-checks`. | ||
/// | ||
/// This exists for performance reasons, as we ship rustc with overflow checks. | ||
/// While overflow checks are perf neutral in almost all of the compiler, there | ||
/// are a few particularly hot areas where we don't want overflow checks in our | ||
/// dist builds. Overflow is still a bug there, so we want overflow check for | ||
/// builds with debug assertions. | ||
/// | ||
/// That's a long way to say that this should be used in areas where overflow | ||
/// is a bug but overflow checking is too slow. | ||
pub trait DebugStrictAdd { | ||
/// See [`DebugStrictAdd`]. | ||
fn debug_strict_add(self, other: Self) -> Self; | ||
} | ||
|
||
macro_rules! impl_debug_strict_add { | ||
($( $ty:ty )*) => { | ||
$( | ||
impl DebugStrictAdd for $ty { | ||
fn debug_strict_add(self, other: Self) -> Self { | ||
if cfg!(debug_assertions) { | ||
self + other | ||
} else { | ||
self.wrapping_add(other) | ||
} | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
/// See [`DebugStrictAdd`]. | ||
pub trait DebugStrictSub { | ||
/// See [`DebugStrictAdd`]. | ||
fn debug_strict_sub(self, other: Self) -> Self; | ||
} | ||
|
||
macro_rules! impl_debug_strict_sub { | ||
($( $ty:ty )*) => { | ||
$( | ||
impl DebugStrictSub for $ty { | ||
fn debug_strict_sub(self, other: Self) -> Self { | ||
if cfg!(debug_assertions) { | ||
self - other | ||
} else { | ||
self.wrapping_sub(other) | ||
} | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
impl_debug_strict_add! { | ||
u8 u16 u32 u64 u128 usize | ||
i8 i16 i32 i64 i128 isize | ||
} | ||
|
||
impl_debug_strict_sub! { | ||
u8 u16 u32 u64 u128 usize | ||
i8 i16 i32 i64 i128 isize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters