Skip to content

Commit 1aeb0d9

Browse files
committed
Disallow frontmatter in --cfg and --check-cfg arguments
1 parent 94722ca commit 1aeb0d9

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_lint::LintStore;
1313
use rustc_middle::ty;
1414
use rustc_middle::ty::CurrentGcx;
1515
use rustc_middle::util::Providers;
16-
use rustc_parse::new_parser_from_source_str;
16+
use rustc_parse::new_parser_from_simple_source_str;
1717
use rustc_parse::parser::attr::AllowLeadingUnsafe;
1818
use rustc_query_impl::QueryCtxt;
1919
use rustc_query_system::query::print_query_stack;
@@ -68,7 +68,7 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
6868
};
6969
}
7070

71-
match new_parser_from_source_str(&psess, filename, s.to_string()) {
71+
match new_parser_from_simple_source_str(&psess, filename, s.to_string()) {
7272
Ok(mut parser) => match parser.parse_meta_item(AllowLeadingUnsafe::No) {
7373
Ok(meta_item) if parser.token == token::Eof => {
7474
if meta_item.path.segments.len() != 1 {
@@ -166,7 +166,7 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
166166
error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
167167
};
168168

169-
let mut parser = match new_parser_from_source_str(&psess, filename, s.to_string()) {
169+
let mut parser = match new_parser_from_simple_source_str(&psess, filename, s.to_string()) {
170170
Ok(parser) => parser,
171171
Err(errs) => {
172172
errs.into_iter().for_each(|err| err.cancel());

compiler/rustc_parse/src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ pub fn new_parser_from_source_str(
6262
source: String,
6363
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
6464
let source_file = psess.source_map().new_source_file(name, source);
65-
new_parser_from_source_file(psess, source_file)
65+
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::Yes)
66+
}
67+
68+
/// Creates a new parser from a simple (no frontmatter) source string.
69+
///
70+
/// On failure, the errors must be consumed via `unwrap_or_emit_fatal`, `emit`, `cancel`,
71+
/// etc., otherwise a panic will occur when they are dropped.
72+
pub fn new_parser_from_simple_source_str(
73+
psess: &ParseSess,
74+
name: FileName,
75+
source: String,
76+
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
77+
let source_file = psess.source_map().new_source_file(name, source);
78+
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::No)
6679
}
6780

6881
/// 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>(
96109
}
97110
err.emit();
98111
});
99-
new_parser_from_source_file(psess, source_file)
112+
new_parser_from_source_file(psess, source_file, FrontmatterAllowed::Yes)
100113
}
101114

102115
pub fn utf8_error<E: EmissionGuarantee>(
@@ -147,9 +160,10 @@ pub fn utf8_error<E: EmissionGuarantee>(
147160
fn new_parser_from_source_file(
148161
psess: &ParseSess,
149162
source_file: Arc<SourceFile>,
163+
frontmatter_allowed: FrontmatterAllowed,
150164
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
151165
let end_pos = source_file.end_position();
152-
let stream = source_file_to_stream(psess, source_file, None, FrontmatterAllowed::Yes)?;
166+
let stream = source_file_to_stream(psess, source_file, None, frontmatter_allowed)?;
153167
let mut parser = Parser::new(psess, stream, None);
154168
if parser.token == token::Eof {
155169
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt(), None);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: invalid `--cfg` argument: `---
2+
---
3+
key` (expected `key` or `key="value"`)
4+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: invalid `--check-cfg` argument: `---
2+
---
3+
cfg(key)`
4+
|
5+
= note: expected `cfg(name, values("value1", "value2", ... "valueN"))`
6+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
7+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use run_make_support::{cwd, diff, rustc};
2+
3+
fn test_and_compare(flag: &str, val: &str) {
4+
let mut cmd = rustc();
5+
6+
let output =
7+
cmd.input("").arg("--crate-type=lib").arg(&format!("--{flag}")).arg(val).run_fail();
8+
9+
diff()
10+
.expected_file(format!("{flag}.stdout"))
11+
.actual_text("output", output.stdout_utf8())
12+
.run();
13+
diff()
14+
.expected_file(format!("{flag}.stderr"))
15+
.actual_text("output", output.stderr_utf8())
16+
.run();
17+
}
18+
19+
fn main() {
20+
// Verify that frontmatter aren't allowed in `--cfg` arguments.
21+
// https://github.com/rust-lang/rust/issues/146130
22+
test_and_compare(
23+
"cfg",
24+
r#"---
25+
---
26+
key"#,
27+
);
28+
29+
// Verify that frontmatter aren't allowed in `--check-cfg` arguments.
30+
// https://github.com/rust-lang/rust/issues/146130
31+
test_and_compare(
32+
"check-cfg",
33+
r#"---
34+
---
35+
cfg(key)"#,
36+
);
37+
}

0 commit comments

Comments
 (0)