Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix confusing diagnostic for reserved ## #133487

Merged
merged 1 commit into from
Nov 28, 2024
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
3 changes: 3 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
lint_reserved_multihash = reserved token in Rust 2024
.suggestion = insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
lint_reserved_prefix = prefix `{$prefix}` is unknown
.label = unknown prefix
.suggestion = insert whitespace here to avoid this being parsed as a prefix in Rust 2021
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
lints::RawPrefix { label: label_span, suggestion: label_span.shrink_to_hi() }
.decorate_lint(diag);
}
BuiltinLintDiag::ReservedString(suggestion) => {
lints::ReservedString { suggestion }.decorate_lint(diag);
BuiltinLintDiag::ReservedString { is_string, suggestion } => {
if is_string {
lints::ReservedString { suggestion }.decorate_lint(diag);
} else {
lints::ReservedMultihash { suggestion }.decorate_lint(diag);
}
}
BuiltinLintDiag::UnusedBuiltinAttribute { attr_name, macro_name, invoc_span } => {
lints::UnusedBuiltinAttribute { invoc_span, attr_name, macro_name }.decorate_lint(diag);
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3059,3 +3059,10 @@ pub(crate) struct ReservedString {
#[suggestion(code = " ", applicability = "machine-applicable")]
pub suggestion: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_reserved_multihash)]
pub(crate) struct ReservedMultihash {
#[suggestion(code = " ", applicability = "machine-applicable")]
pub suggestion: Span,
}
7 changes: 5 additions & 2 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,11 @@ pub enum BuiltinLintDiag {
ReservedPrefix(Span, String),
/// `'r#` in edition < 2021.
RawPrefix(Span),
/// `##` or `#"` is edition < 2024.
ReservedString(Span),
/// `##` or `#"` in edition < 2024.
ReservedString {
is_string: bool,
suggestion: Span,
},
TrailingMacro(bool, Ident),
BreakWithLabelAndLoop(Span),
UnicodeTextFlow(Span, String),
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ parse_require_colon_after_labeled_expression = labeled expression must be follow
.label = the label
.suggestion = add `:` after the label
parse_reserved_multihash = reserved multi-hash token is forbidden
.note = sequences of two or more # are reserved for future use since Rust 2024
.suggestion_whitespace = consider inserting whitespace here
parse_reserved_string = invalid string literal
.note = unprefixed guarded string literals are reserved for future use since Rust 2024
.suggestion_whitespace = consider inserting whitespace here
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ impl<'psess, 'src> StringReader<'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);
Expand All @@ -816,13 +816,13 @@ impl<'psess, 'src> StringReader<'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 {
Expand All @@ -844,7 +844,11 @@ impl<'psess, 'src> StringReader<'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),
Expand All @@ -857,7 +861,7 @@ impl<'psess, 'src> StringReader<'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 `#`
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/rust-2024/reserved-guarded-strings-lexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ macro_rules! demo7 {

fn main() {
demo3!(## "foo");
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!(### "foo");
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!(## "foo"#);
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo7!(### "foo"###);
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024

demo5!(###"foo"#);
Expand All @@ -56,14 +56,14 @@ fn main() {
demo5!(#"foo"###);
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
demo4!("foo"###);
//~^ WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~^ WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024
//~| WARNING parsed as a guarded string in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING reserved token in Rust 2024 [rust_2024_guarded_string_incompatible_syntax]
//~| WARNING hard error in Rust 2024

// Non-ascii identifiers
Expand Down
48 changes: 24 additions & 24 deletions tests/ui/rust-2024/reserved-guarded-strings-lexing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: identifiers cannot contain emoji: `🙃`
LL | demo3!(🙃#"");
| ^^

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:28:12
|
LL | demo3!(## "foo");
Expand All @@ -41,98 +41,98 @@ note: the lint level is defined here
|
LL | #![warn(rust_2024_guarded_string_incompatible_syntax)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo3!(# # "foo");
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:31:12
|
LL | demo4!(### "foo");
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo4!(# ## "foo");
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:31:13
|
LL | demo4!(### "foo");
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo4!(## # "foo");
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:36:12
|
LL | demo4!(## "foo"#);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo4!(# # "foo"#);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:39:12
|
LL | demo7!(### "foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo7!(# ## "foo"###);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:39:13
|
LL | demo7!(### "foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo7!(## # "foo"###);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:39:21
|
LL | demo7!(### "foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo7!(### "foo"# ##);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:39:22
|
LL | demo7!(### "foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo7!(### "foo"## #);
| +
Expand Down Expand Up @@ -189,54 +189,54 @@ help: insert whitespace here to avoid this being parsed as a guarded string in R
LL | demo5!(# "foo"###);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:56:18
|
LL | demo5!(#"foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo5!(#"foo"# ##);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:56:19
|
LL | demo5!(#"foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo5!(#"foo"## #);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:63:17
|
LL | demo4!("foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo4!("foo"# ##);
| +

warning: will be parsed as a guarded string in Rust 2024
warning: reserved token in Rust 2024
--> $DIR/reserved-guarded-strings-lexing.rs:63:18
|
LL | demo4!("foo"###);
| ^^
|
= warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
= note: for more information, see issue #123735 <https://github.com/rust-lang/rust/issues/123735>
help: insert whitespace here to avoid this being parsed as a guarded string in Rust 2024
help: insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024
|
LL | demo4!("foo"## #);
| +
Expand Down
Loading
Loading