mirror of
https://github.com/rust-lang/rust
synced 2024-09-30 22:04:36 +00:00
Warn if include macro fails to include entire file
This commit is contained in:
parent
f3c9cece7b
commit
e068cec13e
|
@ -368,6 +368,12 @@ pub mod parser {
|
|||
Allow,
|
||||
"possible meta-variable misuse at macro definition"
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
pub INCOMPLETE_INCLUDE,
|
||||
Deny,
|
||||
"trailing content in included file"
|
||||
}
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
use crate::hir;
|
||||
use crate::lint::builtin::BuiltinLintDiagnostics;
|
||||
use crate::lint::builtin::parser::{ILL_FORMED_ATTRIBUTE_INPUT, META_VARIABLE_MISUSE};
|
||||
use crate::lint::builtin::parser::INCOMPLETE_INCLUDE;
|
||||
use crate::session::{Session, DiagnosticMessageId};
|
||||
use crate::ty::TyCtxt;
|
||||
use crate::ty::query::Providers;
|
||||
|
@ -83,6 +84,7 @@ pub fn from_parser_lint_id(lint_id: BufferedEarlyLintId) -> &'static Self {
|
|||
match lint_id {
|
||||
BufferedEarlyLintId::IllFormedAttributeInput => ILL_FORMED_ATTRIBUTE_INPUT,
|
||||
BufferedEarlyLintId::MetaVariableMisuse => META_VARIABLE_MISUSE,
|
||||
BufferedEarlyLintId::IncompleteInclude => INCOMPLETE_INCLUDE,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
pub enum BufferedEarlyLintId {
|
||||
IllFormedAttributeInput,
|
||||
MetaVariableMisuse,
|
||||
IncompleteInclude,
|
||||
}
|
||||
|
||||
/// Stores buffered lint info which can later be passed to `librustc`.
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
use syntax::ptr::P;
|
||||
use syntax::symbol::Symbol;
|
||||
use syntax::tokenstream::TokenStream;
|
||||
use syntax::early_buffered_lints::BufferedEarlyLintId;
|
||||
|
||||
use smallvec::SmallVec;
|
||||
use syntax_pos::{self, Pos, Span};
|
||||
|
@ -83,7 +84,16 @@ struct ExpandResult<'a> {
|
|||
}
|
||||
impl<'a> base::MacResult for ExpandResult<'a> {
|
||||
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
|
||||
Some(panictry!(self.p.parse_expr()))
|
||||
let r = panictry!(self.p.parse_expr());
|
||||
if self.p.token != token::Eof {
|
||||
self.p.sess.buffer_lint(
|
||||
BufferedEarlyLintId::IncompleteInclude,
|
||||
self.p.token.span,
|
||||
ast::CRATE_NODE_ID,
|
||||
"include macro expected single expression in source",
|
||||
);
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
|
||||
fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
|
||||
|
|
5
src/test/ui/include-single-expr-helper-1.rs
Normal file
5
src/test/ui/include-single-expr-helper-1.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// ignore-test auxiliary file for include-single-expr.rs
|
||||
|
||||
0
|
||||
|
||||
// trailing comment permitted
|
5
src/test/ui/include-single-expr-helper.rs
Normal file
5
src/test/ui/include-single-expr-helper.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// ignore-test auxiliary file for include-single-expr.rs
|
||||
|
||||
0
|
||||
10
|
||||
100
|
6
src/test/ui/include-single-expr.rs
Normal file
6
src/test/ui/include-single-expr.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
// error-pattern include macro expected single expression
|
||||
|
||||
fn main() {
|
||||
include!("include-single-expr-helper.rs");
|
||||
include!("include-single-expr-helper-1.rs");
|
||||
}
|
10
src/test/ui/include-single-expr.stderr
Normal file
10
src/test/ui/include-single-expr.stderr
Normal file
|
@ -0,0 +1,10 @@
|
|||
error: include macro expected single expression in source
|
||||
--> $DIR/include-single-expr-helper.rs:4:1
|
||||
|
|
||||
LL | 10
|
||||
| ^^
|
||||
|
|
||||
= note: `#[deny(incomplete_include)]` on by default
|
||||
|
||||
error: aborting due to previous error
|
||||
|
|
@ -15,7 +15,7 @@ fn main() {
|
|||
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
|
||||
let file = fs::read_to_string(entry.path()).unwrap()
|
||||
.replace("syntax::register_diagnostics!", "register_diagnostics!");
|
||||
let contents = format!("(|| {{\n{}\n}})();", file);
|
||||
let contents = format!("(|| {{\n{}\n}})()", file);
|
||||
|
||||
fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
|
||||
|
||||
|
|
Loading…
Reference in a new issue