Skip to content

Commit

Permalink
Avoid returning original macro if expansion fails.
Browse files Browse the repository at this point in the history
Closes #11692. Instead of returning the original expression, a dummy expression
(with identical span) is returned. This prevents infinite loops of failed
expansions as well as odd double error messages in certain situations.
  • Loading branch information
rcxdude committed Feb 18, 2014
1 parent 517e389 commit 0bdfd0f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/libsyntax/ext/asm.rs
Expand Up @@ -64,7 +64,7 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
"inline assembly must be a string literal.") {
Some((s, st)) => (s, st),
// let compilation continue
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
};
asm = s;
asm_str_style = Some(style);
Expand Down
14 changes: 10 additions & 4 deletions src/libsyntax/ext/base.rs
Expand Up @@ -101,6 +101,7 @@ pub trait AnyMacro {
fn make_stmt(&self) -> @ast::Stmt;
}


pub enum MacResult {
MRExpr(@ast::Expr),
MRItem(@ast::Item),
Expand All @@ -112,10 +113,15 @@ impl MacResult {
/// type signatures after emitting a non-fatal error (which stop
/// compilation well before the validity (or otherwise)) of the
/// expression are checked.
pub fn dummy_expr() -> MacResult {
MRExpr(@ast::Expr {
id: ast::DUMMY_NODE_ID, node: ast::ExprLogLevel, span: codemap::DUMMY_SP
})
pub fn raw_dummy_expr(sp: codemap::Span) -> @ast::Expr {
@ast::Expr {
id: ast::DUMMY_NODE_ID,
node: ast::ExprLogLevel,
span: sp
}
}
pub fn dummy_expr(sp: codemap::Span) -> MacResult {
MRExpr(MacResult::raw_dummy_expr(sp))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/bytes.rs
Expand Up @@ -21,7 +21,7 @@ use std::char;
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
// Gather all argument expressions
let exprs = match get_exprs_from_tts(cx, sp, tts) {
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
Some(e) => e,
};
let mut bytes = ~[];
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/concat.rs
Expand Up @@ -21,7 +21,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
tts: &[ast::TokenTree]) -> base::MacResult {
let es = match base::get_exprs_from_tts(cx, sp, tts) {
Some(e) => e,
None => return base::MacResult::dummy_expr()
None => return base::MacResult::dummy_expr(sp)
};
let mut accumulator = ~"";
for e in es.move_iter() {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/concat_idents.rs
Expand Up @@ -25,7 +25,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
ast::TTTok(_, token::COMMA) => (),
_ => {
cx.span_err(sp, "concat_idents! expecting comma.");
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
}
} else {
Expand All @@ -35,7 +35,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
}
_ => {
cx.span_err(sp, "concat_idents! requires ident args.");
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/env.rs
Expand Up @@ -26,7 +26,7 @@ use std::os;
pub fn expand_option_env(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> base::MacResult {
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
Some(v) => v
};

Expand All @@ -42,14 +42,14 @@ pub fn expand_env(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
let exprs = match get_exprs_from_tts(cx, sp, tts) {
Some([]) => {
cx.span_err(sp, "env! takes 1 or 2 arguments");
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
Some(exprs) => exprs
};

let var = match expr_to_str(cx, exprs[0], "expected string literal") {
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
Some((v, _style)) => v
};
let msg = match exprs.len() {
Expand All @@ -60,13 +60,13 @@ pub fn expand_env(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
}
2 => {
match expr_to_str(cx, exprs[1], "expected string literal") {
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
Some((s, _style)) => s
}
}
_ => {
cx.span_err(sp, "env! takes 1 or 2 arguments");
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -51,7 +51,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
format!("expected macro name without module \
separators"));
// let compilation continue
return e;
return MacResult::raw_dummy_expr(e.span);
}
let extname = pth.segments[0].identifier;
let extnamestr = token::get_ident(extname);
Expand All @@ -64,7 +64,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
extnamestr.get()));

// let compilation continue
return e;
return MacResult::raw_dummy_expr(e.span);
}
Some(&NormalTT(ref expandfun, exp_span)) => {
fld.cx.bt_push(ExpnInfo {
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
extnamestr.get()
)
);
return e;
return MacResult::raw_dummy_expr(e.span);
}
};

Expand All @@ -111,7 +111,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
format!("'{}' is not a tt-style macro",
extnamestr.get())
);
return e;
return MacResult::raw_dummy_expr(e.span);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/format.rs
Expand Up @@ -811,7 +811,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
expr,
"format argument must be a string literal.") {
Some((fmt, _)) => fmt,
None => return efmt
None => return MacResult::raw_dummy_expr(sp)
};

let mut parser = parse::Parser::new(fmt.get());
Expand All @@ -829,7 +829,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
match parser.errors.shift() {
Some(error) => {
cx.ecx.span_err(efmt.span, "invalid format string: " + error);
return efmt;
return MacResult::raw_dummy_expr(sp);
}
None => {}
}
Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/source_util.rs
Expand Up @@ -83,7 +83,7 @@ pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> base::MacResult {
let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
Some(f) => f,
None => return MacResult::dummy_expr(),
None => return MacResult::dummy_expr(sp),
};
// The file will be added to the code map by the parser
let mut p =
Expand All @@ -101,13 +101,13 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> base::MacResult {
let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
Some(f) => f,
None => return MacResult::dummy_expr()
None => return MacResult::dummy_expr(sp)
};
let file = res_rel_file(cx, sp, &Path::new(file));
let bytes = match File::open(&file).read_to_end() {
Err(e) => {
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
Ok(bytes) => bytes,
};
Expand All @@ -123,7 +123,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
}
None => {
cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display()));
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
}
}
Expand All @@ -133,13 +133,13 @@ pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
{
let file = match get_single_str_from_tts(cx, sp, tts, "include_bin!") {
Some(f) => f,
None => return MacResult::dummy_expr()
None => return MacResult::dummy_expr(sp)
};
let file = res_rel_file(cx, sp, &Path::new(file));
match File::open(&file).read_to_end() {
Err(e) => {
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
return MacResult::dummy_expr();
return MacResult::dummy_expr(sp);
}
Ok(bytes) => {
base::MRExpr(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/trace_macros.rs
Expand Up @@ -33,7 +33,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
cx.set_trace_macros(false);
} else {
cx.span_err(sp, "trace_macros! only accepts `true` or `false`");
return base::MacResult::dummy_expr();
return base::MacResult::dummy_expr(sp);
}

rust_parser.bump();
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/issue-11692.rs
@@ -0,0 +1,19 @@
// 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.

fn main() {
print!(test!());
//~^ ERROR: macro undefined: 'test'
//~^^ ERROR: format argument must be a string literal

concat!(test!());
//~^ ERROR: macro undefined: 'test'
//~^^ ERROR: expected a literal
}

5 comments on commit 0bdfd0f

@bors
Copy link
Contributor

@bors bors commented on 0bdfd0f Feb 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at rcxdude@0bdfd0f

@bors
Copy link
Contributor

@bors bors commented on 0bdfd0f Feb 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging rcxdude/rust/macro_fix = 0bdfd0f into auto

@bors
Copy link
Contributor

@bors bors commented on 0bdfd0f Feb 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rcxdude/rust/macro_fix = 0bdfd0f merged ok, testing candidate = 99f8380

@bors
Copy link
Contributor

@bors bors commented on 0bdfd0f Feb 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 0bdfd0f Feb 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 99f8380

Please sign in to comment.