Skip to content

Commit

Permalink
Simplifications to statement macro handling.
Browse files Browse the repository at this point in the history
SmallVector::pop no longer worries about converting a Many repr downward
to One or Zero.

expand_stmt makes use of `if let` for style purposes.
  • Loading branch information
chris-chambers committed Apr 11, 2015
1 parent fae29e4 commit 22eb319
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
25 changes: 11 additions & 14 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,20 +772,17 @@ fn expand_stmt(stmt: P<Stmt>, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> {
// If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion.
if style == MacStmtWithSemicolon {
match fully_expanded.pop() {
Some(stmt) => {
let new_stmt = stmt.map(|Spanned {node, span}| {
Spanned {
node: match node {
StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
_ => node /* might already have a semi */
},
span: span
}
});
fully_expanded.push(new_stmt);
}
None => (),
if let Some(stmt) = fully_expanded.pop() {
let new_stmt = stmt.map(|Spanned {node, span}| {
Spanned {
node: match node {
StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
_ => node /* might already have a semi */
},
span: span
}
});
fully_expanded.push(new_stmt);
}
}

Expand Down
24 changes: 1 addition & 23 deletions src/libsyntax/util/small_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,7 @@ impl<T> SmallVector<T> {
_ => unreachable!()
}
}
Many(..) => {
let mut many = mem::replace(&mut self.repr, Zero);
let item =
match many {
Many(ref mut vs) if vs.len() == 1 => {
// self.repr is already Zero
vs.pop()
},
Many(ref mut vs) if vs.len() == 2 => {
let item = vs.pop();
mem::replace(&mut self.repr, One(vs.pop().unwrap()));
item
},
Many(ref mut vs) if vs.len() > 2 => {
let item = vs.pop();
let rest = mem::replace(vs, vec!());
mem::replace(&mut self.repr, Many(rest));
item
},
_ => unreachable!()
};
item
}
Many(ref mut vs) => vs.pop(),
}
}

Expand Down

0 comments on commit 22eb319

Please sign in to comment.