diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs
index be0c9bb0ba35..e81df63a67e4 100644
--- a/src/librustc/driver/driver.rs
+++ b/src/librustc/driver/driver.rs
@@ -291,6 +291,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
crate_name: crate_name.to_string(),
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
enable_quotes: sess.features.borrow().quote,
+ recursion_limit: sess.recursion_limit.get(),
};
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
cfg,
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index 212cd33e16e6..a137f6e78b8b 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -463,6 +463,7 @@ pub struct ExtCtxt<'a> {
pub exported_macros: Vec
>,
pub syntax_env: SyntaxEnv,
+ pub recursion_count: uint,
}
impl<'a> ExtCtxt<'a> {
@@ -478,6 +479,7 @@ pub fn new<'a>(parse_sess: &'a parse::ParseSess, cfg: ast::CrateConfig,
trace_mac: false,
exported_macros: Vec::new(),
syntax_env: env,
+ recursion_count: 0,
}
}
@@ -552,6 +554,13 @@ pub fn mod_path(&self) -> Vec {
return v;
}
pub fn bt_push(&mut self, ei: ExpnInfo) {
+ self.recursion_count += 1;
+ if self.recursion_count > self.ecfg.recursion_limit {
+ self.span_fatal(ei.call_site,
+ format!("Recursion limit reached while expanding the macro `{}`",
+ ei.callee.name).as_slice());
+ }
+
let mut call_site = ei.call_site;
call_site.expn_id = self.backtrace;
self.backtrace = self.codemap().record_expansion(ExpnInfo {
@@ -563,6 +572,7 @@ pub fn bt_pop(&mut self) {
match self.backtrace {
NO_EXPANSION => self.bug("tried to pop without a push"),
expn_id => {
+ self.recursion_count -= 1;
self.backtrace = self.codemap().with_expn_info(expn_id, |expn_info| {
expn_info.map_or(NO_EXPANSION, |ei| ei.call_site.expn_id)
});
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index fa3ccc8cf326..efe4b76354f5 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -1069,6 +1069,7 @@ pub struct ExpansionConfig {
pub crate_name: String,
pub deriving_hash_type_parameter: bool,
pub enable_quotes: bool,
+ pub recursion_limit: uint,
}
impl ExpansionConfig {
@@ -1077,6 +1078,7 @@ pub fn default(crate_name: String) -> ExpansionConfig {
crate_name: crate_name,
deriving_hash_type_parameter: false,
enable_quotes: false,
+ recursion_limit: 64,
}
}
}
diff --git a/src/test/compile-fail/infinite-macro-expansion.rs b/src/test/compile-fail/infinite-macro-expansion.rs
new file mode 100644
index 000000000000..3ea5671735d0
--- /dev/null
+++ b/src/test/compile-fail/infinite-macro-expansion.rs
@@ -0,0 +1,22 @@
+// Copyright 2014 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 or the MIT license
+// , at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(macro_rules)]
+
+macro_rules! recursive(
+ () => (
+ recursive!() //~ ERROR Recursion limit reached while expanding the macro `recursive`
+ )
+ )
+
+fn main() {
+ recursive!()
+}
+