rust-lang / rust-lang/rust

Support `#[diagnostic::on_unimplemented]` on methods

Open
#148,512 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics F-on_unimplemented P-low T-compiler WG-diagnostics
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code

struct SetFoo;
struct SetBar;

struct S<Foo, Bar>(Foo, Bar);
impl S<SetFoo, ()> {
    #[diagnostic::on_unimplemented(message= "builder with missing mandatory fields", label="you must set every mandatory field")]
    fn build() {}
}

fn main() {
    S::<(), SetBar>::build();
}

should let you customize the output of E0599. Currently, it is

error[E0599]: no function or associated item named `build` found for struct `S<(), SetBar>` in the current scope
  --> src/main.rs:11:22
   |
 4 | struct S<Foo, Bar>(Foo, Bar);
   | ------------------ function or associated item `build` not found for this struct
...
11 |     S::<(), SetBar>::build();
   |                      ^^^^^ function or associated item not found in `S<(), SetBar>`
   |
   = note: the function or associated item was found for
           - `S<SetFoo, ()>`

but it should allow you to output

error[E0599]: builder with missing mandatory fields
  --> src/main.rs:11:22
   |
 4 | struct S<Foo, Bar>(Foo, Bar);
   | ------------------ function or associated item `build` not found for this struct
...
11 |     S::<(), SetBar>::build();
   |                      ^^^^^ you must set every mandatory field
   |
  = note: function or associated item not found in `S<(), SetBar>`
   = note: the function or associated item was found for
           - `S<SetFoo, ()>`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Rust example and inspect how E0599 is produced for an associated item that exists only on another instantiation. Trace how #[diagnostic::on_unimplemented] is handled for traits, then determine the corresponding method behavior; done means the example emits the requested custom message, label, and remaining notes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.