Commit 4f9c9ed
committed
Auto merge of rust-lang#40847 - jseyfried:decl_macro, r=nrc
Initial implementation of declarative macros 2.0
Implement declarative macros 2.0 (rust-lang/rfcs#1584) behind `#![feature(decl_macro)]`.
Differences from `macro_rules!` include:
- new syntax: `macro m(..) { .. }` instead of `macro_rules! m { (..) => { .. } }`
- declarative macros are items:
```rust
// crate A:
pub mod foo {
m!(); // use before definition; declaration order is irrelevant
pub macro m() {} // `pub`, `pub(super)`, etc. work
}
fn main() {
foo::m!(); // named like other items
{ use foo::m as n; n!(); } // imported like other items
}
pub use foo::m; // re-exported like other items
// crate B:
extern crate A; // no need for `#[macro_use]`
A::foo::m!(); A::m!();
```
- Racket-like hygiene for items, imports, methods, fields, type parameters, privacy, etc.
- Intuitively, names in a macro definition are resolved in the macro definition's scope, not the scope in which the macro is used.
- This [explaination](http://beautifulracket.com/explainer/hygiene.html) of hygiene for Racket applies here (except for the "Breaking Hygiene" section). I wrote a similar [explanation](https://github.com/jseyfried/rfcs/blob/hygiene/text/0000-hygiene.md) for Rust.
- Generally speaking, if `fn f() { <body> }` resolves, `pub macro m() { <body> } ... m!()` also resolves, even if `m!()` is in a separate crate.
- `::foo::bar` in a `macro` behaves like `$crate::foo::bar` in a `macro_rules!`, except it can access everything visible from the `macro` (thus more permissive).
- See [`src/test/{run-pass, compile-fail}/hygiene`](rust-lang@afe7d89) for examples. Small example:
```rust
mod foo {
fn f() { println!("hello world"); }
pub macro m() { f(); }
}
fn main() { foo::m!(); }
```
Limitations:
- This does not address planned changes to matchers (`expr`,`ty`, etc.), c.f. rust-lang#26361.
- Lints (including stability and deprecation) and `unsafe` are not hygienic.
- adding hygiene here will be mostly or entirely backwards compatible
- Nested macro definitions (a `macro` inside another `macro`) don't always work correctly when invoked from external crates.
- pending improvements in how we encode macro definitions in crate metadata
- There is no way to "escape" hygiene without using a procedural macro.
r? @nrcFile tree
67 files changed
+1574
-479
lines changed- src
- doc/unstable-book/src/language-features
- librustc_const_eval
- librustc_metadata
- librustc_passes
- librustc_privacy
- librustc_resolve
- librustc_save_analysis
- librustc_typeck
- check
- method
- librustc
- hir
- map
- ich
- lint
- middle
- ty
- librustdoc
- clean
- libsyntax_ext
- deriving
- generic
- libsyntax_pos
- libsyntax
- diagnostics
- ext
- tt
- parse
- print
- test
- compile-fail
- hygiene
- run-pass/hygiene
- auxiliary
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
67 files changed
+1574
-479
lines changedLines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| 130 | + | |
129 | 131 | | |
130 | 132 | | |
131 | 133 | | |
| |||
393 | 395 | | |
394 | 396 | | |
395 | 397 | | |
396 | | - | |
| 398 | + | |
397 | 399 | | |
398 | 400 | | |
399 | 401 | | |
| |||
495 | 497 | | |
496 | 498 | | |
497 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
498 | 508 | | |
499 | 509 | | |
500 | 510 | | |
| |||
546 | 556 | | |
547 | 557 | | |
548 | 558 | | |
549 | | - | |
| 559 | + | |
550 | 560 | | |
551 | 561 | | |
552 | 562 | | |
| |||
844 | 854 | | |
845 | 855 | | |
846 | 856 | | |
847 | | - | |
| 857 | + | |
848 | 858 | | |
849 | 859 | | |
850 | 860 | | |
| |||
941 | 951 | | |
942 | 952 | | |
943 | 953 | | |
944 | | - | |
| 954 | + | |
945 | 955 | | |
946 | 956 | | |
947 | 957 | | |
| |||
975 | 985 | | |
976 | 986 | | |
977 | 987 | | |
978 | | - | |
| 988 | + | |
979 | 989 | | |
980 | 990 | | |
981 | 991 | | |
| |||
1137 | 1147 | | |
1138 | 1148 | | |
1139 | 1149 | | |
1140 | | - | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
1141 | 1155 | | |
1142 | 1156 | | |
1143 | 1157 | | |
| |||
1146 | 1160 | | |
1147 | 1161 | | |
1148 | 1162 | | |
1149 | | - | |
| 1163 | + | |
1150 | 1164 | | |
1151 | 1165 | | |
1152 | 1166 | | |
| |||
1371 | 1385 | | |
1372 | 1386 | | |
1373 | 1387 | | |
1374 | | - | |
| 1388 | + | |
1375 | 1389 | | |
1376 | 1390 | | |
1377 | 1391 | | |
| |||
1421 | 1435 | | |
1422 | 1436 | | |
1423 | 1437 | | |
1424 | | - | |
| 1438 | + | |
1425 | 1439 | | |
1426 | 1440 | | |
1427 | 1441 | | |
| |||
1432 | 1446 | | |
1433 | 1447 | | |
1434 | 1448 | | |
1435 | | - | |
| 1449 | + | |
1436 | 1450 | | |
1437 | 1451 | | |
1438 | 1452 | | |
| |||
1461 | 1475 | | |
1462 | 1476 | | |
1463 | 1477 | | |
1464 | | - | |
| 1478 | + | |
1465 | 1479 | | |
1466 | 1480 | | |
1467 | 1481 | | |
| |||
1501 | 1515 | | |
1502 | 1516 | | |
1503 | 1517 | | |
| 1518 | + | |
1504 | 1519 | | |
1505 | | - | |
1506 | | - | |
| 1520 | + | |
| 1521 | + | |
1507 | 1522 | | |
1508 | | - | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
1509 | 1530 | | |
1510 | 1531 | | |
1511 | 1532 | | |
1512 | 1533 | | |
1513 | 1534 | | |
1514 | | - | |
1515 | 1535 | | |
1516 | 1536 | | |
1517 | 1537 | | |
| |||
1654 | 1674 | | |
1655 | 1675 | | |
1656 | 1676 | | |
1657 | | - | |
| 1677 | + | |
1658 | 1678 | | |
1659 | 1679 | | |
1660 | 1680 | | |
| |||
1824 | 1844 | | |
1825 | 1845 | | |
1826 | 1846 | | |
1827 | | - | |
| 1847 | + | |
1828 | 1848 | | |
1829 | 1849 | | |
1830 | 1850 | | |
| |||
1923 | 1943 | | |
1924 | 1944 | | |
1925 | 1945 | | |
1926 | | - | |
| 1946 | + | |
| 1947 | + | |
1927 | 1948 | | |
1928 | 1949 | | |
1929 | 1950 | | |
| |||
2641 | 2662 | | |
2642 | 2663 | | |
2643 | 2664 | | |
2644 | | - | |
2645 | | - | |
2646 | | - | |
2647 | | - | |
2648 | | - | |
| 2665 | + | |
| 2666 | + | |
| 2667 | + | |
2649 | 2668 | | |
2650 | 2669 | | |
2651 | 2670 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
57 | | - | |
| 59 | + | |
| 60 | + | |
58 | 61 | | |
59 | 62 | | |
60 | 63 | | |
| |||
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
103 | | - | |
| 106 | + | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
107 | | - | |
| 110 | + | |
108 | 111 | | |
109 | | - | |
110 | | - | |
| 112 | + | |
| 113 | + | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
| |||
135 | 138 | | |
136 | 139 | | |
137 | 140 | | |
138 | | - | |
| 141 | + | |
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
| |||
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
165 | | - | |
166 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
167 | 168 | | |
168 | 169 | | |
169 | 170 | | |
| |||
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
177 | | - | |
| 178 | + | |
178 | 179 | | |
179 | 180 | | |
180 | 181 | | |
| |||
185 | 186 | | |
186 | 187 | | |
187 | 188 | | |
188 | | - | |
| 189 | + | |
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
| |||
195 | 196 | | |
196 | 197 | | |
197 | 198 | | |
198 | | - | |
199 | | - | |
| 199 | + | |
| 200 | + | |
200 | 201 | | |
201 | 202 | | |
202 | 203 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
217 | | - | |
| 217 | + | |
| 218 | + | |
218 | 219 | | |
219 | 220 | | |
220 | 221 | | |
| |||
235 | 236 | | |
236 | 237 | | |
237 | 238 | | |
238 | | - | |
| 239 | + | |
239 | 240 | | |
240 | 241 | | |
241 | 242 | | |
| |||
280 | 281 | | |
281 | 282 | | |
282 | 283 | | |
283 | | - | |
| 284 | + | |
284 | 285 | | |
285 | 286 | | |
286 | 287 | | |
| |||
0 commit comments