Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
narpfel opened this issue Mar 24, 2025 · 0 comments · Fixed by #138911
Closed

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

narpfel opened this issue Mar 24, 2025 · 0 comments · Fixed by #138911
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@narpfel
Copy link
Contributor

narpfel commented Mar 24, 2025

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

@narpfel narpfel added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2025
@bors bors closed this as completed in 5bd69d9 Mar 26, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 26, 2025
Rollup merge of rust-lang#138911 - compiler-errors:define-opaque, r=oli-obk

Allow defining opaques in statics and consts

r? oli-obk

Fixes rust-lang#138902
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant