Skip to content

Commit

Permalink
Rollup merge of #124587 - reitermarkus:use-generic-nonzero, r=dtolnay
Browse files Browse the repository at this point in the history
Generic `NonZero` post-stabilization changes.

Tracking issue: rust-lang/rust#120257

r? ``@dtolnay``
  • Loading branch information
matthiaskrgr committed May 8, 2024
2 parents 1d96885 + 254bd48 commit b8806db
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions tests/fail/enum-set-discriminant-niche-variant-wrong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZeroI32;
use std::num::NonZero;

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

#[custom_mir(dialect = "runtime", phase = "optimized")]
fn set_discriminant(ptr: &mut Option<NonZeroI32>) {
fn set_discriminant(ptr: &mut Option<NonZero<i32>>) {
mir! {
{
// We set the discriminant to `Some`, which is a NOP since this is the niched variant.
Expand Down
6 changes: 3 additions & 3 deletions tests/fail/function_pointers/abi_mismatch_repr_C.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::num::*;
use std::num::NonZero;

#[repr(C)]
struct S1(NonZeroI32);
struct S1(NonZero<i32>);

#[repr(C)]
struct S2(i32);
Expand All @@ -11,6 +11,6 @@ fn callee(_s: S2) {}
fn main() {
let fnptr: fn(S2) = callee;
let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
fnptr(S1(NonZeroI32::new(1).unwrap()));
fnptr(S1(NonZero::new(1).unwrap()));
//~^ ERROR: calling a function with argument of type S2 passing data of type S1
}
4 changes: 2 additions & 2 deletions tests/fail/function_pointers/abi_mismatch_repr_C.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with argument of type S2 passing data of type S1
--> $DIR/abi_mismatch_repr_C.rs:LL:CC
|
LL | fnptr(S1(NonZeroI32::new(1).unwrap()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
LL | fnptr(S1(NonZero::new(1).unwrap()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Expand Down
12 changes: 6 additions & 6 deletions tests/fail/validity/cast_fn_ptr_invalid_callee_ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#![feature(core_intrinsics, custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZeroU32;
use std::num::NonZero;
use std::ptr;

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

fn main() {
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZeroU32) };
// There's a NonZeroU32-to-u32 transmute happening here
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZero<u32>) };
// There's a `NonZero<u32>` to `u32` transmute happening here.
f(); //~ERROR: expected something greater or equal to 1
}
14 changes: 7 additions & 7 deletions tests/fail/validity/cast_fn_ptr_invalid_caller_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
#![feature(core_intrinsics, custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZeroU32;
use std::num::NonZero;
use std::ptr;

fn f(c: u32) {
println!("{c}");
}

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

fn main() {
let f: fn(NonZeroU32) = unsafe { std::mem::transmute(f as fn(u32)) };
let f: fn(NonZero<u32>) = unsafe { std::mem::transmute(f as fn(u32)) };
call(f);
}
8 changes: 4 additions & 4 deletions tests/pass/function_calls/abi_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main() {
test_abi_compat(0usize, 0u64);
test_abi_compat(0isize, 0i64);
}
test_abi_compat(42u32, num::NonZeroU32::new(1).unwrap());
test_abi_compat(42u32, num::NonZero::new(1u32).unwrap());
// - `char` and `u32`.
test_abi_compat(42u32, 'x');
// - Reference/pointer types with the same pointee.
Expand All @@ -86,9 +86,9 @@ fn main() {
// - Guaranteed null-pointer-optimizations (RFC 3391).
test_abi_compat(&0u32 as *const u32, Some(&0u32));
test_abi_compat(main as fn(), Some(main as fn()));
test_abi_compat(0u32, Some(num::NonZeroU32::new(1).unwrap()));
test_abi_compat(0u32, Some(num::NonZero::new(1u32).unwrap()));
test_abi_compat(&0u32 as *const u32, Some(Wrapper(&0u32)));
test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1).unwrap())));
test_abi_compat(0u32, Some(Wrapper(num::NonZero::new(1u32).unwrap())));

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

// Extra test for assumptions made by arbitrary-self-dyn-receivers.
// This is interesting since these types are not `repr(transparent)`. So this is not part of our
Expand Down
4 changes: 2 additions & 2 deletions tests/pass/shims/available-parallelism-miri-num-cpus.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@compile-flags: -Zmiri-num-cpus=1024

use std::num::NonZeroUsize;
use std::num::NonZero;
use std::thread::available_parallelism;

fn main() {
assert_eq!(available_parallelism().unwrap(), NonZeroUsize::new(1024).unwrap());
assert_eq!(available_parallelism().unwrap(), NonZero::new(1024).unwrap());
}
4 changes: 2 additions & 2 deletions tests/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ffi::OsString;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::{env, process::Command};
Expand Down Expand Up @@ -76,7 +76,7 @@ fn miri_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) ->
edition: Some("2021".into()), // keep in sync with `./miri run`
threads: std::env::var("MIRI_TEST_THREADS")
.ok()
.map(|threads| NonZeroUsize::new(threads.parse().unwrap()).unwrap()),
.map(|threads| NonZero::new(threads.parse().unwrap()).unwrap()),
..Config::rustc(path)
};

Expand Down

0 comments on commit b8806db

Please sign in to comment.