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

Don't force zero-yield stream item type of '()' #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 2 additions & 35 deletions async-stream-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct Scrub<'a> {
is_try: bool,
/// The unit expression, `()`.
unit: Box<syn::Expr>,
has_yielded: bool,
crate_path: &'a TokenStream2,
}

Expand All @@ -28,7 +27,6 @@ impl<'a> Scrub<'a> {
Self {
is_try,
unit: syn::parse_quote!(()),
has_yielded: false,
crate_path,
}
}
Expand Down Expand Up @@ -112,12 +110,8 @@ impl VisitMut for Scrub<'_> {
fn visit_expr_mut(&mut self, i: &mut syn::Expr) {
match i {
syn::Expr::Yield(yield_expr) => {
self.has_yielded = true;

let value_expr = yield_expr.expr.as_ref().unwrap_or(&self.unit);

// let ident = &self.yielder;

*i = if self.is_try {
syn::parse_quote! { __yield_tx.send(::core::result::Result::Ok(#value_expr)).await }
} else {
Expand All @@ -126,7 +120,6 @@ impl VisitMut for Scrub<'_> {
}
syn::Expr::Try(try_expr) => {
syn::visit_mut::visit_expr_try_mut(self, try_expr);
// let ident = &self.yielder;
let e = &try_expr.expr;

*i = syn::parse_quote! {
Expand Down Expand Up @@ -211,23 +204,10 @@ pub fn stream_inner(input: TokenStream) -> TokenStream {
};

let mut scrub = Scrub::new(false, &crate_path);

for mut stmt in &mut stmts {
scrub.visit_stmt_mut(&mut stmt);
}

let dummy_yield = if scrub.has_yielded {
None
} else {
Some(quote!(if false {
__yield_tx.send(()).await;
}))
};

stmts.iter_mut().for_each(|s| scrub.visit_stmt_mut(s));
quote!({
let (mut __yield_tx, __yield_rx) = #crate_path::yielder::pair();
#crate_path::AsyncStream::new(__yield_rx, async move {
#dummy_yield
#(#stmts)*
})
})
Expand All @@ -245,23 +225,10 @@ pub fn try_stream_inner(input: TokenStream) -> TokenStream {
};

let mut scrub = Scrub::new(true, &crate_path);

for mut stmt in &mut stmts {
scrub.visit_stmt_mut(&mut stmt);
}

let dummy_yield = if scrub.has_yielded {
None
} else {
Some(quote!(if false {
__yield_tx.send(()).await;
}))
};

stmts.iter_mut().for_each(|s| scrub.visit_stmt_mut(s));
quote!({
let (mut __yield_tx, __yield_rx) = #crate_path::yielder::pair();
#crate_path::AsyncStream::new(__yield_rx, async move {
#dummy_yield
#(#stmts)*
})
})
Expand Down
33 changes: 27 additions & 6 deletions async-stream/tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ use tokio_test::assert_ok;

#[tokio::test]
async fn noop_stream() {
let s = stream! {};
fn any<T>() -> impl Stream<Item = T> {
stream! {}
}

let s = any::<()>();
pin_mut!(s);

while let Some(_) = s.next().await {
unreachable!();
}

let s = any::<usize>();
pin_mut!(s);

while let Some(_) = s.next().await {
unreachable!();
}

let s = any::<String>();
pin_mut!(s);

while let Some(_) = s.next().await {
Expand All @@ -21,11 +39,14 @@ async fn empty_stream() {
let mut ran = false;

{
let r = &mut ran;
let s = stream! {
*r = true;
println!("hello world!");
};
fn unit(r: &mut bool) -> impl Stream<Item = ()> + '_ {
stream! {
*r = true;
println!("hello world!");
}
}

let s = unit(&mut ran);
pin_mut!(s);

while let Some(_) = s.next().await {
Expand Down
18 changes: 15 additions & 3 deletions async-stream/tests/ui/yield_in_nested_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ error[E0658]: yield syntax is experimental
|
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information

error[E0282]: type annotations needed for `(async_stream::yielder::Sender<T>, async_stream::yielder::Receiver<T>)`
--> $DIR/yield_in_nested_fn.rs:4:5
|
4 | / stream! {
5 | | fn foo() {
6 | | yield "hello";
7 | | }
8 | | };
| | ^
| | |
| |______consider giving this pattern the explicit type `(async_stream::yielder::Sender<T>, async_stream::yielder::Receiver<T>)`, where the type parameter `T` is specified
| cannot infer type for type parameter `T` declared on the function `pair`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0627]: yield expression outside of generator literal
--> $DIR/yield_in_nested_fn.rs:6:13
|
6 | yield "hello";
| ^^^^^^^^^^^^^

Some errors have detailed explanations: E0627, E0658.
For more information about an error, try `rustc --explain E0627`.