From 8a1d6d319b02f2da13622b148d7ff0559f836f8a Mon Sep 17 00:00:00 2001 From: Jamie Hill-Daniel Date: Wed, 21 Jan 2026 20:02:57 +0000 Subject: [PATCH] Add test for malformed and misapplied `crate_type` --- tests/ui/attributes/crate-type-non-crate.rs | 19 +++++ .../ui/attributes/crate-type-non-crate.stderr | 70 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/ui/attributes/crate-type-non-crate.rs create mode 100644 tests/ui/attributes/crate-type-non-crate.stderr diff --git a/tests/ui/attributes/crate-type-non-crate.rs b/tests/ui/attributes/crate-type-non-crate.rs new file mode 100644 index 000000000000..bd667b4b0a45 --- /dev/null +++ b/tests/ui/attributes/crate-type-non-crate.rs @@ -0,0 +1,19 @@ +//! Test the behavior of various malformed `crate_type` attributes applied to a non-crate target. +#![allow(unused_attributes)] + +// No arguments +#[crate_type] //~ ERROR malformed `crate_type` attribute input +// List/NameValue with/without strings +#[crate_type(lib)] //~ ERROR malformed `crate_type` attribute input +#[crate_type("lib")] //~ ERROR malformed `crate_type` attribute input +#[crate_type = lib] //~ ERROR attribute value must be a literal +#[crate_type = "lib"] // OK +// Same as above but with invalid names +#[crate_type(foo)] //~ ERROR malformed `crate_type` attribute input +#[crate_type("foo")] //~ ERROR malformed `crate_type` attribute input +#[crate_type = foo] //~ ERROR attribute value must be a literal +#[crate_type = "foo"] // OK - we don't report errors on invalid crate types here +// Non-string literals +#[crate_type(1)] //~ ERROR malformed `crate_type` attribute input +#[crate_type = 1] //~ ERROR malformed `crate_type` attribute input +fn main() {} diff --git a/tests/ui/attributes/crate-type-non-crate.stderr b/tests/ui/attributes/crate-type-non-crate.stderr new file mode 100644 index 000000000000..433c1e3b71a2 --- /dev/null +++ b/tests/ui/attributes/crate-type-non-crate.stderr @@ -0,0 +1,70 @@ +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:5:1 + | +LL | #[crate_type] + | ^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:7:1 + | +LL | #[crate_type(lib)] + | ^^^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:8:1 + | +LL | #[crate_type("lib")] + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: attribute value must be a literal + --> $DIR/crate-type-non-crate.rs:9:16 + | +LL | #[crate_type = lib] + | ^^^ + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:12:1 + | +LL | #[crate_type(foo)] + | ^^^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:13:1 + | +LL | #[crate_type("foo")] + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: attribute value must be a literal + --> $DIR/crate-type-non-crate.rs:14:16 + | +LL | #[crate_type = foo] + | ^^^ + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:17:1 + | +LL | #[crate_type(1)] + | ^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: malformed `crate_type` attribute input + --> $DIR/crate-type-non-crate.rs:18:1 + | +LL | #[crate_type = 1] + | ^^^^^^^^^^^^^^^^^ + | + = note: for more information, visit + +error: aborting due to 9 previous errors +