Skip to content
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

Rename AsyncIterator back to Stream, introduce an AFIT-based AsyncIterator trait #119550

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

let features = match await_kind {
FutureKind::Future => None,
FutureKind::AsyncIterator => Some(self.allow_for_await.clone()),
FutureKind::Stream => Some(self.allow_for_await.clone()),
};
let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, features);
let gen_future_span = self.mark_span_with_reason(
Expand Down Expand Up @@ -830,9 +830,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::LangItem::FuturePoll,
arena_vec![self; new_unchecked, get_context],
),
FutureKind::AsyncIterator => self.expr_call_lang_item_fn(
FutureKind::Stream => self.expr_call_lang_item_fn(
span,
hir::LangItem::AsyncIteratorPollNext,
hir::LangItem::AsyncStreamPollNext,
arena_vec![self; new_unchecked, get_context],
),
};
Expand Down Expand Up @@ -924,8 +924,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
arena_vec![self; *expr],
),
// Not needed for `for await` because we expect to have already called
// `IntoAsyncIterator::into_async_iter` on it.
FutureKind::AsyncIterator => expr,
// `IntoStream::into_stream` on it.
FutureKind::Stream => expr,
};

// match <into_future_expr> {
Expand Down Expand Up @@ -1643,7 +1643,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ForLoopKind::ForAwait => {
// we'll generate `unsafe { Pin::new_unchecked(&mut iter) })` and then pass this
// to make_lowered_await with `FutureKind::AsyncIterator` which will generator
// to make_lowered_await with `FutureKind::Stream` which will generator
// calls to `poll_next`. In user code, this would probably be a call to
// `Pin::as_mut` but here it's easy enough to do `new_unchecked`.

Expand All @@ -1657,7 +1657,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
));
// `unsafe { ... }`
let iter = self.arena.alloc(self.expr_unsafe(iter));
let kind = self.make_lowered_await(head_span, iter, FutureKind::AsyncIterator);
let kind = self.make_lowered_await(head_span, iter, FutureKind::Stream);
self.arena.alloc(hir::Expr { hir_id: self.next_id(), kind, span: head_span })
}
};
Expand Down Expand Up @@ -1692,12 +1692,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
arena_vec![self; head],
)
}
// ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
// ` unsafe { Pin::new_unchecked(&mut into_stream(<head>)) }`
ForLoopKind::ForAwait => {
// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
// `::core::stream::IntoStream::into_stream(<head>)`
let iter = self.expr_call_lang_item_fn(
head_span,
hir::LangItem::IntoAsyncIterIntoIter,
hir::LangItem::IntoAsyncStreamIntoStream,
arena_vec![self; head],
);
let iter = self.expr_mut_addr_of(head_span, iter);
Expand Down Expand Up @@ -2118,7 +2118,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
enum FutureKind {
/// We are awaiting a normal future
Future,
/// We are awaiting something that's known to be an AsyncIterator (i.e. we are in the header of
/// We are awaiting something that's known to be an Stream (i.e. we are in the header of
/// a `for await` loop)
AsyncIterator,
Stream,
}
10 changes: 5 additions & 5 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct LoweringContext<'a, 'hir> {

allow_try_trait: Lrc<[Symbol]>,
allow_gen_future: Lrc<[Symbol]>,
allow_async_iterator: Lrc<[Symbol]>,
allow_async_stream: Lrc<[Symbol]>,
allow_for_await: Lrc<[Symbol]>,

/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
Expand Down Expand Up @@ -177,10 +177,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} else {
[sym::gen_future].into()
},
allow_for_await: [sym::async_iterator].into(),
allow_for_await: [sym::async_stream].into(),
// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
// interact with `gen`/`async gen` blocks
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),
allow_async_stream: [sym::gen_future, sym::async_stream].into(),
generics_def_id_map: Default::default(),
host_param_id: None,
}
Expand Down Expand Up @@ -1959,7 +1959,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
CoroutineKind::Async { return_impl_trait_id, .. } => (return_impl_trait_id, None),
CoroutineKind::Gen { return_impl_trait_id, .. } => (return_impl_trait_id, None),
CoroutineKind::AsyncGen { return_impl_trait_id, .. } => {
(return_impl_trait_id, Some(self.allow_async_iterator.clone()))
(return_impl_trait_id, Some(self.allow_async_stream.clone()))
}
};

Expand Down Expand Up @@ -2021,7 +2021,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let (assoc_ty_name, trait_lang_item) = match coro {
CoroutineKind::Async { .. } => (sym::Output, hir::LangItem::Future),
CoroutineKind::Gen { .. } => (sym::Item, hir::LangItem::Iterator),
CoroutineKind::AsyncGen { .. } => (sym::Item, hir::LangItem::AsyncIterator),
CoroutineKind::AsyncGen { .. } => (sym::Item, hir::LangItem::AsyncStream),
};

let bound_args = self.arena.alloc(hir::GenericArgs {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ language_item_table! {

Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0);
Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
AsyncStream, sym::async_stream, async_stream_trait, Target::Trait, GenericRequirement::Exact(0);
CoroutineState, sym::coroutine_state, coroutine_state, Target::Enum, GenericRequirement::None;
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Minimum(1);
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
Expand Down Expand Up @@ -307,8 +307,8 @@ language_item_table! {
Context, sym::Context, context, Target::Struct, GenericRequirement::None;
FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;

AsyncIteratorPollNext, sym::async_iterator_poll_next, async_iterator_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
IntoAsyncIterIntoIter, sym::into_async_iter_into_iter, into_async_iter_into_iter, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
AsyncStreamPollNext, sym::async_stream_poll_next, async_stream_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
IntoAsyncStreamIntoStream, sym::into_async_stream_into_stream, into_async_stream_into_stream, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);

Option, sym::Option, option_type, Target::Enum, GenericRequirement::None;
OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ symbols! {
async_fn_in_trait,
async_fn_track_caller,
async_for_loop,
async_iterator,
async_iterator_poll_next,
async_stream,
async_stream_poll_next,
atomic,
atomic_mod,
atomics,
Expand Down Expand Up @@ -896,7 +896,7 @@ symbols! {
instruction_set,
integer_: "integer", // underscore to avoid clashing with the function `sym::integer` below
integral,
into_async_iter_into_iter,
into_async_stream_into_stream,
into_future,
into_iter,
intra_doc_pointers,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub(super) trait GoalKind<'tcx>:
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx>;

fn consider_builtin_async_iterator_candidate(
fn consider_builtin_async_stream_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx>;
Expand Down Expand Up @@ -577,8 +577,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
G::consider_builtin_future_candidate(self, goal)
} else if lang_items.iterator_trait() == Some(trait_def_id) {
G::consider_builtin_iterator_candidate(self, goal)
} else if lang_items.async_iterator_trait() == Some(trait_def_id) {
G::consider_builtin_async_iterator_candidate(self, goal)
} else if lang_items.async_stream_trait() == Some(trait_def_id) {
G::consider_builtin_async_stream_candidate(self, goal)
} else if lang_items.coroutine_trait() == Some(trait_def_id) {
G::consider_builtin_coroutine_candidate(self, goal)
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
)
}

fn consider_builtin_async_iterator_candidate(
fn consider_builtin_async_stream_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}

fn consider_builtin_async_iterator_candidate(
fn consider_builtin_async_stream_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
lang_items.coroutine_trait(),
lang_items.future_trait(),
lang_items.iterator_trait(),
lang_items.async_iterator_trait(),
lang_items.async_stream_trait(),
lang_items.fn_trait(),
lang_items.fn_mut_trait(),
lang_items.fn_once_trait(),
Expand Down Expand Up @@ -2051,8 +2051,8 @@ fn confirm_select_candidate<'cx, 'tcx>(
confirm_future_candidate(selcx, obligation, data)
} else if lang_items.iterator_trait() == Some(trait_def_id) {
confirm_iterator_candidate(selcx, obligation, data)
} else if lang_items.async_iterator_trait() == Some(trait_def_id) {
confirm_async_iterator_candidate(selcx, obligation, data)
} else if lang_items.async_stream_trait() == Some(trait_def_id) {
confirm_async_stream_candidate(selcx, obligation, data)
} else if selcx.tcx().fn_trait_kind_from_def_id(trait_def_id).is_some() {
if obligation.predicate.self_ty().is_closure() {
confirm_closure_candidate(selcx, obligation, data)
Expand Down Expand Up @@ -2218,7 +2218,7 @@ fn confirm_iterator_candidate<'cx, 'tcx>(
.with_addl_obligations(obligations)
}

fn confirm_async_iterator_candidate<'cx, 'tcx>(
fn confirm_async_stream_candidate<'cx, 'tcx>(
selcx: &mut SelectionContext<'cx, 'tcx>,
obligation: &ProjectionTyObligation<'tcx>,
nested: Vec<PredicateObligation<'tcx>>,
Expand All @@ -2236,12 +2236,12 @@ fn confirm_async_iterator_candidate<'cx, 'tcx>(
gen_sig,
);

debug!(?obligation, ?gen_sig, ?obligations, "confirm_async_iterator_candidate");
debug!(?obligation, ?gen_sig, ?obligations, "confirm_async_stream_candidate");

let tcx = selcx.tcx();
let iter_def_id = tcx.require_lang_item(LangItem::AsyncIterator, None);
let iter_def_id = tcx.require_lang_item(LangItem::AsyncStream, None);

let (trait_ref, yield_ty) = super::util::async_iterator_trait_ref_and_outputs(
let (trait_ref, yield_ty) = super::util::async_stream_trait_ref_and_outputs(
tcx,
iter_def_id,
obligation.predicate.self_ty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
self.assemble_future_candidates(obligation, &mut candidates);
} else if lang_items.iterator_trait() == Some(def_id) {
self.assemble_iterator_candidates(obligation, &mut candidates);
} else if lang_items.async_iterator_trait() == Some(def_id) {
self.assemble_async_iterator_candidates(obligation, &mut candidates);
} else if lang_items.async_stream_trait() == Some(def_id) {
self.assemble_async_stream_candidates(obligation, &mut candidates);
}

self.assemble_closure_candidates(obligation, &mut candidates);
Expand Down Expand Up @@ -257,7 +257,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

fn assemble_async_iterator_candidates(
fn assemble_async_stream_candidates(
&mut self,
obligation: &PolyTraitObligation<'tcx>,
candidates: &mut SelectionCandidateSet<'tcx>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}

AsyncIteratorCandidate => {
let vtable_iterator = self.confirm_async_iterator_candidate(obligation)?;
let vtable_iterator = self.confirm_async_stream_candidate(obligation)?;
ImplSource::Builtin(BuiltinImplSource::Misc, vtable_iterator)
}

Expand Down Expand Up @@ -818,7 +818,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Ok(nested)
}

fn confirm_async_iterator_candidate(
fn confirm_async_stream_candidate(
&mut self,
obligation: &PolyTraitObligation<'tcx>,
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
Expand All @@ -830,11 +830,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
bug!("closure candidate for non-closure {:?}", obligation);
};

debug!(?obligation, ?coroutine_def_id, ?args, "confirm_async_iterator_candidate");
debug!(?obligation, ?coroutine_def_id, ?args, "confirm_async_stream_candidate");

let gen_sig = args.as_coroutine().sig();

let (trait_ref, _) = super::util::async_iterator_trait_ref_and_outputs(
let (trait_ref, _) = super::util::async_stream_trait_ref_and_outputs(
self.tcx(),
obligation.predicate.def_id(),
obligation.predicate.no_bound_vars().expect("iterator has no bound vars").self_ty(),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ pub fn iterator_trait_ref_and_outputs<'tcx>(
(trait_ref, sig.yield_ty)
}

pub fn async_iterator_trait_ref_and_outputs<'tcx>(
pub fn async_stream_trait_ref_and_outputs<'tcx>(
tcx: TyCtxt<'tcx>,
async_iterator_def_id: DefId,
async_stream_def_id: DefId,
self_ty: Ty<'tcx>,
sig: ty::GenSig<'tcx>,
) -> (ty::TraitRef<'tcx>, Ty<'tcx>) {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = ty::TraitRef::new(tcx, async_iterator_def_id, [self_ty]);
let trait_ref = ty::TraitRef::new(tcx, async_stream_def_id, [self_ty]);
(trait_ref, sig.yield_ty)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn resolve_associated_item<'tcx>(
debug_assert!(tcx.defaultness(trait_item_id).has_value());
Some(Instance::new(trait_item_id, rcvr_args))
}
} else if Some(trait_ref.def_id) == lang_items.async_iterator_trait() {
} else if Some(trait_ref.def_id) == lang_items.async_stream_trait() {
let ty::Coroutine(coroutine_def_id, args) = *rcvr_args.type_at(0).kind() else {
bug!()
};
Expand Down
18 changes: 16 additions & 2 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ use core::ops::{
};
use core::pin::Pin;
use core::ptr::{self, NonNull, Unique};
use core::stream::Stream;
use core::task::{Context, Poll};

#[cfg(not(no_global_oom_handling))]
Expand Down Expand Up @@ -2152,8 +2153,8 @@ where
}
}

#[unstable(feature = "async_iterator", issue = "79024")]
impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
#[unstable(feature = "async_stream", issue = "79024")]
impl<S: ?Sized + Stream + Unpin> Stream for Box<S> {
type Item = S::Item;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Expand All @@ -2165,6 +2166,19 @@ impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
}
}

#[unstable(feature = "async_iterator", issue = "79024")]
impl<I: ?Sized + AsyncIterator> AsyncIterator for Box<I> {
type Item = I::Item;

async fn next(&mut self) -> Option<Self::Item> {
(&mut **self).next().await
}

fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
}

impl dyn Error {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
#![feature(ascii_char)]
#![feature(assert_matches)]
#![feature(async_iterator)]
#![feature(async_stream)]
#![feature(coerce_unsized)]
#![feature(const_align_of_val)]
#![feature(const_box)]
Expand Down
Loading
Loading