Skip to content

Closures always implement FnOnce in new solver #109739

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

Merged
merged 1 commit into from
Mar 31, 2023

Conversation

compiler-errors
Copy link
Member

We should process [closure]: FnOnce(Tys...) -> Ty obligations before fallback and closure analysis. We can do this by taking advantage of the fact that FnOnce is always implemented by closures, even before we definitely know the closure kind.

Fixes compiler-errors/next-solver-hir-issues#15

r? @oli-obk (trying to spread the reviewer load for new trait solver prs, and this one is pretty self-contained, though feel free to reassign 😸)

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 29, 2023
@rustbot
Copy link
Collaborator

rustbot commented Mar 29, 2023

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

Comment on lines 217 to 230
Some(closure_kind) => {
// If the closure's kind doesn't extend the goal kind,
// then the closure doesn't implement the trait.
if !closure_kind.extends(goal_kind) {
return Err(NoSolution);
}
}
// Closure kind is not yet determined
None => {
// All closures implement `FnOnce`
if goal_kind != ty::ClosureKind::FnOnce {
// Otherwise, if we don't know the `ClosureKind`, ambiguity.
return Ok(None);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Some(closure_kind) => {
// If the closure's kind doesn't extend the goal kind,
// then the closure doesn't implement the trait.
if !closure_kind.extends(goal_kind) {
return Err(NoSolution);
}
}
// Closure kind is not yet determined
None => {
// All closures implement `FnOnce`
if goal_kind != ty::ClosureKind::FnOnce {
// Otherwise, if we don't know the `ClosureKind`, ambiguity.
return Ok(None);
}
}
// If the closure's kind doesn't extend the goal kind,
// then the closure doesn't implement the trait.
Some(closure_kind) => {
if !closure_kind.extends(goal_kind) {
return Err(NoSolution);
}
}
// Closure kind is not yet determined, so we return ambiguity unless
// the expected kind is `FnOnce` as that is always implemented.
None => {
if goal_kind != ty::ClosureKind::FnOnce {
return Ok(None);
}
}

style nit: having these kinds of nested comments imo makes the code more verbose and harder to read. Can't explain it well but I guess part of the issue is that I have to switch between reading the comment, and reading the actual source.

r=me with or without this nit applied

@lcnr
Copy link
Contributor

lcnr commented Mar 30, 2023

r? @lcnr 😁

@rustbot rustbot assigned lcnr and unassigned oli-obk Mar 30, 2023
@compiler-errors compiler-errors force-pushed the new-solver-closure-fnonce branch from a11086f to 177997e Compare March 30, 2023 15:12
@compiler-errors
Copy link
Member Author

@bors r=lcnr rollup (new solver)

@bors
Copy link
Collaborator

bors commented Mar 30, 2023

📌 Commit 177997e 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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 30, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 30, 2023
…-fnonce, r=lcnr

Closures always implement `FnOnce` in new solver

We should process `[closure]: FnOnce(Tys...) -> Ty` obligations *before* fallback and closure analysis. We can do this by taking advantage of the fact that `FnOnce` is always implemented by closures, even before we definitely know the closure kind.

Fixes compiler-errors/next-solver-hir-issues#15

r? `@oli-obk` (trying to spread the reviewer load for new trait solver prs, and this one is pretty self-contained, though feel free to reassign 😸)
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 30, 2023
…mpiler-errors

Rollup of 6 pull requests

Successful merges:

 - rust-lang#109347 (Skip no_mangle if the item has no name.)
 - rust-lang#109522 (Implement current_dll_path for AIX)
 - rust-lang#109679 (Freshen normalizes-to hack goal RHS in the evaluate loop)
 - rust-lang#109704 (resolve: Minor improvements to effective visibilities)
 - rust-lang#109739 (Closures always implement `FnOnce` in new solver)
 - rust-lang#109758 (Parallel compiler cleanups)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7cd96ae into rust-lang:master Mar 31, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 31, 2023
@compiler-errors compiler-errors deleted the new-solver-closure-fnonce branch August 11, 2023 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Closure type expectations do not guide type inference?
5 participants