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

Don't deduce a signature that makes a closure cyclic #105409

Merged
merged 3 commits into from
Jan 5, 2023

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Dec 7, 2022

Sometimes when elaborating supertrait bounds for closure signature inference, we end up deducing a closure signature that is cyclical because either a parameter or the return type references a projection mentioning Self that also has escaping bound vars, which means that it's not eagerly replaced with an inference variable.

Interestingly, this is not just related to my PR that elaborates supertrait bounds for closure signature deduction. The committed test supertrait-hint-cycle-3.rs shows stable code that is fixed by this PR:

trait Foo<'a> {
    type Input;
}

impl<F: Fn(u32)> Foo<'_> for F {
    type Input = u32;
}

fn needs_super<F: for<'a> Fn(<F as Foo<'a>>::Input) + for<'a> Foo<'a>>(_: F) {}

fn main() {
    needs_super(|_: u32| {});
}

Fixes #105401
Fixes #105396

r? types

@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 Dec 7, 2022
@compiler-errors
Copy link
Member Author

compiler-errors commented Dec 7, 2022

This also doesn't fix #97680, but it makes it possible to compile with the standard "closure wasn't smart enough at inferring higher-ranked lifetime" trick:

use std::future::Future;

fn hrc<
    R,
    F: for<'a> AsyncClosure<'a, (), R>
        + for<'a> Fn(&'a ()) -> <F as AsyncClosure<'a, (), R>>::Fut,
>(
    f: F,
) -> F {
    f
}

fn main() {
-    hrc(|x| async { });
+    hrc(|x: &_| async { });
}

trait AsyncClosure<'a, I, R>: Fn(&'a I) -> Self::Fut
where
    I: 'a,
{
    type Fut: Future<Output = R> + Send + 'a;
}

impl<'a, I, R, Fut, F> AsyncClosure<'a, I, R> for F
where
    I: 'a,
    F: Fn(&'a I) -> Fut,
    Fut: Future<Output = R> + Send + 'a,
{
    type Fut = Fut;
}

Without this PR, no amount of fiddling with the closure makes this compile as far as I can tell.

@spastorino
Copy link
Member

r? types

@rustbot rustbot assigned jackh726 and unassigned spastorino Dec 12, 2022
@bors
Copy link
Contributor

bors commented Dec 14, 2022

☔ The latest upstream changes (presumably #104986) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Dec 15, 2022

☔ The latest upstream changes (presumably #105657) made this pull request unmergeable. Please resolve the merge conflicts.

@jackh726
Copy link
Member

jackh726 commented Jan 4, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jan 4, 2023

📌 Commit fa2f31b has been approved by jackh726

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-review Status: Awaiting review from the assignee but also interested parties. labels Jan 4, 2023
@bors
Copy link
Contributor

bors commented Jan 5, 2023

⌛ Testing commit fa2f31b with merge 03b9e1d...

@bors
Copy link
Contributor

bors commented Jan 5, 2023

☀️ Test successful - checks-actions
Approved by: jackh726
Pushing 03b9e1d to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 5, 2023
@bors bors merged commit 03b9e1d into rust-lang:master Jan 5, 2023
@rustbot rustbot added this to the 1.68.0 milestone Jan 5, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (03b9e1d): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

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)
2.8% [2.6%, 2.9%] 2
Regressions ❌
(secondary)
1.9% [1.9%, 1.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.8% [2.6%, 2.9%] 2

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
6 participants