Skip to content

Diagnostic misclassifies a complex expression as a unit enum variant when flagging an attempt to call a non-function #99240

Closed
@fmease

Description

@fmease

Given the following code:

fn fmt(it: &(std::cell::Cell<Option<impl FnOnce()>>,)) {
    (it.0.take())()
}

The current output is:

error[E0618]: expected function, found enum variant `(it.0.take())`
  --> src/lib.rs:22:5
   |
22 |     (it.0.take())()
   |     ^^^^^^^^^^^^^--
   |     |
   |     call expression requires function
   |
help: `(it.0.take())` is a unit variant, you need to write it without the parentheses
   |
22 -     (it.0.take())()
22 +     (it.0.take())
   |

  • It classifies the complex expression (it.0.take()) as a (unit) enum variant in both the message and the note which is obviously incorrect.
  • It does not mention anywhere that the expression is of type Option<_> which would've been really helpful. Looking at the current output, it's really hard to see why the expression is not callable.

( As an aside, one actual fix for the given code is the following: it.0.take().unwrap()() )

Link to the playground containing the reproducer, an even smaller reproducer and the original code for context.
I decided to select the slightly larger reproducer (containing the 1-tuple) for this issue instead of the smaller one from the playground (which can probably still be shrunk further) to really highlight the complex expression: (it.0.take()) over just it.take().

Reproduces on stable and on nightly.

@rustbot label C-bug D-incorrect D-confusing

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-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