rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.4k stars 12.73k forks source link

Internally change regions to be covariant #103676

Open lcnr opened 2 years ago

lcnr commented 2 years ago

THIS ISSUE IS ONLY ABOUT AN IMPLEMENTATION DETAIL OF RUSTC, IT DOES NOT AFFECT THE RUST LANGUAGE.

Right now regions in rustc are considered to be contravariant wherever they are used:

https://github.com/rust-lang/rust/blob/cdd7afeaadf1c48eafb4dff4452439fa5d13a775/compiler/rustc_middle/src/ty/relate.rs#L447-L454

https://github.com/rust-lang/rust/blob/cdd7afeaadf1c48eafb4dff4452439fa5d13a775/compiler/rustc_middle/src/ty/relate.rs#L492-L497

This is confusing and different from outlives, as 'a subtype 'b currently means 'b outlives 'a. Whenever we switch between "outlives" and "subtyping" for regions, we have to swap the order, e.g.

https://github.com/rust-lang/rust/blob/cdd7afeaadf1c48eafb4dff4452439fa5d13a775/compiler/rustc_infer/src/infer/outlives/env.rs#L137-L148

We've previously discussed this in https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/relating.20regions.20variance where we ended up agreeing that the variance should be changed to covariance for regions.

Doing so is non-trivial as my first attempt at this ended up missing at least one place resulting in hard to diagnose errors in core. I still want to see this fixed and would be up to review or mentor any work here. This requires some familiarity with the compiler (some knowledge about borrowck and/or the lexical regionck would be helpful) to correctly update relevant comments when doing this change.

edit: we also have to update the crate_variances query for this to work. probably that was the issue I encountered

aliemjay commented 1 year ago

@rustbot claim

lcnr commented 1 year ago

see the discussion starting in https://github.com/rust-lang/rust/pull/107339#issuecomment-1406354300

we still want to remove the current meaning of Sub inside of the region solver where 'a <= 'b if 'b: 'a