@@ -172,6 +172,11 @@ impl<'a> StringReader<'a> {
172
172
self . span_diagnostic . span_err ( sp, m)
173
173
}
174
174
175
+ /// Suggest some help with a given span.
176
+ pub fn help_span ( & self , sp : Span , m : & str ) {
177
+ self . span_diagnostic . span_help ( sp, m)
178
+ }
179
+
175
180
/// Report a fatal error spanning [`from_pos`, `to_pos`).
176
181
fn fatal_span_ ( & self , from_pos : BytePos , to_pos : BytePos , m : & str ) -> ! {
177
182
self . fatal_span ( codemap:: mk_sp ( from_pos, to_pos) , m)
@@ -182,6 +187,11 @@ impl<'a> StringReader<'a> {
182
187
self . err_span ( codemap:: mk_sp ( from_pos, to_pos) , m)
183
188
}
184
189
190
+ /// Suggest some help spanning [`from_pos`, `to_pos`).
191
+ fn help_span_ ( & self , from_pos : BytePos , to_pos : BytePos , m : & str ) {
192
+ self . help_span ( codemap:: mk_sp ( from_pos, to_pos) , m)
193
+ }
194
+
185
195
/// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
186
196
/// escaped character to the error message
187
197
fn fatal_span_char ( & self , from_pos : BytePos , to_pos : BytePos , m : & str , c : char ) -> ! {
@@ -728,19 +738,24 @@ impl<'a> StringReader<'a> {
728
738
return match e {
729
739
'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' => true ,
730
740
'x' => self . scan_byte_escape ( delim, !ascii_only) ,
731
- 'u' if self . curr_is ( '{' ) => {
732
- let valid = self . scan_unicode_escape ( delim) ;
733
- if valid && ascii_only {
734
- self . err_span_ (
735
- escaped_pos,
736
- self . last_pos ,
741
+ 'u' => {
742
+ let valid = if self . curr_is ( '{' ) {
743
+ self . scan_unicode_escape ( delim) && !ascii_only
744
+ } else {
745
+ self . err_span_ ( start, self . last_pos ,
746
+ "incorrect unicode escape sequence" ) ;
747
+ self . help_span_ ( start, self . last_pos ,
748
+ "format of unicode escape sequences is `\\ u{…}`" ) ;
749
+ false
750
+ } ;
751
+ if ascii_only {
752
+ self . err_span_ ( start, self . last_pos ,
737
753
"unicode escape sequences cannot be used as a byte or in \
738
754
a byte string"
739
755
) ;
740
- false
741
- } else {
742
- valid
743
756
}
757
+ valid
758
+
744
759
}
745
760
'\n' if delim == '"' => {
746
761
self . consume_whitespace ( ) ;
@@ -757,16 +772,13 @@ impl<'a> StringReader<'a> {
757
772
if ascii_only { "unknown byte escape" }
758
773
else { "unknown character escape" } ,
759
774
c) ;
760
- let sp = codemap:: mk_sp ( escaped_pos, last_pos) ;
761
775
if e == '\r' {
762
- self . span_diagnostic . span_help (
763
- sp,
776
+ self . help_span_ ( escaped_pos, last_pos,
764
777
"this is an isolated carriage return; consider checking \
765
778
your editor and version control settings")
766
779
}
767
780
if ( e == '{' || e == '}' ) && !ascii_only {
768
- self . span_diagnostic . span_help (
769
- sp,
781
+ self . help_span_ ( escaped_pos, last_pos,
770
782
"if used in a formatting string, \
771
783
curly braces are escaped with `{{` and `}}`")
772
784
}
@@ -848,14 +860,12 @@ impl<'a> StringReader<'a> {
848
860
valid = false ;
849
861
}
850
862
851
- self . bump ( ) ; // past the ending }
852
-
853
863
if valid && ( char:: from_u32 ( accum_int) . is_none ( ) || count == 0 ) {
854
864
self . err_span_ ( start_bpos, self . last_pos , "illegal unicode character escape" ) ;
855
865
valid = false ;
856
866
}
857
867
858
-
868
+ self . bump ( ) ; // past the ending }
859
869
valid
860
870
}
861
871
0 commit comments