rust-lang / rust-lang/rust

Call to function broken by unrelated constraint on generic function containing the call

Open
#125,274 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-system C-bug S-has-mcve T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm a bit new to Rust; sorry if the following is just my misunderstanding.

I tried this code (play):

pub trait MyTrait {
}

// Implement `MyTrait` for `&String`
impl<'a> MyTrait for &'a String {
}

// Function taking a reference to some `T` for which `&T` implements `MyTrait`
//
// So, eg, passing a `&String` (implying `T=String`) is valid
pub fn call_me<T>(_: &T) where for<'a> &'a T: MyTrait {}

// Sure enough, calling `call_me()` with a `&String` works (here in a function generic over some `U`)
pub fn call_compiles<U>() {
   call_me(&String::new());
}

// Do exactly the same (but coincidentally in a function that constrains `U` such that `&U` implements `MyTrait`
//
// This does not compile.
pub fn call_does_not_compile<U>() where for<'b> &'b U: MyTrait {
   call_me(&String::new()); // <---- ****** THIS CALL FAILS ******
}

// It can be made to work by explicitly telling it that matching `&T` to &String implies T=String
pub fn call_can_be_made_to_work<U>() where for<'b> &'b U: MyTrait {
   call_me::<String>(&String::new());
}

I expected to see the compiler accept this code. Instead, I get this error:

error[E0308]: mismatched types
  --> src/lib.rs:22:10
   |
21 | pub fn call_does_not_compile<U>() where for<'b> &'b U: MyTrait {
   |                              - expected this type parameter
22 |     call_me(&String::new()); // <---- ****** THIS CALL FAILS ******
   |     ------- ^^^^^^^^^^^^^^ expected `&U`, found `&String`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected reference `&U`
              found reference `&String`
note: function defined here
  --> src/lib.rs:11:8
   |
11 | pub fn call_me<T>(_: &T) where for<'a> &'a T: MyTrait {}
   |        ^^^^^^^    -----

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

The Playground shows this happening on nightly too.

The error message suggests it thinks call_me() should be passed a &U, but &U has nothing to do with call_me() or the &String it's being passed.

Thank you very much for all work on Rust.

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 in src/lib.rs on stable and nightly, focusing on call_does_not_compile and its generic where-clause. Investigate generic argument inference around the unrelated constraint; done when the call is accepted without explicitly specifying String and the regression is covered by a test.

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.