Rollup merge of #133487 - pitaj:reserve-guarded-strings, r=fee1-dead

fix confusing diagnostic for reserved `##`

Closes #131615
This commit is contained in:
Guillaume Gomez 2024-11-28 12:06:04 +01:00 committed by GitHub
commit ca71c8fe5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 141 additions and 107 deletions

View file

@ -2151,6 +2151,15 @@ pub(crate) enum UnknownPrefixSugg {
},
}
#[derive(Diagnostic)]
#[diag(parse_reserved_multihash)]
#[note]
pub(crate) struct ReservedMultihash {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sugg: Option<GuardedStringSugg>,
}
#[derive(Diagnostic)]
#[diag(parse_reserved_string)]
#[note]

View file

@ -816,7 +816,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let mut cursor = Cursor::new(str_before);
let (span, unterminated) = match cursor.guarded_double_quoted_string() {
let (is_string, span, unterminated) = match cursor.guarded_double_quoted_string() {
Some(rustc_lexer::GuardedStr { n_hashes, terminated, token_len }) => {
let end = start + BytePos(token_len);
let span = self.mk_sp(start, end);
@ -829,13 +829,13 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let unterminated = if terminated { None } else { Some(str_start) };
(span, unterminated)
(true, span, unterminated)
}
_ => {
None => {
// We should only get here in the `##+` case.
debug_assert_eq!(self.str_from_to(start, start + BytePos(2)), "##");
(span, None)
(false, span, None)
}
};
if edition2024 {
@ -857,7 +857,11 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
};
// In Edition 2024 and later, emit a hard error.
let err = self.dcx().emit_err(errors::ReservedString { span, sugg });
let err = if is_string {
self.dcx().emit_err(errors::ReservedString { span, sugg })
} else {
self.dcx().emit_err(errors::ReservedMultihash { span, sugg })
};
token::Literal(token::Lit {
kind: token::Err(err),
@ -870,7 +874,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
span,
ast::CRATE_NODE_ID,
BuiltinLintDiag::ReservedString(space_span),
BuiltinLintDiag::ReservedString { is_string, suggestion: space_span },
);
// For backwards compatibility, roll back to after just the first `#`