Skip to content

Commit 2222e68

Browse files
committed
Update diagnostic to use the attribute template
This updates it to use the attribute template format, and to split the behaviors of the different forms into different rules so they can be referenced.
1 parent 707abe3 commit 2222e68

File tree

1 file changed

+81
-13
lines changed

1 file changed

+81
-13
lines changed

src/attributes/diagnostics.md

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,87 @@ The *`deprecated` [attribute][attributes]* marks an item as deprecated.
316316
r[attributes.diagnostics.deprecated.syntax]
317317
The `deprecated` attribute has several forms:
318318
319-
- `deprecated` --- Issues a generic message.
320-
- `deprecated = "message"` --- Includes the given string in the deprecation message.
321-
- [MetaListNameValueStr] syntax with two optional fields:
322-
- `since` --- Specifies a version number when the item was deprecated. `rustc` does not currently interpret the string, but external tools like [Clippy] may check the validity of the value.
323-
- `note` --- Specifies a string that should be included in the deprecation message. This is typically used to provide an explanation about the deprecation and preferred alternatives.
319+
```grammar,attributes
320+
@root Deprecated ->
321+
`deprecated`
322+
| `deprecated` `=` (STRING_LITERAL | RAW_STRING_LITERAL)
323+
| `deprecated` `(` ( MetaNameValueStr (`,` MetaNameValueStr)* `,`? )? `)`
324+
```
325+
326+
- The [MetaWord] form generates a [generic message][attributes.diagnostics.deprecated.generic-message].
327+
- The [MetaNameValueStr] form allows you to [specify a message][attributes.diagnostics.deprecated.message].
328+
- The [MetaListNameValueStr] form allows you to [specify optional fields][attributes.diagnostics.deprecated.fields].
329+
330+
r[attributes.diagnostics.deprecated.allowed-positions]
331+
The `deprecated` attribute may be applied to:
332+
- [items]
333+
- [trait items]
334+
- [enum variants]
335+
- [struct fields]
336+
- [external block items]
337+
- [macro definitions]
324338

325-
r[attributes.diagnostic.deprecated.allowed-positions]
326-
The `deprecated` attribute may be applied to any [item], [trait item], [enum variant], [struct field], [external block item], or [macro definition]. It cannot be applied to [trait implementation items][trait-impl].
339+
It cannot be applied to [trait implementation items][trait-impl].
327340

328341
<!-- NOTE: It is only rejected for trait impl items
329342
(AnnotationKind::Prohibited). In all other locations, it is silently ignored.
330343
Tuple struct fields are ignored.
331344
-->
332345

346+
r[attributes.diagnostics.deprecated.duplicates]
347+
It is an error to specify the `deprecated` attribute multiple times on an item.
348+
349+
r[attributes.diagnostics.deprecated.behavior]
350+
A warning is emitted when a deprecated item is used.
351+
352+
<!-- TODO: Should we be more specific about what it means to be "used"? -->
353+
354+
> [!EXAMPLE]
355+
> ```rust
356+
> #[deprecated = "use `bar` instead"]
357+
> pub fn foo() {}
358+
>
359+
> fn main() {
360+
> foo(); // WARNING: deprecated
361+
> }
362+
> ```
363+
364+
> [!NOTE]
365+
> [Rustdoc] highlights when an item is deprecated, including the `since` and `note` if available.
366+
367+
r[attributes.diagnostics.deprecated.generic-message]
368+
The [MetaWord] form of the `deprecated` attribute generates a generic message in the diagnostic when the item is used.
369+
370+
> [!EXAMPLE]
371+
> ```rust
372+
> #[deprecated]
373+
> pub fn trim_left() {}
374+
> ```
375+
376+
r[attributes.diagnostics.deprecated.message]
377+
The [MetaNameValueStr] form of the `deprecated` attribute includes the given message in the diagnostic when the item is used. This is typically used to provide an explanation about the deprecation and preferred alternatives.
378+
379+
> [!EXAMPLE]
380+
> ```rust
381+
> #[deprecated = "use `trim_start` instead"]
382+
> pub fn trim_left() {}
383+
> ```
384+
385+
r[attributes.diagnostics.deprecated.fields]
386+
The [MetaListNameValueStr] form of the `deprecated` attribute allows you to specify two optional fields:
387+
388+
- `since` --- Specifies a version number when the item was deprecated.
389+
- `note` --- Specifies a string that should be included in the deprecation message. This is equivalent to the message specified in the [MetaNameValueStr form][attributes.diagnostics.deprecated.message].
390+
391+
> [!EXAMPLE]
392+
> ```rust
393+
> #[deprecated(since = "1.33.0", note = "use `trim_start` instead")]
394+
> pub fn trim_left() {}
395+
> ```
396+
397+
> [!NOTE]
398+
> `rustc` does not currently interpret the `since` string, but external tools like [Clippy] may check the validity of the value.
399+
333400
r[attributes.diagnostics.deprecated.containers]
334401
When `deprecated` is applied to an item containing other items, all child items inherit the deprecation attribute. This includes:
335402
@@ -675,27 +742,28 @@ The first error message includes a somewhat confusing error message about the re
675742
[call expression]: ../expressions/call-expr.md
676743
[crate root]: ../crates-and-source-files.md
677744
[dyn trait]: ../types/trait-object.md
678-
[enum variant]: ../items/enumerations.md
745+
[enum variants]: ../items/enumerations.md
679746
[enum]: ../items/enumerations.md
680747
[expression statement]: ../statements.md#expression-statements
681748
[expression]: ../expressions.md
682-
[external block item]: ../items/external-blocks.md
749+
[external block items]: ../items/external-blocks.md
683750
[external blocks]: ../items/external-blocks.md
684751
[functions]: ../items/functions.md
685752
[impl trait]: ../types/impl-trait.md
686753
[implementations]: ../items/implementations.md
687754
[item]: ../items.md
688755
[let statement]: ../statements.md#let-statements
689-
[macro definition]: ../macros-by-example.md
756+
[macro definitions]: ../macros-by-example.md
690757
[modules]: ../items/modules.md
691758
[rustc book]: ../../rustc/lints/index.html
692759
[rustc-lint-caps]: ../../rustc/lints/levels.html#capping-lints
693760
[rustc-lint-cli]: ../../rustc/lints/levels.html#via-compiler-flag
694-
[rustdoc]: ../../rustdoc/lints.html
695-
[struct field]: ../items/structs.md
761+
[rustdoc]: ../../rustdoc/index.html
762+
[rustdoc-lints]: ../../rustdoc/lints.html
763+
[struct fields]: ../items/structs.md
696764
[struct]: ../items/structs.md
697765
[trait declaration]: ../items/traits.md
698-
[trait item]: ../items/traits.md
766+
[trait items]: ../items/traits.md
699767
[trait-impl]: ../items/implementations.md#trait-implementations
700768
[traits]: ../items/traits.md
701769
[union]: ../items/unions.md

0 commit comments

Comments
 (0)