Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use covariance on type relations of field projection types if possible #107969

Merged
merged 2 commits into from
Feb 20, 2023

Conversation

b-naber
Copy link
Contributor

@b-naber b-naber commented Feb 12, 2023

It's fine to use covariance here unless we're in a mutating context.

Fixes #96514

Supersedes #105958

r? @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2023
@b-naber b-naber force-pushed the proj-relate-variance branch from e3e7055 to 6e1d228 Compare February 12, 2023 22:18
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks really nice 👍 🔥

r=me after nit

fn get_ambient_variance(&self, context: PlaceContext) -> ty::Variance {
match context {
PlaceContext::MutatingUse(_) => ty::Invariant,
PlaceContext::NonMutatingUse(_) | PlaceContext::NonUse(_) => ty::Covariant,
Copy link
Contributor

@lcnr lcnr Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's NonUseContext::AscribeUserTy which I feel should maybe be invariant 🤔

I can also imagine us adding NonMutatingUse in the future which should be invariant. Can you also exhaustively match on the NonMutatingUseContext and probably make all NonUse invariant. Don't know how to write an example where Covariance is needed here.

Considering that covariance can be unsound if used incorrectly, we should be as defensive as possible here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late answer.

We would need NonUseContext::AscribeUserTy for field projections on enums in match statements where we always insert user type projections I believe, e.g.

struct Inv<'a>(&'a mut &'a ());
enum Foo<T> {
    Bar,
    Var(T),
}
type Supertype = Foo<for<'a> fn(Inv<'a>, Inv<'a>)>;

fn foo_nested(x: Foo<Foo<for<'a, 'b> fn(Inv<'a>, Inv<'b>)>>) {
    match x {
        Foo::Bar => {}
        Foo::Var(Supertype::Bar) => {}
        Foo::Var(Supertype::Var(x)) => {}
    }
}

would not compile if we would use ty::Invariant on NonUse(AscribeUserTy).

I agree that we should be very conservative here, but I also can't see how using covariance here might be unsound. Can you show that it is? I've kept this as covariant for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my worry is that we use AscribeUserTy in a position which isn't covariant. E.g. only for arguments of a function we're calling. Thinking more about it I actually can't imagine that happening 🤔

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AscribeUserType statement contains a variance. Should it be carried by NonUseContext::AscribeUserTy for use here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think so 👍 while I don't think is exploitable as we only use contravariance in AscribeUserType for places without any projections. Using the following in super_ascribe_user_ty does not cause any failures

                if variance != $(& $mutability)? ty::Variance::Covariant && !place.projection.is_empty() {
                    bug!()
                }

we should still add the variance to the use context though as this isn't something I want to implicitly rely on.

@lcnr
Copy link
Contributor

lcnr commented Feb 13, 2023

@bors try @rust-timer queue

probably neutral

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 13, 2023
@bors
Copy link
Contributor

bors commented Feb 13, 2023

⌛ Trying commit 6e1d228 with merge 746726458a663cc90ceb903387bfb3974d85ef19...

@bors
Copy link
Contributor

bors commented Feb 13, 2023

☀️ Try build successful - checks-actions
Build commit: 746726458a663cc90ceb903387bfb3974d85ef19 (746726458a663cc90ceb903387bfb3974d85ef19)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (746726458a663cc90ceb903387bfb3974d85ef19): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.2%] 4
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [1.8%, 3.0%] 2
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.1% [-1.1%, -1.1%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 13, 2023
@lcnr
Copy link
Contributor

lcnr commented Feb 15, 2023

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 15, 2023
@lcnr
Copy link
Contributor

lcnr commented Feb 20, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Feb 20, 2023

📌 Commit 758cc95 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 20, 2023
@bors
Copy link
Contributor

bors commented Feb 20, 2023

⌛ Testing commit 758cc95 with merge e7eaed2...

@bors
Copy link
Contributor

bors commented Feb 20, 2023

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing e7eaed2 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 20, 2023
@bors bors merged commit e7eaed2 into rust-lang:master Feb 20, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 20, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e7eaed2): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the type of ProjectionElem::Field is not correct wrt subtyping
6 participants