简体   繁体   中英

What algorithms does the Rust compiler use to infer lifetime variables?

fn foo<'a>(x: &'a i32, y: &'a i32) {}

fn main() { 
    let a = 123;
    {
        let b = 234;
        foo(&a, &b);
    }
}

In the code above &a and &b should hopefully be references with different lifetimes.

How does the compiler infer the lifetime var 'a for foo ? As far as I can tell, it's not using a standard Hindley-Milner unification algorithm. The lifetime must be the inner scope or some intersection of the two lifetimes.

Is lifetime inference a completely separate process to the standard type inference?

Does the compiler use intersection types or use some sub-type relationship between lifetimes to choose the most restricted lifetime?

Rust uses a modified Hindley-Milner unification algorithm because it has sub-typing relationships.

For example, &'static T is a sub-type of &'a T for any 'a .

Your case is relatively easy, when the compiler sees the call foo(&a, &b) it just unifies 'a as the most restrictive of both lifetimes (which is the intersection, since lifetimes are lexical for now).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM