Avoid returning original macro if expansion fails.

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.
This commit is contained in:
Douglas Young 2014-02-18 16:14:12 +00:00
parent 517e38997d
commit 0bdfd0f4c7
11 changed files with 53 additions and 28 deletions

View file

@ -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 =
@ -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,
};
@ -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);
}
}
}
@ -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))))