Skip to content

libsyntax: fix infinite loop when recursively including modules #7585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: ~[],
}
}

Expand All @@ -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: ~[],
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -3834,7 +3833,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();
Expand Down Expand Up @@ -3907,13 +3906,31 @@ 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);
let (inner, next) = p0.parse_inner_attrs_and_next();
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 {
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/circular_modules_hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-test: this is an auxiliary file for circular-modules-main.rs

mod circular_modules_main;

pub fn say_hello() {
println(circular_modules_main::hi_str());
}
20 changes: 20 additions & 0 deletions src/test/compile-fail/circular_modules_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


mod circular_modules_hello; //~ERROR: circular modules

pub fn hi_str() -> ~str {
~"Hi!"
}

fn main() {
circular_modules_hello::say_hello();
}