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 · 3 comments

Comments

Projects
None yet
3 participants
@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

This comment has been minimized.

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
@oli-obk

This comment has been minimized.

Copy link
Contributor

oli-obk commented Oct 1, 2018

duplicate of #51375 ?

@alexreg

This comment has been minimized.

Copy link
Contributor Author

alexreg commented Oct 1, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment