Skip to content

Commit 58b2ac8

Browse files
bircnilucasmerlin
andauthored
Add assert messages and print bad argument values in asserts (#5216)
Enabled the `missing_assert_message` lint * [x] I have followed the instructions in the PR template --------- Co-authored-by: Lucas Meurer <[email protected]>
1 parent 903bd81 commit 58b2ac8

35 files changed

+331
-108
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ match_wild_err_arm = "warn"
207207
match_wildcard_for_single_variants = "warn"
208208
mem_forget = "warn"
209209
mismatching_type_param_order = "warn"
210+
missing_assert_message = "warn"
210211
missing_enforced_import_renames = "warn"
211212
missing_errors_doc = "warn"
212213
missing_safety_doc = "warn"
@@ -274,7 +275,6 @@ zero_sized_map_values = "warn"
274275

275276
# TODO(emilk): maybe enable more of these lints?
276277
iter_over_hash_type = "allow"
277-
missing_assert_message = "allow"
278278
should_panic_without_expect = "allow"
279279
too_many_lines = "allow"
280280
unwrap_used = "allow" # TODO(emilk): We really wanna warn on this one

crates/ecolor/src/color32.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ impl Color32 {
273273
/// This is perceptually even, and faster that [`Self::linear_multiply`].
274274
#[inline]
275275
pub fn gamma_multiply(self, factor: f32) -> Self {
276-
debug_assert!(0.0 <= factor && factor.is_finite());
276+
debug_assert!(
277+
0.0 <= factor && factor.is_finite(),
278+
"factor should be finite, but was {factor}"
279+
);
277280
let Self([r, g, b, a]) = self;
278281
Self([
279282
(r as f32 * factor + 0.5) as u8,
@@ -306,7 +309,10 @@ impl Color32 {
306309
/// You likely want to use [`Self::gamma_multiply`] instead.
307310
#[inline]
308311
pub fn linear_multiply(self, factor: f32) -> Self {
309-
debug_assert!(0.0 <= factor && factor.is_finite());
312+
debug_assert!(
313+
0.0 <= factor && factor.is_finite(),
314+
"factor should be finite, but was {factor}"
315+
);
310316
// As an unfortunate side-effect of using premultiplied alpha
311317
// we need a somewhat expensive conversion to linear space and back.
312318
Rgba::from(self).multiply(factor).into()

crates/ecolor/src/rgba.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,24 @@ impl Rgba {
9090

9191
#[inline]
9292
pub fn from_luminance_alpha(l: f32, a: f32) -> Self {
93-
debug_assert!(0.0 <= l && l <= 1.0);
94-
debug_assert!(0.0 <= a && a <= 1.0);
93+
debug_assert!(
94+
0.0 <= l && l <= 1.0,
95+
"l should be in the range [0, 1], but was {l}"
96+
);
97+
debug_assert!(
98+
0.0 <= a && a <= 1.0,
99+
"a should be in the range [0, 1], but was {a}"
100+
);
95101
Self([l * a, l * a, l * a, a])
96102
}
97103

98104
/// Transparent black
99105
#[inline]
100106
pub fn from_black_alpha(a: f32) -> Self {
101-
debug_assert!(0.0 <= a && a <= 1.0);
107+
debug_assert!(
108+
0.0 <= a && a <= 1.0,
109+
"a should be in the range [0, 1], but was {a}"
110+
);
102111
Self([0.0, 0.0, 0.0, a])
103112
}
104113

crates/eframe/src/stopwatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Stopwatch {
1818
}
1919

2020
pub fn start(&mut self) {
21-
assert!(self.start.is_none());
21+
assert!(self.start.is_none(), "Stopwatch already running");
2222
self.start = Some(Instant::now());
2323
}
2424

@@ -29,7 +29,7 @@ impl Stopwatch {
2929
}
3030

3131
pub fn resume(&mut self) {
32-
assert!(self.start.is_none());
32+
assert!(self.start.is_none(), "Stopwatch still running");
3333
self.start = Some(Instant::now());
3434
}
3535

crates/egui/src/containers/menu.rs

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ impl MenuState {
159159
}
160160

161161
/// Horizontal menu bar where you can add [`MenuButton`]s.
162-
163162
/// The menu bar goes well in a [`crate::TopBottomPanel::top`],
164163
/// but can also be placed in a [`crate::Window`].
165164
/// In the latter case you may want to wrap it in [`Frame`].

crates/egui/src/context.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ impl Default for WrappedTextureManager {
8282
epaint::FontImage::new([0, 0]).into(),
8383
Default::default(),
8484
);
85-
assert_eq!(font_id, TextureId::default());
85+
assert_eq!(
86+
font_id,
87+
TextureId::default(),
88+
"font id should be equal to TextureId::default(), but was {font_id:?}",
89+
);
8690

8791
Self(Arc::new(RwLock::new(tex_mngr)))
8892
}
@@ -804,7 +808,11 @@ impl Context {
804808
let max_passes = self.write(|ctx| ctx.memory.options.max_passes.get());
805809

806810
let mut output = FullOutput::default();
807-
debug_assert_eq!(output.platform_output.num_completed_passes, 0);
811+
debug_assert_eq!(
812+
output.platform_output.num_completed_passes, 0,
813+
"output must be fresh, but had {} passes",
814+
output.platform_output.num_completed_passes
815+
);
808816

809817
loop {
810818
profiling::scope!(
@@ -828,7 +836,11 @@ impl Context {
828836
self.begin_pass(new_input.take());
829837
run_ui(self);
830838
output.append(self.end_pass());
831-
debug_assert!(0 < output.platform_output.num_completed_passes);
839+
debug_assert!(
840+
0 < output.platform_output.num_completed_passes,
841+
"Completed passes was lower than 0, was {}",
842+
output.platform_output.num_completed_passes
843+
);
832844

833845
if !output.platform_output.requested_discard() {
834846
break; // no need for another pass
@@ -3272,7 +3284,11 @@ impl Context {
32723284
#[cfg(feature = "accesskit")]
32733285
self.pass_state_mut(|fs| {
32743286
if let Some(state) = fs.accesskit_state.as_mut() {
3275-
assert_eq!(state.parent_stack.pop(), Some(_id));
3287+
assert_eq!(
3288+
state.parent_stack.pop(),
3289+
Some(_id),
3290+
"Mismatched push/pop in with_accessibility_parent"
3291+
);
32763292
}
32773293
});
32783294

crates/egui/src/hit_test.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,17 @@ pub fn hit_test(
175175
restore_widget_rect(wr);
176176
}
177177
if let Some(wr) = &mut hits.drag {
178-
debug_assert!(wr.sense.senses_drag());
178+
debug_assert!(
179+
wr.sense.senses_drag(),
180+
"We should only return drag hits if they sense drag"
181+
);
179182
restore_widget_rect(wr);
180183
}
181184
if let Some(wr) = &mut hits.click {
182-
debug_assert!(wr.sense.senses_click());
185+
debug_assert!(
186+
wr.sense.senses_click(),
187+
"We should only return click hits if they sense click"
188+
);
183189
restore_widget_rect(wr);
184190
}
185191
}

crates/egui/src/layout.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ impl Region {
7171
}
7272

7373
pub fn sanity_check(&self) {
74-
debug_assert!(!self.min_rect.any_nan());
75-
debug_assert!(!self.max_rect.any_nan());
76-
debug_assert!(!self.cursor.any_nan());
74+
debug_assert!(
75+
!self.min_rect.any_nan(),
76+
"min rect has Nan: {:?}",
77+
self.min_rect
78+
);
79+
debug_assert!(
80+
!self.max_rect.any_nan(),
81+
"max rect has Nan: {:?}",
82+
self.max_rect
83+
);
84+
debug_assert!(!self.cursor.any_nan(), "cursor has Nan: {:?}", self.cursor);
7785
}
7886
}
7987

@@ -394,8 +402,8 @@ impl Layout {
394402
/// ## Doing layout
395403
impl Layout {
396404
pub fn align_size_within_rect(&self, size: Vec2, outer: Rect) -> Rect {
397-
debug_assert!(size.x >= 0.0 && size.y >= 0.0);
398-
debug_assert!(!outer.is_negative());
405+
debug_assert!(size.x >= 0.0 && size.y >= 0.0, "Negative size: {size:?}");
406+
debug_assert!(!outer.is_negative(), "Negative outer: {outer:?}");
399407
self.align2().align_size_within_rect(size, outer).round_ui()
400408
}
401409

@@ -421,7 +429,7 @@ impl Layout {
421429
}
422430

423431
pub(crate) fn region_from_max_rect(&self, max_rect: Rect) -> Region {
424-
debug_assert!(!max_rect.any_nan());
432+
debug_assert!(!max_rect.any_nan(), "max_rect is not NaN: {max_rect:?}");
425433
let mut region = Region {
426434
min_rect: Rect::NOTHING, // temporary
427435
max_rect,
@@ -454,8 +462,8 @@ impl Layout {
454462
/// Given the cursor in the region, how much space is available
455463
/// for the next widget?
456464
fn available_from_cursor_max_rect(&self, cursor: Rect, max_rect: Rect) -> Rect {
457-
debug_assert!(!cursor.any_nan());
458-
debug_assert!(!max_rect.any_nan());
465+
debug_assert!(!cursor.any_nan(), "cursor is NaN: {cursor:?}");
466+
debug_assert!(!max_rect.any_nan(), "max_rect is NaN: {max_rect:?}");
459467

460468
// NOTE: in normal top-down layout the cursor has moved below the current max_rect,
461469
// but the available shouldn't be negative.
@@ -509,7 +517,7 @@ impl Layout {
509517
avail.max.y = y;
510518
}
511519

512-
debug_assert!(!avail.any_nan());
520+
debug_assert!(!avail.any_nan(), "avail is NaN: {avail:?}");
513521

514522
avail
515523
}
@@ -520,7 +528,10 @@ impl Layout {
520528
/// Use `justify_and_align` to get the inner `widget_rect`.
521529
pub(crate) fn next_frame(&self, region: &Region, child_size: Vec2, spacing: Vec2) -> Rect {
522530
region.sanity_check();
523-
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
531+
debug_assert!(
532+
child_size.x >= 0.0 && child_size.y >= 0.0,
533+
"Negative size: {child_size:?}"
534+
);
524535

525536
if self.main_wrap {
526537
let available_size = self.available_rect_before_wrap(region).size();
@@ -600,7 +611,10 @@ impl Layout {
600611

601612
fn next_frame_ignore_wrap(&self, region: &Region, child_size: Vec2) -> Rect {
602613
region.sanity_check();
603-
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
614+
debug_assert!(
615+
child_size.x >= 0.0 && child_size.y >= 0.0,
616+
"Negative size: {child_size:?}"
617+
);
604618

605619
let available_rect = self.available_rect_before_wrap(region);
606620

@@ -633,16 +647,19 @@ impl Layout {
633647
frame_rect = frame_rect.translate(Vec2::Y * (region.cursor.top() - frame_rect.top()));
634648
}
635649

636-
debug_assert!(!frame_rect.any_nan());
637-
debug_assert!(!frame_rect.is_negative());
650+
debug_assert!(!frame_rect.any_nan(), "frame_rect is NaN: {frame_rect:?}");
651+
debug_assert!(!frame_rect.is_negative(), "frame_rect is negative");
638652

639653
frame_rect.round_ui()
640654
}
641655

642656
/// Apply justify (fill width/height) and/or alignment after calling `next_space`.
643657
pub(crate) fn justify_and_align(&self, frame: Rect, mut child_size: Vec2) -> Rect {
644-
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
645-
debug_assert!(!frame.is_negative());
658+
debug_assert!(
659+
child_size.x >= 0.0 && child_size.y >= 0.0,
660+
"Negative size: {child_size:?}"
661+
);
662+
debug_assert!(!frame.is_negative(), "frame is negative");
646663

647664
if self.horizontal_justify() {
648665
child_size.x = child_size.x.at_least(frame.width()); // fill full width
@@ -660,8 +677,8 @@ impl Layout {
660677
) -> Rect {
661678
let frame = self.next_frame_ignore_wrap(region, size);
662679
let rect = self.align_size_within_rect(size, frame);
663-
debug_assert!(!rect.any_nan());
664-
debug_assert!(!rect.is_negative());
680+
debug_assert!(!rect.any_nan(), "rect is NaN: {rect:?}");
681+
debug_assert!(!rect.is_negative(), "rect is negative: {rect:?}");
665682
rect
666683
}
667684

@@ -704,7 +721,7 @@ impl Layout {
704721
widget_rect: Rect,
705722
item_spacing: Vec2,
706723
) {
707-
debug_assert!(!cursor.any_nan());
724+
debug_assert!(!cursor.any_nan(), "cursor is NaN: {cursor:?}");
708725
if self.main_wrap {
709726
if cursor.intersects(frame_rect.shrink(1.0)) {
710727
// make row/column larger if necessary

crates/egui/src/placer.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ impl Placer {
133133

134134
/// Apply justify or alignment after calling `next_space`.
135135
pub(crate) fn justify_and_align(&self, rect: Rect, child_size: Vec2) -> Rect {
136-
debug_assert!(!rect.any_nan());
137-
debug_assert!(!child_size.any_nan());
136+
debug_assert!(!rect.any_nan(), "rect: {rect:?}");
137+
debug_assert!(!child_size.any_nan(), "child_size is NaN: {child_size:?}");
138138

139139
if let Some(grid) = &self.grid {
140140
grid.justify_and_align(rect, child_size)
@@ -164,8 +164,11 @@ impl Placer {
164164
widget_rect: Rect,
165165
item_spacing: Vec2,
166166
) {
167-
debug_assert!(!frame_rect.any_nan());
168-
debug_assert!(!widget_rect.any_nan());
167+
debug_assert!(!frame_rect.any_nan(), "frame_rect: {frame_rect:?}");
168+
debug_assert!(
169+
!widget_rect.any_nan(),
170+
"widget_rect is NaN: {widget_rect:?}"
171+
);
169172
self.region.sanity_check();
170173

171174
if let Some(grid) = &mut self.grid {

crates/egui/src/response.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,10 @@ impl Response {
985985
///
986986
/// You may not call [`Self::interact`] on the resulting `Response`.
987987
pub fn union(&self, other: Self) -> Self {
988-
assert!(self.ctx == other.ctx);
988+
assert!(
989+
self.ctx == other.ctx,
990+
"Responses must be from the same `Context`"
991+
);
989992
debug_assert!(
990993
self.layer_id == other.layer_id,
991994
"It makes no sense to combine Responses from two different layers"

crates/egui/src/text_selection/text_cursor_state.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ pub fn byte_index_from_char_index(s: &str, char_index: usize) -> usize {
271271
}
272272

273273
pub fn slice_char_range(s: &str, char_range: std::ops::Range<usize>) -> &str {
274-
assert!(char_range.start <= char_range.end);
274+
assert!(
275+
char_range.start <= char_range.end,
276+
"Invalid range, start must be less than end, but start = {}, end = {}",
277+
char_range.start,
278+
char_range.end
279+
);
275280
let start_byte = byte_index_from_char_index(s, char_range.start);
276281
let end_byte = byte_index_from_char_index(s, char_range.end);
277282
&s[start_byte..end_byte]

crates/egui/src/text_selection/visuals.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ pub fn paint_text_selection(
5959
// Start by appending the selection rectangle to end of the mesh, as two triangles (= 6 indices):
6060
let num_indices_before = mesh.indices.len();
6161
mesh.add_colored_rect(rect, color);
62-
assert_eq!(num_indices_before + 6, mesh.indices.len());
62+
assert_eq!(
63+
num_indices_before + 6,
64+
mesh.indices.len(),
65+
"We expect exactly 6 new indices"
66+
);
6367

6468
// Copy out the new triangles:
6569
let selection_triangles = [

0 commit comments

Comments
 (0)