Skip to content

Commit 0e3e647

Browse files
Remove BuiltinDiag usage in rustc_parse
1 parent bb689c4 commit 0e3e647

6 files changed

Lines changed: 148 additions & 161 deletions

File tree

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::borrow::Cow;
22

3-
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
43
use rustc_errors::{
54
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, Level,
65
elided_lifetime_in_path_suggestion,
@@ -10,7 +9,6 @@ use rustc_middle::middle::stability;
109
use rustc_middle::ty::TyCtxt;
1110
use rustc_session::Session;
1211
use rustc_session::lint::BuiltinLintDiag;
13-
use rustc_span::BytePos;
1412
use tracing::debug;
1513

1614
use crate::lints;
@@ -28,32 +26,6 @@ pub struct DecorateBuiltinLint<'sess, 'tcx> {
2826
impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
2927
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
3028
match self.diagnostic {
31-
BuiltinLintDiag::UnicodeTextFlow(comment_span, content) => {
32-
let spans: Vec<_> = content
33-
.char_indices()
34-
.filter_map(|(i, c)| {
35-
TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
36-
let lo = comment_span.lo() + BytePos(2 + i as u32);
37-
(c, comment_span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
38-
})
39-
})
40-
.collect();
41-
let characters = spans
42-
.iter()
43-
.map(|&(c, span)| lints::UnicodeCharNoteSub { span, c_debug: format!("{c:?}") })
44-
.collect();
45-
let suggestions = (!spans.is_empty()).then_some(lints::UnicodeTextFlowSuggestion {
46-
spans: spans.iter().map(|(_c, span)| *span).collect(),
47-
});
48-
49-
lints::UnicodeTextFlow {
50-
comment_span,
51-
characters,
52-
suggestions,
53-
num_codepoints: spans.len(),
54-
}
55-
.into_diag(dcx, level)
56-
}
5729
BuiltinLintDiag::AbsPathWithModule(mod_span) => {
5830
let (replacement, applicability) =
5931
match self.sess.source_map().span_to_snippet(mod_span) {
@@ -153,23 +125,6 @@ impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
153125
}
154126
.into_diag(dcx, level)
155127
}
156-
BuiltinLintDiag::ReservedPrefix(label_span, prefix) => lints::ReservedPrefix {
157-
label: label_span,
158-
suggestion: label_span.shrink_to_hi(),
159-
prefix,
160-
}
161-
.into_diag(dcx, level),
162-
BuiltinLintDiag::RawPrefix(label_span) => {
163-
lints::RawPrefix { label: label_span, suggestion: label_span.shrink_to_hi() }
164-
.into_diag(dcx, level)
165-
}
166-
BuiltinLintDiag::ReservedString { is_string, suggestion } => {
167-
if is_string {
168-
lints::ReservedString { suggestion }.into_diag(dcx, level)
169-
} else {
170-
lints::ReservedMultihash { suggestion }.into_diag(dcx, level)
171-
}
172-
}
173128
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
174129
let suggestion = match sugg {
175130
Some((right_sp, sugg)) => lints::DeprecatedWhereClauseLocationSugg::MoveToEnd {

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,46 +3019,6 @@ pub(crate) struct IllFormedAttributeInput {
30193019
pub docs: &'static str,
30203020
}
30213021

3022-
#[derive(Diagnostic)]
3023-
#[diag("unicode codepoint changing visible direction of text present in comment")]
3024-
#[note(
3025-
"these kind of unicode codepoints change the way text flows on applications that support them, but can cause confusion because they change the order of characters on the screen"
3026-
)]
3027-
pub(crate) struct UnicodeTextFlow {
3028-
#[label(
3029-
"{$num_codepoints ->
3030-
[1] this comment contains an invisible unicode text flow control codepoint
3031-
*[other] this comment contains invisible unicode text flow control codepoints
3032-
}"
3033-
)]
3034-
pub comment_span: Span,
3035-
#[subdiagnostic]
3036-
pub characters: Vec<UnicodeCharNoteSub>,
3037-
#[subdiagnostic]
3038-
pub suggestions: Option<UnicodeTextFlowSuggestion>,
3039-
3040-
pub num_codepoints: usize,
3041-
}
3042-
3043-
#[derive(Subdiagnostic)]
3044-
#[label("{$c_debug}")]
3045-
pub(crate) struct UnicodeCharNoteSub {
3046-
#[primary_span]
3047-
pub span: Span,
3048-
pub c_debug: String,
3049-
}
3050-
3051-
#[derive(Subdiagnostic)]
3052-
#[multipart_suggestion(
3053-
"if their presence wasn't intentional, you can remove them",
3054-
applicability = "machine-applicable",
3055-
style = "hidden"
3056-
)]
3057-
pub(crate) struct UnicodeTextFlowSuggestion {
3058-
#[suggestion_part(code = "")]
3059-
pub spans: Vec<Span>,
3060-
}
3061-
30623022
#[derive(Diagnostic)]
30633023
#[diag(
30643024
"absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition"
@@ -3192,34 +3152,6 @@ pub(crate) struct PatternsInFnsWithoutBodySub {
31923152
pub ident: Ident,
31933153
}
31943154

3195-
#[derive(Diagnostic)]
3196-
#[diag("prefix `{$prefix}` is unknown")]
3197-
pub(crate) struct ReservedPrefix {
3198-
#[label("unknown prefix")]
3199-
pub label: Span,
3200-
#[suggestion(
3201-
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
3202-
code = " ",
3203-
applicability = "machine-applicable"
3204-
)]
3205-
pub suggestion: Span,
3206-
3207-
pub prefix: String,
3208-
}
3209-
3210-
#[derive(Diagnostic)]
3211-
#[diag("prefix `'r` is reserved")]
3212-
pub(crate) struct RawPrefix {
3213-
#[label("reserved prefix")]
3214-
pub label: Span,
3215-
#[suggestion(
3216-
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
3217-
code = " ",
3218-
applicability = "machine-applicable"
3219-
)]
3220-
pub suggestion: Span,
3221-
}
3222-
32233155
#[derive(Diagnostic)]
32243156
#[diag("where clause not allowed here")]
32253157
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
@@ -3405,28 +3337,6 @@ pub(crate) enum MutRefSugg {
34053337
#[diag("`use` of a local item without leading `self::`, `super::`, or `crate::`")]
34063338
pub(crate) struct UnqualifiedLocalImportsDiag;
34073339

3408-
#[derive(Diagnostic)]
3409-
#[diag("will be parsed as a guarded string in Rust 2024")]
3410-
pub(crate) struct ReservedString {
3411-
#[suggestion(
3412-
"insert whitespace here to avoid this being parsed as a guarded string in Rust 2024",
3413-
code = " ",
3414-
applicability = "machine-applicable"
3415-
)]
3416-
pub suggestion: Span,
3417-
}
3418-
3419-
#[derive(Diagnostic)]
3420-
#[diag("reserved token in Rust 2024")]
3421-
pub(crate) struct ReservedMultihash {
3422-
#[suggestion(
3423-
"insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024",
3424-
code = " ",
3425-
applicability = "machine-applicable"
3426-
)]
3427-
pub suggestion: Span,
3428-
}
3429-
34303340
#[derive(Diagnostic)]
34313341
#[diag("direct cast of function item into an integer")]
34323342
pub(crate) struct FunctionCastsAsIntegerDiag<'tcx> {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -678,15 +678,7 @@ pub enum BuiltinLintDiag {
678678
ident: Ident,
679679
is_foreign: bool,
680680
},
681-
ReservedPrefix(Span, String),
682-
/// `'r#` in edition < 2021.
683-
RawPrefix(Span),
684681
/// `##` or `#"` in edition < 2024.
685-
ReservedString {
686-
is_string: bool,
687-
suggestion: Span,
688-
},
689-
UnicodeTextFlow(Span, String),
690682
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
691683
SingleUseLifetime {
692684
/// Span of the parameter which declares this lifetime.

compiler/rustc_parse/src/errors.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,3 +4517,93 @@ pub(crate) struct BreakWithLabelAndLoopSub {
45174517
#[suggestion_part(code = ")")]
45184518
pub right: Span,
45194519
}
4520+
4521+
#[derive(Diagnostic)]
4522+
#[diag("prefix `'r` is reserved")]
4523+
pub(crate) struct RawPrefix {
4524+
#[label("reserved prefix")]
4525+
pub label: Span,
4526+
#[suggestion(
4527+
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
4528+
code = " ",
4529+
applicability = "machine-applicable"
4530+
)]
4531+
pub suggestion: Span,
4532+
}
4533+
4534+
#[derive(Diagnostic)]
4535+
#[diag("unicode codepoint changing visible direction of text present in comment")]
4536+
#[note(
4537+
"these kind of unicode codepoints change the way text flows on applications that support them, but can cause confusion because they change the order of characters on the screen"
4538+
)]
4539+
pub(crate) struct UnicodeTextFlow {
4540+
#[label(
4541+
"{$num_codepoints ->
4542+
[1] this comment contains an invisible unicode text flow control codepoint
4543+
*[other] this comment contains invisible unicode text flow control codepoints
4544+
}"
4545+
)]
4546+
pub comment_span: Span,
4547+
#[subdiagnostic]
4548+
pub characters: Vec<UnicodeCharNoteSub>,
4549+
#[subdiagnostic]
4550+
pub suggestions: Option<UnicodeTextFlowSuggestion>,
4551+
4552+
pub num_codepoints: usize,
4553+
}
4554+
4555+
#[derive(Subdiagnostic)]
4556+
#[label("{$c_debug}")]
4557+
pub(crate) struct UnicodeCharNoteSub {
4558+
#[primary_span]
4559+
pub span: Span,
4560+
pub c_debug: String,
4561+
}
4562+
4563+
#[derive(Subdiagnostic)]
4564+
#[multipart_suggestion(
4565+
"if their presence wasn't intentional, you can remove them",
4566+
applicability = "machine-applicable",
4567+
style = "hidden"
4568+
)]
4569+
pub(crate) struct UnicodeTextFlowSuggestion {
4570+
#[suggestion_part(code = "")]
4571+
pub spans: Vec<Span>,
4572+
}
4573+
4574+
#[derive(Diagnostic)]
4575+
#[diag("prefix `{$prefix}` is unknown")]
4576+
pub(crate) struct ReservedPrefix {
4577+
#[label("unknown prefix")]
4578+
pub label: Span,
4579+
#[suggestion(
4580+
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
4581+
code = " ",
4582+
applicability = "machine-applicable"
4583+
)]
4584+
pub suggestion: Span,
4585+
4586+
pub prefix: String,
4587+
}
4588+
4589+
#[derive(Diagnostic)]
4590+
#[diag("will be parsed as a guarded string in Rust 2024")]
4591+
pub(crate) struct ReservedStringLint {
4592+
#[suggestion(
4593+
"insert whitespace here to avoid this being parsed as a guarded string in Rust 2024",
4594+
code = " ",
4595+
applicability = "machine-applicable"
4596+
)]
4597+
pub suggestion: Span,
4598+
}
4599+
4600+
#[derive(Diagnostic)]
4601+
#[diag("reserved token in Rust 2024")]
4602+
pub(crate) struct ReservedMultihashLint {
4603+
#[suggestion(
4604+
"insert whitespace here to avoid this being parsed as a forbidden token in Rust 2024",
4605+
code = " ",
4606+
applicability = "machine-applicable"
4607+
)]
4608+
pub suggestion: Span,
4609+
}

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKin
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_ast::util::unicode::{TEXT_FLOW_CONTROL_CHARS, contains_text_flow_control_chars};
66
use rustc_errors::codes::*;
7-
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, StashKey};
7+
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, StashKey};
88
use rustc_lexer::{
99
Base, Cursor, DocStyle, FrontmatterAllowed, LiteralKind, RawStrError, is_horizontal_whitespace,
1010
};
1111
use rustc_literal_escaper::{EscapeError, Mode, check_for_errors};
12-
use rustc_session::lint::BuiltinLintDiag;
1312
use rustc_session::lint::builtin::{
1413
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
1514
TEXT_DIRECTION_CODEPOINT_IN_COMMENT, TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
@@ -388,7 +387,10 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
388387
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
389388
prefix_span,
390389
ast::CRATE_NODE_ID,
391-
BuiltinLintDiag::RawPrefix(prefix_span),
390+
errors::RawPrefix {
391+
label: prefix_span,
392+
suggestion: prefix_span.shrink_to_hi()
393+
},
392394
);
393395

394396
// Reset the state so we just lex the `'r`.
@@ -498,11 +500,41 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
498500
let content = self.str_from(content_start);
499501
if contains_text_flow_control_chars(content) {
500502
let span = self.mk_sp(start, self.pos);
501-
self.psess.buffer_lint(
503+
let content = content.to_string();
504+
self.psess.dyn_buffer_lint(
502505
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
503506
span,
504507
ast::CRATE_NODE_ID,
505-
BuiltinLintDiag::UnicodeTextFlow(span, content.to_string()),
508+
move |dcx, level| {
509+
let spans: Vec<_> = content
510+
.char_indices()
511+
.filter_map(|(i, c)| {
512+
TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
513+
let lo = span.lo() + BytePos(2 + i as u32);
514+
(c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
515+
})
516+
})
517+
.collect();
518+
let characters = spans
519+
.iter()
520+
.map(|&(c, span)| errors::UnicodeCharNoteSub {
521+
span,
522+
c_debug: format!("{c:?}"),
523+
})
524+
.collect();
525+
let suggestions =
526+
(!spans.is_empty()).then_some(errors::UnicodeTextFlowSuggestion {
527+
spans: spans.iter().map(|(_c, span)| *span).collect(),
528+
});
529+
530+
errors::UnicodeTextFlow {
531+
comment_span: span,
532+
characters,
533+
suggestions,
534+
num_codepoints: spans.len(),
535+
}
536+
.into_diag(dcx, level)
537+
},
506538
);
507539
}
508540
}
@@ -1038,7 +1070,11 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
10381070
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
10391071
prefix_span,
10401072
ast::CRATE_NODE_ID,
1041-
BuiltinLintDiag::ReservedPrefix(prefix_span, prefix.to_string()),
1073+
errors::ReservedPrefix {
1074+
label: prefix_span,
1075+
suggestion: prefix_span.shrink_to_hi(),
1076+
prefix: prefix.to_string(),
1077+
},
10421078
);
10431079
}
10441080
}
@@ -1112,11 +1148,18 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
11121148
})
11131149
} else {
11141150
// Before Rust 2024, only emit a lint for migration.
1115-
self.psess.buffer_lint(
1151+
self.psess.dyn_buffer_lint(
11161152
RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
11171153
span,
11181154
ast::CRATE_NODE_ID,
1119-
BuiltinLintDiag::ReservedString { is_string, suggestion: space_span },
1155+
move |dcx, level| {
1156+
if is_string {
1157+
errors::ReservedStringLint { suggestion: space_span }.into_diag(dcx, level)
1158+
} else {
1159+
errors::ReservedMultihashLint { suggestion: space_span }
1160+
.into_diag(dcx, level)
1161+
}
1162+
},
11201163
);
11211164

11221165
// For backwards compatibility, roll back to after just the first `#`

0 commit comments

Comments
 (0)