Skip to content

Commit 3097951

Browse files
authored
Rollup merge of #130931 - GuillaumeGomez:standalone-crate, r=notriddle
Rename `standalone` doctest attribute into `standalone_crate` Following [zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/266220-t-rustdoc/topic/Renaming.20code.20block.20.22standalone.22.20attribute.3F) and poll results. r? `@notriddle`
2 parents 2da3cb9 + 6f5f21a commit 3097951

File tree

12 files changed

+165
-98
lines changed

12 files changed

+165
-98
lines changed

Diff for: library/core/src/panic/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a> Location<'a> {
4444
///
4545
/// # Examples
4646
///
47-
/// ```standalone
47+
/// ```standalone_crate
4848
/// use std::panic::Location;
4949
///
5050
/// /// Returns the [`Location`] at which it is called.

Diff for: src/doc/rustdoc/src/write-documentation/documentation-tests.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ In some cases, doctests cannot be merged. For example, if you have:
414414
The problem with this code is that, if you change any other doctests, it'll likely break when
415415
runing `rustdoc --test`, making it tricky to maintain.
416416

417-
This is where the `standalone` attribute comes in: it tells `rustdoc` that a doctest
417+
This is where the `standalone_crate` attribute comes in: it tells `rustdoc` that a doctest
418418
should not be merged with the others. So the previous code should use it:
419419

420420
```rust
421-
//! ```standalone
421+
//! ```standalone_crate
422422
//! let location = std::panic::Location::caller();
423423
//! assert_eq!(location.line(), 4);
424424
//! ```

Diff for: src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ impl CreateRunnableDocTests {
837837
let is_standalone = !doctest.can_be_merged
838838
|| scraped_test.langstr.compile_fail
839839
|| scraped_test.langstr.test_harness
840-
|| scraped_test.langstr.standalone
840+
|| scraped_test.langstr.standalone_crate
841841
|| self.rustdoc_options.nocapture
842842
|| self.rustdoc_options.test_args.iter().any(|arg| arg == "--show-output");
843843
if is_standalone {

Diff for: src/librustdoc/doctest/make.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl DocTestBuilder {
4848
) -> Self {
4949
let can_merge_doctests = can_merge_doctests
5050
&& lang_str.is_some_and(|lang_str| {
51-
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone
51+
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone_crate
5252
});
5353

5454
let SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } =

Diff for: src/librustdoc/html/markdown.rs

+40-37
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ pub(crate) struct LangString {
871871
pub(crate) rust: bool,
872872
pub(crate) test_harness: bool,
873873
pub(crate) compile_fail: bool,
874-
pub(crate) standalone: bool,
874+
pub(crate) standalone_crate: bool,
875875
pub(crate) error_codes: Vec<String>,
876876
pub(crate) edition: Option<Edition>,
877877
pub(crate) added_classes: Vec<String>,
@@ -1194,7 +1194,7 @@ impl Default for LangString {
11941194
rust: true,
11951195
test_harness: false,
11961196
compile_fail: false,
1197-
standalone: false,
1197+
standalone_crate: false,
11981198
error_codes: Vec::new(),
11991199
edition: None,
12001200
added_classes: Vec::new(),
@@ -1264,8 +1264,8 @@ impl LangString {
12641264
seen_rust_tags = !seen_other_tags || seen_rust_tags;
12651265
data.no_run = true;
12661266
}
1267-
LangStringToken::LangToken("standalone") => {
1268-
data.standalone = true;
1267+
LangStringToken::LangToken("standalone_crate") => {
1268+
data.standalone_crate = true;
12691269
seen_rust_tags = !seen_other_tags || seen_rust_tags;
12701270
}
12711271
LangStringToken::LangToken(x) if x.starts_with("edition") => {
@@ -1298,44 +1298,47 @@ impl LangString {
12981298
}
12991299
LangStringToken::LangToken(x) if extra.is_some() => {
13001300
let s = x.to_lowercase();
1301-
if let Some((flag, help)) = if s == "compile-fail"
1302-
|| s == "compile_fail"
1303-
|| s == "compilefail"
1304-
{
1305-
Some((
1306-
"compile_fail",
1307-
"the code block will either not be tested if not marked as a rust one \
1308-
or won't fail if it compiles successfully",
1309-
))
1310-
} else if s == "should-panic" || s == "should_panic" || s == "shouldpanic" {
1311-
Some((
1312-
"should_panic",
1313-
"the code block will either not be tested if not marked as a rust one \
1314-
or won't fail if it doesn't panic when running",
1315-
))
1316-
} else if s == "no-run" || s == "no_run" || s == "norun" {
1317-
Some((
1318-
"no_run",
1319-
"the code block will either not be tested if not marked as a rust one \
1320-
or will be run (which you might not want)",
1321-
))
1322-
} else if s == "test-harness" || s == "test_harness" || s == "testharness" {
1323-
Some((
1324-
"test_harness",
1325-
"the code block will either not be tested if not marked as a rust one \
1326-
or the code will be wrapped inside a main function",
1327-
))
1328-
} else {
1329-
None
1301+
if let Some(help) = match s.as_str() {
1302+
"compile-fail" | "compile_fail" | "compilefail" => Some(
1303+
"use `compile_fail` to invert the results of this test, so that it \
1304+
passes if it cannot be compiled and fails if it can",
1305+
),
1306+
"should-panic" | "should_panic" | "shouldpanic" => Some(
1307+
"use `should_panic` to invert the results of this test, so that if \
1308+
passes if it panics and fails if it does not",
1309+
),
1310+
"no-run" | "no_run" | "norun" => Some(
1311+
"use `no_run` to compile, but not run, the code sample during \
1312+
testing",
1313+
),
1314+
"test-harness" | "test_harness" | "testharness" => Some(
1315+
"use `test_harness` to run functions marked `#[test]` instead of a \
1316+
potentially-implicit `main` function",
1317+
),
1318+
"standalone" | "standalone_crate" | "standalone-crate" => {
1319+
if let Some(extra) = extra
1320+
&& extra.sp.at_least_rust_2024()
1321+
{
1322+
Some(
1323+
"use `standalone_crate` to compile this code block \
1324+
separately",
1325+
)
1326+
} else {
1327+
None
1328+
}
1329+
}
1330+
_ => None,
13301331
} {
13311332
if let Some(extra) = extra {
13321333
extra.error_invalid_codeblock_attr_with_help(
13331334
format!("unknown attribute `{x}`"),
13341335
|lint| {
1335-
lint.help(format!(
1336-
"there is an attribute with a similar name: `{flag}`"
1337-
))
1338-
.help(help);
1336+
lint.help(help).help(
1337+
"this code block may be skipped during testing, \
1338+
because unknown attributes are treated as markers for \
1339+
code samples written in other programming languages, \
1340+
unless it is also explicitly marked as `rust`",
1341+
);
13391342
},
13401343
);
13411344
}

Diff for: tests/run-make/doctests-merge/doctest-standalone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![crate_name = "foo"]
22
#![crate_type = "lib"]
33

4-
//! ```standalone
4+
//! ```standalone_crate
55
//! foo::init();
66
//! ```
77
8-
/// ```standalone
8+
/// ```standalone_crate
99
/// foo::init();
1010
/// ```
1111
pub fn init() {

Diff for: tests/rustdoc-ui/doctest/check-attr-test.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ error: unknown attribute `compile-fail`
88
9 | | /// ```
99
| |_______^
1010
|
11-
= help: there is an attribute with a similar name: `compile_fail`
12-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it compiles successfully
11+
= help: use `compile_fail` to invert the results of this test, so that it passes if it cannot be compiled and fails if it can
12+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
1313
note: the lint level is defined here
1414
--> $DIR/check-attr-test.rs:3:9
1515
|
@@ -26,8 +26,8 @@ error: unknown attribute `compilefail`
2626
9 | | /// ```
2727
| |_______^
2828
|
29-
= help: there is an attribute with a similar name: `compile_fail`
30-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it compiles successfully
29+
= help: use `compile_fail` to invert the results of this test, so that it passes if it cannot be compiled and fails if it can
30+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
3131

3232
error: unknown attribute `comPile_fail`
3333
--> $DIR/check-attr-test.rs:5:1
@@ -39,8 +39,8 @@ error: unknown attribute `comPile_fail`
3939
9 | | /// ```
4040
| |_______^
4141
|
42-
= help: there is an attribute with a similar name: `compile_fail`
43-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it compiles successfully
42+
= help: use `compile_fail` to invert the results of this test, so that it passes if it cannot be compiled and fails if it can
43+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
4444

4545
error: unknown attribute `should-panic`
4646
--> $DIR/check-attr-test.rs:12:1
@@ -52,8 +52,8 @@ error: unknown attribute `should-panic`
5252
16 | | /// ```
5353
| |_______^
5454
|
55-
= help: there is an attribute with a similar name: `should_panic`
56-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running
55+
= help: use `should_panic` to invert the results of this test, so that if passes if it panics and fails if it does not
56+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
5757

5858
error: unknown attribute `shouldpanic`
5959
--> $DIR/check-attr-test.rs:12:1
@@ -65,8 +65,8 @@ error: unknown attribute `shouldpanic`
6565
16 | | /// ```
6666
| |_______^
6767
|
68-
= help: there is an attribute with a similar name: `should_panic`
69-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running
68+
= help: use `should_panic` to invert the results of this test, so that if passes if it panics and fails if it does not
69+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
7070

7171
error: unknown attribute `shOuld_panic`
7272
--> $DIR/check-attr-test.rs:12:1
@@ -78,8 +78,8 @@ error: unknown attribute `shOuld_panic`
7878
16 | | /// ```
7979
| |_______^
8080
|
81-
= help: there is an attribute with a similar name: `should_panic`
82-
= help: the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running
81+
= help: use `should_panic` to invert the results of this test, so that if passes if it panics and fails if it does not
82+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
8383

8484
error: unknown attribute `no-run`
8585
--> $DIR/check-attr-test.rs:19:1
@@ -91,8 +91,8 @@ error: unknown attribute `no-run`
9191
23 | | /// ```
9292
| |_______^
9393
|
94-
= help: there is an attribute with a similar name: `no_run`
95-
= help: the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
94+
= help: use `no_run` to compile, but not run, the code sample during testing
95+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
9696

9797
error: unknown attribute `norun`
9898
--> $DIR/check-attr-test.rs:19:1
@@ -104,8 +104,8 @@ error: unknown attribute `norun`
104104
23 | | /// ```
105105
| |_______^
106106
|
107-
= help: there is an attribute with a similar name: `no_run`
108-
= help: the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
107+
= help: use `no_run` to compile, but not run, the code sample during testing
108+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
109109

110110
error: unknown attribute `nO_run`
111111
--> $DIR/check-attr-test.rs:19:1
@@ -117,8 +117,8 @@ error: unknown attribute `nO_run`
117117
23 | | /// ```
118118
| |_______^
119119
|
120-
= help: there is an attribute with a similar name: `no_run`
121-
= help: the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
120+
= help: use `no_run` to compile, but not run, the code sample during testing
121+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
122122

123123
error: unknown attribute `test-harness`
124124
--> $DIR/check-attr-test.rs:26:1
@@ -130,8 +130,8 @@ error: unknown attribute `test-harness`
130130
30 | | /// ```
131131
| |_______^
132132
|
133-
= help: there is an attribute with a similar name: `test_harness`
134-
= help: the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
133+
= help: use `test_harness` to run functions marked `#[test]` instead of a potentially-implicit `main` function
134+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
135135

136136
error: unknown attribute `testharness`
137137
--> $DIR/check-attr-test.rs:26:1
@@ -143,8 +143,8 @@ error: unknown attribute `testharness`
143143
30 | | /// ```
144144
| |_______^
145145
|
146-
= help: there is an attribute with a similar name: `test_harness`
147-
= help: the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
146+
= help: use `test_harness` to run functions marked `#[test]` instead of a potentially-implicit `main` function
147+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
148148

149149
error: unknown attribute `tesT_harness`
150150
--> $DIR/check-attr-test.rs:26:1
@@ -156,8 +156,8 @@ error: unknown attribute `tesT_harness`
156156
30 | | /// ```
157157
| |_______^
158158
|
159-
= help: there is an attribute with a similar name: `test_harness`
160-
= help: the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
159+
= help: use `test_harness` to run functions marked `#[test]` instead of a potentially-implicit `main` function
160+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
161161

162162
error: aborting due to 12 previous errors
163163

Diff for: tests/rustdoc-ui/doctest/standalone-warning-2024.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test checks that it will output warnings for usage of `standalone` or `standalone_crate`.
2+
3+
//@ compile-flags:--test -Zunstable-options --edition 2024
4+
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
5+
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
//@ normalize-stdout-test: ".rs:\d+:\d+" -> ".rs:$$LINE:$$COL"
7+
8+
#![deny(warnings)]
9+
10+
//! ```standalone
11+
//! bla
12+
//! ```
13+
//!
14+
//! ```standalone-crate
15+
//! bla
16+
//! ```
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: unknown attribute `standalone`
2+
--> $DIR/standalone-warning-2024.rs:10:1
3+
|
4+
10 | / //! ```standalone
5+
11 | | //! bla
6+
12 | | //! ```
7+
13 | | //!
8+
14 | | //! ```standalone-crate
9+
15 | | //! bla
10+
16 | | //! ```
11+
| |_______^
12+
|
13+
= help: use `standalone_crate` to compile this code block separately
14+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
15+
note: the lint level is defined here
16+
--> $DIR/standalone-warning-2024.rs:8:9
17+
|
18+
8 | #![deny(warnings)]
19+
| ^^^^^^^^
20+
= note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]`
21+
22+
error: unknown attribute `standalone-crate`
23+
--> $DIR/standalone-warning-2024.rs:10:1
24+
|
25+
10 | / //! ```standalone
26+
11 | | //! bla
27+
12 | | //! ```
28+
13 | | //!
29+
14 | | //! ```standalone-crate
30+
15 | | //! bla
31+
16 | | //! ```
32+
| |_______^
33+
|
34+
= help: use `standalone_crate` to compile this code block separately
35+
= help: this code block may be skipped during testing, because unknown attributes are treated as markers for code samples written in other programming languages, unless it is also explicitly marked as `rust`
36+
37+
error: aborting due to 2 previous errors
38+

Diff for: tests/rustdoc-ui/doctest/standalone-warning.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This test checks that it will not output warning for usage of `standalone` or `standalone_crate`.
2+
//@ check-pass
3+
4+
//! ```standalone
5+
//! bla
6+
//! ```
7+
//!
8+
//! ```standalone-crate
9+
//! bla
10+
//! ```

0 commit comments

Comments
 (0)