18 files changed +124
-0
lines changed Original file line number Diff line number Diff line change
1
+ # ` crate-attr `
2
+
3
+ The tracking issue for this feature is: [ #138287 ] ( https://github.com/rust-lang/rust/issues/138287 ) .
4
+
5
+ ------------------------
6
+
7
+ The ` -Z crate-attr ` flag allows you to inject attributes into the crate root.
8
+ 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.
9
+
10
+ To inject multiple attributes, pass ` -Z crate-attr ` multiple times.
11
+
12
+ Formally, the expansion behaves as follows:
13
+ 1 . The crate is parsed as if ` -Z crate-attr ` were not present.
14
+ 2 . The attributes in ` -Z crate-attr ` are parsed.
15
+ 3 . The attributes are injected at the top of the crate root.
16
+ 4 . Macro expansion is performed.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ // Ensure that `-Z crate-attr=cfg(FALSE)` can comment out the whole crate
2
+ //@ compile-flags: --crate-type=lib -Zcrate-attr=cfg(FALSE)
3
+ //@ check-pass
4
+
5
+ // NOTE: duplicate items are load-bearing
6
+ fn foo ( ) { }
7
+ fn foo ( ) { }
Original file line number Diff line number Diff line change
1
+ //@ check-pass
2
+ //@ compile-flags: -Zcrate-attr=/*hi-there*/feature(rustc_attrs)
3
+
4
+ #[ rustc_dummy]
5
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ // Ensure that `crate_name` and `crate_type` can be set through `-Z crate-attr`.
2
+ //@ check-pass
3
+ //@ compile-flags: -Zcrate-attr=crate_name="override"
4
+ fn main ( ) {
5
+ assert_eq ! ( module_path!( ) , "r#override" ) ;
6
+ }
Original file line number Diff line number Diff line change
1
+ //@ check-pass
2
+ //@ compile-flags: -Zcrate-attr=crate_type="lib"
3
+ // notice the lack of `main` is load-bearing
Original file line number Diff line number Diff line change
1
+ // Show diagnostics for invalid tokens
2
+ //@ compile-flags: -Zcrate-attr=`%~@$#
3
+ //@ error-pattern:unknown start of token
4
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: unknown start of token: `
2
+ --> <crate attribute>:1:1
3
+ |
4
+ LL | `%~@$#
5
+ | ^
6
+ |
7
+ help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
8
+ |
9
+ LL - `%~@$#
10
+ LL + '%~@$#
11
+ |
12
+
13
+ error: expected identifier, found `%`
14
+ --> <crate attribute>:1:2
15
+ |
16
+ LL | `%~@$#
17
+ | ^ expected identifier
18
+
19
+ error: aborting due to 2 previous errors
20
+
Original file line number Diff line number Diff line change
1
+ //@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline'
2
+ //@ error-pattern:unexpected closing delimiter
3
+ fn foo ( ) { }
Original file line number Diff line number Diff line change
1
+ error: unexpected closing delimiter: `]`
2
+ --> <crate attribute>:1:19
3
+ |
4
+ LL | feature(yeet_expr)]fn main(){}#[inline
5
+ | ^ unexpected closing delimiter
6
+
7
+ error: aborting due to 1 previous error
8
+
Original file line number Diff line number Diff line change
1
+ //@ compile-flags: -Zcrate-attr=#![feature(foo)]
2
+ //@ error-pattern:expected identifier
3
+
4
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: expected identifier, found `#`
2
+ --> <crate attribute>:1:1
3
+ |
4
+ LL | #![feature(foo)]
5
+ | ^ expected identifier
6
+
7
+ error: aborting due to 1 previous error
8
+
Original file line number Diff line number Diff line change
1
+ //@ compile-flags: -Zcrate-attr=feature(foo),feature(bar)
2
+ //@ error-pattern:invalid crate attr
3
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: invalid crate attribute
2
+ --> <crate attribute>:1:1
3
+ |
4
+ LL | feature(foo),feature(bar)
5
+ | ^^^^^^^^^^^^^
6
+
7
+ error: aborting due to 1 previous error
8
+
Original file line number Diff line number Diff line change
1
+ // Make sure that existing root attributes are still respected even when `-Zcrate-attr` is present.
2
+ //@ run-pass
3
+ //@ compile-flags: -Zcrate-attr=feature(rustc_attrs)
4
+ #![ crate_name = "override" ]
5
+
6
+ #[ rustc_dummy]
7
+ fn main ( ) {
8
+ assert_eq ! ( module_path!( ) , "r#override" ) ;
9
+ }
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env -S cargo +nightly -Zscript
2
+ // Make sure that shebangs are still allowed even when `-Zcrate-attr` is present.
3
+ //@ check-pass
4
+ //@ compile-flags: -Zcrate-attr=feature(rustc_attrs)
5
+ #[ rustc_dummy]
6
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ // Show diagnostics for unbalanced parens.
2
+ //@ compile-flags: -Zcrate-attr=(
3
+ //@ error-pattern:unclosed delimiter
4
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: this file contains an unclosed delimiter
2
+ --> <crate attribute>:1:2
3
+ |
4
+ LL | (
5
+ | -^
6
+ | |
7
+ | unclosed delimiter
8
+
9
+ error: aborting due to 1 previous error
10
+
0 commit comments