Skip to content

Commit

Permalink
ICE with declarative macro
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulGrandperrin committed Apr 2, 2018
1 parent 227bcc3 commit 3cb0834
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
34 changes: 0 additions & 34 deletions futures-await-async-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,43 +320,9 @@ pub fn async_stream(attribute: TokenStream, function: TokenStream) -> TokenStrea
})
}

#[proc_macro]
pub fn async_block(input: TokenStream) -> TokenStream {
let input = TokenStream::from(TokenTree {
kind: TokenNode::Group(Delimiter::Brace, input),
span: proc_macro::Span::def_site(),
});
let expr = syn::parse(input)
.expect("failed to parse tokens as an expression");
let expr = ExpandAsyncFor.fold_expr(expr);

let mut tokens = quote_cs! {
::futures::__rt::gen
};

// Use some manual token construction here instead of `quote_cs!` to ensure
// that we get the `call_site` span instead of the default span.
let span = Span::call_site();
syn::token::Paren(span).surround(&mut tokens, |tokens| {
syn::token::Unsafe(span).to_tokens(tokens);
syn::token::Brace(span).surround(tokens, |tokens| {
syn::token::Static(span).to_tokens(tokens);
syn::token::Move(span).to_tokens(tokens);
syn::token::OrOr([span, span]).to_tokens(tokens);
syn::token::Brace(span).surround(tokens, |tokens| {
(quote_cs! {
if false { yield ::futures::Async::NotReady }
}).to_tokens(tokens);
expr.to_tokens(tokens);
});
});


});

tokens.into()
}

#[proc_macro]
pub fn async_stream_block(input: TokenStream) -> TokenStream {
let input = TokenStream::from(TokenTree {
Expand Down
18 changes: 18 additions & 0 deletions futures-await-await-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ macro_rules! await {
})
}

#[macro_export]
macro_rules! async_block {
($e:expr) => {
::futures::__rt::gen(unsafe {
static move || {
if false {
yield ::futures::Async::NotReady
}
{
$e
}
}
}
)
}

}

///
/// Await an item from the stream
/// Basically it does same as `await` macro, but for streams
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub use futures::*;

pub mod prelude {
pub use futures::prelude::*;
pub use async_macro::{async, async_stream, async_block, async_stream_block};
pub use await_macro::{await, stream_yield, await_item};
pub use async_macro::{async, async_stream, async_stream_block};
pub use await_macro::{await, async_block, stream_yield, await_item};
}

/// A hidden module that's the "runtime support" for the async/await syntax.
Expand Down

0 comments on commit 3cb0834

Please sign in to comment.