-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix non-capturing closure return type coercion #88147
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
r? @cjgillot (rust-highfive has picked a reviewer for you, use r? to override) |
r? @jackh726 |
So, this is certainly a fix. But I have to think a little harder on the subtlety of this error. Specifically, I would expect that the check if the two types are equal to be an optimization and this could fail in other cases still. Regardless, in the meantime, can you add a comment? |
Sure, where and about what exactly? A reference to #88097 and/or this PR? |
@@ -941,6 +941,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
exprs.len() | |||
); | |||
|
|||
if prev_ty == new_ty { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment here referencing #88097 and coercion of a closure to itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it; fixed now.
Thanks, I'll try to get back to this soon and think more about this in depth. |
I'm not 100% sure this is the only way to do this, but it's a very reasonable change, so I'm going to go ahead and @bors r+ rollup |
📌 Commit bbe3be9 has been approved by |
Fix non-capturing closure return type coercion Fixes rust-lang#88097. For the example given there: ```rust fn peculiar() -> impl Fn(u8) -> u8 { return |x| x + 1 } ``` which incorrectly reports an error, I noticed something weird in the debug log: ``` DEBUG rustc_typeck::check::coercion coercion::try_find_coercion_lub([closure@test.rs:2:12: 2:21], [closure@test.rs:2:12: 2:21], exprs=1 exprs) ``` Apparently, `try_find_coercion_lub()` thinks that the LUB for two closure types always has to be a function pointer (which explains the `expected closure, found fn pointer` error in rust-lang#88097). There is one corner case where that isn't true, though — namely, when the two closure types are equal, in which case the trivial LUB is the type itself. This PR fixes this by inserting an explicit check for type equality in `try_find_coercion_lub()`.
Fix non-capturing closure return type coercion Fixes rust-lang#88097. For the example given there: ```rust fn peculiar() -> impl Fn(u8) -> u8 { return |x| x + 1 } ``` which incorrectly reports an error, I noticed something weird in the debug log: ``` DEBUG rustc_typeck::check::coercion coercion::try_find_coercion_lub([closure@test.rs:2:12: 2:21], [closure@test.rs:2:12: 2:21], exprs=1 exprs) ``` Apparently, `try_find_coercion_lub()` thinks that the LUB for two closure types always has to be a function pointer (which explains the `expected closure, found fn pointer` error in rust-lang#88097). There is one corner case where that isn't true, though — namely, when the two closure types are equal, in which case the trivial LUB is the type itself. This PR fixes this by inserting an explicit check for type equality in `try_find_coercion_lub()`.
…ingjubilee Rollup of 10 pull requests Successful merges: - rust-lang#87904 (Reword description of automatic impls of `Unsize`.) - rust-lang#88147 (Fix non-capturing closure return type coercion) - rust-lang#88209 (Improve error message when _ is used for in/inout asm operands) - rust-lang#88668 (Change more x64 size checks to not apply to x32.) - rust-lang#88733 (Fix ICE for functions with more than 65535 arguments) - rust-lang#88757 (Suggest wapping expr in parentheses on invalid unary negation) - rust-lang#88779 (Use more accurate spans for "unused delimiter" lint) - rust-lang#88830 (Add help for E0463) - rust-lang#88849 (don't clone types that are Copy (clippy::clone_on_copy)) - rust-lang#88850 (don't convert types into identical types) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fixes #88097. For the example given there:
which incorrectly reports an error, I noticed something weird in the debug log:
Apparently,
try_find_coercion_lub()
thinks that the LUB for two closure types always has to be a function pointer (which explains theexpected closure, found fn pointer
error in #88097). There is one corner case where that isn't true, though — namely, when the two closure types are equal, in which case the trivial LUB is the type itself. This PR fixes this by inserting an explicit check for type equality intry_find_coercion_lub()
.