@@ -17,7 +17,7 @@ use crate::symbol::{kw, sym, Symbol};
1717use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
1818use crate :: { ast, attr, attr:: TransparencyError } ;
1919
20- use errors:: FatalError ;
20+ use errors:: { DiagnosticBuilder , FatalError } ;
2121use log:: debug;
2222use syntax_pos:: Span ;
2323
@@ -43,6 +43,18 @@ pub struct ParserAnyMacro<'a> {
4343 arm_span : Span ,
4444}
4545
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+
4658impl < ' a > ParserAnyMacro < ' a > {
4759 pub fn make ( mut self : Box < ParserAnyMacro < ' a > > , kind : AstFragmentKind ) -> AstFragment {
4860 let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = * self ;
@@ -71,27 +83,30 @@ impl<'a> ParserAnyMacro<'a> {
7183 e. span_label( site_span, "in this macro invocation" ) ;
7284 }
7385 match kind {
74- AstFragmentKind :: Ty => {
75- e. span_label(
76- site_span,
77- "this macro call doesn't expand to a type" ,
78- ) ;
79- }
8086 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+ }
85106 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") ;
93108 }
94- _ => { }
109+ _ => annotate_err_with_kind ( & mut e , kind , site_span ) ,
95110 } ;
96111 e
97112 } ) ) ;
0 commit comments