Skip to content

Closure formatting in diagnostics can be a bit verbose #148236

Description

@jieyouxu

Consider inputs like

//! Test that closures only implement `Clone` if all captured values implement `Clone`.
, the diagnostics is:

error[E0277]: the trait bound `NonClone: Clone` is not satisfied in `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`
--> $DIR/closure-clone-requires-captured-clone.rs:17:13
|
LL | let closure = move || {
| ------- within this `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`
...
LL | closure.clone();
| ^^^^^ within `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`, the trait `Clone` is not implemented for `NonClone`

The message mentioning the closure uses a format like

 LL |     let closure = move || { 
    |                   ------- within this `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}` 

which feels quite verbose. I guess the span [lo, hi) (in terms of (start, end) line/col) and file path do make it clear which closure is being referred to when there are multiple closures. So not a priority, just tracking this in case there's ways to perhaps make this a bit more concise without losing info -- or assess if that info is actually fully needed).

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on Oct 29, 2025
  2. changed the title [-]Closures[/-] [+]Closure formatting in diagnostics can be a bit verbose[/+] on Oct 29, 2025
  3. jieyouxu commented on Oct 29, 2025

    @jieyouxu
    MemberAuthor

    (I'm almost certain there's an earlier issue or two about this, but I couldn't find it based on a cursory search.)

  4. added
    A-closuresArea: Closures (`|…| { … }`)
    A-prettyArea: Pretty printing (including `-Z unpretty`)
    on Oct 29, 2025
  5. estebank commented on Oct 31, 2025

    @estebank
    Contributor
  6. xonx4l commented on Nov 5, 2025

    @xonx4l
    Contributor

    @jieyouxu I think we can set a global path for multiple closures in order to reduce repetitiveness?

  7. jieyouxu commented on Nov 5, 2025

    @jieyouxu
    MemberAuthor

    @jieyouxu I think we can set a global path for multiple closures in order to reduce repetitiveness?

    That won't work if you have multiple crates as the context, I think? Haven't thought too hard about it.

  8. estebank commented on Nov 5, 2025

    @estebank
    Contributor

    I just realized that there's a closure format that I haven't seen discussed before: when the closure is from the current file, use {closure@13:19: 13:26}. We already shorten their names in some cases to not include the full path and only print the file name and byte range. At that point, if we really want to shorten them, we can do {closure@13:19}, but I fear removing the file name will be confusing for people (and I don't think we have the machinery to realize what file we're printing a type for).

  9. xonx4l commented on Nov 6, 2025

    @xonx4l
    Contributor

    I just realized that there's a closure format that I haven't seen discussed before: when the closure is from the current file, use {closure@13:19: 13:26}. We already shorten their names in some cases to not include the full path and only print the file name and byte range. At that point, if we really want to shorten them, we can do {closure@13:19}, but I fear removing the file name will be confusing for people (and I don't think we have the machinery to realize what file we're printing a type for).

    To build on your idea, if we are going to shorten it, I think {closure@13:19} is better than {closure@13:19: 13:26} but If the closure is in a different file this becomes ambiguous.

  10. jieyouxu commented on Nov 6, 2025

    @jieyouxu
    MemberAuthor

    I just realized that there's a closure format that I haven't seen discussed before: when the closure is from the current file, use {closure@13:19: 13:26}. We already shorten their names in some cases to not include the full path and only print the file name and byte range. At that point, if we really want to shorten them, we can do {closure@13:19}, but I fear removing the file name will be confusing for people (and I don't think we have the machinery to realize what file we're printing a type for).

    Yeah, the trade-off is between having more info but appearing a bit verbose, or showing less info but risk losing useful context. Particularly because closures are anonymous, and so can only be identified uniquely through their file path + span info.

    Between appearing a bit verbose and losing context, I think retaining sufficient context is more important, because otherwise the user would have to reverse engineer which closure we are talking about.

    (This is issue was mostly that I was wondering if we could somehow shorten the representation without (substantial) loss of info, but I guess I don't see how yet.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-prettyArea: Pretty printing (including `-Z unpretty`)D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions