Rollup merge of #138288 - jyn514:crate-attr, r=Noratrieb

Document -Z crate-attr

and also add a bunch of tests
This commit is contained in:
Jakub Beránek 2025-03-11 13:30:53 +01:00 committed by GitHub
commit 79fa56a026
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 124 additions and 0 deletions

View File

@ -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.

View File

@ -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() {}

View File

@ -0,0 +1,5 @@
//@ check-pass
//@ compile-flags: -Zcrate-attr=/*hi-there*/feature(rustc_attrs)
#[rustc_dummy]
fn main() {}

View File

@ -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");
}

View File

@ -0,0 +1,3 @@
//@ check-pass
//@ compile-flags: -Zcrate-attr=crate_type="lib"
// notice the lack of `main` is load-bearing

View File

@ -0,0 +1,4 @@
// Show diagnostics for invalid tokens
//@ compile-flags: -Zcrate-attr=`%~@$#
//@ error-pattern:unknown start of token
fn main() {}

View File

@ -0,0 +1,20 @@
error: unknown start of token: `
--> <crate attribute>:1:1
|
LL | `%~@$#
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
LL - `%~@$#
LL + '%~@$#
|
error: expected identifier, found `%`
--> <crate attribute>:1:2
|
LL | `%~@$#
| ^ expected identifier
error: aborting due to 2 previous errors

View File

@ -0,0 +1,3 @@
//@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline'
//@ error-pattern:unexpected closing delimiter
fn foo() {}

View File

@ -0,0 +1,8 @@
error: unexpected closing delimiter: `]`
--> <crate attribute>:1:19
|
LL | feature(yeet_expr)]fn main(){}#[inline
| ^ unexpected closing delimiter
error: aborting due to 1 previous error

View File

@ -0,0 +1,4 @@
//@ compile-flags: -Zcrate-attr=#![feature(foo)]
//@ error-pattern:expected identifier
fn main() {}

View File

@ -0,0 +1,8 @@
error: expected identifier, found `#`
--> <crate attribute>:1:1
|
LL | #![feature(foo)]
| ^ expected identifier
error: aborting due to 1 previous error

View File

@ -0,0 +1,3 @@
//@ compile-flags: -Zcrate-attr=feature(foo),feature(bar)
//@ error-pattern:invalid crate attr
fn main() {}

View File

@ -0,0 +1,8 @@
error: invalid crate attribute
--> <crate attribute>:1:1
|
LL | feature(foo),feature(bar)
| ^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@ -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");
}

View File

@ -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() {}

View File

@ -0,0 +1,4 @@
// Show diagnostics for unbalanced parens.
//@ compile-flags: -Zcrate-attr=(
//@ error-pattern:unclosed delimiter
fn main() {}

View File

@ -0,0 +1,10 @@
error: this file contains an unclosed delimiter
--> <crate attribute>:1:2
|
LL | (
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error