Skip to content

let (_ = 3; causes suggestion to remove a paren that is only hypothetical #103497

Closed

Description

Given the following code (playground):

fn main() {
    let (_ = 3;
}

The current output is:

error: expected one of `)`, `,`, or `|`, found `=`
 --> es4.rs:2:9
  |
2 |     let (_ = 3;
  |         ^ -^
  |         | |
  |         | help: `)` may belong here
  |         unclosed delimiter

error: expected expression, found `)`
 --> es4.rs:3:1
  |
3 | }
  | ^ expected expression

warning: unnecessary parentheses around pattern
 --> es4.rs:2:9
  |
2 |     let (_ = 3;
  |         ^ ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     let (_ = 3;
2 +     let _ = 3;
  |

The final warning, "unnecessary parentheses around pattern", is sus:

  • The compiler shouldn't be so sure that the parens are "unnecessary" when the compiler had to guess where the end-paren was
  • In the suggestion, the second span is empty (the location at which a hypothetical end-paren might be missing) and its suggested replacement is also empty (the lack of a paren).
    • This is verifiable with --error-format=json.
    • The second yellow caret-arrow, appearing to point to the space after the underscore, is a visual sign that something is not quite right.
    • In rustc builds with debug_assert enables, this trips a recently-added assertion.
thread 'rustc' panicked at 'Span must not be empty and have no suggestion', compiler/rustc_errors/src/diagnostic.rs:570:9

Found by fuzzing with a modified fuzz-rustc, combined with this very reasonable new assertion in rustc (added by @kper in #102922).

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

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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