Skip to content

Commit 2eeb780

Browse files
committed
Remove #[suggestion_*] attributes
1 parent cd621be commit 2eeb780

File tree

5 files changed

+121
-87
lines changed

5 files changed

+121
-87
lines changed

compiler/rustc_macros/src/diagnostics/utils.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syn::{spanned::Spanned, Attribute, Field, Meta, Type, TypeTuple};
1212
use syn::{MetaList, MetaNameValue, NestedMeta, Path};
1313
use synstructure::{BindingInfo, VariantInfo};
1414

15-
use super::error::invalid_nested_attr;
15+
use super::error::{invalid_attr, invalid_nested_attr};
1616

1717
thread_local! {
1818
pub static CODE_IDENT_COUNT: RefCell<u32> = RefCell::new(0);
@@ -496,6 +496,18 @@ impl FromStr for SuggestionKind {
496496
}
497497
}
498498

499+
impl fmt::Display for SuggestionKind {
500+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
501+
match self {
502+
SuggestionKind::Normal => write!(f, "normal"),
503+
SuggestionKind::Short => write!(f, "short"),
504+
SuggestionKind::Hidden => write!(f, "hidden"),
505+
SuggestionKind::Verbose => write!(f, "verbose"),
506+
SuggestionKind::ToolOnly => write!(f, "tool-only"),
507+
}
508+
}
509+
}
510+
499511
impl SuggestionKind {
500512
pub fn to_suggestion_style(&self) -> TokenStream {
501513
match self {
@@ -577,21 +589,24 @@ impl SubdiagnosticKind {
577589

578590
let meta = attr.parse_meta()?;
579591

580-
let mut opt_suggestion_kind = None;
581592
let mut kind = match name {
582593
"label" => SubdiagnosticKind::Label,
583594
"note" => SubdiagnosticKind::Note,
584595
"help" => SubdiagnosticKind::Help,
585596
"warning" => SubdiagnosticKind::Warn,
586597
_ => {
587-
// FIXME(#100717): remove #[suggestion_{short,verbose,hidden}] attributes, use
588-
// #[suggestion(style = "...")] instead
598+
// Recover old `#[(multipart_)suggestion_*]` syntaxes
599+
// FIXME(#100717): remove
589600
if let Some(suggestion_kind) =
590601
name.strip_prefix("suggestion").and_then(SuggestionKind::from_suffix)
591602
{
592603
if suggestion_kind != SuggestionKind::Normal {
593-
// Plain `#[suggestion]` can have a `style = "..."` attribute later, so don't set it here
594-
opt_suggestion_kind.set_once(suggestion_kind, attr.path.span().unwrap());
604+
invalid_attr(attr, &meta)
605+
.help(format!(
606+
r#"Use `#[suggestion(..., style = "{}")]` instead"#,
607+
suggestion_kind
608+
))
609+
.emit();
595610
}
596611

597612
SubdiagnosticKind::Suggestion {
@@ -604,8 +619,12 @@ impl SubdiagnosticKind {
604619
name.strip_prefix("multipart_suggestion").and_then(SuggestionKind::from_suffix)
605620
{
606621
if suggestion_kind != SuggestionKind::Normal {
607-
// Plain `#[multipart_suggestion]` can have a `style = "..."` attribute later, so don't set it here
608-
opt_suggestion_kind.set_once(suggestion_kind, attr.path.span().unwrap());
622+
invalid_attr(attr, &meta)
623+
.help(format!(
624+
r#"Use `#[multipart_suggestion(..., style = "{}")]` instead"#,
625+
suggestion_kind
626+
))
627+
.emit();
609628
}
610629

611630
SubdiagnosticKind::MultipartSuggestion {
@@ -649,6 +668,7 @@ impl SubdiagnosticKind {
649668
};
650669

651670
let mut code = None;
671+
let mut suggestion_kind = None;
652672

653673
let mut nested_iter = nested.into_iter().peekable();
654674

@@ -727,7 +747,7 @@ impl SubdiagnosticKind {
727747
SuggestionKind::Normal
728748
});
729749

730-
opt_suggestion_kind.set_once(value, span);
750+
suggestion_kind.set_once(value, span);
731751
}
732752

733753
// Invalid nested attribute
@@ -753,11 +773,11 @@ impl SubdiagnosticKind {
753773
SubdiagnosticKind::Suggestion {
754774
ref code_field,
755775
ref mut code_init,
756-
ref mut suggestion_kind,
776+
suggestion_kind: ref mut kind_field,
757777
..
758778
} => {
759-
if let Some(kind) = opt_suggestion_kind.value() {
760-
*suggestion_kind = kind;
779+
if let Some(kind) = suggestion_kind.value() {
780+
*kind_field = kind;
761781
}
762782

763783
*code_init = if let Some(init) = code.value() {
@@ -767,9 +787,11 @@ impl SubdiagnosticKind {
767787
quote! { let #code_field = std::iter::empty(); }
768788
};
769789
}
770-
SubdiagnosticKind::MultipartSuggestion { ref mut suggestion_kind, .. } => {
771-
if let Some(kind) = opt_suggestion_kind.value() {
772-
*suggestion_kind = kind;
790+
SubdiagnosticKind::MultipartSuggestion {
791+
suggestion_kind: ref mut kind_field, ..
792+
} => {
793+
if let Some(kind) = suggestion_kind.value() {
794+
*kind_field = kind;
773795
}
774796
}
775797
SubdiagnosticKind::Label

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ struct LabelOnNonSpan {
211211
#[diag(compiletest_example, code = "E0123")]
212212
struct Suggest {
213213
#[suggestion(suggestion, code = "This is the suggested code")]
214-
#[suggestion_short(suggestion, code = "This is the suggested code")]
215-
#[suggestion_hidden(suggestion, code = "This is the suggested code")]
216-
#[suggestion_verbose(suggestion, code = "This is the suggested code")]
214+
#[suggestion(suggestion, code = "This is the suggested code", style = "normal")]
215+
#[suggestion(suggestion, code = "This is the suggested code", style = "short")]
216+
#[suggestion(suggestion, code = "This is the suggested code", style = "hidden")]
217+
#[suggestion(suggestion, code = "This is the suggested code", style = "verbose")]
217218
suggestion: (Span, Applicability),
218219
}
219220

@@ -730,7 +731,7 @@ struct SubdiagnosticEagerCorrect {
730731
// after the `span_suggestion` call - which breaks eager translation.
731732

732733
#[derive(Subdiagnostic)]
733-
#[suggestion_short(use_instead, applicability = "machine-applicable", code = "{correct}")]
734+
#[suggestion(use_instead, applicability = "machine-applicable", code = "{correct}")]
734735
pub(crate) struct SubdiagnosticWithSuggestion {
735736
#[primary_span]
736737
span: Span,

0 commit comments

Comments
 (0)