Skip to content

Commit

Permalink
Allow ExprLit expression macros to be used in patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Aug 30, 2014
1 parent f297366 commit a9c3109
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libsyntax/ext/base.rs
Expand Up @@ -154,6 +154,16 @@ impl MacResult for MacExpr {
fn make_expr(&self) -> Option<Gc<ast::Expr>> {
Some(self.e)
}
fn make_pat(&self) -> Option<Gc<ast::Pat>> {
match self.e.node {
ast::ExprLit(_) => Some(box(GC) ast::Pat {
id: ast::DUMMY_NODE_ID,
node: ast::PatLit(self.e),
span: self.e.span
}),
_ => None
}
}
}
/// A convenience type for macros that return a single pattern.
pub struct MacPat {
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/concat.rs
Expand Up @@ -18,4 +18,9 @@ pub fn main() {
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()),
"12344.0atrue"
);

assert!(match "12344.0atrue" {
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()) => true,
_ => false
})
}
5 changes: 5 additions & 0 deletions src/test/run-pass/syntax-extension-source-utils.rs
Expand Up @@ -43,4 +43,9 @@ pub fn main() {
[1] == (42 as u8)); // '*'
// The Windows tests are wrapped in an extra module for some reason
assert!((m1::m2::where_am_i().as_slice().ends_with("m1::m2")));

assert!(match (47, "( 2 * 3 ) + 5") {
(line!(), stringify!((2*3) + 5)) => true,
_ => false
})
}

5 comments on commit a9c3109

@bors
Copy link
Contributor

@bors bors commented on a9c3109 Sep 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from kballard
at eddyb@a9c3109

@bors
Copy link
Contributor

@bors bors commented on a9c3109 Sep 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging eddyb/rust/patlit-from-expr-macros = a9c3109 into auto

@bors
Copy link
Contributor

@bors bors commented on a9c3109 Sep 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eddyb/rust/patlit-from-expr-macros = a9c3109 merged ok, testing candidate = 3768ef4

@bors
Copy link
Contributor

@bors bors commented on a9c3109 Sep 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 3768ef4

Please sign in to comment.