Skip to content

Commit

Permalink
Rollup merge of rust-lang#64284 - Mark-Simulacrum:include-warn, r=pet…
Browse files Browse the repository at this point in the history
…rochenkov

Warn if include macro fails to include entire file

This currently introduces an error, mainly because that was just simpler, and I'm not entirely certain if we can introduce a lint without an RFC and such.

This is primarily to get feedback on the approach and overall aim -- in particular, do we think this is helpful? If so, we probably will need lang-team sign off and decide if it should be an error (as currently introduced by this PR), a lint, or a warning.

r? @petrochenkov

cc rust-lang#35560
  • Loading branch information
Centril authored Oct 8, 2019
2 parents 2748a9f + e068cec commit fc06822
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::hir::intravisit;
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;
Expand Down Expand Up @@ -83,6 +84,7 @@ impl Lint {
match lint_id {
BufferedEarlyLintId::IllFormedAttributeInput => ILL_FORMED_ATTRIBUTE_INPUT,
BufferedEarlyLintId::MetaVariableMisuse => META_VARIABLE_MISUSE,
BufferedEarlyLintId::IncompleteInclude => INCOMPLETE_INCLUDE,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/early_buffered_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use syntax_pos::MultiSpan;
pub enum BufferedEarlyLintId {
IllFormedAttributeInput,
MetaVariableMisuse,
IncompleteInclude,
}

/// Stores buffered lint info which can later be passed to `librustc`.
Expand Down
12 changes: 11 additions & 1 deletion src/libsyntax_ext/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syntax::print::pprust;
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};
Expand Down Expand Up @@ -83,7 +84,16 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
}
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]>> {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/include-single-expr-helper-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// ignore-test auxiliary file for include-single-expr.rs

0

// trailing comment permitted
5 changes: 5 additions & 0 deletions src/test/ui/include-single-expr-helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// ignore-test auxiliary file for include-single-expr.rs

0
10
100
6 changes: 6 additions & 0 deletions src/test/ui/include-single-expr.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions src/test/ui/include-single-expr.stderr
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion src/tools/error_index_generator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit fc06822

Please sign in to comment.