From 8f2ab66ab631a11113ac2fbb66384ce7c2fc34d5 Mon Sep 17 00:00:00 2001 From: Leo Testard Date: Sat, 7 Feb 2015 17:23:33 +0100 Subject: [PATCH] Fix handling of parse errors when using `include!()`. Makes the compilation abort when a parse error is encountered while trying to parse an item in an included file. The previous behaviour was to stop processing the file when a token that can't start an item was encountered, without producing any error. Fixes #21146. --- src/libsyntax/ext/source_util.rs | 8 ++++++-- src/test/auxiliary/issue-21146-inc.rs | 13 +++++++++++++ src/test/compile-fail/issue-21146.rs | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/test/auxiliary/issue-21146-inc.rs create mode 100644 src/test/compile-fail/issue-21146.rs diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index be02ba5ddc22..7a3a3562bdfd 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -111,10 +111,14 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree fn make_items(mut self: Box>) -> Option>> { let mut ret = SmallVector::zero(); - loop { + while self.p.token != token::Eof { match self.p.parse_item_with_outer_attributes() { Some(item) => ret.push(item), - None => break + None => self.p.span_fatal( + self.p.span, + &format!("expected item, found `{}`", + self.p.this_token_to_string())[] + ) } } Some(ret) diff --git a/src/test/auxiliary/issue-21146-inc.rs b/src/test/auxiliary/issue-21146-inc.rs new file mode 100644 index 000000000000..1b740d112e9d --- /dev/null +++ b/src/test/auxiliary/issue-21146-inc.rs @@ -0,0 +1,13 @@ +// Copyright 2015 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. + +// include file for issue-21146.rs + +parse_error diff --git a/src/test/compile-fail/issue-21146.rs b/src/test/compile-fail/issue-21146.rs new file mode 100644 index 000000000000..4c6059c132ad --- /dev/null +++ b/src/test/compile-fail/issue-21146.rs @@ -0,0 +1,13 @@ +// Copyright 2015 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. + +// error-pattern: expected item, found `parse_error` +include!("../auxiliary/issue-21146-inc.rs"); +fn main() {}