rust-lang / rust-lang/rust

Conditional assignment with multiple if/else appears broken

Open
#144,911 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When attempting a conditional assignment after a statement containing an early return, the compiler seems to think that the assignment type is a return type when using if/else, but this doesn't occur when using the an equivalent match with conditionals.

Example with failing and working code here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ad9db532628940de405ab21ff4a5a3a4

The playground example error message doesn't show this, but when working on my code, the error message references the early return line. So it would say something like "note: return type inferred to be () here" and reference line 8 in the example. Unfortunately, I can't reproduce that error message hint, nor can I share my code.

This occurs on stable and nightly.

EDIT:
This actually appears to be simpler and not require an early return.

Simpler example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8d8d4dada286cad5934bc728c9ea92c4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run both linked Rust Playground examples first and compare the conditional-assignment behavior with the equivalent match expression. Trace the compiler's type inference and diagnostic handling from the minimized reproduction; done means the discrepancy is reproduced in a regression test and the conditional form receives the correct inferred type.

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
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.