-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Document -Z crate-attr #138288
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
jyn514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.