about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-07-07 03:40:59 -0700
committerbors <bors@rust-lang.org>2013-07-07 03:40:59 -0700
commitd91ac39cd522dd40b80372baeb693680c1d15927 (patch)
tree3bf64e258265fc62391dbee6fb5e8c649d157d8a /src/libsyntax/parse
parent63f785769740b3e61e1c4d908c7a97f836c3cdc4 (diff)
parent3c5cfdf2e7e53967eb330928aaeedfbb8ea1db4d (diff)
downloadrust-d91ac39cd522dd40b80372baeb693680c1d15927.tar.gz
rust-d91ac39cd522dd40b80372baeb693680c1d15927.zip
auto merge of #7585 : Blei/rust/fix-circular-modules, r=huonw
Fixes #7276
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/mod.rs4
-rw-r--r--src/libsyntax/parse/parser.rs21
2 files changed, 23 insertions, 2 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index 6dd8d4880e3..67ae695508b 100644
--- a/src/libsyntax/parse/mod.rs
+++ b/src/libsyntax/parse/mod.rs
@@ -44,6 +44,8 @@ pub struct ParseSess {
     cm: @codemap::CodeMap, // better be the same as the one in the reader!
     next_id: node_id,
     span_diagnostic: @span_handler, // better be the same as the one in the reader!
+    /// Used to determine and report recursive mod inclusions
+    included_mod_stack: ~[Path],
 }
 
 pub fn new_parse_sess(demitter: Option<Emitter>) -> @mut ParseSess {
@@ -52,6 +54,7 @@ pub fn new_parse_sess(demitter: Option<Emitter>) -> @mut ParseSess {
         cm: cm,
         next_id: 1,
         span_diagnostic: mk_span_handler(mk_handler(demitter), cm),
+        included_mod_stack: ~[],
     }
 }
 
@@ -62,6 +65,7 @@ pub fn new_parse_sess_special_handler(sh: @span_handler,
         cm: cm,
         next_id: 1,
         span_diagnostic: sh,
+        included_mod_stack: ~[],
     }
 }
 
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index ae87fd8774a..f496af92f93 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -264,7 +264,6 @@ pub struct Parser {
     obsolete_set: @mut HashSet<ObsoleteSyntax>,
     /// Used to determine the path to externally loaded source files
     mod_path_stack: @mut ~[@str],
-
 }
 
 #[unsafe_destructor]
@@ -3833,7 +3832,7 @@ impl Parser {
         (id, item_static(ty, m, e), None)
     }
 
-    // parse a mod { ...}  item
+    // parse a `mod <foo> { ... }` or `mod <foo>;` item
     fn parse_item_mod(&self, outer_attrs: ~[ast::attribute]) -> item_info {
         let id_span = *self.span;
         let id = self.parse_ident();
@@ -3906,6 +3905,23 @@ impl Parser {
             prefix.push_many(path.components)
         };
         let full_path = full_path.normalize();
+
+        let maybe_i = do self.sess.included_mod_stack.iter().position_ |&p| { p == full_path };
+        match maybe_i {
+            Some(i) => {
+                let stack = &self.sess.included_mod_stack;
+                let mut err = ~"circular modules: ";
+                for stack.slice(i, stack.len()).iter().advance |p| {
+                    err.push_str(p.to_str());
+                    err.push_str(" -> ");
+                }
+                err.push_str(full_path.to_str());
+                self.span_fatal(id_sp, err);
+            }
+            None => ()
+        }
+        self.sess.included_mod_stack.push(full_path.clone());
+
         let p0 =
             new_sub_parser_from_file(self.sess, copy self.cfg,
                                      &full_path, id_sp);
@@ -3913,6 +3929,7 @@ impl Parser {
         let mod_attrs = vec::append(outer_attrs, inner);
         let first_item_outer_attrs = next;
         let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
+        self.sess.included_mod_stack.pop();
         return (ast::item_mod(m0), mod_attrs);
 
         fn cdir_path_opt(default: @str, attrs: ~[ast::attribute]) -> @str {