Skip to content

Commit

Permalink
[cargo-zerocopy] Pass failure return codes through (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski authored Feb 27, 2024
1 parent d56206e commit 978b400
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 27 deletions.
2 changes: 2 additions & 0 deletions cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6084,12 +6084,14 @@ mod tests {
#[test]
#[cfg_attr(miri, ignore)]
fn test_validate_cast_and_convert_metadata() {
#[allow(non_local_definitions)]
impl From<usize> 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 })
Expand Down Expand Up @@ -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>(
Expand Down
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
}
Expand Down Expand Up @@ -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() {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-msrv/transmute-mut-dst-not-intobytes.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/ui-msrv/transmute-mut-src-not-intobytes.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/ui-msrv/transmute-ptr-to-usize.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
54 changes: 45 additions & 9 deletions tools/cargo-zerocopy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use std::{
env, fmt,
io::{self, Read as _},
process::{self, Command},
process::{self, Command, Output},
};

use serde_json::{Map, Value};
Expand All @@ -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,
Expand Down Expand Up @@ -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::<Value>(&output.stdout).unwrap();
let packages = json.as_object().unwrap().get("packages").unwrap();
Expand Down Expand Up @@ -129,7 +167,7 @@ fn is_toolchain_installed(versions: &Versions, name: &str) -> Result<bool, Error
let version = versions.get(name)?;
let output = rustup(["run", version, "cargo", "version"], None).output().unwrap();
if output.status.success() {
let output = rustup([&format!("+{version}"), "component", "list"], None).output().unwrap();
let output = rustup([&format!("+{version}"), "component", "list"], None).output_or_exit();
let stdout = String::from_utf8(output.stdout).unwrap();
Ok(stdout.contains("rust-src (installed)"))
} else {
Expand Down Expand Up @@ -157,7 +195,7 @@ fn install_toolchain_or_exit(versions: &Versions, name: &str) -> 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(())
}
Expand Down Expand Up @@ -203,8 +241,7 @@ fn delegate_cargo() -> Result<(), Error> {
Command::new(this.clone())
.arg(format!("+{toolchain}"))
.args(args.clone())
.status()
.unwrap();
.execute();
}
Ok(())
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions zerocopy-derive/tests/ui-msrv/derive_transparent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotZerocopy>: 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<NotZerocopy>`
note: required because of the requirements on the impl of `FromZeros` for `TransparentStruct<NotZerocopy>`
--> tests/ui-msrv/derive_transparent.rs:24:21
|
24 | #[derive(IntoBytes, FromBytes, Unaligned)]
Expand Down
8 changes: 4 additions & 4 deletions zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 978b400

Please sign in to comment.