Skip to content

Commit 0ee3efa

Browse files
committed
Stabilize the #[diagnostic] namespace and #[diagnostic::on_unimplemented] attribute
This PR stabilizes the `#[diagnostic]` attribute namespace and a minimal option of the `#[diagnostic::on_unimplemented]` attribute. The `#[diagnostic]` attribute namespace is meant to provide a home for attributes that allow users to influence error messages emitted by the compiler. The compiler is not guaranteed to use any of this hints, however it should accept any (non-)existing attribute in this namespace and potentially emit lint-warnings for unused attributes and options. This is meant to allow discarding certain attributes/options in the future to allow fundamental changes to the compiler without the need to keep then non-meaningful options working. The `#[diagnostic::on_unimplemented]` attribute is allowed to appear on a trait definition. This allows crate authors to hint the compiler to emit a specific error message if a certain trait is not implemented. For the `#[diagnostic::on_unimplemented]` attribute the following options are implemented: * `message` which provides the text for the top level error message * `label` which provides the text for the label shown inline in the broken code in the error message * `note` which provides additional notes. The `note` option can appear several times, which results in several note messages being emitted. If any of the other options appears several times the first occurrence of the relevant option specifies the actually used value. Any other occurrence generates an lint warning. For any other non-existing option a lint-warning is generated. All three options accept a text as argument. This text is allowed to contain format parameters referring to generic argument or `Self` by name via the `{Self}` or `{NameOfGenericArgument}` syntax. For any non-existing argument a lint warning is generated. Tracking issue: #111996
1 parent 039d887 commit 0ee3efa

27 files changed

+93
-273
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

-8
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
200200
);
201201
}
202202
}
203-
if !attr.is_doc_comment()
204-
&& let [seg, _] = attr.get_normal_item().path.segments.as_slice()
205-
&& seg.ident.name == sym::diagnostic
206-
&& !self.features.diagnostic_namespace
207-
{
208-
let msg = "`#[diagnostic]` attribute name space is experimental";
209-
gate!(self, diagnostic_namespace, seg.ident.span, msg);
210-
}
211203

212204
// Emit errors for non-staged-api crates.
213205
if !self.features.staged_api {

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ declare_features! (
144144
(accepted, derive_default_enum, "1.62.0", Some(86985)),
145145
/// Allows the use of destructuring assignments.
146146
(accepted, destructuring_assignment, "1.59.0", Some(71126)),
147+
/// Allows using the `#[diagnostic]` attribute tool namespace
148+
(accepted, diagnostic_namespace, "CURRENT_RUSTC_VERSION", Some(111996)),
147149
/// Allows `#[doc(alias = "...")]`.
148150
(accepted, doc_alias, "1.48.0", Some(50146)),
149151
/// Allows `..` in tuple (struct) patterns.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,6 @@ declare_features! (
436436
(unstable, deprecated_safe, "1.61.0", Some(94978)),
437437
/// Allows having using `suggestion` in the `#[deprecated]` attribute.
438438
(unstable, deprecated_suggestion, "1.61.0", Some(94785)),
439-
/// Allows using the `#[diagnostic]` attribute tool namespace
440-
(unstable, diagnostic_namespace, "1.73.0", Some(111996)),
441439
/// Controls errors in trait implementations.
442440
(unstable, do_not_recommend, "1.67.0", Some(51992)),
443441
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
//
201201
// Language features:
202202
// tidy-alphabetical-start
203+
#![cfg_attr(bootstrap, feature(diagnostic_namespace))]
203204
#![cfg_attr(not(bootstrap), feature(is_val_statically_known))]
204205
#![feature(abi_unadjusted)]
205206
#![feature(adt_const_params)]
@@ -221,7 +222,6 @@
221222
#![feature(const_trait_impl)]
222223
#![feature(decl_macro)]
223224
#![feature(deprecated_suggestion)]
224-
#![feature(diagnostic_namespace)]
225225
#![feature(doc_cfg)]
226226
#![feature(doc_cfg_hide)]
227227
#![feature(doc_notable_trait)]

src/doc/unstable-book/src/language-features/diagnostic-namespace.md

-84
This file was deleted.

tests/ui/diagnostic_namespace/existing_proc_macros.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(diagnostic_namespace)]
21
// check-pass
32
// aux-build:proc-macro-helper.rs
43

tests/ui/diagnostic_namespace/feature-gate-diagnostic_namespace.rs

-13
This file was deleted.

tests/ui/diagnostic_namespace/feature-gate-diagnostic_namespace.stderr

-37
This file was deleted.

tests/ui/diagnostic_namespace/non_existing_attributes_accepted.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(diagnostic_namespace)]
21
// check-pass
32
#[diagnostic::non_existing_attribute]
43
//~^WARN unknown diagnostic attribute

tests/ui/diagnostic_namespace/non_existing_attributes_accepted.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
warning: unknown diagnostic attribute
2-
--> $DIR/non_existing_attributes_accepted.rs:3:15
2+
--> $DIR/non_existing_attributes_accepted.rs:2:15
33
|
44
LL | #[diagnostic::non_existing_attribute]
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
88

99
warning: unknown diagnostic attribute
10-
--> $DIR/non_existing_attributes_accepted.rs:8:15
10+
--> $DIR/non_existing_attributes_accepted.rs:7:15
1111
|
1212
LL | #[diagnostic::non_existing_attribute(with_option = "foo")]
1313
| ^^^^^^^^^^^^^^^^^^^^^^

tests/ui/diagnostic_namespace/on_unimplemented/auxiliary/other.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(diagnostic_namespace)]
2-
31
#[diagnostic::on_unimplemented(
42
message = "Message",
53
note = "Note",

tests/ui/diagnostic_namespace/on_unimplemented/do_not_accept_options_of_the_internal_rustc_attribute.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(diagnostic_namespace)]
2-
31
#[diagnostic::on_unimplemented(
42
on(_Self = "&str"),
53
//~^WARN malformed `on_unimplemented` attribute

0 commit comments

Comments
 (0)