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

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

alexreg opened this issue Sep 25, 2018 · 4 comments

Comments

@alexreg
Copy link
Contributor

@alexreg 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 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
@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Oct 1, 2018

duplicate of #51375 ?

@alexreg
Copy link
Contributor Author

@alexreg alexreg commented Oct 1, 2018

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

@estebank
Copy link
Contributor

@estebank estebank commented May 23, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants