Skip to content

Commit

Permalink
Rollup merge of #103402 - joshtriplett:niche-wrap-fix, r=oli-obk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
notriddle authored Oct 23, 2022
2 parents 646e0d3 + 36662df commit 9f06fbd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2526,7 +2526,10 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
// return `Bound::Excluded`. (And we have tests checking that we
// handle the attribute correctly.)
// We don't add a span since users cannot declare such types anyway.
(Bound::Included(lo), _) if lo > 0 => {
(Bound::Included(lo), Bound::Included(hi)) if 0 < lo && lo < hi => {
return Some((format!("`{}` must be non-null", ty), None));
}
(Bound::Included(lo), Bound::Unbounded) if 0 < lo => {
return Some((format!("`{}` must be non-null", ty), None));
}
(Bound::Included(_), _) | (_, Bound::Included(_))
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/lint/invalid_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ enum TwoUninhabited {
B(Void),
}

#[rustc_layout_scalar_valid_range_start(254)]
#[rustc_layout_scalar_valid_range_end(1)]
pub(crate) struct WrapAroundRange(u8);

#[allow(unused)]
fn generic<T: 'static>() {
unsafe {
Expand Down Expand Up @@ -131,6 +135,9 @@ fn main() {
let _val: *const [()] = mem::zeroed();
let _val: *const [()] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized

let _val: WrapAroundRange = mem::zeroed();
let _val: WrapAroundRange = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized

// Things where 0 is okay due to rustc implementation details,
// but that are not guaranteed to keep working.
let _val: Result<i32, i32> = mem::zeroed();
Expand Down
Loading

0 comments on commit 9f06fbd

Please sign in to comment.