rust-lang / rust-lang/rust

Misleading diagnostic output for typecheck failures that involve type inference

Open
#132,165 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-inference D-confusing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e5cc14f0aef315131a9e430f0ff34b06

Summarized:

  • The input is a vector of schedule events: vec[x] contains the start time and end time for event x, and a target event number
  • Whenever an event begins, the lowest-numbered resource must be allocated to that event
  • The output is the ID number of the resource allocated to the target event
  • My implementation used two BinaryHeaps, one to track allocated resources, and a freelist to track available resources
  • The first heap's item type is (Reverse(end_time), resource_id), so the heap is ordered by time-of-release ascending (so pop()/peek() refer to the next event that will end)
  • The second heap's item type is Reverse(resource_id) so that pop yields the lowest-numbered resource
  • A missing Reverse() when pushing onto the second heap caused the first heap's type to incorrectly inferred
  • This causes a typecheck failure on a correct insert into the first heap
  • The diagnostic hint explaining the first heap's type inference points to a statement that could not have possibly contributed to type inference
Current output
error[E0308]: mismatched types
   --> main.rs:58:30
    |
33  |                             departures.pop();
    |                             ---------- here the type of `departures` is inferred to be `BinaryHeap<(Reverse<_>, Reverse<i32>)>`
...
58  |             departures.push( dep_item );
    |                        ----  ^^^^^^^^ expected `(Reverse<_>, Reverse<i32>)`, found `(Reverse<i32>, i32)`
    |                        |
    |                        arguments to this method are incorrect
    |
    = note: expected tuple `(Reverse<_>, Reverse<i32>)`
               found tuple `(Reverse<i32>, i32)`
note: method defined here
   --> C:\Users\Stevie-O\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\collections\binary_heap\mod.rs:616:12
    |
616 |     pub fn push(&mut self, item: T) {
    |            ^^^^
Desired output
error[E0308]: mismatched types
   --> main.rs:58:30
    |
29  |                    Some( &(Reverse(dt), seat_num) )
    |                           ----------------------- here the type of `departures` is inferred to be `BinaryHeap<(Reverse<_>, Reverse<i32>)>`
Rationale and extra context

I don't really know exactly what it should say, but I'm quite certain that it shouldn't say that the type of departures was inferred from the departures.pop() call, a statement from which virtually zero type information can be inferred.

If I could get anything I wanted plus a pony, it would have wanted it to point me to line 29, which is where I'm pretty sure that the actual type inference came from line 29:

Some( &(Reverse(dt), seat_num) )

And then point out that seat_num is a Reverse<i32> because of line 34:

free_seats.push(seat_num); // <- NOTE the missing Reverse() around seat_num

Getting all of that seems rather impractical; however, I expect that if it had told me that the second tuple element was inferred to be a Reverse<> from the match pattern, I would have spent a less time looking for the real bug, which was the erroneous statement above.

Other cases

No response

Rust Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-pc-windows-msvc
release: 1.82.0
LLVM version: 19.1.1

Anything else?

No response

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

Reproduce the linked Rust Playground example with rustc 1.82.0 and compare the current E0308 output with the requested output. Start from main.rs lines 29, 33, 34, and 58, then trace how the type-inference note is selected; done means the diagnostic points to the inference source rather than departures.pop().

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.