about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-01-01 07:21:23 +0000
committerbors <bors@rust-lang.org>2018-01-01 07:21:23 +0000
commit1bcc6dc7ea430b7d403a57f60e48b3c3cdfcf562 (patch)
tree2073115c9aa906116a5698340385e1aacfca21b4 /src/test
parentf3ca88cff7dc397f409f561321ee3c4021c05436 (diff)
parent8b4bdc2f3f753e0d0b00ecc892a813e9786621e9 (diff)
Auto merge of #46895 - ricochet1k:macro-lifetimes, r=jseyfried
Allow lifetimes in macros

This is a resurrection of PR #41927 which was a resurrection of #33135, which is intended to fix #34303.

In short, this allows macros_rules! to use :lifetime as a matcher to match 'lifetimes.

Still to do:
- [x]  Feature gate
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/macro-lifetime-used-with-bound.rs25
-rw-r--r--src/test/run-pass/macro-lifetime-used-with-labels.rs44
-rw-r--r--src/test/run-pass/macro-lifetime-used-with-static.rs25
-rw-r--r--src/test/run-pass/macro-lifetime.rs25
-rw-r--r--src/test/ui/feature-gate-macro-lifetime-matcher.rs19
-rw-r--r--src/test/ui/feature-gate-macro-lifetime-matcher.stderr10
6 files changed, 148 insertions, 0 deletions
diff --git a/src/test/run-pass/macro-lifetime-used-with-bound.rs b/src/test/run-pass/macro-lifetime-used-with-bound.rs
new file mode 100644
index 00000000000..b0c9280b6ce
--- /dev/null
+++ b/src/test/run-pass/macro-lifetime-used-with-bound.rs
@@ -0,0 +1,25 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(macro_lifetime_matcher)]
+
+macro_rules! foo {
+    ($l:lifetime, $l2:lifetime) => {
+        fn f<$l: $l2, $l2>(arg: &$l str, arg2: &$l2 str) -> &$l str {
+            arg
+        }
+    }
+}
+
+pub fn main() {
+    foo!('a, 'b);
+    let x: &'static str = f("hi", "there");
+    assert_eq!("hi", x);
+}
diff --git a/src/test/run-pass/macro-lifetime-used-with-labels.rs b/src/test/run-pass/macro-lifetime-used-with-labels.rs
new file mode 100644
index 00000000000..8a2d76e17df
--- /dev/null
+++ b/src/test/run-pass/macro-lifetime-used-with-labels.rs
@@ -0,0 +1,44 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(unreachable_code)]
+#![feature(macro_lifetime_matcher)]
+
+macro_rules! x {
+    ($a:lifetime) => {
+        $a: loop {
+            break $a;
+            panic!("failed");
+        }
+    }
+}
+macro_rules! br {
+    ($a:lifetime) => {
+        break $a;
+    }
+}
+macro_rules! br2 {
+    ($b:lifetime) => {
+        'b: loop {
+            break $b; // this $b should refer to the outer loop.
+        }
+    }
+}
+fn main() {
+    x!('a);
+    'c: loop {
+        br!('c);
+        panic!("failed");
+    }
+    'b: loop {
+        br2!('b);
+        panic!("failed");
+    }
+}
diff --git a/src/test/run-pass/macro-lifetime-used-with-static.rs b/src/test/run-pass/macro-lifetime-used-with-static.rs
new file mode 100644
index 00000000000..468ee2e9436
--- /dev/null
+++ b/src/test/run-pass/macro-lifetime-used-with-static.rs
@@ -0,0 +1,25 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(macro_lifetime_matcher)]
+
+macro_rules! foo {
+    ($l:lifetime) => {
+        fn f(arg: &$l str) -> &$l str {
+            arg
+        }
+    }
+}
+
+pub fn main() {
+    foo!('static);
+    let x: &'static str = f("hi");
+    assert_eq!("hi", x);
+}
diff --git a/src/test/run-pass/macro-lifetime.rs b/src/test/run-pass/macro-lifetime.rs
new file mode 100644
index 00000000000..db521ca7f10
--- /dev/null
+++ b/src/test/run-pass/macro-lifetime.rs
@@ -0,0 +1,25 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(macro_lifetime_matcher)]
+
+macro_rules! foo {
+    ($l:lifetime) => {
+        fn f<$l>(arg: &$l str) -> &$l str {
+            arg
+        }
+    }
+}
+
+pub fn main() {
+    foo!('a);
+    let x: &'static str = f("hi");
+    assert_eq!("hi", x);
+}
diff --git a/src/test/ui/feature-gate-macro-lifetime-matcher.rs b/src/test/ui/feature-gate-macro-lifetime-matcher.rs
new file mode 100644
index 00000000000..0d107d283cd
--- /dev/null
+++ b/src/test/ui/feature-gate-macro-lifetime-matcher.rs
@@ -0,0 +1,19 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that the :lifetime macro fragment cannot be used when macro_lifetime_matcher
+// feature gate is not used.
+
+macro_rules! m { ($lt:lifetime) => {} }
+//~^ ERROR :lifetime fragment specifier is experimental and subject to change
+
+fn main() {
+    m!('a);
+}
diff --git a/src/test/ui/feature-gate-macro-lifetime-matcher.stderr b/src/test/ui/feature-gate-macro-lifetime-matcher.stderr
new file mode 100644
index 00000000000..e78f7684cf2
--- /dev/null
+++ b/src/test/ui/feature-gate-macro-lifetime-matcher.stderr
@@ -0,0 +1,10 @@
+error: :lifetime fragment specifier is experimental and subject to change (see issue #46895)
+  --> $DIR/feature-gate-macro-lifetime-matcher.rs:14:19
+   |
+14 | macro_rules! m { ($lt:lifetime) => {} }
+   |                   ^^^^^^^^^^^^
+   |
+   = help: add #![feature(macro_lifetime_matcher)] to the crate attributes to enable
+
+error: aborting due to previous error
+