Draft of a type inference algorithm - Shiroy/intellij-rust GitHub Wiki

Crate items

Modules

Modules have no type

Extern crate declaration

Extern crate declaration have no type

Use declaration

Use declarations have no type

Functions declaration / Generic functions declaration

fn foo(x1 : T1, x2: T2, ..., xn: Tn) -> T { value }

Functions declaration and Generic functions declaration does not have a type

Constraints

  • typeof(value) = T

Diverging function declaration

fn foo(x1 : T1, x2: T2, ..., xn: Tn) -> !

Diverging function declaration does not have a type

Extern functions decalaration

extern fn "ABI" foo(x1 : T1, x2: T2, ..., xn: Tn) -> T { value }

This does not have a type Constraints

  • typeof(value) = T

Type aliases

type A = B

Constraints

  • A = B

Struct declaration

Struct declarations have no types

struct S {
    f1: T1,
    f2: T2,
    ...
    fn: TN
}

Constraint

  • S becomes a new type
  • If S implement a trait T, there is an implicit conversion of S to T

Enumeration declaration

enum E {
    V1,
    V2,
    ...
    Vn
}

Constraints

  • E becomes a new type
  • For all i from 1 to N
    • typeof(Vi) = (x1 : T1, x2: T2, ..., xn: Tn) -> E

Constant items declaration

const x: T = value

Constant items declaration does not have type

Constraints

  • typeof(value) = T

Static items declaration

static x: T = value

Static items declaration does not have type

Constraints

  • typeof(value) = T

Trait declaration

trait T {

}

Trait declaration does not have type

Constraints

  • T become a new type
  • If T implement S then T there is an implicit conversion of T to S

Implementation

Implementation does not have a type

Struct implementation

struct S {}

impl S {
    fn foo(self: T1, x2: T2, ...) -> R {}
}

Constraint

  • if a function contains a self argument, then typeof(self) = S

Trait implementation

struct S {}

trait T {}

impl T for S {
    fn foo(self: T1, x2: T2, ...) -> R {}
}

Constraint

  • if a function contains a self argument, then typeof(self) = S

External blocks

External blocks does not have a type