-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add iter
macro
#137725
Open
oli-obk
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
oli-obk:i-want-to-move-it-move-it
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+427
−26
Open
Add iter
macro
#137725
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use rustc_ast::ptr::P; | ||
use rustc_ast::tokenstream::TokenStream; | ||
use rustc_ast::{ | ||
CaptureBy, ClosureBinder, Const, CoroutineKind, DUMMY_NODE_ID, Expr, ExprKind, ast, token, | ||
}; | ||
use rustc_errors::PResult; | ||
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult}; | ||
use rustc_span::Span; | ||
|
||
pub(crate) fn expand<'cx>( | ||
cx: &'cx mut ExtCtxt<'_>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> MacroExpanderResult<'cx> { | ||
let closure = match parse_closure(cx, sp, tts) { | ||
Ok(parsed) => parsed, | ||
Err(err) => { | ||
return ExpandResult::Ready(DummyResult::any(sp, err.emit())); | ||
} | ||
}; | ||
|
||
ExpandResult::Ready(base::MacEager::expr(closure)) | ||
} | ||
|
||
fn parse_closure<'a>( | ||
cx: &mut ExtCtxt<'a>, | ||
span: Span, | ||
stream: TokenStream, | ||
) -> PResult<'a, P<Expr>> { | ||
let mut parser = cx.new_parser_from_tts(stream); | ||
let mut closure_parser = parser.clone(); | ||
|
||
let coroutine_kind = Some(CoroutineKind::Gen { | ||
span, | ||
closure_id: DUMMY_NODE_ID, | ||
return_impl_trait_id: DUMMY_NODE_ID, | ||
}); | ||
|
||
match closure_parser.parse_expr() { | ||
Ok(mut closure) => { | ||
if let ast::ExprKind::Closure(c) = &mut closure.kind { | ||
if let Some(kind) = c.coroutine_kind { | ||
cx.dcx().span_err(kind.span(), "only plain closures allowed in `iter!`"); | ||
} | ||
c.coroutine_kind = coroutine_kind; | ||
if closure_parser.token != token::Eof { | ||
closure_parser.unexpected()?; | ||
} | ||
return Ok(closure); | ||
} | ||
} | ||
Err(diag) => diag.cancel(), | ||
} | ||
|
||
let lo = parser.token.span.shrink_to_lo(); | ||
let block = parser.parse_block_tail( | ||
lo, | ||
ast::BlockCheckMode::Default, | ||
rustc_parse::parser::AttemptLocalParseRecovery::No, | ||
)?; | ||
let fn_decl = cx.fn_decl(Default::default(), ast::FnRetTy::Default(span)); | ||
let closure = ast::Closure { | ||
binder: ClosureBinder::NotPresent, | ||
capture_clause: CaptureBy::Ref, | ||
constness: Const::No, | ||
coroutine_kind, | ||
movability: ast::Movability::Movable, | ||
fn_decl, | ||
body: cx.expr_block(block), | ||
fn_decl_span: span, | ||
fn_arg_span: span, | ||
}; | ||
if parser.token != token::Eof { | ||
parser.unexpected()?; | ||
} | ||
let span = lo.to(parser.token.span); | ||
Ok(cx.expr(span, ExprKind::Closure(Box::new(closure)))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/// Creates a new closure that returns an iterator where each iteration steps the given | ||
/// generator to the next `yield` statement. | ||
/// | ||
/// Similar to [`iter::from_fn`], but allows arbitrary control flow. | ||
/// | ||
/// [`iter::from_fn`]: crate::iter::from_fn | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(iter_macro, coroutines)] | ||
/// | ||
/// let it = std::iter::iter!{ | ||
/// yield 1; | ||
/// yield 2; | ||
/// yield 3; | ||
/// }; | ||
/// let v: Vec<_> = it.collect(); | ||
/// assert_eq!(v, [1, 2, 3]); | ||
/// ``` | ||
#[unstable(feature = "iter_macro", issue = "none", reason = "generators are unstable")] | ||
#[allow_internal_unstable(coroutines, iter_from_coroutine)] | ||
#[cfg_attr(not(bootstrap), rustc_builtin_macro)] | ||
pub macro iter($($t:tt)*) { | ||
/* compiler-builtin */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//@ run-pass | ||
|
||
#![feature(iter_macro, yield_expr)] | ||
|
||
use std::iter::iter; | ||
|
||
fn main() { | ||
let i = iter! { | ||
yield 0; | ||
for x in 5..10 { | ||
yield x * 2; | ||
} | ||
}; | ||
let mut i = i(); | ||
assert_eq!(i.next(), Some(0)); | ||
assert_eq!(i.next(), Some(10)); | ||
assert_eq!(i.next(), Some(12)); | ||
assert_eq!(i.next(), Some(14)); | ||
assert_eq!(i.next(), Some(16)); | ||
assert_eq!(i.next(), Some(18)); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//@ run-pass | ||
|
||
#![feature(iter_macro, yield_expr)] | ||
|
||
use std::iter::iter; | ||
|
||
fn main() { | ||
let i = iter! {|foo| { | ||
yield foo; | ||
for x in 5..10 { | ||
yield x * 2; | ||
} | ||
}}; | ||
let mut i = i(3); | ||
assert_eq!(i.next(), Some(3)); | ||
assert_eq!(i.next(), Some(10)); | ||
assert_eq!(i.next(), Some(12)); | ||
assert_eq!(i.next(), Some(14)); | ||
assert_eq!(i.next(), Some(16)); | ||
assert_eq!(i.next(), Some(18)); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@ run-pass | ||
|
||
#![feature(iter_macro, yield_expr)] | ||
|
||
use std::iter::iter; | ||
|
||
fn main() { | ||
let i = { | ||
let s = String::new(); | ||
iter! { move || { | ||
yield s.len(); | ||
for x in 5..10 { | ||
yield x * 2; | ||
} | ||
}} | ||
}; | ||
let mut i = i(); | ||
assert_eq!(i.next(), Some(0)); | ||
assert_eq!(i.next(), Some(10)); | ||
assert_eq!(i.next(), Some(12)); | ||
assert_eq!(i.next(), Some(14)); | ||
assert_eq!(i.next(), Some(16)); | ||
assert_eq!(i.next(), Some(18)); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
assert_eq!(i.next(), None); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this part can be ripped out now