Skip to content

Commit cea973f

Browse files
committed
try adding a test that LowerHex and friends don't panic, but it doesn't work
1 parent e7fbf72 commit cea973f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

library/core/benches/fmt.rs

+14
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,17 @@ fn write_u64_min(bh: &mut Bencher) {
148148
test::black_box(format!("{}", 0u64));
149149
});
150150
}
151+
152+
#[bench]
153+
fn write_u8_max(bh: &mut Bencher) {
154+
bh.iter(|| {
155+
test::black_box(format!("{}", u8::MAX));
156+
});
157+
}
158+
159+
#[bench]
160+
fn write_u8_min(bh: &mut Bencher) {
161+
bh.iter(|| {
162+
test::black_box(format!("{}", 0u8));
163+
});
164+
}

tests/codegen/fmt_int_no_panic.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Trying to check that formatting u8/u32/u64/etc do not panic.
2+
//
3+
// This test does not correctly do so yet.
4+
5+
//@ compile-flags: -O
6+
7+
#![crate_type = "lib"]
8+
9+
// expected to need to write some kind of `impl core::fmt::Write` on a struct like this to avoid
10+
// unrelated panics if `String::write_str` can't make space..
11+
// struct CanAlwaysBeWrittenTo;
12+
13+
use std::fmt::Write;
14+
15+
// CHECK-LABEL: @format_int_doesnt_panic
16+
#[no_mangle]
17+
pub fn format_int_doesnt_panic(s: &mut String) -> std::fmt::Result {
18+
// CHECK-NOT: panic
19+
// ... but wait! this will definitely panic if `s.vec.reserve_for_push()` cannot alloc! this
20+
// shouldn't pass!
21+
write!(s, "{:x}", 0u8)?;
22+
write!(s, "{:x}", u8::MAX)?;
23+
Ok(())
24+
}

0 commit comments

Comments
 (0)