Skip to content

Commit e5c4b11

Browse files
committed
[test] Move impl_or_verify! tests to doctests
They require a lot of fragile machinery as UI tests, and are much simpler as doc tests. gherrit-pr-id: G793f61a136b912fbd5ee4315cdea7cf3cf3cf8fb
1 parent 5ed532f commit e5c4b11

File tree

9 files changed

+22
-504
lines changed

9 files changed

+22
-504
lines changed

src/doctests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,20 @@ enum TransmuteRefMutDstOffsetNotMultiple {}
149149
/// let _: &mut SliceDst<(), [u8; 2]> = zerocopy::transmute_mut!(src);
150150
/// ```
151151
enum TransmuteRefMutDstElemSizeNotMultiple {}
152+
153+
/// ```compile_fail,E0277
154+
/// use zerocopy::*;
155+
///
156+
/// #[derive(FromBytes, IntoBytes, Unaligned)]
157+
/// #[repr(transparent)]
158+
/// struct Foo<T>(T);
159+
///
160+
/// const _: () = unsafe {
161+
/// impl_or_verify!(T => TryFromBytes for Foo<T>);
162+
/// impl_or_verify!(T => FromZeros for Foo<T>);
163+
/// impl_or_verify!(T => FromBytes for Foo<T>);
164+
/// impl_or_verify!(T => IntoBytes for Foo<T>);
165+
/// impl_or_verify!(T => Unaligned for Foo<T>);
166+
/// };
167+
/// ```
168+
enum InvalidImplOrVerify {}

src/util/macros.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ macro_rules! opt_unsafe_fn {
322322
($($args:ident),* -> $ret:ident) => { Option<unsafe fn($($args),*) -> $ret> };
323323
}
324324

325+
// This `allow` is needed because, when testing, we export this macro so it can
326+
// be used in `doctests`.
327+
#[allow(rustdoc::private_intra_doc_links)]
325328
/// Implements trait(s) for a type or verifies the given implementation by
326329
/// referencing an existing (derived) implementation.
327330
///
@@ -342,8 +345,7 @@ macro_rules! opt_unsafe_fn {
342345
/// `const _: () = unsafe` macro). The reason for this restriction is that,
343346
/// while `impl_or_verify!` can guarantee that the provided impl is sound when
344347
/// it is compiled with the appropriate cfgs, there is no way to guarantee that
345-
/// it is
346-
/// ever compiled with those cfgs. In particular, it would be possible to
348+
/// it is ever compiled with those cfgs. In particular, it would be possible to
347349
/// accidentally place an `impl_or_verify!` call in a context that is only ever
348350
/// compiled when the `derive` feature is disabled. If that were to happen,
349351
/// there would be nothing to prevent an unsound trait impl from being emitted.
@@ -372,6 +374,7 @@ macro_rules! opt_unsafe_fn {
372374
/// impl_or_verify!(T: Unaligned => Unaligned for Wrapper<T>);
373375
/// }
374376
/// ```
377+
#[cfg_attr(__ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE, macro_export)] // Used in `doctests.rs`
375378
macro_rules! impl_or_verify {
376379
// The following two match arms follow the same pattern as their
377380
// counterparts in `unsafe_impl!`; see the documentation on those arms for

tests/trybuild.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,3 @@ fn ui() {
3737
let t = trybuild::TestCases::new();
3838
t.compile_fail(format!("tests/{}/*.rs", source_files_dirname));
3939
}
40-
41-
#[test]
42-
#[cfg_attr(miri, ignore)]
43-
fn ui_invalid_impls() {
44-
let version = ToolchainVersion::extract_from_pwd().unwrap();
45-
// See the doc comment on this method for an explanation of what this does
46-
// and why we store source files in different directories.
47-
let source_files_dirname = version.get_ui_source_files_dirname_and_maybe_print_warning();
48-
49-
// Set `-Wwarnings` in the `RUSTFLAGS` environment variable to ensure that
50-
// `.stderr` files reflect what the typical user would encounter.
51-
set_rustflags_w_warnings();
52-
53-
let t = trybuild::TestCases::new();
54-
t.compile_fail(format!("tests/{}/invalid-impls/*.rs", source_files_dirname));
55-
}

tests/ui-msrv/invalid-impls/invalid-impls.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ui-msrv/invalid-impls/invalid-impls.stderr

Lines changed: 0 additions & 159 deletions
This file was deleted.

tests/ui-nightly/invalid-impls/invalid-impls.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)