]> git.lizzy.rs Git - rust.git/commit - compiler/rustc_trait_selection/src/traits/fulfill.rs
Rollup merge of #107339 - aliemjay:covariant, r=lcnr
authorMatthias Krüger <matthias.krueger@famsik.de>
Sat, 28 Jan 2023 04:20:18 +0000 (05:20 +0100)
committerGitHub <noreply@github.com>
Sat, 28 Jan 2023 04:20:18 +0000 (05:20 +0100)
commita5caa989c94f0a0ca3f977b013a1a7c2c2c5c3a0
tree58f37390e1e5146c188a26da7a508834ddf8d52e
parentfa2cd945af6bfb14c16cfaa8b74c636dfb821b48
parent43cb610464393640a56fd7aa528c1c8f5b33ad0d
Rollup merge of #107339 - aliemjay:covariant, r=lcnr

internally change regions to be covariant

Surprisingly, we consider the reference type `&'a T` to be contravaraint in its lifetime parameter. This is confusing and conflicts with the documentation we have in the reference, rustnomicon, and rustc-dev-guide. This also arguably not the correct use of terminology since we can use `&'static u8` in a place where `&' a u8` is expected, this implies that `&'static u8 <: &' a u8` and consequently `'static <: ' a`, hence covariance.

Because of this, when relating two types, we used to switch the argument positions in a confusing way:
`Subtype(&'a u8 <: &'b u8) => Subtype('b <: 'a) => Outlives('a: 'b) => RegionSubRegion('b <= 'a)`

The reason for the current behavior is probably that we wanted `Subtype('b <: 'a)` and `RegionSubRegion('b <= 'a)` to be equivalent, but I don' t think this is a good reason since these relations are sufficiently different in that the first is a relation in the subtyping lattice and is intrinsic to the type-systems, while the the second relation is an implementation detail of regionck.

This PR changes this behavior to use covariance, so..
`Subtype(&'a u8 <: &'b u8) => Subtype('a <: 'b) => Outlives('a: 'b) => RegionSubRegion('b <= 'a) `

Resolves #103676

r? `@lcnr`
compiler/rustc_hir_analysis/src/variance/constraints.rs
compiler/rustc_middle/src/ty/relate.rs