Skip to content

Commit 3c5cfdf

Browse files
committed
libsyntax: fix infinite loop when recursively including modules
Fixes #7276
1 parent dd4f6bb commit 3c5cfdf

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]
@@ -3834,7 +3833,7 @@ impl Parser {
38343833
(id, item_static(ty, m, e), None)
38353834
}
38363835

3837-
// parse a mod { ...} item
3836+
// parse a `mod <foo> { ... }` or `mod <foo>;` item
38383837
fn parse_item_mod(&self, outer_attrs: ~[ast::attribute]) -> item_info {
38393838
let id_span = *self.span;
38403839
let id = self.parse_ident();
@@ -3907,13 +3906,31 @@ impl Parser {
39073906
prefix.push_many(path.components)
39083907
};
39093908
let full_path = full_path.normalize();
3909+
3910+
let maybe_i = do self.sess.included_mod_stack.iter().position_ |&p| { p == full_path };
3911+
match maybe_i {
3912+
Some(i) => {
3913+
let stack = &self.sess.included_mod_stack;
3914+
let mut err = ~"circular modules: ";
3915+
for stack.slice(i, stack.len()).iter().advance |p| {
3916+
err.push_str(p.to_str());
3917+
err.push_str(" -> ");
3918+
}
3919+
err.push_str(full_path.to_str());
3920+
self.span_fatal(id_sp, err);
3921+
}
3922+
None => ()
3923+
}
3924+
self.sess.included_mod_stack.push(full_path.clone());
3925+
39103926
let p0 =
39113927
new_sub_parser_from_file(self.sess, copy self.cfg,
39123928
&full_path, id_sp);
39133929
let (inner, next) = p0.parse_inner_attrs_and_next();
39143930
let mod_attrs = vec::append(outer_attrs, inner);
39153931
let first_item_outer_attrs = next;
39163932
let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
3933+
self.sess.included_mod_stack.pop();
39173934
return (ast::item_mod(m0), mod_attrs);
39183935
39193936
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)