Skip to content

Commit b8806db

Browse files
authored
Rollup merge of #124587 - reitermarkus:use-generic-nonzero, r=dtolnay
Generic `NonZero` post-stabilization changes. Tracking issue: rust-lang/rust#120257 r? ``@dtolnay``
2 parents 1d96885 + 254bd48 commit b8806db

8 files changed

+28
-28
lines changed

tests/fail/enum-set-discriminant-niche-variant-wrong.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroI32;
5+
use std::num::NonZero;
66

77
// We define our own option type so that we can control the variant indices.
88
#[allow(unused)]
@@ -13,7 +13,7 @@ enum Option<T> {
1313
use Option::*;
1414

1515
#[custom_mir(dialect = "runtime", phase = "optimized")]
16-
fn set_discriminant(ptr: &mut Option<NonZeroI32>) {
16+
fn set_discriminant(ptr: &mut Option<NonZero<i32>>) {
1717
mir! {
1818
{
1919
// We set the discriminant to `Some`, which is a NOP since this is the niched variant.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::num::*;
1+
use std::num::NonZero;
22

33
#[repr(C)]
4-
struct S1(NonZeroI32);
4+
struct S1(NonZero<i32>);
55

66
#[repr(C)]
77
struct S2(i32);
@@ -11,6 +11,6 @@ fn callee(_s: S2) {}
1111
fn main() {
1212
let fnptr: fn(S2) = callee;
1313
let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
14-
fnptr(S1(NonZeroI32::new(1).unwrap()));
14+
fnptr(S1(NonZero::new(1).unwrap()));
1515
//~^ ERROR: calling a function with argument of type S2 passing data of type S1
1616
}

tests/fail/function_pointers/abi_mismatch_repr_C.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: calling a function with argument of type S2 passing data of type S1
22
--> $DIR/abi_mismatch_repr_C.rs:LL:CC
33
|
4-
LL | fnptr(S1(NonZeroI32::new(1).unwrap()));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
4+
LL | fnptr(S1(NonZero::new(1).unwrap()));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/validity/cast_fn_ptr_invalid_callee_ret.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#![feature(core_intrinsics, custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroU32;
5+
use std::num::NonZero;
66
use std::ptr;
77

8-
// This function supposedly returns a NonZeroU32, but actually returns something invalid in a way that
9-
// never materializes a bad NonZeroU32 value: we take a pointer to the return place and cast the pointer
8+
// This function supposedly returns a `NonZero<u32>`, but actually returns something invalid in a way that
9+
// never materializes a bad `NonZero<u32>` value: we take a pointer to the return place and cast the pointer
1010
// type. That way we never get an "invalid value constructed" error inside the function, it can
1111
// only possibly be detected when the return value is passed to the caller.
1212
#[custom_mir(dialect = "runtime", phase = "optimized")]
13-
fn f() -> NonZeroU32 {
13+
fn f() -> NonZero<u32> {
1414
mir! {
1515
{
1616
let tmp = ptr::addr_of_mut!(RET);
@@ -22,7 +22,7 @@ fn f() -> NonZeroU32 {
2222
}
2323

2424
fn main() {
25-
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZeroU32) };
26-
// There's a NonZeroU32-to-u32 transmute happening here
25+
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZero<u32>) };
26+
// There's a `NonZero<u32>` to `u32` transmute happening here.
2727
f(); //~ERROR: expected something greater or equal to 1
2828
}

tests/fail/validity/cast_fn_ptr_invalid_caller_arg.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
#![feature(core_intrinsics, custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroU32;
5+
use std::num::NonZero;
66
use std::ptr;
77

88
fn f(c: u32) {
99
println!("{c}");
1010
}
1111

12-
// Call that function in a bad way, with an invalid NonZeroU32, but without
13-
// ever materializing this as a NonZeroU32 value outside the call itself.
12+
// Call that function in a bad way, with an invalid `NonZero<u32>`, but without
13+
// ever materializing this as a `NonZero<u32>` value outside the call itself.
1414
#[custom_mir(dialect = "runtime", phase = "optimized")]
15-
fn call(f: fn(NonZeroU32)) {
15+
fn call(f: fn(NonZero<u32>)) {
1616
mir! {
1717
let _res: ();
1818
{
1919
let c = 0;
2020
let tmp = ptr::addr_of!(c);
21-
let ptr = tmp as *const NonZeroU32;
22-
// The call site now is a NonZeroU32-to-u32 transmute.
21+
let ptr = tmp as *const NonZero<u32>;
22+
// The call site now is a `NonZero<u32>` to `u32` transmute.
2323
Call(_res = f(*ptr), ReturnTo(retblock), UnwindContinue()) //~ERROR: expected something greater or equal to 1
2424
}
2525
retblock = {
@@ -29,6 +29,6 @@ fn call(f: fn(NonZeroU32)) {
2929
}
3030

3131
fn main() {
32-
let f: fn(NonZeroU32) = unsafe { std::mem::transmute(f as fn(u32)) };
32+
let f: fn(NonZero<u32>) = unsafe { std::mem::transmute(f as fn(u32)) };
3333
call(f);
3434
}

tests/pass/function_calls/abi_compat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn main() {
7070
test_abi_compat(0usize, 0u64);
7171
test_abi_compat(0isize, 0i64);
7272
}
73-
test_abi_compat(42u32, num::NonZeroU32::new(1).unwrap());
73+
test_abi_compat(42u32, num::NonZero::new(1u32).unwrap());
7474
// - `char` and `u32`.
7575
test_abi_compat(42u32, 'x');
7676
// - Reference/pointer types with the same pointee.
@@ -86,9 +86,9 @@ fn main() {
8686
// - Guaranteed null-pointer-optimizations (RFC 3391).
8787
test_abi_compat(&0u32 as *const u32, Some(&0u32));
8888
test_abi_compat(main as fn(), Some(main as fn()));
89-
test_abi_compat(0u32, Some(num::NonZeroU32::new(1).unwrap()));
89+
test_abi_compat(0u32, Some(num::NonZero::new(1u32).unwrap()));
9090
test_abi_compat(&0u32 as *const u32, Some(Wrapper(&0u32)));
91-
test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1).unwrap())));
91+
test_abi_compat(0u32, Some(Wrapper(num::NonZero::new(1u32).unwrap())));
9292

9393
// These must work for *any* type, since we guarantee that `repr(transparent)` is ABI-compatible
9494
// with the wrapped field.
@@ -102,7 +102,7 @@ fn main() {
102102
test_abi_newtype::<[u32; 2]>();
103103
test_abi_newtype::<[u32; 32]>();
104104
test_abi_newtype::<Option<i32>>();
105-
test_abi_newtype::<Option<num::NonZeroU32>>();
105+
test_abi_newtype::<Option<num::NonZero<u32>>>();
106106

107107
// Extra test for assumptions made by arbitrary-self-dyn-receivers.
108108
// This is interesting since these types are not `repr(transparent)`. So this is not part of our
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-num-cpus=1024
22

3-
use std::num::NonZeroUsize;
3+
use std::num::NonZero;
44
use std::thread::available_parallelism;
55

66
fn main() {
7-
assert_eq!(available_parallelism().unwrap(), NonZeroUsize::new(1024).unwrap());
7+
assert_eq!(available_parallelism().unwrap(), NonZero::new(1024).unwrap());
88
}

tests/ui.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::ffi::OsString;
2-
use std::num::NonZeroUsize;
2+
use std::num::NonZero;
33
use std::path::{Path, PathBuf};
44
use std::sync::OnceLock;
55
use std::{env, process::Command};
@@ -76,7 +76,7 @@ fn miri_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) ->
7676
edition: Some("2021".into()), // keep in sync with `./miri run`
7777
threads: std::env::var("MIRI_TEST_THREADS")
7878
.ok()
79-
.map(|threads| NonZeroUsize::new(threads.parse().unwrap()).unwrap()),
79+
.map(|threads| NonZero::new(threads.parse().unwrap()).unwrap()),
8080
..Config::rustc(path)
8181
};
8282

0 commit comments

Comments
 (0)