Skip to content

Trying to capture a local variable in a const results in an unrelated error #51375

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

Closed
varkor opened this issue Jun 5, 2018 · 9 comments
Closed
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

@varkor
Copy link
Member

varkor commented Jun 5, 2018

fn main() {
    let x: u8 = 5;
    const Y: u8 = x;
}

results in:

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

error: aborting due to previous error

(which of course doesn't work — actually carrying out the suggestion results in the same suggestion).

@hellow554
Copy link
Contributor

I tried to use different stable compilers to see, if there's an regression. Long long time ago, in the dark ages of 2016, there was a good error message.

the good

# rustc --version
rustc 1.10.0 (cfcb716cf 2016-07-03)
op@VBOX /t/t/foo> rustc src/main.rs --crate-type bin 
src/main.rs:3:19: 3:20 error: attempt to use a non-constant value in a constant [E0435]
src/main.rs:3     const Y: u8 = x;
                                ^
src/main.rs:3:19: 3:20 help: run `rustc --explain E0435` to see a detailed explanation
error: aborting due to previous error
op@VBOX /t/t/foo> rustc --explain E0435
A non-constant value was used to initialise a constant. Example of erroneous
code:

```
let foo = 42u32;
const FOO : u32 = foo; // error: attempt to use a non-constant value in a
                       //        constant
```

To fix this error, please replace the value with a constant. Example:

```
const FOO : u32 = 42u32; // ok!
```

Or:

```
const OTHER_FOO : u32 = 42u32;
const FOO : u32 = OTHER_FOO; // ok!
```

the bad

# rustc --version
rustc 1.15.0 (10893a9a3 2017-01-19)
# rustc src/main.rs --crate-type bin 
error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:19
  |
3 |     const Y: u8 = x;
  |                   ^ non-constant used with constant

error: aborting due to previous error

the ugly

# rustc --version
rustc 1.20.0 (f3d6973f4 2017-08-27)
# rustc src/main.rs --crate-type bin 
error[E0434]: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
 --> src/main.rs:3:19
  |
3 |     const Y: u8 = x;
  |                   ^

error: aborting due to previous error

@hellow554
Copy link
Contributor

hellow554 commented Jun 6, 2018

Doing a bisect right now. Let's see what happend. (cargo-bisect-rustc sucks :| )
Somewhere between 1.11.0 and 1.12.0
Somewhere between 080e0e0...576f766

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 6, 2018
@hellow554
Copy link
Contributor

fad4f32c3147cca9f2b107d0440065b9e3d6c10f is the first bad commit
commit fad4f32c3147cca9f2b107d0440065b9e3d6c10f
Author: Jonathan Turner <[email protected]>
Date:   Fri Aug 5 15:58:31 2016 -0700

    Turn on new errors, json mode. Remove duplicate unicode test

@kennytm kennytm added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Jun 17, 2018
@nikomatsakis nikomatsakis added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Jun 21, 2018
@nikomatsakis
Copy link
Contributor

This is not a regression. This error message is old as dirt. That said, I've always found it confusing. I think we should brainstorm an improved message for this case (where a nested item captures something from an outer item).

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jun 21, 2018

I would suggest a note

error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:3:19
  |
3 |     const Y: u8 = x;
  |                   ^ did you mean `Y`?
  |
  | note: the variable `x` defined in the surrounding fn is not in scope within a nested `const`

or maybe something like

error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:3:19
  |
2 |     let x: u32 = 22;
  |         - note: this `x` is not in scope within the nested `const`
3 |     const Y: u8 = x;
  |                   ^ did you mean `Y`?

@nikomatsakis
Copy link
Contributor

cc @estebank

@estebank
Copy link
Contributor

error[E0425]: cannot find value `x` in `const` scope
 --> src/main.rs:3:19
  |
2 |     let x: u32 = 22;
  |         - this binding is not in scope for `const` bindings
3 |     const Y: u8 = x;
  |                   ^ not found in `const` scope
  = note: `const` bindings can't access non-`const` bindings, like `x`

@hellow554
Copy link
Contributor

I don't see, why this is an improvement to the old error message, especially to the explanation E0435 (see my comment).
binding and scope may be the right technical terms, but sound not intuitive at all.
But I agree, that attempt to use a non-constant value in a constant sounds incomplete and in a constant scope/environment sounds better to my (german) ears, but I like the wording "attempt to use".

@estebank
Copy link
Contributor

estebank commented May 3, 2019

I believe the current output is good enough:

error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:19
  |
3 |     const Y: u8 = x;
  |                   ^ non-constant value

@varkor varkor closed this as completed May 3, 2019
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

No branches or pull requests

6 participants