Skip to content

Commit

Permalink
Avoid calling to_vec() unnecessarily in parser.
Browse files Browse the repository at this point in the history
Also, rename the OptVec-to-vector conversion method to
opt_vec::take_vec() and convert from a method into a fn
because I fear strange bugs.
  • Loading branch information
nikomatsakis committed Mar 2, 2013
1 parent 50c08db commit ca9549b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ pub fn trans_arg_expr(bcx: block,

ast::by_copy => {
debug!("by copy arg with type %s, storing to scratch",
ty_to_str(ccx.tcx, arg_datum.ty));
bcx.ty_to_str(arg_datum.ty));
let scratch = scratch_datum(bcx, arg_datum.ty, false);

arg_datum.store_to_datum(bcx, arg_expr.id,
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/auto_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ fn mk_impl(
let ty = cx.ty_path(
span,
~[ident],
generics.ty_params.map(
|tp| cx.ty_path(span, ~[tp.ident], ~[])).to_vec()
opt_vec::take_vec(generics.ty_params.map(
|tp| cx.ty_path(span, ~[tp.ident], ~[])))
);

let generics = ast::Generics {
Expand Down
10 changes: 6 additions & 4 deletions src/libsyntax/ext/pipes/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,15 @@ impl ext_ctxt_ast_builder for ext_ctxt {
}

fn ty_vars(&self, ty_params: &OptVec<ast::TyParam>) -> ~[@ast::Ty] {
ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))).to_vec()
opt_vec::take_vec(
ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))))
}

fn ty_vars_global(&self,
ty_params: &OptVec<ast::TyParam>) -> ~[@ast::Ty] {
ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))).to_vec()
opt_vec::take_vec(
ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))))
}
}
18 changes: 13 additions & 5 deletions src/libsyntax/opt_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ pub fn with<T>(+t: T) -> OptVec<T> {
Vec(~[t])
}

pub fn from<T>(+t: ~[T]) -> OptVec<T> {
if t.len() == 0 {
Empty
} else {
Vec(t)
}
}

impl<T> OptVec<T> {
fn push(&mut self, +t: T) {
match *self {
Expand Down Expand Up @@ -70,12 +78,12 @@ impl<T> OptVec<T> {
Vec(ref v) => v.len()
}
}
}

pure fn to_vec(self) -> ~[T] {
match self {
Empty => ~[],
Vec(v) => v
}
pub fn take_vec<T>(+v: OptVec<T>) -> ~[T] {
match v {
Empty => ~[],
Vec(v) => v
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ pub impl Parser {
let remaining_exprs =
self.parse_seq_to_end(token::RBRACKET,
seq_sep_trailing_allowed(token::COMMA),
|p| p.parse_expr()).to_vec();
|p| p.parse_expr());
ex = expr_vec(~[first_expr] + remaining_exprs, mutbl);
} else {
// Vector with one element.
Expand Down Expand Up @@ -1419,7 +1419,7 @@ pub impl Parser {
vec::append(
self.parse_seq_to_before_end(
ket, seq_sep_none(),
|p| p.parse_token_tree()).to_vec(),
|p| p.parse_token_tree()),
// the close delimiter:
~[parse_any_tt_tok(self)])))
}
Expand Down Expand Up @@ -2727,7 +2727,7 @@ pub impl Parser {
let result = self.parse_seq_to_gt(
Some(token::COMMA),
|p| p.parse_ty(false));
result.to_vec()
opt_vec::take_vec(result)
}

fn parse_fn_decl(parse_arg_fn: fn(Parser) -> arg_or_capture_item)
Expand Down Expand Up @@ -2819,7 +2819,7 @@ pub impl Parser {
args_or_capture_items =
self.parse_seq_to_before_end(token::RPAREN,
sep,
parse_arg_fn).to_vec();
parse_arg_fn);
}
token::RPAREN => {
args_or_capture_items = ~[];
Expand All @@ -2835,7 +2835,7 @@ pub impl Parser {
args_or_capture_items =
self.parse_seq_to_before_end(token::RPAREN,
sep,
parse_arg_fn).to_vec();
parse_arg_fn);
}

self.expect(token::RPAREN);
Expand Down Expand Up @@ -3032,7 +3032,7 @@ pub impl Parser {
fn parse_trait_ref_list(ket: token::Token) -> ~[@trait_ref] {
self.parse_seq_to_before_end(
ket, seq_sep_none(),
|p| p.parse_trait_ref()).to_vec()
|p| p.parse_trait_ref())
}

fn parse_item_struct() -> item_info {
Expand Down

0 comments on commit ca9549b

Please sign in to comment.