Skip to content

Limit recursion depth for macro expansions, closes #17628 #17678

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
Oct 1, 2014
Merged
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
1 change: 1 addition & 0 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
@@ -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,
10 changes: 10 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
@@ -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)
});
2 changes: 2 additions & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
22 changes: 22 additions & 0 deletions src/test/compile-fail/infinite-macro-expansion.rs
Original file line number Diff line number Diff line change
@@ -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!()
}