Remove extra_asserts and extra_debug_asserts feature flags (#4478)

Removes `egui_assert` etc and replaces it with normal `debug_assert`
calls.

Previously you could opt-in to more runtime checks using feature flags.
Now these extra runtime checks are always enabled for debug builds.

You are most likely to encounter them if you use negative sizes or NaNs
or other similar bugs.
These usually indicate bugs in user space.
This commit is contained in:
Emil Ernerfeldt
2024-05-10 19:39:08 +02:00
committed by GitHub
parent 155e138998
commit f19f99180e
32 changed files with 90 additions and 182 deletions
-5
View File
@@ -52,11 +52,6 @@ deadlock_detection = ["epaint/deadlock_detection"]
## If you plan on specifying your own fonts you may disable this feature.
default_fonts = ["epaint/default_fonts"]
## Enable additional checks if debug assertions are enabled (debug builds).
extra_debug_asserts = ["epaint/extra_debug_asserts"]
## Always enable additional checks.
extra_asserts = ["epaint/extra_asserts"]
## Turn on the `log` feature, that makes egui log some errors using the [`log`](https://docs.rs/log) crate.
log = ["dep:log", "epaint/log"]
+1 -1
View File
@@ -1748,7 +1748,7 @@ impl Context {
let name = name.into();
let image = image.into();
let max_texture_side = self.input(|i| i.max_texture_side);
crate::egui_assert!(
debug_assert!(
image.width() <= max_texture_side && image.height() <= max_texture_side,
"Texture {:?} has size {}x{}, but the maximum texture side is {}",
name,
+5 -5
View File
@@ -117,7 +117,7 @@ impl FrameState {
/// This is the "background" area, what egui doesn't cover with panels (but may cover with windows).
/// This is also the area to which windows are constrained.
pub(crate) fn available_rect(&self) -> Rect {
crate::egui_assert!(
debug_assert!(
self.available_rect.is_finite(),
"Called `available_rect()` before `Context::run()`"
);
@@ -126,7 +126,7 @@ impl FrameState {
/// Shrink `available_rect`.
pub(crate) fn allocate_left_panel(&mut self, panel_rect: Rect) {
crate::egui_assert!(
debug_assert!(
panel_rect.min.distance(self.available_rect.min) < 0.1,
"Mismatching left panel. You must not create a panel from within another panel."
);
@@ -137,7 +137,7 @@ impl FrameState {
/// Shrink `available_rect`.
pub(crate) fn allocate_right_panel(&mut self, panel_rect: Rect) {
crate::egui_assert!(
debug_assert!(
panel_rect.max.distance(self.available_rect.max) < 0.1,
"Mismatching right panel. You must not create a panel from within another panel."
);
@@ -148,7 +148,7 @@ impl FrameState {
/// Shrink `available_rect`.
pub(crate) fn allocate_top_panel(&mut self, panel_rect: Rect) {
crate::egui_assert!(
debug_assert!(
panel_rect.min.distance(self.available_rect.min) < 0.1,
"Mismatching top panel. You must not create a panel from within another panel."
);
@@ -159,7 +159,7 @@ impl FrameState {
/// Shrink `available_rect`.
pub(crate) fn allocate_bottom_panel(&mut self, panel_rect: Rect) {
crate::egui_assert!(
debug_assert!(
panel_rect.max.distance(self.available_rect.max) < 0.1,
"Mismatching bottom panel. You must not create a panel from within another panel."
);
+1 -1
View File
@@ -85,7 +85,7 @@ impl GridLayout {
// TODO(emilk): respect current layout
let initial_available = ui.placer().max_rect().intersect(ui.cursor());
crate::egui_assert!(
debug_assert!(
initial_available.min.x.is_finite(),
"Grid not yet available for right-to-left layouts"
);
+23 -23
View File
@@ -1,4 +1,4 @@
use crate::{egui_assert, emath::*, Align};
use crate::{emath::*, Align};
use std::f32::INFINITY;
// ----------------------------------------------------------------------------
@@ -66,9 +66,9 @@ impl Region {
}
pub fn sanity_check(&self) {
egui_assert!(!self.min_rect.any_nan());
egui_assert!(!self.max_rect.any_nan());
egui_assert!(!self.cursor.any_nan());
debug_assert!(!self.min_rect.any_nan());
debug_assert!(!self.max_rect.any_nan());
debug_assert!(!self.cursor.any_nan());
}
}
@@ -389,8 +389,8 @@ impl Layout {
/// ## Doing layout
impl Layout {
pub fn align_size_within_rect(&self, size: Vec2, outer: Rect) -> Rect {
egui_assert!(size.x >= 0.0 && size.y >= 0.0);
egui_assert!(!outer.is_negative());
debug_assert!(size.x >= 0.0 && size.y >= 0.0);
debug_assert!(!outer.is_negative());
self.align2().align_size_within_rect(size, outer)
}
@@ -416,8 +416,8 @@ impl Layout {
}
pub(crate) fn region_from_max_rect(&self, max_rect: Rect) -> Region {
egui_assert!(!max_rect.any_nan());
egui_assert!(max_rect.is_finite());
debug_assert!(!max_rect.any_nan());
debug_assert!(max_rect.is_finite());
let mut region = Region {
min_rect: Rect::NOTHING, // temporary
max_rect,
@@ -450,9 +450,9 @@ impl Layout {
/// Given the cursor in the region, how much space is available
/// for the next widget?
fn available_from_cursor_max_rect(&self, cursor: Rect, max_rect: Rect) -> Rect {
egui_assert!(!cursor.any_nan());
egui_assert!(!max_rect.any_nan());
egui_assert!(max_rect.is_finite());
debug_assert!(!cursor.any_nan());
debug_assert!(!max_rect.any_nan());
debug_assert!(max_rect.is_finite());
// NOTE: in normal top-down layout the cursor has moved below the current max_rect,
// but the available shouldn't be negative.
@@ -506,7 +506,7 @@ impl Layout {
avail.max.y = y;
}
egui_assert!(!avail.any_nan());
debug_assert!(!avail.any_nan());
avail
}
@@ -517,7 +517,7 @@ impl Layout {
/// Use `justify_and_align` to get the inner `widget_rect`.
pub(crate) fn next_frame(&self, region: &Region, child_size: Vec2, spacing: Vec2) -> Rect {
region.sanity_check();
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
if self.main_wrap {
let available_size = self.available_rect_before_wrap(region).size();
@@ -597,7 +597,7 @@ impl Layout {
fn next_frame_ignore_wrap(&self, region: &Region, child_size: Vec2) -> Rect {
region.sanity_check();
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
let available_rect = self.available_rect_before_wrap(region);
@@ -630,16 +630,16 @@ impl Layout {
frame_rect = frame_rect.translate(Vec2::Y * (region.cursor.top() - frame_rect.top()));
}
egui_assert!(!frame_rect.any_nan());
egui_assert!(!frame_rect.is_negative());
debug_assert!(!frame_rect.any_nan());
debug_assert!(!frame_rect.is_negative());
frame_rect
}
/// Apply justify (fill width/height) and/or alignment after calling `next_space`.
pub(crate) fn justify_and_align(&self, frame: Rect, mut child_size: Vec2) -> Rect {
egui_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
egui_assert!(!frame.is_negative());
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(!frame.is_negative());
if self.horizontal_justify() {
child_size.x = child_size.x.at_least(frame.width()); // fill full width
@@ -657,10 +657,10 @@ impl Layout {
) -> Rect {
let frame = self.next_frame_ignore_wrap(region, size);
let rect = self.align_size_within_rect(size, frame);
egui_assert!(!rect.any_nan());
egui_assert!(!rect.is_negative());
egui_assert!((rect.width() - size.x).abs() < 1.0 || size.x == f32::INFINITY);
egui_assert!((rect.height() - size.y).abs() < 1.0 || size.y == f32::INFINITY);
debug_assert!(!rect.any_nan());
debug_assert!(!rect.is_negative());
debug_assert!((rect.width() - size.x).abs() < 1.0 || size.x == f32::INFINITY);
debug_assert!((rect.height() - size.y).abs() < 1.0 || size.y == f32::INFINITY);
rect
}
@@ -703,7 +703,7 @@ impl Layout {
widget_rect: Rect,
item_spacing: Vec2,
) {
egui_assert!(!cursor.any_nan());
debug_assert!(!cursor.any_nan());
if self.main_wrap {
if cursor.intersects(frame_rect.shrink(1.0)) {
// make row/column larger if necessary
-16
View File
@@ -549,22 +549,6 @@ macro_rules! github_link_file {
// ----------------------------------------------------------------------------
/// An assert that is only active when `egui` is compiled with the `extra_asserts` feature
/// or with the `extra_debug_asserts` feature in debug builds.
#[macro_export]
macro_rules! egui_assert {
($($arg: tt)*) => {
if cfg!(any(
feature = "extra_asserts",
all(feature = "extra_debug_asserts", debug_assertions),
)) {
assert!($($arg)*);
}
}
}
// ----------------------------------------------------------------------------
/// The minus character: <https://www.compart.com/en/unicode/U+2212>
pub(crate) const MINUS_CHAR_STR: &str = "−";
+6 -6
View File
@@ -106,7 +106,7 @@ impl Placer {
/// This is what you then pass to `advance_after_rects`.
/// Use `justify_and_align` to get the inner `widget_rect`.
pub(crate) fn next_space(&self, child_size: Vec2, item_spacing: Vec2) -> Rect {
egui_assert!(child_size.is_finite() && child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(child_size.is_finite() && child_size.x >= 0.0 && child_size.y >= 0.0);
self.region.sanity_check();
if let Some(grid) = &self.grid {
grid.next_cell(self.region.cursor, child_size)
@@ -127,8 +127,8 @@ impl Placer {
/// Apply justify or alignment after calling `next_space`.
pub(crate) fn justify_and_align(&self, rect: Rect, child_size: Vec2) -> Rect {
crate::egui_assert!(!rect.any_nan());
crate::egui_assert!(!child_size.any_nan());
debug_assert!(!rect.any_nan());
debug_assert!(!child_size.any_nan());
if let Some(grid) = &self.grid {
grid.justify_and_align(rect, child_size)
@@ -140,7 +140,7 @@ impl Placer {
/// Advance the cursor by this many points.
/// [`Self::min_rect`] will expand to contain the cursor.
pub(crate) fn advance_cursor(&mut self, amount: f32) {
crate::egui_assert!(
debug_assert!(
self.grid.is_none(),
"You cannot advance the cursor when in a grid layout"
);
@@ -158,8 +158,8 @@ impl Placer {
widget_rect: Rect,
item_spacing: Vec2,
) {
egui_assert!(!frame_rect.any_nan());
egui_assert!(!widget_rect.any_nan());
debug_assert!(!frame_rect.any_nan());
debug_assert!(!widget_rect.any_nan());
self.region.sanity_check();
if let Some(grid) = &mut self.grid {
+1 -1
View File
@@ -936,7 +936,7 @@ impl Response {
/// You may not call [`Self::interact`] on the resulting `Response`.
pub fn union(&self, other: Self) -> Self {
assert!(self.ctx == other.ctx);
crate::egui_assert!(
debug_assert!(
self.layer_id == other.layer_id,
"It makes no sense to combine Responses from two different layers"
);
+7 -7
View File
@@ -111,7 +111,7 @@ impl Ui {
layout: Layout,
id_source: impl Hash,
) -> Self {
crate::egui_assert!(!max_rect.any_nan());
debug_assert!(!max_rect.any_nan());
let next_auto_id_source = Id::new(self.next_auto_id_source).with("child").value();
self.next_auto_id_source = self.next_auto_id_source.wrapping_add(1);
let child_ui = Ui {
@@ -530,14 +530,14 @@ impl Ui {
/// Set the minimum width of the ui.
/// This can't shrink the ui, only make it larger.
pub fn set_min_width(&mut self, width: f32) {
egui_assert!(0.0 <= width);
debug_assert!(0.0 <= width);
self.placer.set_min_width(width);
}
/// Set the minimum height of the ui.
/// This can't shrink the ui, only make it larger.
pub fn set_min_height(&mut self, height: f32) {
egui_assert!(0.0 <= height);
debug_assert!(0.0 <= height);
self.placer.set_min_height(height);
}
@@ -847,7 +847,7 @@ impl Ui {
fn allocate_space_impl(&mut self, desired_size: Vec2) -> Rect {
let item_spacing = self.spacing().item_spacing;
let frame_rect = self.placer.next_space(desired_size, item_spacing);
egui_assert!(!frame_rect.any_nan());
debug_assert!(!frame_rect.any_nan());
let widget_rect = self.placer.justify_and_align(frame_rect, desired_size);
self.placer
@@ -870,7 +870,7 @@ impl Ui {
/// Allocate a rect without interacting with it.
pub fn advance_cursor_after_rect(&mut self, rect: Rect) -> Id {
egui_assert!(!rect.any_nan());
debug_assert!(!rect.any_nan());
let item_spacing = self.spacing().item_spacing;
self.placer.advance_after_rects(rect, rect, item_spacing);
@@ -939,7 +939,7 @@ impl Ui {
layout: Layout,
add_contents: Box<dyn FnOnce(&mut Self) -> R + 'c>,
) -> InnerResponse<R> {
crate::egui_assert!(desired_size.x >= 0.0 && desired_size.y >= 0.0);
debug_assert!(desired_size.x >= 0.0 && desired_size.y >= 0.0);
let item_spacing = self.spacing().item_spacing;
let frame_rect = self.placer.next_space(desired_size, item_spacing);
let child_rect = self.placer.justify_and_align(frame_rect, desired_size);
@@ -964,7 +964,7 @@ impl Ui {
max_rect: Rect,
add_contents: impl FnOnce(&mut Self) -> R,
) -> InnerResponse<R> {
egui_assert!(max_rect.is_finite());
debug_assert!(max_rect.is_finite());
let mut child_ui = self.child_ui(max_rect, *self.layout());
let ret = add_contents(&mut child_ui);
let final_child_rect = child_ui.min_rect();
+1 -1
View File
@@ -139,7 +139,7 @@ impl WidgetRects {
// e.g. calling `response.interact(…)` to add more interaction.
let (idx_in_layer, existing) = entry.get_mut();
egui_assert!(
debug_assert!(
existing.layer_id == widget_rect.layer_id,
"Widget changed layer_id during the frame"
);
+1 -1
View File
@@ -746,7 +746,7 @@ pub fn paint_texture_at(
Some((rot, origin)) => {
// TODO(emilk): implement this using `PathShape` (add texture support to it).
// This will also give us anti-aliasing of rotated images.
egui_assert!(
debug_assert!(
options.rounding == Rounding::ZERO,
"Image had both rounding and rotation. Please pick only one"
);
+1 -1
View File
@@ -170,7 +170,7 @@ impl Label {
let cursor = ui.cursor();
let first_row_indentation = available_width - ui.available_size_before_wrap().x;
egui_assert!(first_row_indentation.is_finite());
debug_assert!(first_row_indentation.is_finite());
layout_job.wrap.max_width = available_width;
layout_job.first_row_min_height = cursor.height();
+3 -3
View File
@@ -983,7 +983,7 @@ fn value_from_normalized(normalized: f64, range: RangeInclusive<f64>, spec: &Sli
}
}
} else {
crate::egui_assert!(
debug_assert!(
min.is_finite() && max.is_finite(),
"You should use a logarithmic range"
);
@@ -1032,7 +1032,7 @@ fn normalized_from_value(value: f64, range: RangeInclusive<f64>, spec: &SliderSp
}
}
} else {
crate::egui_assert!(
debug_assert!(
min.is_finite() && max.is_finite(),
"You should use a logarithmic range"
);
@@ -1080,6 +1080,6 @@ fn logarithmic_zero_cutoff(min: f64, max: f64) -> f64 {
};
let cutoff = min_magnitude / (min_magnitude + max_magnitude);
crate::egui_assert!(0.0 <= cutoff && cutoff <= 1.0);
debug_assert!(0.0 <= cutoff && cutoff <= 1.0);
cutoff
}