Skip to content

TAIT constrained by const generates wrong #[define_opaque] hint #138902

Closed
@narpfel

Description

@narpfel

Code

#![feature(type_alias_impl_trait)]

type Closure = impl Fn(u32) -> u32;

const ADDER: Closure = |x| x + 1;

Current output

error: unconstrained opaque type
 --> <source>:3:16
  |
3 | type Closure = impl Fn(u32) -> u32;
  |                ^^^^^^^^^^^^^^^^^^^
  |
  = note: `Closure` must be used in combination with a concrete type within the same crate

error[E0308]: mismatched types
 --> <source>:5:24
  |
3 | type Closure = impl Fn(u32) -> u32;
  |                ------------------- the expected opaque type
4 |
5 | const ADDER: Closure = |x| x + 1;
  |                        ^^^^^^^^^ expected opaque type, found closure
  |
  = note: expected opaque type `Closure`
                 found closure `{closure@<source>:5:24: 5:27}`
note: this item must have a `#[define_opaque(Closure)]` attribute to be able to define hidden types
 --> <source>:5:7
  |
5 | const ADDER: Closure = |x| x + 1;
  |       ^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

Desired output

[...]
note: only functions and methods can define opaque types
 --> <source>:5:7
  |
5 | const ADDER: Closure = |x| x + 1;
  |       ^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

Applying #[define_opaque] to the const item does nothing (and emits another error), so it should not be suggested.

Other cases

Rust Version

rustc 1.87.0-nightly (aa8f0fd71 2025-03-23)
binary: rustc
commit-hash: aa8f0fd7163a2f23aa958faed30c9c2b77b934a5
commit-date: 2025-03-23
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1

Anything else?

Godbolt link: https://godbolt.org/z/r1654KqG5

Metadata

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