wrong suggestion when deriving clone for struct containing `dyn Fn`

Aperta Adatta ai principianti
#166,979 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Inizia leggendo il codice diagnostico di derive-macro e i test per i suggerimenti E0277 che riguardano gli oggetti trait Fn; l’issue fornisce una riproduzione minima e il suggerimento errato. Aggiungi un test di regressione per il campo della struct mostrato qui, poi esegui il test diagnostico pertinente. Il lavoro è completo quando il suggerimento è assente in questo caso e la diagnostica continua a segnalare l’implementazione mancante di Clone.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-diagnostics A-dyn-trait A-macros A-suggestion-diagnostics D-invalid-suggestion T-compiler
Code
#[derive(Clone)]
struct S {
    f: Box<dyn Fn()>,
}
Current output
error[E0277]: the trait bound `dyn Fn(): Clone` is not satisfied
 --> src/lib.rs:3:5
  |
1 | #[derive(Clone)]
  |          ----- in this derive macro expansion
2 | struct S {
3 |     f: Box<dyn Fn()>,
  |     ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `dyn Fn()`
  |
  = note: required for `Box<dyn Fn()>` to implement `Clone`
help: use parentheses to call this trait object
  |
3 |     f: Box<dyn Fn()>(),
  |                     ++
Desired output
error[E0277]: the trait bound `dyn Fn(): Clone` is not satisfied
 --> src/lib.rs:3:5
  |
1 | #[derive(Clone)]
  |          ----- in this derive macro expansion
2 | struct S {
3 |     f: Box<dyn Fn()>,
  |     ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `dyn Fn()`
  |
  = note: required for `Box<dyn Fn()>` to implement `Clone`
Rationale and extra context

Applying the suggestion results in a syntax error:

error: expected `,`, or `}`, found `(`
 --> src/lib.rs:3:21
  |
2 | struct S {
  |        - while parsing this struct
3 |     f: Box<dyn Fn()>(),
  |                     ^
Other cases

Rust Version
rustc 1.100.0 (0ef1bc386 2026-11-09)
binary: rustc
commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
commit-date: 2026-11-09
host: x86_64-unknown-linux-gnu
release: 1.100.0
LLVM version: 23.1.5
Anything else?

No response

Lingua principale
Rust
Stelle
123k
Fork
16.6k
Merge medio
2g 17h
PR unite (30g)
515

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.