Avoid GenFuture
shim when compiling async constructs
Previously, async constructs would be lowered to "normal" generators, with an additional `from_generator` / `GenFuture` shim in between to convert from `Generator` to `Future`. The compiler will now special-case these generators internally so that async constructs will *directly* implement `Future` without the need to go through the `from_generator` / `GenFuture` shim. The primary motivation for this change was hiding this implementation detail in stack traces and debuginfo, but it can in theory also help the optimizer as there is less abstractions to see through.
This commit is contained in:
parent
fd815a5091
commit
9f36f988ad
41 changed files with 459 additions and 211 deletions
|
@ -314,7 +314,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates);
|
||||
}
|
||||
|
||||
self.assemble_generator_candidates(obligation, &mut candidates);
|
||||
if lang_items.gen_trait() == Some(def_id) {
|
||||
self.assemble_generator_candidates(obligation, &mut candidates);
|
||||
} else if lang_items.future_trait() == Some(def_id) {
|
||||
self.assemble_future_candidates(obligation, &mut candidates);
|
||||
}
|
||||
|
||||
self.assemble_closure_candidates(obligation, &mut candidates);
|
||||
self.assemble_fn_pointer_candidates(obligation, &mut candidates);
|
||||
self.assemble_candidates_from_impls(obligation, &mut candidates);
|
||||
|
@ -402,10 +407,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
obligation: &TraitObligation<'tcx>,
|
||||
candidates: &mut SelectionCandidateSet<'tcx>,
|
||||
) {
|
||||
if self.tcx().lang_items().gen_trait() != Some(obligation.predicate.def_id()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Okay to skip binder because the substs on generator types never
|
||||
// touch bound regions, they just capture the in-scope
|
||||
// type/region parameters.
|
||||
|
@ -424,6 +425,23 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn assemble_future_candidates(
|
||||
&mut self,
|
||||
obligation: &TraitObligation<'tcx>,
|
||||
candidates: &mut SelectionCandidateSet<'tcx>,
|
||||
) {
|
||||
let self_ty = obligation.self_ty().skip_binder();
|
||||
if let ty::Generator(did, ..) = self_ty.kind() {
|
||||
if let Some(rustc_hir::GeneratorKind::Async(_generator_kind)) =
|
||||
self.tcx().generator_kind(did)
|
||||
{
|
||||
debug!(?self_ty, ?obligation, "assemble_future_candidates",);
|
||||
|
||||
candidates.vec.push(FutureCandidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks for the artificial impl that the compiler will create for an obligation like `X :
|
||||
/// FnMut<..>` where `X` is a closure type.
|
||||
///
|
||||
|
|
|
@ -23,10 +23,11 @@ use crate::traits::{
|
|||
BuiltinDerivedObligation, ImplDerivedObligation, ImplDerivedObligationCause, ImplSource,
|
||||
ImplSourceAutoImplData, ImplSourceBuiltinData, ImplSourceClosureData,
|
||||
ImplSourceConstDestructData, ImplSourceDiscriminantKindData, ImplSourceFnPointerData,
|
||||
ImplSourceGeneratorData, ImplSourceObjectData, ImplSourcePointeeData, ImplSourceTraitAliasData,
|
||||
ImplSourceTraitUpcastingData, ImplSourceUserDefinedData, Normalized, ObjectCastObligation,
|
||||
Obligation, ObligationCause, OutputTypeParameterMismatch, PredicateObligation, Selection,
|
||||
SelectionError, TraitNotObjectSafe, TraitObligation, Unimplemented, VtblSegment,
|
||||
ImplSourceFutureData, ImplSourceGeneratorData, ImplSourceObjectData, ImplSourcePointeeData,
|
||||
ImplSourceTraitAliasData, ImplSourceTraitUpcastingData, ImplSourceUserDefinedData, Normalized,
|
||||
ObjectCastObligation, Obligation, ObligationCause, OutputTypeParameterMismatch,
|
||||
PredicateObligation, Selection, SelectionError, TraitNotObjectSafe, TraitObligation,
|
||||
Unimplemented, VtblSegment,
|
||||
};
|
||||
|
||||
use super::BuiltinImplConditions;
|
||||
|
@ -89,6 +90,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplSource::Generator(vtable_generator)
|
||||
}
|
||||
|
||||
FutureCandidate => {
|
||||
let vtable_future = self.confirm_future_candidate(obligation)?;
|
||||
ImplSource::Future(vtable_future)
|
||||
}
|
||||
|
||||
FnPointerCandidate { .. } => {
|
||||
let data = self.confirm_fn_pointer_candidate(obligation)?;
|
||||
ImplSource::FnPointer(data)
|
||||
|
@ -685,7 +691,21 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
|
||||
debug!(?obligation, ?generator_def_id, ?substs, "confirm_generator_candidate");
|
||||
|
||||
let trait_ref = self.generator_trait_ref_unnormalized(obligation, substs);
|
||||
let gen_sig = substs.as_generator().poly_sig();
|
||||
|
||||
// (1) Feels icky to skip the binder here, but OTOH we know
|
||||
// that the self-type is an generator type and hence is
|
||||
// in fact unparameterized (or at least does not reference any
|
||||
// regions bound in the obligation). Still probably some
|
||||
// refactoring could make this nicer.
|
||||
|
||||
let trait_ref = super::util::generator_trait_ref_and_outputs(
|
||||
self.tcx(),
|
||||
obligation.predicate.def_id(),
|
||||
obligation.predicate.skip_binder().self_ty(), // (1)
|
||||
gen_sig,
|
||||
)
|
||||
.map_bound(|(trait_ref, ..)| trait_ref);
|
||||
|
||||
let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
|
||||
debug!(?trait_ref, ?nested, "generator candidate obligations");
|
||||
|
@ -693,6 +713,36 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
Ok(ImplSourceGeneratorData { generator_def_id, substs, nested })
|
||||
}
|
||||
|
||||
fn confirm_future_candidate(
|
||||
&mut self,
|
||||
obligation: &TraitObligation<'tcx>,
|
||||
) -> Result<ImplSourceFutureData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
||||
// Okay to skip binder because the substs on generator types never
|
||||
// touch bound regions, they just capture the in-scope
|
||||
// type/region parameters.
|
||||
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
|
||||
let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
|
||||
bug!("closure candidate for non-closure {:?}", obligation);
|
||||
};
|
||||
|
||||
debug!(?obligation, ?generator_def_id, ?substs, "confirm_future_candidate");
|
||||
|
||||
let gen_sig = substs.as_generator().poly_sig();
|
||||
|
||||
let trait_ref = super::util::future_trait_ref_and_outputs(
|
||||
self.tcx(),
|
||||
obligation.predicate.def_id(),
|
||||
obligation.predicate.no_bound_vars().expect("future has no bound vars").self_ty(),
|
||||
gen_sig,
|
||||
)
|
||||
.map_bound(|(trait_ref, ..)| trait_ref);
|
||||
|
||||
let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
|
||||
debug!(?trait_ref, ?nested, "future candidate obligations");
|
||||
|
||||
Ok(ImplSourceFutureData { generator_def_id, substs, nested })
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
fn confirm_closure_candidate(
|
||||
&mut self,
|
||||
|
|
|
@ -1139,9 +1139,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ProjectionCandidate(_, ty::BoundConstness::ConstIfConst) => {}
|
||||
// auto trait impl
|
||||
AutoImplCandidate => {}
|
||||
// generator, this will raise error in other places
|
||||
// generator / future, this will raise error in other places
|
||||
// or ignore error with const_async_blocks feature
|
||||
GeneratorCandidate => {}
|
||||
FutureCandidate => {}
|
||||
// FnDef where the function is const
|
||||
FnPointerCandidate { is_const: true } => {}
|
||||
ConstDestructCandidate(_) => {}
|
||||
|
@ -1620,6 +1621,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(..)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -1638,6 +1640,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(_)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -1668,6 +1671,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(..)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -1680,6 +1684,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(..)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -1761,6 +1766,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(_)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -1770,6 +1776,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
ImplCandidate(_)
|
||||
| ClosureCandidate
|
||||
| GeneratorCandidate
|
||||
| FutureCandidate
|
||||
| FnPointerCandidate { .. }
|
||||
| BuiltinObjectCandidate
|
||||
| BuiltinUnsizeCandidate
|
||||
|
@ -2279,28 +2286,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
.map_bound(|(trait_ref, _)| trait_ref)
|
||||
}
|
||||
|
||||
fn generator_trait_ref_unnormalized(
|
||||
&mut self,
|
||||
obligation: &TraitObligation<'tcx>,
|
||||
substs: SubstsRef<'tcx>,
|
||||
) -> ty::PolyTraitRef<'tcx> {
|
||||
let gen_sig = substs.as_generator().poly_sig();
|
||||
|
||||
// (1) Feels icky to skip the binder here, but OTOH we know
|
||||
// that the self-type is an generator type and hence is
|
||||
// in fact unparameterized (or at least does not reference any
|
||||
// regions bound in the obligation). Still probably some
|
||||
// refactoring could make this nicer.
|
||||
|
||||
super::util::generator_trait_ref_and_outputs(
|
||||
self.tcx(),
|
||||
obligation.predicate.def_id(),
|
||||
obligation.predicate.skip_binder().self_ty(), // (1)
|
||||
gen_sig,
|
||||
)
|
||||
.map_bound(|(trait_ref, ..)| trait_ref)
|
||||
}
|
||||
|
||||
/// Returns the obligations that are implied by instantiating an
|
||||
/// impl or trait. The obligations are substituted and fully
|
||||
/// normalized. This is used when confirming an impl or default
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue