Skip to content

Commit

Permalink
Rollup merge of rust-lang#119888 - weiznich:stablize_diagnostic_names…
Browse files Browse the repository at this point in the history
…pace, r=compiler-errors

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.

This allows to have a trait definition like:

```rust
#[diagnostic::on_unimplemented(
    message = "My Message for `ImportantTrait<{A}>` is not implemented for `{Self}`",
    label = "My Label",
    note = "Note 1",
    note = "Note 2"
)]
trait ImportantTrait<A> {}

```

which then generates for the following code

```rust
fn use_my_trait(_: impl ImportantTrait<i32>) {}

fn main() {
    use_my_trait(String::new());
}
```

this error message:

```
error[E0277]: My Message for `ImportantTrait<i32>` is not implemented for `String`
  --> src/main.rs:14:18
   |
14 |     use_my_trait(String::new());
   |     ------------ ^^^^^^^^^^^^^ My Label
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `ImportantTrait<i32>` is not implemented for `String`
   = note: Note 1
   = note: Note 2
```

[Playground with the unstable feature](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=05133acce8e1d163d481e97631f17536)

Fixes rust-lang#111996
  • Loading branch information
matthiaskrgr committed Mar 6, 2024
2 parents 6f857a3 + d013b5a commit 400410e
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 273 deletions.
8 changes: 0 additions & 8 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
);
}
}
if !attr.is_doc_comment()
&& let [seg, _] = attr.get_normal_item().path.segments.as_slice()
&& seg.ident.name == sym::diagnostic
&& !self.features.diagnostic_namespace
{
let msg = "`#[diagnostic]` attribute name space is experimental";
gate!(self, diagnostic_namespace, seg.ident.span, msg);
}

// Emit errors for non-staged-api crates.
if !self.features.staged_api {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ declare_features! (
(accepted, derive_default_enum, "1.62.0", Some(86985)),
/// Allows the use of destructuring assignments.
(accepted, destructuring_assignment, "1.59.0", Some(71126)),
/// Allows using the `#[diagnostic]` attribute tool namespace
(accepted, diagnostic_namespace, "CURRENT_RUSTC_VERSION", Some(111996)),
/// Allows `#[doc(alias = "...")]`.
(accepted, doc_alias, "1.48.0", Some(50146)),
/// Allows `..` in tuple (struct) patterns.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,6 @@ declare_features! (
(unstable, deprecated_safe, "1.61.0", Some(94978)),
/// Allows having using `suggestion` in the `#[deprecated]` attribute.
(unstable, deprecated_suggestion, "1.61.0", Some(94785)),
/// Allows using the `#[diagnostic]` attribute tool namespace
(unstable, diagnostic_namespace, "1.73.0", Some(111996)),
/// Controls errors in trait implementations.
(unstable, do_not_recommend, "1.67.0", Some(51992)),
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(diagnostic_namespace))]
#![cfg_attr(bootstrap, feature(platform_intrinsics))]
#![feature(abi_unadjusted)]
#![feature(adt_const_params)]
Expand All @@ -223,7 +224,6 @@
#![feature(const_trait_impl)]
#![feature(decl_macro)]
#![feature(deprecated_suggestion)]
#![feature(diagnostic_namespace)]
#![feature(doc_cfg)]
#![feature(doc_cfg_hide)]
#![feature(doc_notable_trait)]
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion tests/ui/diagnostic_namespace/existing_proc_macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(diagnostic_namespace)]
//@ check-pass
//@ aux-build:proc-macro-helper.rs

Expand Down
13 changes: 0 additions & 13 deletions tests/ui/diagnostic_namespace/feature-gate-diagnostic_namespace.rs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(diagnostic_namespace)]
//@ check-pass
#[diagnostic::non_existing_attribute]
//~^WARN unknown diagnostic attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
warning: unknown diagnostic attribute
--> $DIR/non_existing_attributes_accepted.rs:3:15
--> $DIR/non_existing_attributes_accepted.rs:2:15
|
LL | #[diagnostic::non_existing_attribute]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default

warning: unknown diagnostic attribute
--> $DIR/non_existing_attributes_accepted.rs:8:15
--> $DIR/non_existing_attributes_accepted.rs:7:15
|
LL | #[diagnostic::non_existing_attribute(with_option = "foo")]
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(diagnostic_namespace)]

#[diagnostic::on_unimplemented(
message = "Message",
note = "Note",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(diagnostic_namespace)]

#[diagnostic::on_unimplemented(
on(_Self = "&str"),
//~^WARN malformed `on_unimplemented` attribute
Expand Down

0 comments on commit 400410e

Please sign in to comment.