1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![feature(macro_literal_matcher)]
13 macro_rules! mtester {
15 &format!("macro caught literal: {}", $l)
18 &format!("macro caught expr: {}", $e)
22 macro_rules! two_negative_literals {
23 ($l1:literal $l2:literal) => {
24 &format!("macro caught literals: {}, {}", $l1, $l2)
28 macro_rules! only_expr {
30 &format!("macro caught expr: {}", $e)
34 macro_rules! mtester_dbg {
36 &format!("macro caught literal: {:?}", $l)
39 &format!("macro caught expr: {:?}", $e)
43 macro_rules! catch_range {
44 ($s:literal ..= $e:literal) => {
45 &format!("macro caught literal: {} ..= {}", $s, $e)
47 (($s:expr) ..= ($e:expr)) => { // Must use ')' before '..='
48 &format!("macro caught expr: {} ..= {}", $s, $e)
52 macro_rules! pat_match {
53 ($s:literal ..= $e:literal) => {
55 $s ..= $e => "literal, in range",
56 _ => "literal, other",
67 macro_rules! match_attr {
68 (#[$attr:meta] $e:literal) => {
69 "attr matched literal"
71 (#[$attr:meta] $e:expr) => {
76 macro_rules! match_produced_attr {
78 // Struct with doc comment passed via $literal
80 struct LiteralProduced;
87 macro_rules! test_user {
88 ($s:literal, $e:literal) => {
90 let mut v = Vec::new();
97 ($s:expr, $e: expr) => {
99 let mut v = Vec::new();
109 // Cases where 'literal' catches
110 assert_eq!(mtester!("str"), "macro caught literal: str");
111 assert_eq!(mtester!(2), "macro caught literal: 2");
112 assert_eq!(mtester!(2.2), "macro caught literal: 2.2");
113 assert_eq!(mtester!(1u32), "macro caught literal: 1");
114 assert_eq!(mtester!(0x32), "macro caught literal: 50");
115 assert_eq!(mtester!('c'), "macro caught literal: c");
116 assert_eq!(mtester!(-1.2), "macro caught literal: -1.2");
117 assert_eq!(two_negative_literals!(-2 -3), "macro caught literals: -2, -3");
118 assert_eq!(catch_range!(2 ..= 3), "macro caught literal: 2 ..= 3");
119 assert_eq!(match_attr!(#[attr] 1), "attr matched literal");
120 assert_eq!(test_user!(10, 20), "literal");
121 assert_eq!(mtester!(false), "macro caught literal: false");
122 assert_eq!(mtester!(true), "macro caught literal: true");
123 match_produced_attr!("a");
124 let _a = LiteralProduced;
125 assert_eq!(pat_match!(1 ..= 3), "literal, in range");
126 assert_eq!(pat_match!(4 ..= 6), "literal, other");
128 // Cases where 'expr' catches
129 assert_eq!(mtester!((-1.2)), "macro caught expr: -1.2");
130 assert_eq!(only_expr!(-1.2), "macro caught expr: -1.2");
131 assert_eq!(mtester!((1 + 3)), "macro caught expr: 4");
132 assert_eq!(mtester_dbg!(()), "macro caught expr: ()");
133 assert_eq!(catch_range!((1 + 1) ..= (2 + 2)), "macro caught expr: 2 ..= 4");
134 assert_eq!(match_attr!(#[attr] (1 + 2)), "attr matched expr");
135 assert_eq!(test_user!(10, (20 + 2)), "expr");
137 match_produced_attr!((3 + 2));
138 let _b = ExprProduced;
140 // Cases where 'pat' matched
141 assert_eq!(pat_match!(3), "pat, single");
142 assert_eq!(pat_match!(6), "pat, other");