-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
type inference doesn't work in async fn that return Box<dyn SomeTrait> #60424
Comments
I'd put this under a similar camp as #54326 -- the issue here is that we need to fix the output of the |
Discussed in meeting today. Adding the "unclear" tag: this one is tricky to fix and it's not clear that it should block stabilization. It would definitely be good to have, though. In particular, it seems unlikely to be a forwards compatibility issue, since this is basically a problem of failing to do the coercion that is needed and hence getting more compilation errors (but it would be helpful if people have counterexamples in mind where behavior might change, seems possible). |
Discussed in the rust-lang/lang meeting -- we decided this is, ultimately, not a blocker but we ought to "try real hard" to fix it. |
This even more minimal example has the same problem: async fn func(s: &str) -> &[&str] {
&[s]
} |
@leo60228 Yup, you need async fn func(s: &str) -> &[&str] {
&[s] as &[&str]
} which correctly generates an error:
|
@cramertj Whoops, I didn't test the non-async version... This does have the problem I intended to demonstrate, though: async fn func() -> &'static [&'static str] {
&["hi"]
} |
@nikomatsakis Any notes on this one ? |
OK, so I have been doing a bit of digging into this. Our previous efforts to fix those were focused on keeping the existing desugaring. But @Centril was suggesting that we should modify the HIR and I think they are correct. Let's walk through what happens here. First off, I'll run with this example: async fn func() -> &'static [&'static str] {
&["hi"]
} This presently gets desugared to an async block, but in the compiler that async block is itself desugared into a call to fn func() -> &'static [&'static str] {
::std::future::from_generator(gen || {
&["hi"]
})
} I think what we want to do, in short, is to generate a return type annotation on that generator. You can see that this will fix the compile error in this manually desugared version from the playground. However, I think the right way to do this is probably to extend the HIR type with some kind of "current function return type" node for internal use that expands to the return type from the current function. The reason to do this is that we can't just "copy-n-paste" the return type from the function declaration into this position -- the references to lifetimes will not work out correctly, unfortunately. In particular, elided lifetimes are handled by a later pass and they would be treated differently in this position. A few more tips. This desugaring is done here: rust/src/librustc/hir/lowering/item.rs Lines 1223 to 1258 in c7bc0bf
This is a call to rust/src/librustc/hir/lowering/expr.rs Line 451 in c7bc0bf
This argument supplies the return type annotation on the closure. When we invoke it from |
Ah, actually, I realize something even easier. We already supply a flag to the generator code that indicates this is a generator. If we supply a flag that indicates that this is the "async move from an async fn body", we could supply the correct typing hints in type-check. |
I may take a stab at this in a few, else I'll poke somebody to give it a try in Zulip. |
Oh dang it, I just realized the obvious complication -- the return type of the function is actually a future, so what we really need to do is to extract the |
I'm sure this has already been considered, but this bug would be trivially fixable if it were possible to have types from the AST appear twice in the HIR. It also seems plausible that such a thing would be useful elsewhere. Is there a sensible path to allowing this? |
Got a pending fix in #64999 |
…rn-inference, r=cramertj extract expected return type for async fn generators Fixes rust-lang#60424 cc @Centril, I know you've been eager to see this fixed. r? @cramertj
…rn-inference, r=cramertj extract expected return type for async fn generators Fixes rust-lang#60424 cc @Centril, I know you've been eager to see this fixed. r? @cramertj
…rn-inference, r=cramertj extract expected return type for async fn generators Fixes rust-lang#60424 cc @Centril, I know you've been eager to see this fixed. r? @cramertj
The explicit boxing is not needed anymore as rust-lang/rust#60424 has been fixed.
The cast is not needed anymore as rust-lang/rust#60424 has been fixed.
The cast is not needed anymore as rust-lang/rust#60424 has been fixed.
The bug has been fixed at rust-lang/rust#60424.
The bug has been fixed at rust-lang/rust#60424.
The bug has been fixed at rust-lang/rust#60424.
i have following fn that works completly fine:
if i try trivialy make it async just by adding
async
keyword i get build error:i can work around this by defining types of variables explicitly:
but for obvious reasons this is not as nice as sync version
maybe related to #60414 ?
rustc --version
:rustc 1.36.0-nightly (00859e3e6 2019-04-29)
The text was updated successfully, but these errors were encountered: