Skip to content

Commit d91ac39

Browse files
committed
auto merge of #7585 : Blei/rust/fix-circular-modules, r=huonw
Fixes #7276
2 parents 63f7857 + 3c5cfdf commit d91ac39

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/libsyntax/parse/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub struct ParseSess {
4444
cm: @codemap::CodeMap, // better be the same as the one in the reader!
4545
next_id: node_id,
4646
span_diagnostic: @span_handler, // better be the same as the one in the reader!
47+
/// Used to determine and report recursive mod inclusions
48+
included_mod_stack: ~[Path],
4749
}
4850

4951
pub fn new_parse_sess(demitter: Option<Emitter>) -> @mut ParseSess {
@@ -52,6 +54,7 @@ pub fn new_parse_sess(demitter: Option<Emitter>) -> @mut ParseSess {
5254
cm: cm,
5355
next_id: 1,
5456
span_diagnostic: mk_span_handler(mk_handler(demitter), cm),
57+
included_mod_stack: ~[],
5558
}
5659
}
5760

@@ -62,6 +65,7 @@ pub fn new_parse_sess_special_handler(sh: @span_handler,
6265
cm: cm,
6366
next_id: 1,
6467
span_diagnostic: sh,
68+
included_mod_stack: ~[],
6569
}
6670
}
6771

src/libsyntax/parse/parser.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ pub struct Parser {
264264
obsolete_set: @mut HashSet<ObsoleteSyntax>,
265265
/// Used to determine the path to externally loaded source files
266266
mod_path_stack: @mut ~[@str],
267-
268267
}
269268

270269
#[unsafe_destructor]
@@ -3833,7 +3832,7 @@ impl Parser {
38333832
(id, item_static(ty, m, e), None)
38343833
}
38353834

3836-
// parse a mod { ...} item
3835+
// parse a `mod <foo> { ... }` or `mod <foo>;` item
38373836
fn parse_item_mod(&self, outer_attrs: ~[ast::attribute]) -> item_info {
38383837
let id_span = *self.span;
38393838
let id = self.parse_ident();
@@ -3906,13 +3905,31 @@ impl Parser {
39063905
prefix.push_many(path.components)
39073906
};
39083907
let full_path = full_path.normalize();
3908+
3909+
let maybe_i = do self.sess.included_mod_stack.iter().position_ |&p| { p == full_path };
3910+
match maybe_i {
3911+
Some(i) => {
3912+
let stack = &self.sess.included_mod_stack;
3913+
let mut err = ~"circular modules: ";
3914+
for stack.slice(i, stack.len()).iter().advance |p| {
3915+
err.push_str(p.to_str());
3916+
err.push_str(" -> ");
3917+
}
3918+
err.push_str(full_path.to_str());
3919+
self.span_fatal(id_sp, err);
3920+
}
3921+
None => ()
3922+
}
3923+
self.sess.included_mod_stack.push(full_path.clone());
3924+
39093925
let p0 =
39103926
new_sub_parser_from_file(self.sess, copy self.cfg,
39113927
&full_path, id_sp);
39123928
let (inner, next) = p0.parse_inner_attrs_and_next();
39133929
let mod_attrs = vec::append(outer_attrs, inner);
39143930
let first_item_outer_attrs = next;
39153931
let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
3932+
self.sess.included_mod_stack.pop();
39163933
return (ast::item_mod(m0), mod_attrs);
39173934
39183935
fn cdir_path_opt(default: @str, attrs: ~[ast::attribute]) -> @str {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-test: this is an auxiliary file for circular-modules-main.rs
12+
13+
mod circular_modules_main;
14+
15+
pub fn say_hello() {
16+
println(circular_modules_main::hi_str());
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
mod circular_modules_hello; //~ERROR: circular modules
13+
14+
pub fn hi_str() -> ~str {
15+
~"Hi!"
16+
}
17+
18+
fn main() {
19+
circular_modules_hello::say_hello();
20+
}

0 commit comments

Comments
 (0)