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