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

Properly deal with function pointer Fn candidates with escaping bound vars #104929

Conversation

compiler-errors
Copy link
Member

Function pointers in where clauses can reference both bound regions from their own binder, and from the binder of the where clause predicate that they're the Self type of.

The UI test demonstrates that we need to be able to deal with both of these cases for Fn trait bounds, sometimes at the same time.

This PR introduces a function called flatten_binders which takes a Binder<Binder<T>> and flattens it into a Binder<T>, concatenating the variables list and adjusting the de Bruijn indices accordingly. If we see a function pointer's signature that captures bound vars from the where-clause, flatten those bound variables so that we can subtype signatures properly in confirm_poly_trait_refs.

cc @lcnr @jackh726 who know more about binders and higher-ranked subtyping than I do.
r? types


I'm pretty sure this approach is (at least mostly) correct, but maybe it's overkill. The where clauses in the UI test I added are certainly a bit contrived, but perhaps they could show up in the wild... I recall seeing an ICE that looked like this, but I can't find it anymore.

We could alternatively deal with this in a forwards-compatible way by just erroring out if we see escaping bound vars in the fn ptr type... I mostly want to make sure that this code no longer ICEs:

fn foo2()
where
    for<'a> fn(&'a ()): Fn(&'a ()),
{
}

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

Oh yeah, UI tests will hang unless I cherry pick #104927. I'll do that later.

@compiler-errors
Copy link
Member Author

@lcnr has opinions about this apparently

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 10, 2023
…pastorino

Do not ICE when we have fn pointer `Fn` obligations with bound vars in the self type

We never supported solving `for<'a> fn(&'a ()): Fn(&'a ())` -- I tried to add that support in rust-lang#104929, but iirc `@lcnr` wanted to support this more generally by eagerly instantiating trait predicate binders with placeholders. That never happened due to blockers in the old solver, but we probably shouldn't ICE in any case.

On the bright side, this passes on the new solver :^)
@compiler-errors compiler-errors deleted the fn-ptr-escaping-bound-var branch August 11, 2023 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants