You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-macro-reform.md
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -74,16 +74,26 @@ crate should be imported to the syntax environment:
74
74
#[use_macros(vec, panic="fail")]
75
75
externcrate std;
76
76
77
-
#[use_macros(*)]
77
+
#[use_macros]
78
78
externcrate core;
79
79
```
80
80
81
+
The list of macros to import is optional. Omitting the list imports all macros,
82
+
similar to a glob `use`. (This is also the mechanism by which `std` will
83
+
inject its macros into every non-`no_std` crate.)
84
+
85
+
Importing with rename is an optional part of this proposal that will be
86
+
implemented for 1.0 only if time permits.
87
+
81
88
Macros imported this way can be used anywhere in the module after the
82
89
`extern crate` item, including in child modules. Since a macro-importing
83
90
`extern crate` must appear at the crate root, and view items come before
84
91
other items, this effectively means imported macros will be visible for
85
92
the entire crate.
86
93
94
+
Any name collision between macros, whether imported or defined in-crate, is a
95
+
hard error.
96
+
87
97
Many macros expand using other "helper macros" as an implementation detail.
88
98
For example, librustc's `declare_lint!` uses `lint_initializer!`. The client
89
99
should not know about this macro, although it still needs to be exported for
@@ -110,6 +120,11 @@ The macro `lint_initializer!`, imported from the same crate as `declare_lint!`,
110
120
will be visible only during further expansion of the result of invoking
111
121
`declare_lint!`.
112
122
123
+
`use_macros` on `macro_rules` is an optional part of this proposal that will be
124
+
implemented for 1.0 only if time permits. Without it, libraries that use
125
+
helper macros will need to list them in documentation so that users can import
126
+
them.
127
+
113
128
Procedural macros need their own way to manipulate the syntax environment, but
114
129
that's an unstable internal API, so it's outside the scope of this RFC.
115
130
@@ -125,17 +140,17 @@ rest of the enclosing module, including any child modules. A crate might start
125
140
with
126
141
127
142
```rust
128
-
#[use_macros(*)]
143
+
#[use_macros]
129
144
modmacros;
130
145
```
131
146
132
147
to define some macros for use by the whole crate, without putting those
133
148
definitions in `lib.rs`.
134
149
135
-
Note that `#[use_macros(*)]` is equivalent to the current `#[macro_escape]`.
136
-
However, the new convention is to use an outer attribute, in the file whose
137
-
syntax environment is affected, rather than an inner attribute in the file
138
-
defining the macros.
150
+
Note that `#[use_macros]`(without a list of names) is equivalent to the
151
+
current `#[macro_escape]`. However, the new convention is to use an outer
152
+
attribute, in the file whose syntax environment is affected, rather than an
153
+
inner attribute in the file defining the macros.
139
154
140
155
## Macro export and re-export
141
156
@@ -265,7 +280,7 @@ however I think we should allow it anyway, to encourage the habit of writing
265
280
This proposal is edited by Keegan McAllister. It has been refined through many
266
281
engaging discussions with:
267
282
268
-
* Brian Anderson, Shachaf Ben-Kiki, Lars Bergstrom, Nick Cameron, John Clements, Alex Crichton, Cathy Douglass, Steven Fackler, Manish Goregaokar, Dave Herman, Steve Klabnik, Felix S. Klock II, Niko Matsakis, Matthew McPherrin, Paul Stansifer, Sam Tobin-Hochstadt, Aaron Turon, Huon Wilson, Brendan Zabarauskas, Cameron Zwarich
283
+
* Brian Anderson, Shachaf Ben-Kiki, Lars Bergstrom, Nick Cameron, John Clements, Alex Crichton, Cathy Douglass, Steven Fackler, Manish Goregaokar, Dave Herman, Steve Klabnik, Felix S. Klock II, Niko Matsakis, Matthew McPherrin, Paul Stansifer, Sam Tobin-Hochstadt, Erick Tryzelaar, Aaron Turon, Huon Wilson, Brendan Zabarauskas, Cameron Zwarich
0 commit comments