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

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

Open
alexreg opened this issue Sep 25, 2018 · 4 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-enhancement Category: An issue proposing an enhancement or a PR with one. F-impl_trait_in_bindings `#![feature(impl_trait_in_bindings)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexreg
Copy link
Contributor

alexreg commented Sep 25, 2018

#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

@alexreg
Copy link
Contributor Author

alexreg commented Sep 25, 2018

In fact, when trying to use a generic-typed const in a fn, the error messages aren't very good either!

Playground

Errors:

error[E0401]: can't use type parameters from outer function
 --> src/lib.rs:3:16
  |
2 | fn a<T: Clone>(x: T) {
  |    - - type variable from outer function
  |    |
  |    try adding a local type parameter in this method instead
3 |     const foo: T = x;
  |                ^ use of type variable from outer function

error[E0434]: can't capture dynamic environment in a fn item
 --> src/lib.rs:3:20
  |
3 |     const foo: T = x;
  |                    ^
  |
  = help: use the `|| { ... }` closure form instead

error: aborting due to 2 previous errors

@Havvy Havvy added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. labels Sep 25, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Oct 1, 2018

duplicate of #51375 ?

@alexreg
Copy link
Contributor Author

alexreg commented Oct 1, 2018

@oli-obk At the very least, I think it's closely related.

@estebank
Copy link
Contributor

Current output:

error[E0435]: attempt to use a non-constant value in a constant
 --> src/lib.rs:5:29
  |
5 |     const foo: impl Clone = x;
  |                             ^ non-constant value

@Centril Centril added F-impl_trait_in_bindings `#![feature(impl_trait_in_bindings)]` requires-nightly This issue requires a nightly compiler in some way. labels Jul 28, 2019
@crlf0710 crlf0710 added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 11, 2020
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 A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-enhancement Category: An issue proposing an enhancement or a PR with one. F-impl_trait_in_bindings `#![feature(impl_trait_in_bindings)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants