Skip to content

Internally change regions to be covariant #103676

Description

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:

let region_bound = relation.with_cause(Cause::ExistentialRegionBound, |relation| {
relation.relate_with_variance(
ty::Contravariant,
ty::VarianceDiagInfo::default(),
a_region,
b_region,
)
})?;

let r = relation.relate_with_variance(
ty::Contravariant,
ty::VarianceDiagInfo::default(),
a_r,
b_r,
)?;

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.

OutlivesBound::RegionSubParam(r_a, param_b) => {
self.region_bound_pairs
.insert(ty::OutlivesPredicate(GenericKind::Param(param_b), r_a));
}
OutlivesBound::RegionSubProjection(r_a, projection_b) => {
self.region_bound_pairs
.insert(ty::OutlivesPredicate(GenericKind::Projection(projection_b), r_a));
}
OutlivesBound::RegionSubOpaque(r_a, def_id, substs) => {
self.region_bound_pairs
.insert(ty::OutlivesPredicate(GenericKind::Opaque(def_id, substs), r_a));
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-typesRelevant to the types 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