From 2439b97b3afa804991a4f73446d90d470022c529 Mon Sep 17 00:00:00 2001 From: Ted Driggs Date: Wed, 23 Nov 2022 05:31:57 -0800 Subject: [PATCH] Add compile tests for crate = "..." --- .../tests/compile-fail/crate_root.rs | 16 +++ .../tests/compile-fail/crate_root.stderr | 97 +++++++++++++++++++ derive_builder/tests/run-pass/crate_alias.rs | 24 +++++ 3 files changed, 137 insertions(+) create mode 100644 derive_builder/tests/compile-fail/crate_root.rs create mode 100644 derive_builder/tests/compile-fail/crate_root.stderr create mode 100644 derive_builder/tests/run-pass/crate_alias.rs diff --git a/derive_builder/tests/compile-fail/crate_root.rs b/derive_builder/tests/compile-fail/crate_root.rs new file mode 100644 index 00000000..6f6cd301 --- /dev/null +++ b/derive_builder/tests/compile-fail/crate_root.rs @@ -0,0 +1,16 @@ +#[macro_use] +extern crate derive_builder; + +// This is a stand-in for any bad crate directive +mod empty {} + +#[derive(Builder)] +// It would be nice if the "failed to resolve" errors would identify `"empty"` as the error span, +// but doing so would require rewriting a lot of the code generation to use the crate_root span +// for the full path of the thing being imported, and that doesn't seem worth the code churn. +#[builder(crate = "empty")] +struct BadCrate { + lorem: String, +} + +fn main() {} diff --git a/derive_builder/tests/compile-fail/crate_root.stderr b/derive_builder/tests/compile-fail/crate_root.stderr new file mode 100644 index 00000000..326b80aa --- /dev/null +++ b/derive_builder/tests/compile-fail/crate_root.stderr @@ -0,0 +1,97 @@ +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ could not find `export` in `empty` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty::export::core::option` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this enum + | +5 | use derive_builder::export::core::option::Option; + | + +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty::export::core::clone` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this trait + | +5 | use derive_builder::export::core::clone::Clone; + | + +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty::export::core::result` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing one of these items + | +5 | use derive_builder::export::core::fmt::Result; + | +5 | use derive_builder::export::core::io::Result; + | +5 | use derive_builder::export::core::result::Result; + | +5 | use derive_builder::export::core::thread::Result; + | + +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty::export::core::convert` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this trait + | +5 | use derive_builder::export::core::convert::Into; + | + +error[E0433]: failed to resolve: could not find `UninitializedFieldError` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this struct + | +5 | use derive_builder::UninitializedFieldError; + | + +error[E0433]: failed to resolve: could not find `export` in `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty::export::core::default` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this trait + | +5 | use derive_builder::export::core::default::Default; + | + +error[E0412]: cannot find type `UninitializedFieldError` in module `empty` + --> tests/compile-fail/crate_root.rs:7:10 + | +7 | #[derive(Builder)] + | ^^^^^^^ not found in `empty` + | + = note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider importing this struct + | +5 | use derive_builder::UninitializedFieldError; + | diff --git a/derive_builder/tests/run-pass/crate_alias.rs b/derive_builder/tests/run-pass/crate_alias.rs new file mode 100644 index 00000000..d2e6b473 --- /dev/null +++ b/derive_builder/tests/run-pass/crate_alias.rs @@ -0,0 +1,24 @@ +//! Test that an alias of derive_builder is valid in #[builder(crate = "...")] +//! +//! This test is imperfect, as it still passes without setting `crate = "..."`. +//! This is likely because `derive_builder` is automatically present in 2018 +//! without needing the extern crate line. +//! +//! The test will fail if an incorrect alias is used, so it adds limited value. + +#[macro_use] +extern crate derive_builder as db; + +#[derive(Builder)] +#[builder(crate = "db")] +struct AliasedCrate { + #[builder(setter(into))] + lorem: String, +} + +fn main() { + AliasedCrateBuilder::default() + .lorem("hello") + .build() + .unwrap(); +}