Skip to content

Commit 8f2ab66

Browse files
committed
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.
1 parent 80627cd commit 8f2ab66

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/libsyntax/ext/source_util.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree
111111
fn make_items(mut self: Box<ExpandResult<'a>>)
112112
-> Option<SmallVector<P<ast::Item>>> {
113113
let mut ret = SmallVector::zero();
114-
loop {
114+
while self.p.token != token::Eof {
115115
match self.p.parse_item_with_outer_attributes() {
116116
Some(item) => ret.push(item),
117-
None => break
117+
None => self.p.span_fatal(
118+
self.p.span,
119+
&format!("expected item, found `{}`",
120+
self.p.this_token_to_string())[]
121+
)
118122
}
119123
}
120124
Some(ret)

src/test/auxiliary/issue-21146-inc.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// include file for issue-21146.rs
12+
13+
parse_error

src/test/compile-fail/issue-21146.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: expected item, found `parse_error`
12+
include!("../auxiliary/issue-21146-inc.rs");
13+
fn main() {}

0 commit comments

Comments
 (0)