rustc since 1.36 accepts invalid code after return
Open
Nobody has claimed this yet.
C-discussion
T-compiler
T-lang
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
I tried this code:
fn foo() -> u8 {
return 0;
let a: u8 = 1;
a = 2; // error: second write to an immutable variable
return a;
}
fn main() -> () {
println!("{}", foo());
}
rustc-1.35 rejects the code and says:
error[E0384]: cannot assign twice to immutable variable `a`
--> <source>:4:5
|
3 | let a: u8 = 1;
| - first assignment to `a`
4 | a = 2;
| ^^^^^ cannot assign twice to immutable variable
Current rustc accepts the code
I acknowledge that the invalid write happens in a dead region, but still report it as all rustc versions reject the following code:
fn foo() -> u8 {
if true {
return 0;
}
let a: u8 = 1;
a = 2; // error: second write to an immutable variable
return a;
}
fn main() -> () {
println!("{}", foo());
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the two Rust reproductions with rustc 1.35 and a current compiler, comparing diagnostics for the unreachable assignment. Trace the compiler's unreachable-code and immutable-assignment checks to find where the regression was introduced. Done means the invalid write is rejected again, with a regression test covering both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100