-
Notifications
You must be signed in to change notification settings - Fork 13.4k
extract expected return type for async fn generators #64999
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
bors
merged 16 commits into
rust-lang:master
from
nikomatsakis:issue-60424-async-return-inference
Oct 3, 2019
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c845f3d
track the kind of async generator we are creating
nikomatsakis 44e801a
thread down the body so we can check if this is an async fn body
nikomatsakis c8e5851
improved debug output
nikomatsakis f7ed53c
extract expected return type from `-> impl Future` obligation
nikomatsakis 094f3a8
WIP tidy hir/lowering/expr.rs
nikomatsakis 81cd596
WIP rebase for `shallow_resolve` call
nikomatsakis dce20bf
WIP fix tests
nikomatsakis 3ae4abb
correct coercion comments
nikomatsakis a999132
improve comments on `GeneratorKind` and `AsyncGeneratorKind`
nikomatsakis 5d64b3d
document `ret_coercion` and `ret_coercion_span`
nikomatsakis 5fea1d2
document `shallow_resolve`
nikomatsakis 3f277e1
s/`async` fn/`async fn`/
nikomatsakis a96bce7
avoid using `skip_binder` and instead look for bound vars properly
nikomatsakis 4a49351
add unsize slice-str coercion
nikomatsakis 19c07cc
fix example (le sigh)
nikomatsakis a807032
./x.py test --bless --compare-mode=nll
nikomatsakis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,7 +337,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
) -> ClosureSignatures<'tcx> { | ||
debug!("sig_of_closure_no_expectation()"); | ||
|
||
let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl); | ||
let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, body); | ||
|
||
self.closure_sigs(expr_def_id, body, bound_sig) | ||
} | ||
|
@@ -490,7 +490,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
// | ||
// (See comment on `sig_of_closure_with_expectation` for the | ||
// meaning of these letters.) | ||
let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl); | ||
let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, body); | ||
|
||
debug!( | ||
"check_supplied_sig_against_expectation: supplied_sig={:?}", | ||
|
@@ -591,14 +591,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
&self, | ||
expr_def_id: DefId, | ||
decl: &hir::FnDecl, | ||
body: &hir::Body, | ||
) -> ty::PolyFnSig<'tcx> { | ||
let astconv: &dyn AstConv<'_> = self; | ||
|
||
debug!( | ||
"supplied_sig_of_closure(decl={:?}, body.generator_kind={:?})", | ||
decl, | ||
body.generator_kind, | ||
); | ||
|
||
// First, convert the types that the user supplied (if any). | ||
let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a)); | ||
let supplied_return = match decl.output { | ||
hir::Return(ref output) => astconv.ast_ty_to_ty(&output), | ||
hir::DefaultReturn(_) => astconv.ty_infer(None, decl.output.span()), | ||
hir::DefaultReturn(_) => match body.generator_kind { | ||
// In the case of the async block that we create for a function body, | ||
// we expect the return type of the block to match that of the enclosing | ||
// function. | ||
Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => { | ||
debug!("supplied_sig_of_closure: closure is async fn body"); | ||
self.deduce_future_output_from_obligations(expr_def_id) | ||
} | ||
|
||
_ => astconv.ty_infer(None, decl.output.span()), | ||
} | ||
}; | ||
|
||
let result = ty::Binder::bind(self.tcx.mk_fn_sig( | ||
|
@@ -620,6 +637,104 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
result | ||
} | ||
|
||
/// Invoked when we are translating the generator that results | ||
/// from desugaring an `async fn`. Returns the "sugared" return | ||
/// type of the `async fn` -- that is, the return type that the | ||
/// user specified. The "desugared" return type is a `impl | ||
/// Future<Output = T>`, so we do this by searching through the | ||
/// obligations to extract the `T`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe leave a FIXME/open an issue somewhere about generalizing this so it can rely entirely on the |
||
fn deduce_future_output_from_obligations( | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&self, | ||
expr_def_id: DefId, | ||
) -> Ty<'tcx> { | ||
debug!("deduce_future_output_from_obligations(expr_def_id={:?})", expr_def_id); | ||
|
||
let ret_coercion = | ||
self.ret_coercion | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.as_ref() | ||
.unwrap_or_else(|| span_bug!( | ||
self.tcx.def_span(expr_def_id), | ||
"async fn generator outside of a fn" | ||
)); | ||
|
||
// In practice, the return type of the surrounding function is | ||
// always a (not yet resolved) inference variable, because it | ||
// is the hidden type for an `impl Trait` that we are going to | ||
// be inferring. | ||
let ret_ty = ret_coercion.borrow().expected_ty(); | ||
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty); | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let ret_vid = match ret_ty.kind { | ||
ty::Infer(ty::TyVar(ret_vid)) => ret_vid, | ||
_ => { | ||
span_bug!( | ||
self.tcx.def_span(expr_def_id), | ||
"async fn generator return type not an inference variable" | ||
) | ||
} | ||
}; | ||
|
||
// Search for a pending obligation like | ||
// | ||
// `<R as Future>::Output = T` | ||
// | ||
// where R is the return type we are expecting. This type `T` | ||
// will be our output. | ||
let output_ty = self.obligations_for_self_ty(ret_vid) | ||
.find_map(|(_, obligation)| { | ||
if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate { | ||
self.deduce_future_output_from_projection( | ||
obligation.cause.span, | ||
proj_predicate | ||
) | ||
} else { | ||
None | ||
} | ||
}) | ||
.unwrap(); | ||
|
||
debug!("deduce_future_output_from_obligations: output_ty={:?}", output_ty); | ||
output_ty | ||
} | ||
|
||
/// Given a projection like | ||
/// | ||
/// `<_ as Future>::Output = T` | ||
/// | ||
/// returns `Some(T)`. If the projection is for some other trait, | ||
/// returns `None`. | ||
fn deduce_future_output_from_projection( | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&self, | ||
cause_span: Span, | ||
projection: &ty::PolyProjectionPredicate<'tcx>, | ||
) -> Option<Ty<'tcx>> { | ||
debug!("deduce_future_output_from_projection(projection={:?})", projection); | ||
|
||
let trait_ref = projection.to_poly_trait_ref(self.tcx); | ||
let future_trait = self.tcx.lang_items().future_trait().unwrap(); | ||
if trait_ref.def_id() != future_trait { | ||
debug!("deduce_future_output_from_projection: not a future"); | ||
return None; | ||
} | ||
|
||
// The `Future` trait has only one associted item, `Output`, | ||
// so check that this is what we see. | ||
let output_assoc_item = self.tcx.associated_items(future_trait).nth(0).unwrap().def_id; | ||
if output_assoc_item != projection.projection_def_id() { | ||
span_bug!( | ||
cause_span, | ||
"projecting associated item `{:?}` from future, which is not Output `{:?}`", | ||
projection.projection_def_id(), | ||
output_assoc_item, | ||
); | ||
} | ||
|
||
// Extract the type from the projection. | ||
let output_ty = projection.skip_binder().ty; | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let output_ty = self.resolve_vars_if_possible(&output_ty); | ||
debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty); | ||
Some(output_ty) | ||
} | ||
|
||
/// Converts the types that the user supplied, in case that doing | ||
/// so should yield an error, but returns back a signature where | ||
/// all parameters are of type `TyErr`. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Check that we apply unsizing coercions based on the return type. | ||
// | ||
// Also serves as a regression test for #60424. | ||
// | ||
// edition:2018 | ||
// check-pass | ||
|
||
#![allow(warnings)] | ||
|
||
use std::fmt::Debug; | ||
|
||
const TMP: u32 = 22; | ||
|
||
// Coerce from `Box<"asdf">` to `Box<dyn Debug>`. | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn raw_pointer_coercion() { | ||
fn sync_example() -> *const u32 { | ||
&TMP | ||
} | ||
|
||
async fn async_example() -> *const u32 { | ||
&TMP | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Check that we apply unsizing coercions based on the return type. | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Also serves as a regression test for #60424. | ||
// | ||
// edition:2018 | ||
// check-pass | ||
|
||
#![allow(warnings)] | ||
|
||
use std::fmt::Debug; | ||
|
||
// Coerce from `Box<"asdf">` to `Box<dyn Debug>`. | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn unsize_trait_coercion() { | ||
fn sync_example() -> Box<dyn Debug> { | ||
Box::new("asdf") | ||
} | ||
|
||
async fn async_example() -> Box<dyn Debug> { | ||
Box::new("asdf") | ||
} | ||
} | ||
|
||
// Coerce from `Box<[u32; N]>` to `Box<[32]>`. | ||
fn unsize_slice_coercion() { | ||
fn sync_example() -> Box<[u32]> { | ||
Box::new([0]) | ||
} | ||
|
||
async fn async_example() -> Box<[u32]> { | ||
Box::new([0]) | ||
} | ||
} | ||
|
||
fn main() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.