Skip to content

Confusing error message for impl Trait consts/statics that use generics #54560

Closed
@alexreg

Description

@alexreg

#53542 introduced impl Trait bindings in consts and statics (along with let bindings), but of course they cannot be assigned a value of a generic type. The error message is presently a bit confusing:

error[E0434]: can't capture dynamic environment in a fn item
  --> .../bindings.rs:4:29
   |
 4 |     const foo: impl Clone = x;
   |                             ^
   |
   = help: use the `|| { ... }` closure form instead

Example:

#![feature(impl_trait_in_bindings)]

fn a<T: Clone>(x: T) {
    const foo: impl Clone = x;
}

A message like cannot assign value of generic type to const or static of opaque type would be better, I think (unless there's already a suitable error that can be reused).

See also the bindings test from the above PR and its stderr.

CC @cramertj @oli-obk

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.F-impl_trait_in_bindings`#![feature(impl_trait_in_bindings)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions