@@ -28,6 +28,39 @@ enum SubdiagnosticSuggestionKind {
28
28
Verbose ,
29
29
}
30
30
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
+
31
64
/// Which kind of subdiagnostic is being created from a variant?
32
65
#[ derive( Clone , Copy ) ]
33
66
enum SubdiagnosticKind {
@@ -52,17 +85,15 @@ impl FromStr for SubdiagnosticKind {
52
85
"note" => Ok ( SubdiagnosticKind :: Note ) ,
53
86
"help" => Ok ( SubdiagnosticKind :: Help ) ,
54
87
"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 ( ( ) )
64
96
}
65
- _ => Err ( ( ) ) ,
66
97
}
67
98
}
68
99
}
@@ -74,18 +105,7 @@ impl quote::IdentFragment for SubdiagnosticKind {
74
105
SubdiagnosticKind :: Note => write ! ( f, "note" ) ,
75
106
SubdiagnosticKind :: Help => write ! ( f, "help" ) ,
76
107
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" ) ,
89
109
}
90
110
}
91
111
@@ -461,25 +481,31 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
461
481
let diag = & self . diag ;
462
482
let name = format_ident ! ( "{}{}" , if span_field. is_some( ) { "span_" } else { "" } , kind) ;
463
483
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
+ }
470
494
}
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
+ }
477
502
}
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
+ }
483
509
}
484
510
} ;
485
511
0 commit comments