Skip to content
Draft
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
3 changes: 2 additions & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ parse_bare_cr = {$double_quotes ->
}
.escape = escape the character

parse_bare_cr_in_frontmatter = bare CR not allowed in frontmatter

parse_bare_cr_in_raw_string = bare CR not allowed in raw string

parse_binder_and_polarity = `for<...>` binder not allowed with `{$polarity}` trait polarity modifier
Expand Down Expand Up @@ -350,7 +352,6 @@ parse_frontmatter_length_mismatch = frontmatter close does not match the opening
parse_frontmatter_too_many_dashes = too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found {$len_opening}
parse_frontmatter_unclosed = unclosed frontmatter
.note = frontmatter opening here was not closed

parse_function_body_equals_expr = function body cannot be `= expression;`
.suggestion = surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,13 @@ pub(crate) struct FrontmatterTooManyDashes {
pub len_opening: usize,
}

#[derive(Diagnostic)]
#[diag(parse_bare_cr_in_frontmatter)]
pub(crate) struct BareCrFrontmatter {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_leading_plus_not_supported)]
pub(crate) struct LeadingPlusNotSupported {
Expand Down
22 changes: 15 additions & 7 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,9 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let s = self.str_from(start);
let real_start = s.find("---").unwrap();
let frontmatter_opening_pos = BytePos(real_start as u32) + start;
let s_new = &s[real_start..];
let within = s_new.trim_start_matches('-');
let len_opening = s_new.len() - within.len();
let real_s = &s[real_start..];
let within = real_s.trim_start_matches('-');
let len_opening = real_s.len() - within.len();

let frontmatter_opening_end_pos = frontmatter_opening_pos + BytePos(len_opening as u32);
if has_invalid_preceding_whitespace {
Expand All @@ -611,19 +611,27 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
});
}

let line_end = real_s.find('\n').unwrap_or(real_s.len());
if invalid_infostring {
let line_end = s[real_start..].find('\n').unwrap_or(s[real_start..].len());
let span = self.mk_sp(
frontmatter_opening_end_pos,
frontmatter_opening_pos + BytePos(line_end as u32),
);
self.dcx().emit_err(errors::FrontmatterInvalidInfostring { span });
}

let last_line_start = within.rfind('\n').map_or(0, |i| i + 1);
let last_line = &within[last_line_start..];
let last_line_start = real_s.rfind('\n').map_or(0, |i| i + 1);

let content = &real_s[line_end..last_line_start];
if let Some(cr_offset) = content.find('\r') {
let cr_pos = start + BytePos((real_start + line_end + cr_offset) as u32);
let span = self.mk_sp(cr_pos, cr_pos + BytePos(1 as u32));
self.dcx().emit_err(errors::BareCrFrontmatter { span });
}

let last_line = &real_s[last_line_start..];
let last_line_trimmed = last_line.trim_start_matches(is_horizontal_whitespace);
let last_line_start_pos = frontmatter_opening_end_pos + BytePos(last_line_start as u32);
let last_line_start_pos = frontmatter_opening_pos + BytePos(last_line_start as u32);

let frontmatter_span = self.mk_sp(frontmatter_opening_pos, self.pos);
self.psess.gated_spans.gate(sym::frontmatter, frontmatter_span);
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/frontmatter/content-cr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
package.name = "" # //~ ERROR bare CR not allowed in frontmatter
package.description = "é"
---

// ignore-tidy-cr

#![feature(frontmatter)]

pub fn main() {}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/frontmatter/content-cr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: bare CR not allowed in frontmatter
--> $DIR/content-cr.rs:2:17
|
LL | package.name = "␍" #
| ^

error: aborting due to 1 previous error

Loading