Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 17 additions & 3 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/run-make/multiline-args-value/cfg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: invalid `--cfg` argument: `---
---
key` (expected `key` or `key="value"`)

7 changes: 7 additions & 0 deletions tests/run-make/multiline-args-value/check-cfg.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions tests/run-make/multiline-args-value/rmake.rs
Original file line number Diff line number Diff line change
@@ -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