about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2025-03-11 13:30:53 +0100
committerGitHub <noreply@github.com>2025-03-11 13:30:53 +0100
commit79fa56a02691b2fc73fda54a775e967d57a869b6 (patch)
tree5d52757ee7c0122e71e413422cb0a30ca71e0000
parent09cc57e3f82c60615ea4072056a27cb1308d570a (diff)
parent512ebed59a37fc6bbb84195843878f279a3401ad (diff)
downloadrust-79fa56a02691b2fc73fda54a775e967d57a869b6.tar.gz
rust-79fa56a02691b2fc73fda54a775e967d57a869b6.zip
Rollup merge of #138288 - jyn514:crate-attr, r=Noratrieb
Document -Z crate-attr

and also add a bunch of tests
-rw-r--r--src/doc/unstable-book/src/compiler-flags/crate-attr.md16
-rw-r--r--tests/ui/attributes/z-crate-attr/basic.rs (renamed from tests/ui/attributes/z-crate-attr.rs)0
-rw-r--r--tests/ui/attributes/z-crate-attr/cfg-false.rs7
-rw-r--r--tests/ui/attributes/z-crate-attr/comments.rs5
-rw-r--r--tests/ui/attributes/z-crate-attr/crate-name.rs6
-rw-r--r--tests/ui/attributes/z-crate-attr/crate-type.rs3
-rw-r--r--tests/ui/attributes/z-crate-attr/garbage.rs4
-rw-r--r--tests/ui/attributes/z-crate-attr/garbage.stderr20
-rw-r--r--tests/ui/attributes/z-crate-attr/injection.rs3
-rw-r--r--tests/ui/attributes/z-crate-attr/injection.stderr8
-rw-r--r--tests/ui/attributes/z-crate-attr/inner-attr.rs4
-rw-r--r--tests/ui/attributes/z-crate-attr/inner-attr.stderr8
-rw-r--r--tests/ui/attributes/z-crate-attr/multiple.rs3
-rw-r--r--tests/ui/attributes/z-crate-attr/multiple.stderr8
-rw-r--r--tests/ui/attributes/z-crate-attr/respect-existing-attrs.rs9
-rw-r--r--tests/ui/attributes/z-crate-attr/shebang.rs6
-rw-r--r--tests/ui/attributes/z-crate-attr/unbalanced-paren.rs4
-rw-r--r--tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr10
18 files changed, 124 insertions, 0 deletions
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 00000000000..8c9c501a23e
--- /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
index 119a48d5d65..119a48d5d65 100644
--- a/tests/ui/attributes/z-crate-attr.rs
+++ b/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 00000000000..db37cfdd086
--- /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 00000000000..c1ab041f344
--- /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 00000000000..d49830390e2
--- /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 00000000000..0e7411865af
--- /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 00000000000..ec81dd1bcaa
--- /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 00000000000..082046e31f8
--- /dev/null
+++ b/tests/ui/attributes/z-crate-attr/garbage.stderr
@@ -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
+
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 00000000000..0c5c81ca71a
--- /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 00000000000..6fec98baf8d
--- /dev/null
+++ b/tests/ui/attributes/z-crate-attr/injection.stderr
@@ -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
+
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 00000000000..522c906dcd8
--- /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 00000000000..06a063d310b
--- /dev/null
+++ b/tests/ui/attributes/z-crate-attr/inner-attr.stderr
@@ -0,0 +1,8 @@
+error: expected identifier, found `#`
+  --> <crate attribute>: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 00000000000..ee13253f625
--- /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 00000000000..9f968a7e134
--- /dev/null
+++ b/tests/ui/attributes/z-crate-attr/multiple.stderr
@@ -0,0 +1,8 @@
+error: invalid crate attribute
+  --> <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 00000000000..71f2559998f
--- /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 00000000000..195393acaf5
--- /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 00000000000..fc1d7f39a59
--- /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 00000000000..47b1b764ba9
--- /dev/null
+++ b/tests/ui/attributes/z-crate-attr/unbalanced-paren.stderr
@@ -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
+