Skip to content

Type inference issues with well specified trait function call #135698

Open
@weiznich

Description

@weiznich

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: uuid-rs/uuid#787

rustc --version --verbose:

Build using the Nightly version: 1.86.0-nightly

(2025-01-17 6067b36314ab5eb2eb47)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions