diff --git a/cargo.sh b/cargo.sh index 9a083b9157..e9553baf4a 100755 --- a/cargo.sh +++ b/cargo.sh @@ -6,6 +6,8 @@ # This file may not be copied, modified, or distributed except according to # those terms. +set -eo pipefail + # Build `cargo-zerocopy` without any RUSTFLAGS set in the environment env -u RUSTFLAGS cargo +stable build --manifest-path tools/Cargo.toml -p cargo-zerocopy -q # Thin wrapper around the `cargo-zerocopy` binary in `tools/cargo-zerocopy` diff --git a/src/lib.rs b/src/lib.rs index a1a2865dd6..329f5300b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6084,12 +6084,14 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] fn test_validate_cast_and_convert_metadata() { + #[allow(non_local_definitions)] impl From for SizeInfo { fn from(_size: usize) -> SizeInfo { SizeInfo::Sized { _size } } } + #[allow(non_local_definitions)] impl From<(usize, usize)> for SizeInfo { fn from((_offset, _elem_size): (usize, usize)) -> SizeInfo { SizeInfo::SliceDst(TrailingSliceLayout { _offset, _elem_size }) @@ -8413,7 +8415,7 @@ mod tests { ($ty:ty: TryFromBytes) => { // "Default" implementations that match the "real" // implementations defined in the `autoref_trick` module above. - #[allow(unused)] + #[allow(unused, non_local_definitions)] impl AutorefWrapper<$ty> { #[allow(clippy::needless_lifetimes)] fn test_is_bit_valid_shared<'ptr, A: invariant::at_least::Shared>( diff --git a/src/macros.rs b/src/macros.rs index 1f82770ad8..a325e90869 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -129,6 +129,7 @@ macro_rules! unsafe_impl { => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)? ) => { $(#[$attr])* + #[allow(non_local_definitions)] unsafe impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),* $(, const $constname: $constty,)*> $trait for $ty { unsafe_impl!(@method $trait $(; |$candidate: $(MaybeAligned<$ref_repr>)? $(Maybe<$ptr_repr>)?| $is_bit_valid)?); } @@ -397,6 +398,7 @@ macro_rules! unsafe_impl_known_layout { const _: () = { use core::ptr::NonNull; + #[allow(non_local_definitions)] unsafe impl<$($tyvar: ?Sized + KnownLayout)?> KnownLayout for $ty { #[allow(clippy::missing_inline_in_public_items)] fn only_derive_is_allowed_to_implement_this_trait() {} diff --git a/tests/ui-msrv/transmute-mut-dst-not-intobytes.stderr b/tests/ui-msrv/transmute-mut-dst-not-intobytes.stderr index 50a7afc279..4ceffcd010 100644 --- a/tests/ui-msrv/transmute-mut-dst-not-intobytes.stderr +++ b/tests/ui-msrv/transmute-mut-dst-not-intobytes.stderr @@ -1,8 +1,8 @@ -error[E0277]: the trait bound `Dst: AsBytes` is not satisfied +error[E0277]: the trait bound `Dst: IntoBytes` is not satisfied --> tests/ui-msrv/transmute-mut-dst-not-intobytes.rs:24:36 | 24 | const DST_NOT_AS_BYTES: &mut Dst = transmute_mut!(&mut Src); - | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `Dst` + | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `Dst` | note: required by `AssertDstIsIntoBytes` --> tests/ui-msrv/transmute-mut-dst-not-intobytes.rs:24:36 diff --git a/tests/ui-msrv/transmute-mut-src-not-intobytes.stderr b/tests/ui-msrv/transmute-mut-src-not-intobytes.stderr index 36be71417b..7522b32a69 100644 --- a/tests/ui-msrv/transmute-mut-src-not-intobytes.stderr +++ b/tests/ui-msrv/transmute-mut-src-not-intobytes.stderr @@ -1,8 +1,8 @@ -error[E0277]: the trait bound `Src: AsBytes` is not satisfied +error[E0277]: the trait bound `Src: IntoBytes` is not satisfied --> tests/ui-msrv/transmute-mut-src-not-intobytes.rs:24:36 | 24 | const SRC_NOT_AS_BYTES: &mut Dst = transmute_mut!(&mut Src); - | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `Src` + | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `Src` | note: required by `AssertSrcIsIntoBytes` --> tests/ui-msrv/transmute-mut-src-not-intobytes.rs:24:36 @@ -11,11 +11,11 @@ note: required by `AssertSrcIsIntoBytes` | ^^^^^^^^^^^^^^^^^^^^^^^^ = note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `Src: AsBytes` is not satisfied +error[E0277]: the trait bound `Src: IntoBytes` is not satisfied --> tests/ui-msrv/transmute-mut-src-not-intobytes.rs:24:36 | 24 | const SRC_NOT_AS_BYTES: &mut Dst = transmute_mut!(&mut Src); - | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `Src` + | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `Src` | note: required by a bound in `AssertSrcIsIntoBytes` --> tests/ui-msrv/transmute-mut-src-not-intobytes.rs:24:36 diff --git a/tests/ui-msrv/transmute-ptr-to-usize.stderr b/tests/ui-msrv/transmute-ptr-to-usize.stderr index 81d60c71a1..fab734f39f 100644 --- a/tests/ui-msrv/transmute-ptr-to-usize.stderr +++ b/tests/ui-msrv/transmute-ptr-to-usize.stderr @@ -1,8 +1,8 @@ -error[E0277]: the trait bound `*const usize: IntoBytes` is not satisfied +error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied --> tests/ui-msrv/transmute-ptr-to-usize.rs:20:30 | 20 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `*const usize` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `*const usize` | note: required by `AssertIsIntoBytes` --> tests/ui-msrv/transmute-ptr-to-usize.rs:20:30 @@ -11,11 +11,11 @@ note: required by `AssertIsIntoBytes` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this error originates in the macro `transmute` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `*const usize: IntoBytes` is not satisfied +error[E0277]: the trait bound `*const usize: AsBytes` is not satisfied --> tests/ui-msrv/transmute-ptr-to-usize.rs:20:30 | 20 | const POINTER_VALUE: usize = transmute!(&0usize as *const usize); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `*const usize` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `*const usize` | note: required by a bound in `AssertIsIntoBytes` --> tests/ui-msrv/transmute-ptr-to-usize.rs:20:30 diff --git a/tools/cargo-zerocopy/src/main.rs b/tools/cargo-zerocopy/src/main.rs index 703b706521..a2386eaa31 100644 --- a/tools/cargo-zerocopy/src/main.rs +++ b/tools/cargo-zerocopy/src/main.rs @@ -27,7 +27,7 @@ use std::{ env, fmt, io::{self, Read as _}, - process::{self, Command}, + process::{self, Command, Output}, }; use serde_json::{Map, Value}; @@ -53,6 +53,45 @@ impl fmt::Display for Error { impl std::error::Error for Error {} +trait CommandExt { + fn output_or_exit(&mut self) -> Output; + fn execute(&mut self); +} + +impl CommandExt for Command { + fn output_or_exit(&mut self) -> Output { + if let Ok(output) = self.output() { + if !output.status.success() { + eprintln!( + "[cargo-zerocopy] failed while capturing output from command: {:?}", + self + ); + let stdout = std::str::from_utf8(&output.stdout).unwrap(); + let stderr = std::str::from_utf8(&output.stderr).unwrap(); + eprintln!("[cargo-zerocopy] stdout: {stdout}"); + eprintln!("[cargo-zerocopy] stderr: {stderr}"); + process::exit(output.status.code().unwrap_or(1)); + } + output + } else { + eprintln!("[cargo-zerocopy] failed to run command: {:?}", self); + process::exit(1); + } + } + + fn execute(&mut self) { + if let Ok(status) = self.status() { + if !status.success() { + eprintln!("[cargo-zerocopy] failed while executing command: {:?}", self); + process::exit(status.code().unwrap_or(1)); + } + } else { + eprintln!("[cargo-zerocopy] failed to run command: {:?}", self); + process::exit(1); + } + } +} + struct Versions { msrv: String, stable: String, @@ -97,8 +136,7 @@ fn get_toolchain_versions() -> Versions { Some(("CARGO_TARGET_DIR", "target/cargo-zerocopy")), ) .env_remove("RUSTFLAGS") - .output() - .unwrap(); + .output_or_exit(); let json = serde_json::from_slice::(&output.stdout).unwrap(); let packages = json.as_object().unwrap().get("packages").unwrap(); @@ -129,7 +167,7 @@ fn is_toolchain_installed(versions: &Versions, name: &str) -> Result Result<(), Erro } let version = versions.get(name)?; - rustup(["toolchain", "install", &version, "-c", "rust-src"], None).status().unwrap(); + rustup(["toolchain", "install", &version, "-c", "rust-src"], None).execute(); Ok(()) } @@ -203,8 +241,7 @@ fn delegate_cargo() -> Result<(), Error> { Command::new(this.clone()) .arg(format!("+{toolchain}")) .args(args.clone()) - .status() - .unwrap(); + .execute(); } Ok(()) } @@ -224,8 +261,7 @@ fn delegate_cargo() -> Result<(), Error> { let rustflags = format!("{}{}", get_rustflags(name), env_rustflags); rustup(["run", &version, "cargo"], Some(("RUSTFLAGS", &rustflags))) .args(args) - .status() - .unwrap(); + .execute(); Ok(()) } else { diff --git a/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr b/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr index 00eb8c1425..05378d1662 100644 --- a/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr +++ b/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr @@ -16,13 +16,13 @@ note: required by a bound in `_::{closure#0}::_::{closure#0}::assert_impl_all` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `_::{closure#0}::_::{closure#0}::assert_impl_all` = note: this error originates in the macro `::static_assertions::assert_impl_all` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `NotZerocopy: FromZeroes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: FromZeros` is not satisfied --> tests/ui-msrv/derive_transparent.rs:35:1 | 35 | util_assert_impl_all!(TransparentStruct: FromZeros); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromZeroes` is not implemented for `NotZerocopy` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromZeros` is not implemented for `NotZerocopy` | -note: required because of the requirements on the impl of `FromZeroes` for `TransparentStruct` +note: required because of the requirements on the impl of `FromZeros` for `TransparentStruct` --> tests/ui-msrv/derive_transparent.rs:24:21 | 24 | #[derive(IntoBytes, FromBytes, Unaligned)] diff --git a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr index d262c34480..814f48d5d5 100644 --- a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr @@ -24,11 +24,11 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied = help: see issue #48214 = note: this error originates in the derive macro `FromZeros` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `NotZerocopy: FromZeroes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: FromZeros` is not satisfied --> tests/ui-msrv/late_compile_pass.rs:37:10 | 37 | #[derive(FromZeros)] - | ^^^^^^^^^ the trait `FromZeroes` is not implemented for `NotZerocopy` + | ^^^^^^^^^ the trait `FromZeros` is not implemented for `NotZerocopy` | = help: see issue #48214 = note: this error originates in the derive macro `FromZeros` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -42,11 +42,11 @@ error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied = help: see issue #48214 = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `NotZerocopy: FromZeroes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: FromZeros` is not satisfied --> tests/ui-msrv/late_compile_pass.rs:46:10 | 46 | #[derive(FromBytes)] - | ^^^^^^^^^ the trait `FromZeroes` is not implemented for `NotZerocopy` + | ^^^^^^^^^ the trait `FromZeros` is not implemented for `NotZerocopy` | = help: see issue #48214 = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)