Skip to content

Commit 3768ef4

Browse files
committed
auto merge of #16891 : eddyb/rust/patlit-from-expr-macros, r=kballard
Enables any macros using `MacExpr` to be treated as patterns when they produce a literal in the form `ExprLit` (e.g. `stringify!` or `line!`). Fixes #16876.
2 parents eb7589a + a9c3109 commit 3768ef4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/libsyntax/ext/base.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ impl MacResult for MacExpr {
154154
fn make_expr(&self) -> Option<Gc<ast::Expr>> {
155155
Some(self.e)
156156
}
157+
fn make_pat(&self) -> Option<Gc<ast::Pat>> {
158+
match self.e.node {
159+
ast::ExprLit(_) => Some(box(GC) ast::Pat {
160+
id: ast::DUMMY_NODE_ID,
161+
node: ast::PatLit(self.e),
162+
span: self.e.span
163+
}),
164+
_ => None
165+
}
166+
}
157167
}
158168
/// A convenience type for macros that return a single pattern.
159169
pub struct MacPat {

src/test/run-pass/concat.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ pub fn main() {
1818
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()),
1919
"12344.0atrue"
2020
);
21+
22+
assert!(match "12344.0atrue" {
23+
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()) => true,
24+
_ => false
25+
})
2126
}

src/test/run-pass/syntax-extension-source-utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ pub fn main() {
4343
[1] == (42 as u8)); // '*'
4444
// The Windows tests are wrapped in an extra module for some reason
4545
assert!((m1::m2::where_am_i().as_slice().ends_with("m1::m2")));
46+
47+
assert!(match (47, "( 2 * 3 ) + 5") {
48+
(line!(), stringify!((2*3) + 5)) => true,
49+
_ => false
50+
})
4651
}

0 commit comments

Comments
 (0)