]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/macro-pub-matcher.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / macro-pub-matcher.rs
1 // Copyright 2017 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.
4 //
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.
10
11 #![allow(dead_code, unused_imports)]
12 #![feature(macro_vis_matcher, crate_visibility_modifier)]
13
14 /**
15 Ensure that `:vis` matches can be captured in existing positions, and passed
16 through without the need for reparse tricks.
17 */
18 macro_rules! vis_passthru {
19 ($vis:vis const $name:ident: $ty:ty = $e:expr;) => { $vis const $name: $ty = $e; };
20 ($vis:vis enum $name:ident {}) => { $vis struct $name {} };
21 ($vis:vis extern "C" fn $name:ident() {}) => { $vis extern "C" fn $name() {} };
22 ($vis:vis fn $name:ident() {}) => { $vis fn $name() {} };
23 ($vis:vis mod $name:ident {}) => { $vis mod $name {} };
24 ($vis:vis static $name:ident: $ty:ty = $e:expr;) => { $vis static $name: $ty = $e; };
25 ($vis:vis struct $name:ident;) => { $vis struct $name; };
26 ($vis:vis trait $name:ident {}) => { $vis trait $name {} };
27 ($vis:vis type $name:ident = $ty:ty;) => { $vis type $name = $ty; };
28 ($vis:vis use $path:ident as $name:ident;) => { $vis use self::$path as $name; };
29 }
30
31 mod with_pub {
32 vis_passthru! { pub const A: i32 = 0; }
33 vis_passthru! { pub enum B {} }
34 vis_passthru! { pub extern "C" fn c() {} }
35 vis_passthru! { pub mod d {} }
36 vis_passthru! { pub static E: i32 = 0; }
37 vis_passthru! { pub struct F; }
38 vis_passthru! { pub trait G {} }
39 vis_passthru! { pub type H = i32; }
40 vis_passthru! { pub use A as I; }
41 }
42
43 mod without_pub {
44 vis_passthru! { const A: i32 = 0; }
45 vis_passthru! { enum B {} }
46 vis_passthru! { extern "C" fn c() {} }
47 vis_passthru! { mod d {} }
48 vis_passthru! { static E: i32 = 0; }
49 vis_passthru! { struct F; }
50 vis_passthru! { trait G {} }
51 vis_passthru! { type H = i32; }
52 vis_passthru! { use A as I; }
53 }
54
55 mod with_pub_restricted {
56 vis_passthru! { pub(crate) const A: i32 = 0; }
57 vis_passthru! { pub(crate) enum B {} }
58 vis_passthru! { pub(crate) extern "C" fn c() {} }
59 vis_passthru! { pub(crate) mod d {} }
60 vis_passthru! { pub(crate) static E: i32 = 0; }
61 vis_passthru! { pub(crate) struct F; }
62 vis_passthru! { pub(crate) trait G {} }
63 vis_passthru! { pub(crate) type H = i32; }
64 vis_passthru! { pub(crate) use A as I; }
65 }
66
67 mod with_crate {
68 vis_passthru! { crate const A: i32 = 0; }
69 vis_passthru! { crate enum B {} }
70 vis_passthru! { crate extern "C" fn c() {} }
71 vis_passthru! { crate mod d {} }
72 vis_passthru! { crate static E: i32 = 0; }
73 vis_passthru! { crate struct F; }
74 vis_passthru! { crate trait G {} }
75 vis_passthru! { crate type H = i32; }
76 vis_passthru! { crate use A as I; }
77 }
78
79 mod garden {
80 mod with_pub_restricted_path {
81 vis_passthru! { pub(in garden) const A: i32 = 0; }
82 vis_passthru! { pub(in garden) enum B {} }
83 vis_passthru! { pub(in garden) extern "C" fn c() {} }
84 vis_passthru! { pub(in garden) mod d {} }
85 vis_passthru! { pub(in garden) static E: i32 = 0; }
86 vis_passthru! { pub(in garden) struct F; }
87 vis_passthru! { pub(in garden) trait G {} }
88 vis_passthru! { pub(in garden) type H = i32; }
89 vis_passthru! { pub(in garden) use A as I; }
90 }
91 }
92
93 /*
94 Ensure that the `:vis` matcher works in a more complex situation: parsing a
95 struct definition.
96 */
97 macro_rules! vis_parse_struct {
98 ($(#[$($attrs:tt)*])* $vis:vis struct $name:ident {$($body:tt)*}) => {
99 vis_parse_struct! { @parse_fields $(#[$($attrs)*])*, $vis, $name, $($body)* }
100 };
101
102 ($(#[$($attrs:tt)*])* $vis:vis struct $name:ident ($($body:tt)*);) => {
103 vis_parse_struct! { @parse_tuple $(#[$($attrs)*])*, $vis, $name, $($body)* }
104 };
105
106 (@parse_fields
107 $(#[$attrs:meta])*, $vis:vis, $name:ident, $($fvis:vis $fname:ident: $fty:ty),* $(,)*) => {
108 $(#[$attrs])* $vis struct $name { $($fvis $fname: $fty,)* }
109 };
110
111 (@parse_tuple
112 $(#[$attrs:meta])*, $vis:vis, $name:ident, $($fvis:vis $fty:ty),* $(,)*) => {
113 $(#[$attrs])* $vis struct $name ( $($fvis $fty,)* );
114 };
115 }
116
117 mod test_struct {
118 vis_parse_struct! { pub(crate) struct A { pub a: i32, b: i32, pub(crate) c: i32 } }
119 vis_parse_struct! { pub struct B { a: i32, pub(crate) b: i32, pub c: i32 } }
120 vis_parse_struct! { struct C { pub(crate) a: i32, pub b: i32, c: i32 } }
121
122 vis_parse_struct! { pub(crate) struct D (pub i32, i32, pub(crate) i32); }
123 vis_parse_struct! { pub struct E (i32, pub(crate) i32, pub i32); }
124 vis_parse_struct! { struct F (pub(crate) i32, pub i32, i32); }
125 }
126
127 fn main() {}