about summary refs log tree commit diff
path: root/src/libsyntax/visit.rs
diff options
context:
space:
mode:
authorJohn Clements <clements@racket-lang.org>2014-07-09 14:48:12 -0700
committerJohn Clements <clements@racket-lang.org>2014-07-11 10:32:41 -0700
commit53642eed801d157613de0998cdcf0a3da8c36cb3 (patch)
tree4d9232bac52072c177895e7de997b5ca00da98d5 /src/libsyntax/visit.rs
parentf1ad425199b0d89dab275a8c8f6f29a73d316f70 (diff)
downloadrust-53642eed801d157613de0998cdcf0a3da8c36cb3.tar.gz
rust-53642eed801d157613de0998cdcf0a3da8c36cb3.zip
make walk/visit_mac opt-in only
macros can expand into arbitrary items, exprs, etc. This
means that using a default walker or folder on an AST before
macro expansion is complete will miss things (the things that
the macros expand into). As a partial fence against this, this
commit moves the default traversal of macros into a separate
procedure, and makes the default trait implementation signal
an error. This means that Folders and Visitors can traverse
macros if they want to, but they need to explicitly add an
impl that calls the walk_mac or fold_mac procedure

This should prevent problems down the road.
Diffstat (limited to 'src/libsyntax/visit.rs')
-rw-r--r--src/libsyntax/visit.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index 9298b58c426..7caaf2f6cc1 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -20,6 +20,10 @@
 //! execute before AST node B, then A is visited first.  The borrow checker in
 //! particular relies on this property.
 //!
+//! Note: walking an AST before macro expansion is probably a bad idea. For
+//! instance, a walker looking for item names in a module will miss all of
+//! those that are created by the expansion of a macro.
+
 use abi::Abi;
 use ast::*;
 use ast;
@@ -124,8 +128,13 @@ pub trait Visitor<E: Clone> {
     fn visit_explicit_self(&mut self, es: &ExplicitSelf, e: E) {
         walk_explicit_self(self, es, e)
     }
-    fn visit_mac(&mut self, macro: &Mac, e: E) {
-        walk_mac(self, macro, e)
+    fn visit_mac(&mut self, _macro: &Mac, _e: E) {
+        fail!("visit_mac disabled by default");
+        // NB: see note about macros above.
+        // if you really want a visitor that
+        // works on macros, use this
+        // definition in your trait impl:
+        // visit::walk_mac(self, _macro, _e)
     }
     fn visit_path(&mut self, path: &Path, _id: ast::NodeId, e: E) {
         walk_path(self, path, e)