rust-lang / rust-lang/rust

Integer method earlier than function with known type is called

Open
#161,090 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

fn show(n: u8) {
 println!("{n}")
}
fn main() {
 let mut n = 0;
 n = n.wrapping_add(1);
 show(n)
}

If show() is called later, compiler can't see the type for wrapping_add(). But if show() is earlier than wrapping_add():

fn show(n: u8) {
 println!("{n}")
}
fn main() {
 let mut n = 0;
 show(n);
 n = n.wrapping_add(1)
}

Compiler sees the type.

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

Start by compiling the two minimal Rust programs in the issue and comparing their diagnostics. Investigate how the compiler infers the integer type for wrapping_add and how the later call to show affects that inference. Done means equivalent code ordering no longer changes whether the method call receives a known 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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.