Skip to content

Commit

Permalink
Don't force zero-yield stream item type of '()'
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed May 30, 2021
1 parent 8bf33a6 commit e23c14c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 44 deletions.
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
19 changes: 13 additions & 6 deletions async-stream/tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use tokio_test::assert_ok;

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

let s = unit();
pin_mut!(s);

while let Some(_) = s.next().await {
Expand All @@ -21,11 +25,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
5 changes: 5 additions & 0 deletions async-stream/tests/ui/yield_in_async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ error[E0271]: type mismatch resolving `<[static generator@$DIR/src/lib.rs:201:9:
10 | | };
| |______^ expected `()`, found integer
|
::: $RUST/core/src/future/mod.rs
|
| T: Generator<ResumeTy, Yield = ()>,
| ---------- required by this bound in `from_generator`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0698]: type inside `async` block must be known in this context
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`.

0 comments on commit e23c14c

Please sign in to comment.