Skip to content

Closure with a typed function argument and top level struct constructor break in nightly #107461

Closed
@nicopap

Description

@nicopap

Code

I tried this code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7f3bdb07bcc3a0a30d2ef80cf0adfd22

let closure = |f: fn(&f32) -> f32| MyStruct { x: f(&y) };

This code should compile.

Instead of compiling, the compiler generates the following error:

error: anonymous enums are not supported
 --> src/main.rs:8:38
  |
8 |     let closure = |f: fn(&f32) -> f32| MyStruct { x: f(&y) };
  |                                   ---^ --------
  |
  = help: create a named `enum` and use it here instead:
          enum Name {
              Variant1(f32),
              Variant2(MyStruct),
          }

error: expected one of `!`, `(`, `,`, `::`, `<`, or `|`, found `{`
 --> src/main.rs:8:49
  |
8 |     let closure = |f: fn(&f32) -> f32| MyStruct { x: f(&y) };
  |                                                 ^ expected one of `!`, `(`, `,`, `::`, `<`, or `|`

error: could not compile `playground` due to 2 previous errors

Version it worked on

It most recently worked on: (1.67)

Version with regression

Breaks in latest nightly 1.69.0-nightly (2023-01-29 e972bc8)

Related:

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.P-criticalCritical priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-untriagedUntriaged performance or correctness regression.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions