@@ -17,7 +17,7 @@ use crate::symbol::{kw, sym, Symbol};
17
17
use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
18
18
use crate :: { ast, attr, attr:: TransparencyError } ;
19
19
20
- use errors:: FatalError ;
20
+ use errors:: { DiagnosticBuilder , FatalError } ;
21
21
use log:: debug;
22
22
use syntax_pos:: Span ;
23
23
@@ -43,6 +43,18 @@ pub struct ParserAnyMacro<'a> {
43
43
arm_span : Span ,
44
44
}
45
45
46
+ pub fn annotate_err_with_kind ( err : & mut DiagnosticBuilder < ' _ > , kind : AstFragmentKind , span : Span ) {
47
+ match kind {
48
+ AstFragmentKind :: Ty => {
49
+ err. span_label ( span, "this macro call doesn't expand to a type" ) ;
50
+ }
51
+ AstFragmentKind :: Pat => {
52
+ err. span_label ( span, "this macro call doesn't expand to a pattern" ) ;
53
+ }
54
+ _ => { }
55
+ } ;
56
+ }
57
+
46
58
impl < ' a > ParserAnyMacro < ' a > {
47
59
pub fn make ( mut self : Box < ParserAnyMacro < ' a > > , kind : AstFragmentKind ) -> AstFragment {
48
60
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = * self ;
@@ -71,27 +83,30 @@ impl<'a> ParserAnyMacro<'a> {
71
83
e. span_label( site_span, "in this macro invocation" ) ;
72
84
}
73
85
match kind {
74
- AstFragmentKind :: Ty => {
75
- e. span_label(
76
- site_span,
77
- "this macro call doesn't expand to a type" ,
78
- ) ;
79
- }
80
86
AstFragmentKind :: Pat if macro_ident. name == sym:: vec => {
81
- e. span_label(
82
- site_span,
83
- "use a slice pattern here instead" ,
84
- ) ;
87
+ let mut suggestion = None ;
88
+ if let Ok ( code) = parser. sess. source_map( ) . span_to_snippet( site_span) {
89
+ if let Some ( bang) = code. find( '!' ) {
90
+ suggestion = Some ( code[ bang + 1 ..] . to_string( ) ) ;
91
+ }
92
+ }
93
+ if let Some ( suggestion) = suggestion {
94
+ e. span_suggestion(
95
+ site_span,
96
+ "use a slice pattern here instead" ,
97
+ suggestion,
98
+ Applicability :: MachineApplicable ,
99
+ ) ;
100
+ } else {
101
+ e. span_label(
102
+ site_span,
103
+ "use a slice pattern here instead" ,
104
+ ) ;
105
+ }
85
106
e. help( "for more information, see https://doc.rust-lang.org/edition-guide/\
86
- rust-2018/slice-patterns.html") ;
87
- }
88
- AstFragmentKind :: Pat => {
89
- e. span_label(
90
- site_span,
91
- "this macro call doesn't expand to a pattern" ,
92
- ) ;
107
+ rust-2018/slice-patterns.html") ;
93
108
}
94
- _ => { }
109
+ _ => annotate_err_with_kind ( & mut e , kind , site_span ) ,
95
110
} ;
96
111
e
97
112
} ) ) ;
0 commit comments