diff --git a/src/doc/unstable-book/src/compiler-flags/crate-attr.md b/src/doc/unstable-book/src/compiler-flags/crate-attr.md new file mode 100644 index 0000000000000..8c9c501a23e3d --- /dev/null +++ b/src/doc/unstable-book/src/compiler-flags/crate-attr.md @@ -0,0 +1,16 @@ +# `crate-attr` + +The tracking issue for this feature is: [#138287](https://github.com/rust-lang/rust/issues/138287). + +------------------------ + +The `-Z crate-attr` flag allows you to inject attributes into the crate root. +For example, `-Z crate-attr=crate_name="test"` acts as if `#![crate_name="test"]` were present before the first source line of the crate root. + +To inject multiple attributes, pass `-Z crate-attr` multiple times. + +Formally, the expansion behaves as follows: +1. The crate is parsed as if `-Z crate-attr` were not present. +2. The attributes in `-Z crate-attr` are parsed. +3. The attributes are injected at the top of the crate root. +4. Macro expansion is performed. diff --git a/tests/ui/attributes/z-crate-attr.rs b/tests/ui/attributes/z-crate-attr/basic.rs similarity index 100% rename from tests/ui/attributes/z-crate-attr.rs rename to tests/ui/attributes/z-crate-attr/basic.rs diff --git a/tests/ui/attributes/z-crate-attr/cfg-false.rs b/tests/ui/attributes/z-crate-attr/cfg-false.rs new file mode 100644 index 0000000000000..db37cfdd08637 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/cfg-false.rs @@ -0,0 +1,7 @@ +// Ensure that `-Z crate-attr=cfg(FALSE)` can comment out the whole crate +//@ compile-flags: --crate-type=lib -Zcrate-attr=cfg(FALSE) +//@ check-pass + +// NOTE: duplicate items are load-bearing +fn foo() {} +fn foo() {} diff --git a/tests/ui/attributes/z-crate-attr/comments.rs b/tests/ui/attributes/z-crate-attr/comments.rs new file mode 100644 index 0000000000000..c1ab041f34477 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/comments.rs @@ -0,0 +1,5 @@ +//@ check-pass +//@ compile-flags: -Zcrate-attr=/*hi-there*/feature(rustc_attrs) + +#[rustc_dummy] +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/crate-name.rs b/tests/ui/attributes/z-crate-attr/crate-name.rs new file mode 100644 index 0000000000000..d49830390e2ec --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/crate-name.rs @@ -0,0 +1,6 @@ +// Ensure that `crate_name` and `crate_type` can be set through `-Z crate-attr`. +//@ check-pass +//@ compile-flags: -Zcrate-attr=crate_name="override" +fn main() { + assert_eq!(module_path!(), "r#override"); +} diff --git a/tests/ui/attributes/z-crate-attr/crate-type.rs b/tests/ui/attributes/z-crate-attr/crate-type.rs new file mode 100644 index 0000000000000..0e7411865af9c --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/crate-type.rs @@ -0,0 +1,3 @@ +//@ check-pass +//@ compile-flags: -Zcrate-attr=crate_type="lib" +// notice the lack of `main` is load-bearing diff --git a/tests/ui/attributes/z-crate-attr/garbage.rs b/tests/ui/attributes/z-crate-attr/garbage.rs new file mode 100644 index 0000000000000..ec81dd1bcaa08 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/garbage.rs @@ -0,0 +1,4 @@ +// Show diagnostics for invalid tokens +//@ compile-flags: -Zcrate-attr=`%~@$# +//@ error-pattern:unknown start of token +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/garbage.stderr b/tests/ui/attributes/z-crate-attr/garbage.stderr new file mode 100644 index 0000000000000..082046e31f8cd --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/garbage.stderr @@ -0,0 +1,20 @@ +error: unknown start of token: ` + --> :1:1 + | +LL | `%~@$# + | ^ + | +help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not + | +LL - `%~@$# +LL + '%~@$# + | + +error: expected identifier, found `%` + --> :1:2 + | +LL | `%~@$# + | ^ expected identifier + +error: aborting due to 2 previous errors + diff --git a/tests/ui/attributes/z-crate-attr/injection.rs b/tests/ui/attributes/z-crate-attr/injection.rs new file mode 100644 index 0000000000000..0c5c81ca71a92 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/injection.rs @@ -0,0 +1,3 @@ +//@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline' +//@ error-pattern:unexpected closing delimiter +fn foo() {} diff --git a/tests/ui/attributes/z-crate-attr/injection.stderr b/tests/ui/attributes/z-crate-attr/injection.stderr new file mode 100644 index 0000000000000..6fec98baf8dfe --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/injection.stderr @@ -0,0 +1,8 @@ +error: unexpected closing delimiter: `]` + --> :1:19 + | +LL | feature(yeet_expr)]fn main(){}#[inline + | ^ unexpected closing delimiter + +error: aborting due to 1 previous error + diff --git a/tests/ui/attributes/z-crate-attr/inner-attr.rs b/tests/ui/attributes/z-crate-attr/inner-attr.rs new file mode 100644 index 0000000000000..522c906dcd817 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/inner-attr.rs @@ -0,0 +1,4 @@ +//@ compile-flags: -Zcrate-attr=#![feature(foo)] +//@ error-pattern:expected identifier + +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/inner-attr.stderr b/tests/ui/attributes/z-crate-attr/inner-attr.stderr new file mode 100644 index 0000000000000..06a063d310b6b --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/inner-attr.stderr @@ -0,0 +1,8 @@ +error: expected identifier, found `#` + --> :1:1 + | +LL | #![feature(foo)] + | ^ expected identifier + +error: aborting due to 1 previous error + diff --git a/tests/ui/attributes/z-crate-attr/multiple.rs b/tests/ui/attributes/z-crate-attr/multiple.rs new file mode 100644 index 0000000000000..ee13253f62551 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/multiple.rs @@ -0,0 +1,3 @@ +//@ compile-flags: -Zcrate-attr=feature(foo),feature(bar) +//@ error-pattern:invalid crate attr +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/multiple.stderr b/tests/ui/attributes/z-crate-attr/multiple.stderr new file mode 100644 index 0000000000000..9f968a7e1346f --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/multiple.stderr @@ -0,0 +1,8 @@ +error: invalid crate attribute + --> :1:1 + | +LL | feature(foo),feature(bar) + | ^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/attributes/z-crate-attr/respect-existing-attrs.rs b/tests/ui/attributes/z-crate-attr/respect-existing-attrs.rs new file mode 100644 index 0000000000000..71f2559998fba --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/respect-existing-attrs.rs @@ -0,0 +1,9 @@ +// Make sure that existing root attributes are still respected even when `-Zcrate-attr` is present. +//@ run-pass +//@ compile-flags: -Zcrate-attr=feature(rustc_attrs) +#![crate_name = "override"] + +#[rustc_dummy] +fn main() { + assert_eq!(module_path!(), "r#override"); +} diff --git a/tests/ui/attributes/z-crate-attr/shebang.rs b/tests/ui/attributes/z-crate-attr/shebang.rs new file mode 100644 index 0000000000000..195393acaf5ec --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/shebang.rs @@ -0,0 +1,6 @@ +#!/usr/bin/env -S cargo +nightly -Zscript +// Make sure that shebangs are still allowed even when `-Zcrate-attr` is present. +//@ check-pass +//@ compile-flags: -Zcrate-attr=feature(rustc_attrs) +#[rustc_dummy] +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/unbalanced-paren.rs b/tests/ui/attributes/z-crate-attr/unbalanced-paren.rs new file mode 100644 index 0000000000000..fc1d7f39a5972 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/unbalanced-paren.rs @@ -0,0 +1,4 @@ +// Show diagnostics for unbalanced parens. +//@ compile-flags: -Zcrate-attr=( +//@ error-pattern:unclosed delimiter +fn main() {} diff --git a/tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr b/tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr new file mode 100644 index 0000000000000..47b1b764ba9a7 --- /dev/null +++ b/tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr @@ -0,0 +1,10 @@ +error: this file contains an unclosed delimiter + --> :1:2 + | +LL | ( + | -^ + | | + | unclosed delimiter + +error: aborting due to 1 previous error +