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

"error[E0435]: attempt to use a non-constant value in a constant" shows wrong fix with mut variables #91560

Closed
yedayak opened this issue Dec 5, 2021 · 1 comment · Fixed by #91607
Assignees
Labels
C-bug Category: This is a bug.

Comments

@yedayak
Copy link
Contributor

yedayak commented Dec 5, 2021

I tried this code:

fn main() {
    let mut length: usize = 2;
    let arr = [0; length];
}

I expected to see this happen: Get an error about using non constant values in a constant, with a help pointer showing where to change "let" to "const", like in this example without the mut keyword:

2 |     let length: usize = 2;
  |     ---------- help: consider using `const` instead of `let`: `const length`

Instead, this happened: I get the correct error, but it points to a wrong place to fix, off by one character:

2 |     let mut length: usize = 2;
  |      ------------- help: consider using `const` instead of `let`: `const length`

This means auto correcting it using the rust-analyzer vscode plugin produces invalid code:

lconst length: usize = 2;

Meta

rustc --version --verbose:

rustc 1.59.0-nightly (efec54529 2021-12-04)
binary: rustc
commit-hash: efec545293b9263be9edfb283a7aa66350b3acbf
commit-date: 2021-12-04
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0
@yedayak yedayak added the C-bug Category: This is a bug. label Dec 5, 2021
@FabianWolff
Copy link
Contributor

The same problem occurs when there is additional whitespace:

fn main() {
    let   length: usize = 2;
    let arr = [0; length];
}
error[E0435]: attempt to use a non-constant value in a constant
 --> t2.rs:3:19
  |
2 |     let   length: usize = 2;
  |       ---------- help: consider using `const` instead of `let`: `const length`
3 |     let arr = [0; length];
  |                   ^^^^^^ non-constant value

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 30, 2022
… r=jackh726

Make `span_extend_to_prev_str()` more robust

Fixes rust-lang#91560. The logic in `span_extend_to_prev_str()` is currently quite brittle and fails if there is extra whitespace or something else in between, and it also should return an `Option` but doesn't currently.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 11, 2022
… r=jackh726

Make `span_extend_to_prev_str()` more robust

Fixes rust-lang#91560. The logic in `span_extend_to_prev_str()` is currently quite brittle and fails if there is extra whitespace or something else in between, and it also should return an `Option` but doesn't currently.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 11, 2022
… r=jackh726

Make `span_extend_to_prev_str()` more robust

Fixes rust-lang#91560. The logic in `span_extend_to_prev_str()` is currently quite brittle and fails if there is extra whitespace or something else in between, and it also should return an `Option` but doesn't currently.
@bors bors closed this as completed in b7c48b4 Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants