rust-lang / rust-lang/rust

Type inference issues with well specified trait function call

Open
#135,698 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

While debugging a recent build failure in diesel I noticed that type inference fails in situation where the type is fully specified. I've build the following minimal example for this:

#[derive(Debug)]
struct Error;

trait FromSql<ST, DB>: Sized {
    fn from_sql(bytes: &[u8]) -> Result<Self, Error>;
}

struct UuidST;
struct Integer;
struct Pg;

#[derive(Debug, PartialEq)]
struct Uuid;

struct OtherType;

// remove this rather unrelated impl to fix the compilation
impl PartialEq<OtherType> for Uuid {
    fn eq(&self, other: &OtherType) -> bool {
        false
    }
}


impl FromSql<UuidST, Pg> for Uuid {
    fn from_sql(bytes: &[u8]) -> Result<Self, Error> {
        todo!()
    }
}

impl FromSql<Integer, Pg> for i32 {
    fn from_sql(bytes: &[u8]) -> Result<Self, Error> {
        todo!()
    }
}

fn main() {
    let input = &[];
    
    let out = FromSql::<UuidST, Pg>::from_sql(input).unwrap();

    assert_eq!(Uuid, out);
}

This fails to compile with the following error:

error[[E0790]](https://doc.rust-lang.org/nightly/error_codes/E0790.html): cannot call associated function on trait without specifying the corresponding `impl` type
  --> src/main.rs:43:15
   |
7  |     fn from_sql(bytes: &[u8]) -> Result<Self, Error>;
   |     ------------------------------------------------- `FromSql::from_sql` defined here
...
43 |     let out = FromSql::<UuidST, Pg>::from_sql(input).unwrap();
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
   |
help: use a fully-qualified path to one of the available implementations
   |
43 -     let out = FromSql::<UuidST, Pg>::from_sql(input).unwrap();
43 +     let out = <Uuid as FromSql<UuidST, Pg>>::from_sql(input).unwrap();
   |
43 -     let out = FromSql::<UuidST, Pg>::from_sql(input).unwrap();
43 +     let out = <i32 as FromSql<UuidST, Pg>>::from_sql(input).unwrap();
   |

This seems to be caused by the additional PartialEq implementation that breaks inferring the type of the out variable. For me it looks strange that rustc cannot infer that there is only a single possible impl that fulfills the given constraints, so it should be possible to infer the correct type here.

(Also even if that's desired behavior the suggestion is definitively off, as the second variant (that with i32 as FromSql<UuidST, Pg> would result in a compilation error.

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

Related: https://github.com/uuid-rs/uuid/issues/787

rustc --version --verbose:

Build using the Nightly version: 1.86.0-nightly

(2025-01-17 6067b36314ab5eb2eb47)

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 minimal example in src/main.rs with the stated nightly toolchain and compare behavior with and without the PartialEq implementation. Trace the type-inference and diagnostic path for the FromSql::<UuidST, Pg>::from_sql call; done means the fully specified call is handled consistently and any suggested fully qualified path names a valid implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.