Skip to content

Commit 1726e7d

Browse files
author
Keegan McAllister
committed
---
yaml --- r: 193114 b: refs/heads/beta c: df08657 h: refs/heads/master v: v3
1 parent 9de1b9d commit 1726e7d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3131
refs/heads/issue-18208-method-dispatch-3-quick-reject: 2009f85b9f99dedcec4404418eda9ddba90258a2
3232
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3333
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
34-
refs/heads/beta: 1804242a2d4037fd3ae854989a97a65e16bda05a
34+
refs/heads/beta: df0865754e56d3d804df3b2bb15d405e344e2015
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3737
refs/heads/tmp: de8a23bbc3a7b9cbd7574b5b91a34af59bf030e6

branches/beta/src/doc/trpl/macros.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ shorthand for a data type could be valid as either an expression or a pattern.
189189

190190
## Repetition
191191

192-
The repetition behavior can seem somewhat magical, especially when multiple
193-
names are bound at multiple nested levels of repetition. The two rules to keep
194-
in mind are:
192+
The repetition operator follows two principal rules:
195193

196-
1. the behavior of `$(...)*` is to walk through one "layer" of repetitions, for
197-
all of the `$name`s it contains, in lockstep, and
194+
1. `$(...)*` walks through one "layer" of repetitions, for all of the `$name`s
195+
it contains, in lockstep, and
198196
2. each `$name` must be under at least as many `$(...)*`s as it was matched
199-
against. If it is under more, it'll be duplicated, as appropriate.
197+
against. If it is under more, it'll be duplicated, as appropriate.
200198

201199
This baroque macro illustrates the duplication of variables from outer
202200
repetition levels.
@@ -226,6 +224,10 @@ That's most of the matcher syntax. These examples use `$(...)*`, which is a
226224
more" match. Both forms optionally include a separator, which can be any token
227225
except `+` or `*`.
228226

227+
This system is based on
228+
"[Macro-by-Example](http://www.cs.indiana.edu/ftp/techreports/TR206.pdf)"
229+
(PDF link).
230+
229231
# Hygiene
230232

231233
Some languages implement macros using simple text substitution, which leads to
@@ -273,19 +275,26 @@ macro, using [a GNU C extension] to emulate Rust's expression blocks.
273275
})
274276
```
275277

276-
This looks reasonable, but watch what happens in this example:
278+
Here's a simple use case that goes terribly wrong:
277279

278280
```text
279281
const char *state = "reticulating splines";
280-
LOG(state);
282+
LOG(state)
281283
```
282284

283-
The program will likely segfault, after it tries to execute
285+
This expands to
284286

285287
```text
286-
printf("log(%d): %s\n", state, state);
288+
const char *state = "reticulating splines";
289+
int state = get_log_state();
290+
if (state > 0) {
291+
printf("log(%d): %s\n", state, state);
292+
}
287293
```
288294

295+
The second variable named `state` shadows the first one. This is a problem
296+
because the print statement should refer to both of them.
297+
289298
The equivalent Rust macro has the desired behavior.
290299

291300
```rust

0 commit comments

Comments
 (0)