Skip to content

Commit dda31c8

Browse files
authored
Merge pull request #1754 from ehuss/note-blocks
Rework note blocks and change admonition rendering
2 parents 6e23c07 + dec2ef8 commit dda31c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+312
-280
lines changed

docs/authoring.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ Admonitions use a style similar to GitHub-flavored markdown, where the style nam
167167
```markdown
168168
> [!WARNING]
169169
> This is a warning.
170+
171+
> [!NOTE]
172+
> This is a note.
170173
```
171174

172-
All this does is apply a CSS class to the blockquote. You should define the color or style of the rule in the [`theme/reference.css`](https://github.com/rust-lang/reference/blob/master/theme/reference.css) file if it isn't already defined.
175+
The color and styling is defined in [`theme/reference.css`](https://github.com/rust-lang/reference/blob/master/theme/reference.css) and the transformation and icons are in [`mdbook-spec/src/lib.rs`](https://github.com/rust-lang/reference/blob/HEAD/mdbook-spec/src/lib.rs).
173176

174177
## Style
175178

mdbook-spec/src/lib.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,38 @@ impl Spec {
144144
/// > ...
145145
/// ```
146146
///
147-
/// This will add a `<div class="warning">` around the blockquote so that
148-
/// it can be styled differently. Any text between the brackets that can
149-
/// be a CSS class is valid. The actual styling needs to be added in a CSS
150-
/// file.
151-
fn admonitions(&self, chapter: &Chapter) -> String {
147+
/// This will add a `<div class="alert alert-warning">` around the
148+
/// blockquote so that it can be styled differently, and injects an icon.
149+
/// The actual styling needs to be added in the `reference.css` CSS file.
150+
fn admonitions(&self, chapter: &Chapter, diag: &mut Diagnostics) -> String {
152151
ADMONITION_RE
153152
.replace_all(&chapter.content, |caps: &Captures<'_>| {
154153
let lower = caps["admon"].to_lowercase();
155154
let term = to_initial_case(&caps["admon"]);
156155
let blockquote = &caps["blockquote"];
157156
let initial_spaces = blockquote.chars().position(|ch| ch != ' ').unwrap_or(0);
158157
let space = &blockquote[..initial_spaces];
158+
// These icons are from GitHub, MIT License, see https://github.com/primer/octicons
159+
let svg = match lower.as_str() {
160+
"note" => "<path d=\"M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z\"></path>",
161+
"warning" => "<path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path>",
162+
_ => {
163+
warn_or_err!(
164+
diag,
165+
"admonition `{lower}` in {:?} is incorrect or not yet supported",
166+
chapter.path.as_ref().unwrap()
167+
);
168+
""
169+
}
170+
};
159171
format!(
160-
"{space}<div class=\"{lower}\">\n\
172+
"{space}<div class=\"alert alert-{lower}\">\n\
161173
\n\
162-
{space}> ***{term}:***\n\
174+
{space}> <p class=\"alert-title\">\
175+
<svg viewBox=\"0 0 16 16\" width=\"18\" height=\"18\">\
176+
{svg}\
177+
</svg>{term}</p>\n\
178+
{space} >\n\
163179
{blockquote}\n\
164180
\n\
165181
{space}</div>\n",
@@ -226,7 +242,7 @@ impl Preprocessor for Spec {
226242
if ch.is_draft_chapter() {
227243
return;
228244
}
229-
ch.content = self.admonitions(&ch);
245+
ch.content = self.admonitions(&ch, &mut diag);
230246
ch.content = self.auto_link_references(&ch, &rules);
231247
ch.content = self.render_rule_definitions(&ch.content, &tests, &git_ref);
232248
if ch.name == "Test summary" {

src/attributes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ struct S {
227227
pub fn f() {}
228228
```
229229

230-
> Note: `rustc` currently recognizes the tools "clippy", "rustfmt", "diagnostic",
231-
> "miri" and "rust_analyzer".
230+
> [!NOTE]
231+
> `rustc` currently recognizes the tools "clippy", "rustfmt", "diagnostic", "miri" and "rust_analyzer".
232232
233233
r[attributes.builtin]
234234
## Built-in attributes index

src/attributes/codegen.md

+14-21
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ The *`inline` [attribute]* suggests that a copy of the attributed function
2626
should be placed in the caller, rather than generating code to call the
2727
function where it is defined.
2828

29-
> ***Note***: The `rustc` compiler automatically inlines functions based on
30-
> internal heuristics. Incorrectly inlining functions can make the program
31-
> slower, so this attribute should be used with care.
29+
> [!NOTE]
30+
> The `rustc` compiler automatically inlines functions based on internal heuristics. Incorrectly inlining functions can make the program slower, so this attribute should be used with care.
3231
3332
r[attributes.codegen.inline.modes]
3433
There are three ways to use the inline attribute:
@@ -39,8 +38,8 @@ There are three ways to use the inline attribute:
3938
* `#[inline(never)]` *suggests* that an inline expansion should never be
4039
performed.
4140

42-
> ***Note***: `#[inline]` in every form is a hint, with no *requirements*
43-
> on the language to place a copy of the attributed function in the caller.
41+
> [!NOTE]
42+
> `#[inline]` in every form is a hint, with no *requirements* on the language to place a copy of the attributed function in the caller.
4443
4544
r[attributes.codegen.cold]
4645
### The `cold` attribute
@@ -229,8 +228,8 @@ Reference Manual], or elsewhere on [developer.arm.com].
229228
[ARM Architecture Reference Manual]: https://developer.arm.com/documentation/ddi0487/latest
230229
[developer.arm.com]: https://developer.arm.com
231230

232-
> ***Note***: The following pairs of features should both be marked as enabled
233-
> or disabled together if used:
231+
> [!NOTE]
232+
> The following pairs of features should both be marked as enabled or disabled together if used:
234233
> - `paca` and `pacg`, which LLVM currently implements as one feature.
235234
236235

@@ -390,10 +389,8 @@ r[attributes.codegen.target_feature.remark-rt]
390389
See the [`is_x86_feature_detected`] or [`is_aarch64_feature_detected`] macros
391390
in the standard library for runtime feature detection on these platforms.
392391

393-
> Note: `rustc` has a default set of features enabled for each target and CPU.
394-
> The CPU may be chosen with the [`-C target-cpu`] flag. Individual features
395-
> may be enabled or disabled for an entire crate with the
396-
> [`-C target-feature`] flag.
392+
> [!NOTE]
393+
> `rustc` has a default set of features enabled for each target and CPU. The CPU may be chosen with the [`-C target-cpu`] flag. Individual features may be enabled or disabled for an entire crate with the [`-C target-feature`] flag.
397394
398395
r[attributes.codegen.track_caller]
399396
## The `track_caller` attribute
@@ -427,11 +424,11 @@ fn f() {
427424
}
428425
```
429426

430-
> Note: `core` provides [`core::panic::Location::caller`] for observing caller locations. It wraps
431-
> the [`core::intrinsics::caller_location`] intrinsic implemented by `rustc`.
427+
> [!NOTE]
428+
> `core` provides [`core::panic::Location::caller`] for observing caller locations. It wraps the [`core::intrinsics::caller_location`] intrinsic implemented by `rustc`.
432429
433-
> Note: because the resulting `Location` is a hint, an implementation may halt its walk up the stack
434-
> early. See [Limitations](#limitations) for important caveats.
430+
> [!NOTE]
431+
> Because the resulting `Location` is a hint, an implementation may halt its walk up the stack early. See [Limitations](#limitations) for important caveats.
435432
436433
#### Examples
437434

@@ -504,12 +501,8 @@ appears to observers to have been called at the attributed function's definition
504501
caller information across virtual calls. A common example of this coercion is the creation of a
505502
trait object whose methods are attributed.
506503

507-
> Note: The aforementioned shim for function pointers is necessary because `rustc` implements
508-
> `track_caller` in a codegen context by appending an implicit parameter to the function ABI, but
509-
> this would be unsound for an indirect call because the parameter is not a part of the function's
510-
> type and a given function pointer type may or may not refer to a function with the attribute. The
511-
> creation of a shim hides the implicit parameter from callers of the function pointer, preserving
512-
> soundness.
504+
> [!NOTE]
505+
> The aforementioned shim for function pointers is necessary because `rustc` implements `track_caller` in a codegen context by appending an implicit parameter to the function ABI, but this would be unsound for an indirect call because the parameter is not a part of the function's type and a given function pointer type may or may not refer to a function with the attribute. The creation of a shim hides the implicit parameter from callers of the function pointer, preserving soundness.
513506
514507
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
515508
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu

src/attributes/debugger.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ r[attributes.debugger.collapse_debuginfo.default]
173173
The `external` behavior is the default for macros that don't have this attribute, unless they are built-in macros.
174174
For built-in macros the default is `yes`.
175175

176-
> **Note**: `rustc` has a `-C collapse-macro-debuginfo` CLI option to override both the default collapsing behavior and `#[collapse_debuginfo]` attributes.
176+
> [!NOTE]
177+
> `rustc` has a `-C collapse-macro-debuginfo` CLI option to override both the default collapsing behavior and `#[collapse_debuginfo]` attributes.
177178
178179
```rust
179180
#[collapse_debuginfo(yes)]

src/attributes/diagnostics.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ r[attributes.diagnostics.lint.forbid]
3737
* `#[forbid(C)]` is the same as `deny(C)`, but also forbids changing the lint
3838
level afterwards,
3939

40-
> Note: The lint checks supported by `rustc` can be found via `rustc -W help`,
41-
> along with their default settings and are documented in the [rustc book].
40+
> [!NOTE]
41+
> The lint checks supported by `rustc` can be found via `rustc -W help`, along with their default settings and are documented in the [rustc book].
4242
4343
```rust
4444
pub mod m1 {
@@ -98,9 +98,8 @@ pub mod m3 {
9898
}
9999
```
100100

101-
> Note: `rustc` allows setting lint levels on the
102-
> [command-line][rustc-lint-cli], and also supports [setting
103-
> caps][rustc-lint-caps] on the lints that are reported.
101+
> [!NOTE]
102+
> `rustc` allows setting lint levels on the [command-line][rustc-lint-cli], and also supports [setting caps][rustc-lint-caps] on the lints that are reported.
104103
105104
r[attributes.diagnostics.lint.reason]
106105
### Lint Reasons
@@ -222,8 +221,8 @@ pub fn another_example() {
222221
}
223222
```
224223

225-
> Note: The behavior of `#[expect(unfulfilled_lint_expectations)]` is currently
226-
> defined to always generate the `unfulfilled_lint_expectations` lint.
224+
> [!NOTE]
225+
> The behavior of `#[expect(unfulfilled_lint_expectations)]` is currently defined to always generate the `unfulfilled_lint_expectations` lint.
227226
228227
r[attributes.diagnostics.lint.group]
229228
### Lint groups
@@ -297,7 +296,8 @@ fn foo() {
297296
}
298297
```
299298

300-
> Note: `rustc` currently recognizes the tool lints for "[clippy]" and "[rustdoc]".
299+
> [!NOTE]
300+
> `rustc` currently recognizes the tool lints for "[clippy]" and "[rustdoc]".
301301
302302
r[attributes.diagnostics.deprecated]
303303
## The `deprecated` attribute
@@ -433,10 +433,8 @@ impl Trait for i32 {
433433
r[attributes.diagnostics.must_use.trait-impl-function]
434434
When used on a function in a trait implementation, the attribute does nothing.
435435

436-
> Note: Trivial no-op expressions containing the value will not violate the
437-
> lint. Examples include wrapping the value in a type that does not implement
438-
> [`Drop`] and then not using that type and being the final expression of a
439-
> [block expression] that is not used.
436+
> [!NOTE]
437+
> Trivial no-op expressions containing the value will not violate the lint. Examples include wrapping the value in a type that does not implement [`Drop`] and then not using that type and being the final expression of a [block expression] that is not used.
440438
>
441439
> ```rust
442440
> #[must_use]
@@ -452,8 +450,8 @@ When used on a function in a trait implementation, the attribute does nothing.
452450
> };
453451
> ```
454452
455-
> Note: It is idiomatic to use a [let statement] with a pattern of `_`
456-
> when a must-used value is purposely discarded.
453+
> [!NOTE]
454+
> It is idiomatic to use a [let statement] with a pattern of `_` when a must-used value is purposely discarded.
457455
>
458456
> ```rust
459457
> #[must_use]
@@ -558,7 +556,8 @@ r[attributes.diagnostic.do_not_recommend]
558556
r[attributes.diagnostic.do_not_recommend.intro]
559557
The `#[diagnostic::do_not_recommend]` attribute is a hint to the compiler to not show the annotated trait implementation as part of a diagnostic message.
560558

561-
> **Note**: Suppressing the recommendation can be useful if you know that the recommendation would normally not be useful to the programmer. This often occurs with broad, blanket impls. The recommendation may send the programmer down the wrong path, or the trait implementation may be an internal detail that you don't want to expose, or the bounds may not be able to be satisfied by the programmer.
559+
> [!NOTE]
560+
> Suppressing the recommendation can be useful if you know that the recommendation would normally not be useful to the programmer. This often occurs with broad, blanket impls. The recommendation may send the programmer down the wrong path, or the trait implementation may be an internal detail that you don't want to expose, or the bounds may not be able to be satisfied by the programmer.
562561
>
563562
> For example, in an error message about a type not implementing a required trait, the compiler may find a trait implementation that would satisfy the requirements if it weren't for specific bounds in the trait implementation. The compiler may tell the user that there is an impl, but the problem is the bounds in the trait implementation. The `#[diagnostic::do_not_recommend]` attribute can be used to tell the compiler to *not* tell the user about the trait implementation, and instead simply tell the user the type doesn't implement the required trait.
564563

src/attributes/limits.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ r[attributes.limits.recursion_limit.syntax]
1616
It uses the [_MetaNameValueStr_]
1717
syntax to specify the recursion depth.
1818

19-
> Note: The default in `rustc` is 128.
19+
> [!NOTE]
20+
> The default in `rustc` is 128.
2021
2122
```rust,compile_fail
2223
#![recursion_limit = "4"]
@@ -44,7 +45,8 @@ r[attributes.limits.type_length_limit]
4445
## The `type_length_limit` attribute
4546

4647

47-
> **Note**: This limit is only enforced when the nightly `-Zenforce-type-length-limit` flag is active.
48+
> [!NOTE]
49+
> This limit is only enforced when the nightly `-Zenforce-type-length-limit` flag is active.
4850
>
4951
> For more information, see <https://github.com/rust-lang/rust/pull/127670>.
5052
@@ -56,7 +58,8 @@ r[attributes.limits.type_length_limit.syntax]
5658
It is applied at the [crate] level, and uses the [_MetaNameValueStr_] syntax
5759
to set the limit based on the number of type substitutions.
5860

59-
> Note: The default in `rustc` is 1048576.
61+
> [!NOTE]
62+
> The default in `rustc` is 1048576.
6063
6164
```rust,ignore
6265
#![type_length_limit = "4"]

src/attributes/testing.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Test functions must be free, monomorphic functions that take no arguments, and t
2626
<!-- If the previous section needs updating (from "must take no arguments"
2727
onwards, also update it in the crates-and-source-files.md file -->
2828

29-
> Note: The test mode is enabled by passing the `--test` argument to `rustc`
30-
> or using `cargo test`.
29+
> [!NOTE]
30+
> The test mode is enabled by passing the `--test` argument to `rustc` or using `cargo test`.
3131
3232
r[attributes.testing.test.success]
3333
The test harness calls the returned value's [`report`] method, and classifies the test as passed or failed depending on whether the resulting [`ExitCode`] represents successful termination.
@@ -69,8 +69,8 @@ fn mytest() {
6969
}
7070
```
7171

72-
> **Note**: The `rustc` test harness supports the `--include-ignored` flag to
73-
> force ignored tests to be run.
72+
> [!NOTE]
73+
> The `rustc` test harness supports the `--include-ignored` flag to force ignored tests to be run.
7474
7575
r[attributes.testing.should_panic]
7676
## The `should_panic` attribute

src/behavior-considered-undefined.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ r[undefined.runtime]
101101
* For assumptions specifically related to unwinding, see the [panic documentation][unwinding-ffi].
102102
* The runtime assumes that a Rust stack frame is not deallocated without executing destructors for local variables owned by the stack frame. This assumption can be violated by C functions like `longjmp`.
103103

104-
> **Note**: Undefined behavior affects the entire program. For example, calling
105-
> a function in C that exhibits undefined behavior of C means your entire
106-
> program contains undefined behaviour that can also affect the Rust code. And
107-
> vice versa, undefined behavior in Rust can cause adverse affects on code
108-
> executed by any FFI calls to other languages.
104+
> [!NOTE]
105+
> Undefined behavior affects the entire program. For example, calling a function in C that exhibits undefined behavior of C means your entire program contains undefined behaviour that can also affect the Rust code. And vice versa, undefined behavior in Rust can cause adverse affects on code executed by any FFI calls to other languages.
109106
110107
r[undefined.pointed-to]
111108
### Pointed-to bytes
@@ -237,8 +234,8 @@ r[undefined.validity.valid-range]
237234
* If a type has a custom range of a valid values, then a valid value must be in that range.
238235
In the standard library, this affects [`NonNull<T>`] and [`NonZero<T>`].
239236

240-
> **Note**: `rustc` achieves this with the unstable
241-
> `rustc_layout_scalar_valid_range_*` attributes.
237+
> [!NOTE]
238+
> `rustc` achieves this with the unstable `rustc_layout_scalar_valid_range_*` attributes.
242239
243240
r[undefined.validity.undef]
244241
**Note:** Uninitialized memory is also implicitly invalid for any type that has

src/comments.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ modules that occupy a source file.
7171
r[comments.doc.bare-crs]
7272
The character `U+000D` (CR) is not allowed in doc comments.
7373

74-
> **Note**: It is conventional for doc comments to contain Markdown, as expected by
75-
> `rustdoc`. However, the comment syntax does not respect any internal Markdown.
76-
> ``/** `glob = "*/*.rs";` */`` terminates the comment at the first `*/`, and the
77-
> remaining code would cause a syntax error. This slightly limits the content of
78-
> block doc comments compared to line doc comments.
74+
> [!NOTE]
75+
> It is conventional for doc comments to contain Markdown, as expected by `rustdoc`. However, the comment syntax does not respect any internal Markdown. ``/** `glob = "*/*.rs";` */`` terminates the comment at the first `*/`, and the remaining code would cause a syntax error. This slightly limits the content of block doc comments compared to line doc comments.
7976
80-
> **Note**: The sequence `U+000D` (CR) immediately followed by `U+000A` (LF) would have been previously transformed into a single `U+000A` (LF).
77+
> [!NOTE]
78+
> The sequence `U+000D` (CR) immediately followed by `U+000A` (LF) would have been previously transformed into a single `U+000A` (LF).
8179
8280
## Examples
8381

0 commit comments

Comments
 (0)