Skip to content

Commit e068cec

Browse files
Warn if include macro fails to include entire file
1 parent f3c9cec commit e068cec

File tree

9 files changed

+47
-2
lines changed

9 files changed

+47
-2
lines changed

src/librustc/lint/builtin.rs

+6
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ pub mod parser {
368368
Allow,
369369
"possible meta-variable misuse at macro definition"
370370
}
371+
372+
declare_lint! {
373+
pub INCOMPLETE_INCLUDE,
374+
Deny,
375+
"trailing content in included file"
376+
}
371377
}
372378

373379
declare_lint! {

src/librustc/lint/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::hir::intravisit;
2828
use crate::hir;
2929
use crate::lint::builtin::BuiltinLintDiagnostics;
3030
use crate::lint::builtin::parser::{ILL_FORMED_ATTRIBUTE_INPUT, META_VARIABLE_MISUSE};
31+
use crate::lint::builtin::parser::INCOMPLETE_INCLUDE;
3132
use crate::session::{Session, DiagnosticMessageId};
3233
use crate::ty::TyCtxt;
3334
use crate::ty::query::Providers;
@@ -83,6 +84,7 @@ impl Lint {
8384
match lint_id {
8485
BufferedEarlyLintId::IllFormedAttributeInput => ILL_FORMED_ATTRIBUTE_INPUT,
8586
BufferedEarlyLintId::MetaVariableMisuse => META_VARIABLE_MISUSE,
87+
BufferedEarlyLintId::IncompleteInclude => INCOMPLETE_INCLUDE,
8688
}
8789
}
8890

src/libsyntax/early_buffered_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use syntax_pos::MultiSpan;
1111
pub enum BufferedEarlyLintId {
1212
IllFormedAttributeInput,
1313
MetaVariableMisuse,
14+
IncompleteInclude,
1415
}
1516

1617
/// Stores buffered lint info which can later be passed to `librustc`.

src/libsyntax_ext/source_util.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use syntax::print::pprust;
55
use syntax::ptr::P;
66
use syntax::symbol::Symbol;
77
use syntax::tokenstream::TokenStream;
8+
use syntax::early_buffered_lints::BufferedEarlyLintId;
89

910
use smallvec::SmallVec;
1011
use syntax_pos::{self, Pos, Span};
@@ -83,7 +84,16 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
8384
}
8485
impl<'a> base::MacResult for ExpandResult<'a> {
8586
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
86-
Some(panictry!(self.p.parse_expr()))
87+
let r = panictry!(self.p.parse_expr());
88+
if self.p.token != token::Eof {
89+
self.p.sess.buffer_lint(
90+
BufferedEarlyLintId::IncompleteInclude,
91+
self.p.token.span,
92+
ast::CRATE_NODE_ID,
93+
"include macro expected single expression in source",
94+
);
95+
}
96+
Some(r)
8797
}
8898

8999
fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ignore-test auxiliary file for include-single-expr.rs
2+
3+
0
4+
5+
// trailing comment permitted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ignore-test auxiliary file for include-single-expr.rs
2+
3+
0
4+
10
5+
100

src/test/ui/include-single-expr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern include macro expected single expression
2+
3+
fn main() {
4+
include!("include-single-expr-helper.rs");
5+
include!("include-single-expr-helper-1.rs");
6+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: include macro expected single expression in source
2+
--> $DIR/include-single-expr-helper.rs:4:1
3+
|
4+
LL | 10
5+
| ^^
6+
|
7+
= note: `#[deny(incomplete_include)]` on by default
8+
9+
error: aborting due to previous error
10+

src/tools/error_index_generator/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
1616
let file = fs::read_to_string(entry.path()).unwrap()
1717
.replace("syntax::register_diagnostics!", "register_diagnostics!");
18-
let contents = format!("(|| {{\n{}\n}})();", file);
18+
let contents = format!("(|| {{\n{}\n}})()", file);
1919

2020
fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
2121

0 commit comments

Comments
 (0)