auto merge of #17678 : fhahn/rust/issue-17628-infinite-recursion, r=alexcrichton

This is a patch for #17628, thanks to @kmcallister for your helpful hints!
This commit is contained in:
bors 2014-10-01 19:37:18 +00:00
commit b224dfe1a6
4 changed files with 35 additions and 0 deletions

View File

@ -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,

View File

@ -463,6 +463,7 @@ pub struct ExtCtxt<'a> {
pub exported_macros: Vec<P<ast::Item>>,
pub syntax_env: SyntaxEnv,
pub recursion_count: uint,
}
impl<'a> ExtCtxt<'a> {
@ -478,6 +479,7 @@ impl<'a> ExtCtxt<'a> {
trace_mac: false,
exported_macros: Vec::new(),
syntax_env: env,
recursion_count: 0,
}
}
@ -552,6 +554,13 @@ impl<'a> ExtCtxt<'a> {
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 @@ impl<'a> ExtCtxt<'a> {
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)
});

View File

@ -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 @@ impl ExpansionConfig {
crate_name: crate_name,
deriving_hash_type_parameter: false,
enable_quotes: false,
recursion_limit: 64,
}
}
}

View File

@ -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 <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.
#![feature(macro_rules)]
macro_rules! recursive(
() => (
recursive!() //~ ERROR Recursion limit reached while expanding the macro `recursive`
)
)
fn main() {
recursive!()
}