Skip to content

Conversation

ali90h
Copy link

@ali90h ali90h commented Aug 30, 2025

Type-mismatch messages now respect the existing rust-analyzer.inlayHints.closureStyle setting.Default stays {closure#…}. If users set ImplFn, diagnostics render impl Fn… signatures.

  • Add closure_style to DiagnosticsConfig (default ClosureWithId)
  • Use ctx.config.closure_style in the type-mismatch handler
  • Reuse the existing inlayHints.closureStyle (no new setting)
  • Add focused tests for both styles

Before / After

Example 1

Code

fn main() {
    let _: bool = || { 0u8 };
}

Before

error: expected bool, found {closure#12345}

After (with ImplFn):

error: expected bool, found impl Fn() -> u8

Example 2

Code

fn needs_f(_: impl FnMut(&i32) -> bool) {}
fn main() {
    let g = |_x: &u32| -> bool { true };
    needs_f(g);
}

Before

error: expected {closure#…}, found {closure#…}

After (with ImplFn):

error: expected impl FnMut(&i32) -> bool, found impl FnMut(&u32) -> bool

Refs #20555

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2025
@davidbarsky
Copy link
Contributor

Thanks! Instead of creating a crates/hir-ty/src/mir/lower/tests.rs, can you move the test to somewhere in crates/hir-ty/src/tests?

@ali90h ali90h force-pushed the fix/diagnostics-readable-closures branch from 2e4c11f to 5afd0b2 Compare August 30, 2025 18:35
@ChayimFriedman2
Copy link
Contributor

I personally think we should not switch this. Unlike other IDE features, the identity of a closure is important for mismatches (different closures are not the same).

But if we do, a setting most likely will make sense, like we do for inlay hints (rust-analyzer.inlayHints.closureStyle). We can also unify the settings.

@ali90h
Copy link
Author

ali90h commented Aug 30, 2025

Thanks! Instead of creating a crates/hir-ty/src/mir/lower/tests.rs, can you move the test to somewhere in crates/hir-ty/src/tests?

Thanks! That hir-ty test came from an upstream commit slipped into my branch. I’ve rebased; this PR now only changes ide-diagnostics and the focused test. No hir-ty files are part of this PR anymore.

…efault ClosureWithId; thread through CLI/bench initializers
@ali90h ali90h changed the title diagnostics: show closure signatures in type mismatch messages Diagnostics: show closure signatures (configurable) Aug 30, 2025
@ali90h
Copy link
Author

ali90h commented Aug 30, 2025

I personally think we should not switch this. Unlike other IDE features, the identity of a closure is important for mismatches (different closures are not the same).

But if we do, a setting most likely will make sense, like we do for inlay hints (rust-analyzer.inlayHints.closureStyle). We can also unify the settings.

Thank you, the update has been completed.
I'd love to hear your feedback.

No behavior change by default; setting ImplFn shows signatures in diagnostics.

@flodiebold
Copy link
Member

IMO we should just do whatever rustc does here?

@ali90h
Copy link
Author

ali90h commented Aug 31, 2025

IMO we should just do whatever rustc does here?

Checked against rustc: in the simple case it prints “closure”, and in bound cases it shows a fn(..) -> _ signature. So there isn’t one single format to match. This PR keeps diagnostics configurable; users can pick RANotation today for a rustc-like shape. If there’s agreement to make that the default, I can do it in a small follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants