rust/library/core/src
Guillaume Gomez b0d7f2bb0e
Rollup merge of #119888 - weiznich:stablize_diagnostic_namespace, 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 #111996
2024-03-07 18:32:46 +01:00
..
alloc rename ptr::invalid -> ptr::without_provenance 2024-02-21 20:15:52 +01:00
array add missing PartialOrd impl doc for array 2024-03-06 10:28:56 +01:00
ascii implement Default for AsciiChar 2024-02-13 12:04:44 +01:00
async_iter Step all bootstrap cfgs forward 2024-02-08 07:44:34 -05:00
cell Add some optimizations 2023-10-13 14:54:33 +02:00
char Auto merge of #121138 - Swatinem:grapheme-extend-ascii, r=cuviper 2024-03-05 10:28:55 +00:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert include feedback from workingjubilee 2024-03-04 10:04:46 +01:00
ffi const_eval_select: make it safe but be careful with what we expose on stable for now 2024-03-02 16:09:31 +01:00
fmt Rollup merge of #121065 - CAD97:display-i18n, r=cuviper 2024-03-05 22:09:59 +01:00
future Use root obligation on E0277 for some cases 2024-03-03 18:53:35 +00:00
hash Allow newly added non_local_definitions in std 2024-02-17 13:59:46 +01:00
intrinsics remove platform-intrinsics ABI; make SIMD intrinsics be regular intrinsics 2024-02-25 08:14:52 +01:00
io Auto merge of #120741 - a1phyr:safe_buffer_advance, r=m-ou-se 2024-02-17 00:23:15 +00:00
iter Be more lax in .into_iter() suggestion when encountering Iterator methods on non-Iterator 2024-03-03 18:53:36 +00:00
macros Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
mem safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
net net: Add branch to Parser::read_number for parsing without checked 2024-03-04 18:46:09 -05:00
num const_eval_select: make it safe but be careful with what we expose on stable for now 2024-03-02 16:09:31 +01:00
ops Replace NonZero::<_>::new with NonZero::new. 2024-02-15 08:09:42 +01:00
panic Replace some usage of #[rustc_on_unimplemented] with 2024-01-05 15:23:09 +01:00
prelude Add Future and IntoFuture to the 2024 prelude 2024-02-18 23:20:05 +01:00
ptr Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
slice Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
str const_eval_select: make it safe but be careful with what we expose on stable for now 2024-03-02 16:09:31 +01:00
sync Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
task Auto merge of #119863 - tmiasko:will-wake, r=m-ou-se 2024-02-15 14:43:29 +00:00
unicode Bump Unicode to version 15.1.0, regenerate tables 2024-02-09 17:35:46 +01:00
any.rs update version placeholders 2023-12-22 11:01:42 +01:00
arch.rs Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
ascii.rs Use generic NonZero internally. 2024-02-15 08:09:42 +01:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs core is now compilable 2023-04-16 07:20:26 +00:00
borrow.rs doc: replace wrong punctuation mark 2023-07-28 14:46:17 +02:00
cell.rs Add rustc_confusables annotations to some stdlib APIs 2024-02-22 18:04:55 +00:00
clone.rs remove redundant imports 2023-12-10 10:56:22 +08:00
cmp.rs Rollup merge of #115386 - RalfJung:partial-eq-chain, r=dtolnay 2024-02-05 11:07:25 +01:00
default.rs implement Default for AsciiChar 2024-02-13 12:04:44 +01:00
error.md Fix minor grammar typo 2023-09-06 09:47:22 -07:00
error.rs style library/core/src/error.rs 2024-03-02 16:03:23 +08:00
escape.rs Use generic NonZero internally. 2024-02-15 08:09:42 +01:00
hint.rs Rewrite assert_unsafe_precondition around the new intrinsic 2024-02-08 11:52:14 -05:00
internal_macros.rs add track_caller for arith ops 2023-11-24 00:54:06 +08:00
intrinsics.rs Rollup merge of #121894 - RalfJung:const_eval_select, r=oli-obk 2024-03-05 22:10:01 +01:00
lib.rs Stabilize the #[diagnostic] namespace and #[diagnostic::on_unimplemented] attribute 2024-02-27 08:50:56 +01:00
marker.rs Step all bootstrap cfgs forward 2024-02-08 07:44:34 -05:00
option.rs Auto merge of #121454 - reitermarkus:generic-nonzero-library, r=dtolnay 2024-02-23 14:27:33 +00:00
panic.rs Convert debug_assert_nounwind to intrinsics::debug_assertions 2024-02-19 20:38:09 -05:00
panicking.rs const_eval_select: make it safe but be careful with what we expose on stable for now 2024-03-02 16:09:31 +01:00
pin.rs Rename pointer field on Pin 2024-01-16 14:58:42 -05:00
primitive.rs
primitive_docs.rs Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
result.rs Add flatmap/flat_map -> and_then suggestions 2024-02-22 18:05:28 +00:00
time.rs Rollup merge of #120307 - djc:duration-constructors, r=Mark-Simulacrum 2024-02-11 08:25:42 +01:00
tuple.rs Rollup merge of #118307 - scottmcm:tuple-eq-simpler, r=joshtriplett 2024-02-11 08:25:41 +01:00
unit.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00