-
Notifications
You must be signed in to change notification settings - Fork 13k
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 syntax extension ary![expr, ..N] #14272
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Implementation of the ary![] macro | ||
|
||
use ast; | ||
use codemap::Span; | ||
use ext::base; | ||
use ext::base::{ExtCtxt, MacExpr, MacResult, DummyResult}; | ||
use ext::build::AstBuilder; | ||
use parse; | ||
use parse::token; | ||
|
||
pub fn expand_ary(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) | ||
-> Box<base::MacResult> { | ||
let mut p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), Vec::from_slice(tts)); | ||
let val_expr = p.parse_expr(); | ||
p.expect(&token::COMMA); | ||
p.expect(&token::DOTDOT); | ||
// negative literals should not be a fatal error | ||
if p.eat(&token::BINOP(token::MINUS)) { | ||
p.span_err(p.last_span, "expected positive integral literal"); | ||
return DummyResult::expr(sp); | ||
} | ||
let lit = p.parse_lit(); | ||
let count = match lit.node { | ||
ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) if i > 0 => i as u64, | ||
ast::LitUint(u, _) if u > 0 => u, | ||
_ => { | ||
p.span_err(lit.span, "expected positive integral literal"); | ||
return DummyResult::expr(sp); | ||
} | ||
}; | ||
let count = match count.to_uint() { | ||
None => { | ||
p.span_err(lit.span, "integral literal out of range"); | ||
return DummyResult::expr(sp); | ||
} | ||
Some(x) => x | ||
}; | ||
let exprs = Vec::from_fn(count, |_| val_expr.clone()); | ||
MacExpr::new(cx.expr_vec(sp, exprs)) | ||
} |
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,14 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let count = 4u; | ||
let v = ary![3i, ..count]; //~ ERROR unexpected token: `count` | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/syntax-extension-ary-non-positive-integral.rs
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,15 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let a = ary![42i, ..-5]; //~ ERROR expected positive integral literal | ||
let b = ary![42i, ..0i32]; //~ ERROR expected positive integral literal | ||
let c = ary![42i, ..0u]; //~ ERROR expected positive integral literal | ||
} |
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,58 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let a = ary![42i, ..1]; | ||
assert_eq!(a.as_slice(), [42i].as_slice()); | ||
assert_eq!(a.as_slice(), [42i, ..1].as_slice()); | ||
|
||
let b = ary![42i, ..10]; | ||
assert_eq!(b.as_slice(), [42i, ..10].as_slice()); | ||
|
||
let c = ary![None::<Vec<u8>>, ..10]; | ||
assert_eq!(c.len(), 10); | ||
assert_eq!(c.as_slice(), Vec::from_elem(10, None::<Vec<u8>>).as_slice()); | ||
|
||
#[deriving(Eq)] | ||
// non-Copy, non-Clone | ||
struct Foo { | ||
x: uint, | ||
nocopy: ::std::kinds::marker::NoCopy | ||
} | ||
|
||
impl Foo { | ||
fn new(x: uint) -> Foo { | ||
Foo { | ||
x: x, | ||
nocopy: ::std::kinds::marker::NoCopy | ||
} | ||
} | ||
} | ||
|
||
let d = ary![Foo::new(42), ..10]; | ||
assert_eq!(d.len(), 10); | ||
assert!(d.as_slice() == Vec::from_fn(10, |_| Foo::new(42)).as_slice()); | ||
|
||
let mut it = range(0, 4); | ||
let e = ary![it.next(), ..8]; | ||
assert_eq!(e.as_slice(), &[Some(0), Some(1), Some(2), Some(3), None, None, None, None]); | ||
|
||
let f = ary![format!("{}", 42i), ..4]; | ||
assert_eq!(f.len(), 4); | ||
for s in f.iter() { | ||
assert_eq!(s.as_slice(), "42"); | ||
} | ||
|
||
let g = ary![vec![1u,2,3], ..4]; | ||
assert_eq!(g.len(), 4); | ||
for v in g.iter() { | ||
assert_eq!(v.as_slice(), &[1u,2,3]); | ||
} | ||
} |
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 references the same
Expr
repeatedly in the vector. Is this ok, or should I be calling.duplicate()
on it?