Skip to content

Commit 6b3824e

Browse files
authored
Unrolled build for #146137
Rollup merge of #146137 - Urgau:cfg-disallow-frontmatter, r=fmease Disallow frontmatter in `--cfg` and `--check-cfg` arguments This PR disallows the frontmatter syntax in `--cfg` and `--check-cfg` arguments. Fixes #146130 r? fmease
2 parents 9385c64 + 0fa93a3 commit 6b3824e

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
assert_eq!(output.stdout_utf8(), "");
10+
diff()
11+
.expected_file(format!("{flag}.stderr"))
12+
.actual_text("output", output.stderr_utf8())
13+
.run();
14+
}
15+
16+
fn main() {
17+
// Verify that frontmatter isn't allowed in `--cfg` arguments.
18+
// https://github.com/rust-lang/rust/issues/146130
19+
test_and_compare(
20+
"cfg",
21+
r#"---
22+
---
23+
key"#,
24+
);
25+
26+
// Verify that frontmatter isn't allowed in `--check-cfg` arguments.
27+
// https://github.com/rust-lang/rust/issues/146130
28+
test_and_compare(
29+
"check-cfg",
30+
r#"---
31+
---
32+
cfg(key)"#,
33+
);
34+
}

0 commit comments

Comments
 (0)