Skip to content

Diagnostics: show closure signatures (configurable)#20575

Open
ali90h wants to merge 2 commits intorust-lang:masterfrom
ali90h:fix/diagnostics-readable-closures
Open

Diagnostics: show closure signatures (configurable)#20575
ali90h wants to merge 2 commits intorust-lang:masterfrom
ali90h:fix/diagnostics-readable-closures

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.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I'm onboard with this, but I do prefer to keep the current style the default (can be done by making inlayHints_closureStyle be Option<ClosureStyle> then matching on that).

Also, maybe we should rename the setting given it's no longer inlay-hint-only? Not sure.

}
#[test]
fn shows_closure_signatures_in_mismatch() {
let mut config = crate::DiagnosticsConfig::test_sample();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Struct update syntax (Struct { ..FOO }) is useful here.

Comment on lines +1771 to +1776
closure_style: match self.inlayHints_closureStyle() {
ClosureStyle::ImplFn => hir::ClosureStyle::ImplFn,
ClosureStyle::RustAnalyzer => hir::ClosureStyle::RANotation,
ClosureStyle::WithId => hir::ClosureStyle::ClosureWithId,
ClosureStyle::Hide => hir::ClosureStyle::Hide,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since inlay hints also do this conversion, better to extract it into a function (or a From implementation).

@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2026

☔ The latest upstream changes (possibly #21804) made this pull request unmergeable. Please resolve the merge conflicts.

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