Skip to content

Commit

Permalink
Add the small amount i know about hygiene (#167)
Browse files Browse the repository at this point in the history
* add the small amount i know about hygiene

* use en-dash
  • Loading branch information
mark-i-m authored and petrochenkov committed Jul 9, 2018
1 parent bf2f7c8 commit 9112311
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/macro-expansion.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,55 @@ in [`src/libsyntax/ext/tt/macro_parser.rs`][code_mp].

### Hygiene

If you have ever used C/C++ preprocessor macros, you know that there are some
annoying and hard-to-debug gotchas! For example, consider the following C code:

```c
#define DEFINE_FOO struct Bar {int x;}; struct Foo {Bar bar;};

// Then, somewhere else
struct Bar {
...
};

DEFINE_FOO
```

Most people avoid writing C like this – and for good reason: it doesn't
compile. The `struct Bar` defined by the macro clashes names with the `struct
Bar` defined in the code. Consider also the following example:

```c
#define DO_FOO(x) {\
int y = 0;\
foo(x, y);\
}

// Then elsewhere
int y = 22;
DO_FOO(y);
```
Do you see the problem? We wanted to generate a call `foo(22, 0)`, but instead
we got `foo(0, 0)` because the macro defined its own `y`!
These are both examples of _macro hygiene_ issues. _Hygiene_ relates to how to
handle names defined _within a macro_. In particular, a hygienic macro system
prevents errors due to names introduced within a macro. Rust macros are hygienic
in that they do not allow one to write the sorts of bugs above.
At a high level, hygiene within the rust compiler is accomplished by keeping
track of the context where a name is introduced and used. We can then
disambiguate names based on that context. Future iterations of the macro system
will allow greater control to the macro author to use that context. For example,
a macro author may want to introduce a new name to the context where the macro
was called. Alternately, the macro author may be defining a variable for use
only within the macro (i.e. it should not be visible outside the macro).
In rustc, this "context" is tracked via `Span`s.
TODO: what is call-site hygiene? what is def-site hygiene?
TODO
### Procedural Macros
Expand All @@ -153,6 +202,7 @@ TODO
TODO
TODO: maybe something about macros 2.0?
[code_dir]: https://github.com/rust-lang/rust/tree/master/src/libsyntax/ext/tt
Expand Down

0 comments on commit 9112311

Please sign in to comment.