Skip to content

Commit 9f06fbd

Browse files
authored
Rollup merge of #103402 - joshtriplett:niche-wrap-fix, r=oli-obk
Fix wrapped valid-range handling in ty_find_init_error Rust's niche handling allows for wrapping valid ranges with end < start; for instance, a valid range with start=43 and end=41 means a niche of 42. Most places in the compiler handle this correctly, but `ty_find_init_error` assumed that `lo > 0` means the type cannot contain a zero. Fix it to handle wrapping ranges.
2 parents 646e0d3 + 36662df commit 9f06fbd

File tree

3 files changed

+73
-52
lines changed

3 files changed

+73
-52
lines changed

compiler/rustc_lint/src/builtin.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,10 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
25262526
// return `Bound::Excluded`. (And we have tests checking that we
25272527
// handle the attribute correctly.)
25282528
// We don't add a span since users cannot declare such types anyway.
2529-
(Bound::Included(lo), _) if lo > 0 => {
2529+
(Bound::Included(lo), Bound::Included(hi)) if 0 < lo && lo < hi => {
2530+
return Some((format!("`{}` must be non-null", ty), None));
2531+
}
2532+
(Bound::Included(lo), Bound::Unbounded) if 0 < lo => {
25302533
return Some((format!("`{}` must be non-null", ty), None));
25312534
}
25322535
(Bound::Included(_), _) | (_, Bound::Included(_))

src/test/ui/lint/invalid_value.rs

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ enum TwoUninhabited {
4444
B(Void),
4545
}
4646

47+
#[rustc_layout_scalar_valid_range_start(254)]
48+
#[rustc_layout_scalar_valid_range_end(1)]
49+
pub(crate) struct WrapAroundRange(u8);
50+
4751
#[allow(unused)]
4852
fn generic<T: 'static>() {
4953
unsafe {
@@ -131,6 +135,9 @@ fn main() {
131135
let _val: *const [()] = mem::zeroed();
132136
let _val: *const [()] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
133137

138+
let _val: WrapAroundRange = mem::zeroed();
139+
let _val: WrapAroundRange = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
140+
134141
// Things where 0 is okay due to rustc implementation details,
135142
// but that are not guaranteed to keep working.
136143
let _val: Result<i32, i32> = mem::zeroed();

0 commit comments

Comments
 (0)