rust-lang / rust-lang/rust

Unexpected behavior with type inference

Open
#134,854 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi Rust! 🦀

I tried this code:

struct Foo;
struct Target;

impl From<Target> for Foo {
    fn from(_t: Target) -> Self {
        Self
    }
}


fn may_use_some_type<T>(_t: T) where
    Foo: From<T>
{
}


fn use_another_type<T>(t: T) where
    Foo: From<T>
{
    let other_type = Target;
    may_use_some_type(other_type);
}

I expected to see this happen: rustc compiles the above code successfully.

Instead, this happened: rustc raises the following type error (rustc --crate-type lib test.rs):

error[E0308]: mismatched types
  --> test.rs:21:23
   |
17 | fn use_another_type<T>(t: T) where
   |                     - expected this type parameter
...
21 |     may_use_some_type(other_type);
   |     ----------------- ^^^^^^^^^^ expected type parameter `T`, found `Target`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected type parameter `T`
                      found struct `Target`
note: function defined here
  --> test.rs:11:4
   |
11 | fn may_use_some_type<T>(_t: T) where
   |    ^^^^^^^^^^^^^^^^^    -----

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

However, if we change the following:

    may_use_some_type(other_type);

to

    may_use_some_type::<Target>(other_type);

Then, it compiles successfully.

From my understanding, there is no link/dependency between the T of may_use_some_type and the one of use_another_type. Therefore I feel like the type inference system (whatever it is called) is mixing things here.

The where clause form Cond: T may be causing that bug (if it is a bug in the first place), because if we change it to something like T: Send, then it also works.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

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 behavior with the supplied test.rs example using rustc --crate-type lib test.rs, then compare implicit type inference with the explicit ::<Target> call. Investigate rustc's generic type inference and where-clause handling; done means the implicit call behaves as expected or the issue is documented as intentional.

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.