Skip to content

Document -Z crate-attr #138288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/crate-attr.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions tests/ui/attributes/z-crate-attr/cfg-false.rs
Original file line number Diff line number Diff line change
@@ -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() {}
5 changes: 5 additions & 0 deletions tests/ui/attributes/z-crate-attr/comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@ check-pass
//@ compile-flags: -Zcrate-attr=/*hi-there*/feature(rustc_attrs)

#[rustc_dummy]
fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/attributes/z-crate-attr/crate-name.rs
Original file line number Diff line number Diff line change
@@ -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");
}
3 changes: 3 additions & 0 deletions tests/ui/attributes/z-crate-attr/crate-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//@ check-pass
//@ compile-flags: -Zcrate-attr=crate_type="lib"
// notice the lack of `main` is load-bearing
4 changes: 4 additions & 0 deletions tests/ui/attributes/z-crate-attr/garbage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Show diagnostics for invalid tokens
//@ compile-flags: -Zcrate-attr=`%~@$#
//@ error-pattern:unknown start of token
fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/attributes/z-crate-attr/garbage.stderr
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions tests/ui/attributes/z-crate-attr/injection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline'
//@ error-pattern:unexpected closing delimiter
fn foo() {}
8 changes: 8 additions & 0 deletions tests/ui/attributes/z-crate-attr/injection.stderr
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions tests/ui/attributes/z-crate-attr/inner-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//@ compile-flags: -Zcrate-attr=#![feature(foo)]
//@ error-pattern:expected identifier

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/attributes/z-crate-attr/inner-attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected identifier, found `#`
--> <crate attribute>:1:1
|
LL | #![feature(foo)]
| ^ expected identifier

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui/attributes/z-crate-attr/multiple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//@ compile-flags: -Zcrate-attr=feature(foo),feature(bar)
//@ error-pattern:invalid crate attr
fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/attributes/z-crate-attr/multiple.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: invalid crate attribute
--> <crate attribute>:1:1
|
LL | feature(foo),feature(bar)
| ^^^^^^^^^^^^^

error: aborting due to 1 previous error

9 changes: 9 additions & 0 deletions tests/ui/attributes/z-crate-attr/respect-existing-attrs.rs
Original file line number Diff line number Diff line change
@@ -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");
}
6 changes: 6 additions & 0 deletions tests/ui/attributes/z-crate-attr/shebang.rs
Original file line number Diff line number Diff line change
@@ -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() {}
4 changes: 4 additions & 0 deletions tests/ui/attributes/z-crate-attr/unbalanced-paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Show diagnostics for unbalanced parens.
//@ compile-flags: -Zcrate-attr=(
//@ error-pattern:unclosed delimiter
fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading