Skip to content

Commit 4c27952

Browse files
committed
Use span_suggestion_with_style in SessionSubdiagnostic derive
1 parent cb843a0 commit 4c27952

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ enum SubdiagnosticSuggestionKind {
2828
Verbose,
2929
}
3030

31+
impl FromStr for SubdiagnosticSuggestionKind {
32+
type Err = ();
33+
34+
fn from_str(s: &str) -> Result<Self, Self::Err> {
35+
match s {
36+
"" => Ok(SubdiagnosticSuggestionKind::Normal),
37+
"_short" => Ok(SubdiagnosticSuggestionKind::Short),
38+
"_hidden" => Ok(SubdiagnosticSuggestionKind::Hidden),
39+
"_verbose" => Ok(SubdiagnosticSuggestionKind::Verbose),
40+
_ => Err(()),
41+
}
42+
}
43+
}
44+
45+
impl SubdiagnosticSuggestionKind {
46+
pub fn to_suggestion_style(&self) -> TokenStream {
47+
match self {
48+
SubdiagnosticSuggestionKind::Normal => {
49+
quote! { rustc_errors::SuggestionStyle::ShowCode }
50+
}
51+
SubdiagnosticSuggestionKind::Short => {
52+
quote! { rustc_errors::SuggestionStyle::HideCodeInline }
53+
}
54+
SubdiagnosticSuggestionKind::Hidden => {
55+
quote! { rustc_errors::SuggestionStyle::HideCodeAlways }
56+
}
57+
SubdiagnosticSuggestionKind::Verbose => {
58+
quote! { rustc_errors::SuggestionStyle::ShowAlways }
59+
}
60+
}
61+
}
62+
}
63+
3164
/// Which kind of subdiagnostic is being created from a variant?
3265
#[derive(Clone, Copy)]
3366
enum SubdiagnosticKind {
@@ -52,17 +85,15 @@ impl FromStr for SubdiagnosticKind {
5285
"note" => Ok(SubdiagnosticKind::Note),
5386
"help" => Ok(SubdiagnosticKind::Help),
5487
"warn_" => Ok(SubdiagnosticKind::Warn),
55-
"suggestion" => Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Normal)),
56-
"suggestion_short" => {
57-
Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Short))
58-
}
59-
"suggestion_hidden" => {
60-
Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Hidden))
61-
}
62-
"suggestion_verbose" => {
63-
Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Verbose))
88+
_ => {
89+
if let Some(suggestion_kind) =
90+
s.strip_prefix("suggestion").and_then(|s| s.parse().ok())
91+
{
92+
return Ok(SubdiagnosticKind::Suggestion(suggestion_kind));
93+
};
94+
95+
Err(())
6496
}
65-
_ => Err(()),
6697
}
6798
}
6899
}
@@ -74,18 +105,7 @@ impl quote::IdentFragment for SubdiagnosticKind {
74105
SubdiagnosticKind::Note => write!(f, "note"),
75106
SubdiagnosticKind::Help => write!(f, "help"),
76107
SubdiagnosticKind::Warn => write!(f, "warn"),
77-
SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Normal) => {
78-
write!(f, "suggestion")
79-
}
80-
SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Short) => {
81-
write!(f, "suggestion_short")
82-
}
83-
SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Hidden) => {
84-
write!(f, "suggestion_hidden")
85-
}
86-
SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Verbose) => {
87-
write!(f, "suggestion_verbose")
88-
}
108+
SubdiagnosticKind::Suggestion(..) => write!(f, "suggestion_with_style"),
89109
}
90110
}
91111

@@ -461,25 +481,31 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
461481
let diag = &self.diag;
462482
let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
463483
let message = quote! { rustc_errors::fluent::#slug };
464-
let call = if matches!(kind, SubdiagnosticKind::Suggestion(..)) {
465-
if let Some(span) = span_field {
466-
quote! { #diag.#name(#span, #message, #code, #applicability); }
467-
} else {
468-
span_err(self.span, "suggestion without `#[primary_span]` field").emit();
469-
quote! { unreachable!(); }
484+
let call = match kind {
485+
SubdiagnosticKind::Suggestion(style) => {
486+
if let Some(span) = span_field {
487+
let style = style.to_suggestion_style();
488+
489+
quote! { #diag.#name(#span, #message, #code, #applicability, #style); }
490+
} else {
491+
span_err(self.span, "suggestion without `#[primary_span]` field").emit();
492+
quote! { unreachable!(); }
493+
}
470494
}
471-
} else if matches!(kind, SubdiagnosticKind::Label) {
472-
if let Some(span) = span_field {
473-
quote! { #diag.#name(#span, #message); }
474-
} else {
475-
span_err(self.span, "label without `#[primary_span]` field").emit();
476-
quote! { unreachable!(); }
495+
SubdiagnosticKind::Label => {
496+
if let Some(span) = span_field {
497+
quote! { #diag.#name(#span, #message); }
498+
} else {
499+
span_err(self.span, "label without `#[primary_span]` field").emit();
500+
quote! { unreachable!(); }
501+
}
477502
}
478-
} else {
479-
if let Some(span) = span_field {
480-
quote! { #diag.#name(#span, #message); }
481-
} else {
482-
quote! { #diag.#name(#message); }
503+
_ => {
504+
if let Some(span) = span_field {
505+
quote! { #diag.#name(#span, #message); }
506+
} else {
507+
quote! { #diag.#name(#message); }
508+
}
483509
}
484510
};
485511

0 commit comments

Comments
 (0)