about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-03-07 21:57:52 -0500
committerGitHub <noreply@github.com>2025-03-07 21:57:52 -0500
commita29e3af87d987000b9d8b9c72750aeca0df352c7 (patch)
treea4994485b24e94676de21af6dba931de86218925 /tests
parentf1051ed6f105e73a7fff34a472ced9cf19c93f96 (diff)
parente07d9a8200bc37e3cd816386d35931f9f02e9464 (diff)
Rollup merge of #138033 - obi1kenobi:pg/json-attrs-tests, r=aDotInTheVoid
rustdoc: Add attribute-related tests for rustdoc JSON.

Add rustdoc JSON tests covering the use of the following attributes:
- `#[non_exhaustive]` applied to enums, variants, and structs
- `#[must_use]`, both with and without a message
- `#[no_mangle]`, in both edition 2021 and 2024 (`#[unsafe(no_mangle)]`) flavors
- `#[export_name]`, also in both edition 2021 and 2024 flavors

Related to #137645; this is a subset of the attributes that `cargo-semver-checks` relies on and tests in its own test suite or in the test suites of its components such as `trustfall-rustdoc-adapter`.

Helps with #81359

r? `@aDotInTheVoid`
Diffstat (limited to 'tests')
-rw-r--r--tests/rustdoc-json/attrs/export_name_2021.rs6
-rw-r--r--tests/rustdoc-json/attrs/export_name_2024.rs9
-rw-r--r--tests/rustdoc-json/attrs/must_use.rs9
-rw-r--r--tests/rustdoc-json/attrs/no_mangle_2021.rs6
-rw-r--r--tests/rustdoc-json/attrs/no_mangle_2024.rs9
-rw-r--r--tests/rustdoc-json/attrs/non_exhaustive.rs19
6 files changed, 58 insertions, 0 deletions
diff --git a/tests/rustdoc-json/attrs/export_name_2021.rs b/tests/rustdoc-json/attrs/export_name_2021.rs
new file mode 100644
index 00000000000..badf124bdde
--- /dev/null
+++ b/tests/rustdoc-json/attrs/export_name_2021.rs
@@ -0,0 +1,6 @@
+//@ edition: 2021
+#![no_std]
+
+//@ is "$.index[*][?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
+#[export_name = "altered"]
+pub extern "C" fn example() {}
diff --git a/tests/rustdoc-json/attrs/export_name_2024.rs b/tests/rustdoc-json/attrs/export_name_2024.rs
new file mode 100644
index 00000000000..c5bb9dcc8f6
--- /dev/null
+++ b/tests/rustdoc-json/attrs/export_name_2024.rs
@@ -0,0 +1,9 @@
+//@ edition: 2024
+#![no_std]
+
+// The representation of `#[unsafe(export_name = ..)]` in rustdoc in edition 2024
+// is still `#[export_name = ..]` without the `unsafe` attribute wrapper.
+
+//@ is "$.index[*][?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
+#[unsafe(export_name = "altered")]
+pub extern "C" fn example() {}
diff --git a/tests/rustdoc-json/attrs/must_use.rs b/tests/rustdoc-json/attrs/must_use.rs
new file mode 100644
index 00000000000..dca73abc76a
--- /dev/null
+++ b/tests/rustdoc-json/attrs/must_use.rs
@@ -0,0 +1,9 @@
+#![no_std]
+
+//@ is "$.index[*][?(@.name=='example')].attrs" '["#[must_use]"]'
+#[must_use]
+pub fn example() -> impl Iterator<Item = i64> {}
+
+//@ is "$.index[*][?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]"]'
+#[must_use = "does nothing if you do not use it"]
+pub fn explicit_message() -> impl Iterator<Item = i64> {}
diff --git a/tests/rustdoc-json/attrs/no_mangle_2021.rs b/tests/rustdoc-json/attrs/no_mangle_2021.rs
new file mode 100644
index 00000000000..258542086ec
--- /dev/null
+++ b/tests/rustdoc-json/attrs/no_mangle_2021.rs
@@ -0,0 +1,6 @@
+//@ edition: 2021
+#![no_std]
+
+//@ is "$.index[*][?(@.name=='example')].attrs" '["#[no_mangle]"]'
+#[no_mangle]
+pub extern "C" fn example() {}
diff --git a/tests/rustdoc-json/attrs/no_mangle_2024.rs b/tests/rustdoc-json/attrs/no_mangle_2024.rs
new file mode 100644
index 00000000000..4c01082d045
--- /dev/null
+++ b/tests/rustdoc-json/attrs/no_mangle_2024.rs
@@ -0,0 +1,9 @@
+//@ edition: 2024
+#![no_std]
+
+// The representation of `#[unsafe(no_mangle)]` in rustdoc in edition 2024
+// is still `#[no_mangle]` without the `unsafe` attribute wrapper.
+
+//@ is "$.index[*][?(@.name=='example')].attrs" '["#[no_mangle]"]'
+#[unsafe(no_mangle)]
+pub extern "C" fn example() {}
diff --git a/tests/rustdoc-json/attrs/non_exhaustive.rs b/tests/rustdoc-json/attrs/non_exhaustive.rs
new file mode 100644
index 00000000000..5d738fc0560
--- /dev/null
+++ b/tests/rustdoc-json/attrs/non_exhaustive.rs
@@ -0,0 +1,19 @@
+#![no_std]
+
+//@ is "$.index[*][?(@.name=='MyEnum')].attrs" '["#[non_exhaustive]"]'
+#[non_exhaustive]
+pub enum MyEnum {
+    First,
+}
+
+pub enum NonExhaustiveVariant {
+    //@ is "$.index[*][?(@.name=='Variant')].attrs" '["#[non_exhaustive]"]'
+    #[non_exhaustive]
+    Variant(i64),
+}
+
+//@ is "$.index[*][?(@.name=='MyStruct')].attrs" '["#[non_exhaustive]"]'
+#[non_exhaustive]
+pub struct MyStruct {
+    pub x: i64,
+}