Disallow frontmatter in --cfg and --check-cfg arguments
This commit is contained in:
parent
94722cabf4
commit
0fa93a3434
5 changed files with 65 additions and 6 deletions
|
|
@ -13,7 +13,7 @@ use rustc_lint::LintStore;
|
|||
use rustc_middle::ty;
|
||||
use rustc_middle::ty::CurrentGcx;
|
||||
use rustc_middle::util::Providers;
|
||||
use rustc_parse::new_parser_from_source_str;
|
||||
use rustc_parse::new_parser_from_simple_source_str;
|
||||
use rustc_parse::parser::attr::AllowLeadingUnsafe;
|
||||
use rustc_query_impl::QueryCtxt;
|
||||
use rustc_query_system::query::print_query_stack;
|
||||
|
|
@ -68,7 +68,7 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
|
|||
};
|
||||
}
|
||||
|
||||
match new_parser_from_source_str(&psess, filename, s.to_string()) {
|
||||
match new_parser_from_simple_source_str(&psess, filename, s.to_string()) {
|
||||
Ok(mut parser) => match parser.parse_meta_item(AllowLeadingUnsafe::No) {
|
||||
Ok(meta_item) if parser.token == token::Eof => {
|
||||
if meta_item.path.segments.len() != 1 {
|
||||
|
|
@ -166,7 +166,7 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
|
|||
error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
|
||||
};
|
||||
|
||||
let mut parser = match new_parser_from_source_str(&psess, filename, s.to_string()) {
|
||||
let mut parser = match new_parser_from_simple_source_str(&psess, filename, s.to_string()) {
|
||||
Ok(parser) => parser,
|
||||
Err(errs) => {
|
||||
errs.into_iter().for_each(|err| err.cancel());
|
||||
|
|
|
|||
|
|
@ -62,7 +62,20 @@ pub fn new_parser_from_source_str(
|
|||
source: String,
|
||||
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
|
||||
let source_file = psess.source_map().new_source_file(name, source);
|
||||
new_parser_from_source_file(psess, source_file)
|
||||
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::Yes)
|
||||
}
|
||||
|
||||
/// Creates a new parser from a simple (no frontmatter) source string.
|
||||
///
|
||||
/// On failure, the errors must be consumed via `unwrap_or_emit_fatal`, `emit`, `cancel`,
|
||||
/// etc., otherwise a panic will occur when they are dropped.
|
||||
pub fn new_parser_from_simple_source_str(
|
||||
psess: &ParseSess,
|
||||
name: FileName,
|
||||
source: String,
|
||||
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
|
||||
let source_file = psess.source_map().new_source_file(name, source);
|
||||
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::No)
|
||||
}
|
||||
|
||||
/// Creates a new parser from a filename. On failure, the errors must be consumed via
|
||||
|
|
@ -96,7 +109,7 @@ pub fn new_parser_from_file<'a>(
|
|||
}
|
||||
err.emit();
|
||||
});
|
||||
new_parser_from_source_file(psess, source_file)
|
||||
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::Yes)
|
||||
}
|
||||
|
||||
pub fn utf8_error<E: EmissionGuarantee>(
|
||||
|
|
@ -147,9 +160,10 @@ pub fn utf8_error<E: EmissionGuarantee>(
|
|||
fn new_parser_from_source_file(
|
||||
psess: &ParseSess,
|
||||
source_file: Arc<SourceFile>,
|
||||
frontmatter_allowed: FrontmatterAllowed,
|
||||
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
|
||||
let end_pos = source_file.end_position();
|
||||
let stream = source_file_to_stream(psess, source_file, None, FrontmatterAllowed::Yes)?;
|
||||
let stream = source_file_to_stream(psess, source_file, None, frontmatter_allowed)?;
|
||||
let mut parser = Parser::new(psess, stream, None);
|
||||
if parser.token == token::Eof {
|
||||
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt(), None);
|
||||
|
|
|
|||
4
tests/run-make/multiline-args-value/cfg.stderr
Normal file
4
tests/run-make/multiline-args-value/cfg.stderr
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
error: invalid `--cfg` argument: `---
|
||||
---
|
||||
key` (expected `key` or `key="value"`)
|
||||
|
||||
7
tests/run-make/multiline-args-value/check-cfg.stderr
Normal file
7
tests/run-make/multiline-args-value/check-cfg.stderr
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
error: invalid `--check-cfg` argument: `---
|
||||
---
|
||||
cfg(key)`
|
||||
|
|
||||
= note: expected `cfg(name, values("value1", "value2", ... "valueN"))`
|
||||
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
|
||||
|
||||
34
tests/run-make/multiline-args-value/rmake.rs
Normal file
34
tests/run-make/multiline-args-value/rmake.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use run_make_support::{cwd, diff, rustc};
|
||||
|
||||
fn test_and_compare(flag: &str, val: &str) {
|
||||
let mut cmd = rustc();
|
||||
|
||||
let output =
|
||||
cmd.input("").arg("--crate-type=lib").arg(&format!("--{flag}")).arg(val).run_fail();
|
||||
|
||||
assert_eq!(output.stdout_utf8(), "");
|
||||
diff()
|
||||
.expected_file(format!("{flag}.stderr"))
|
||||
.actual_text("output", output.stderr_utf8())
|
||||
.run();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Verify that frontmatter isn't allowed in `--cfg` arguments.
|
||||
// https://github.com/rust-lang/rust/issues/146130
|
||||
test_and_compare(
|
||||
"cfg",
|
||||
r#"---
|
||||
---
|
||||
key"#,
|
||||
);
|
||||
|
||||
// Verify that frontmatter isn't allowed in `--check-cfg` arguments.
|
||||
// https://github.com/rust-lang/rust/issues/146130
|
||||
test_and_compare(
|
||||
"check-cfg",
|
||||
r#"---
|
||||
---
|
||||
cfg(key)"#,
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue