@@ -153,16 +153,37 @@ pub(crate) fn emit_unescape_error(
153153 EscapeError :: NonAsciiCharInByte => {
154154 assert ! ( mode. is_bytes( ) ) ;
155155 let ( c, span) = last_char ( ) ;
156- handler
157- . struct_span_err ( span, "non-ASCII character in byte constant" )
158- . span_label ( span , "byte constant must be ASCII" )
159- . span_suggestion (
156+ let mut err = handler. struct_span_err ( span , "non-ASCII character in byte constant" ) ;
157+ err . span_label ( span, "byte constant must be ASCII" ) ;
158+ if ( c as u32 ) <= 0xFF {
159+ err . span_suggestion (
160160 span,
161- "use a \\ xHH escape for a non-ASCII byte" ,
161+ & format ! (
162+ "if you meant to use the unicode code point for '{}', use a \\ xHH escape" ,
163+ c
164+ ) ,
162165 format ! ( "\\ x{:X}" , c as u32 ) ,
163- Applicability :: MachineApplicable ,
164- )
165- . emit ( ) ;
166+ Applicability :: MaybeIncorrect ,
167+ ) ;
168+ } else if matches ! ( mode, Mode :: Byte ) {
169+ err. span_label ( span, "this multibyte character does not fit into a single byte" ) ;
170+ } else if matches ! ( mode, Mode :: ByteStr ) {
171+ let mut utf8 = String :: new ( ) ;
172+ utf8. push ( c) ;
173+ err. span_suggestion (
174+ span,
175+ & format ! (
176+ "if you meant to use the UTF-8 encoding of '{}', use \\ xHH escapes" ,
177+ c
178+ ) ,
179+ utf8. as_bytes ( )
180+ . iter ( )
181+ . map ( |b : & u8 | format ! ( "\\ x{:X}" , * b) )
182+ . fold ( "" . to_string ( ) , |a, c| a + & c) ,
183+ Applicability :: MaybeIncorrect ,
184+ ) ;
185+ }
186+ err. emit ( ) ;
166187 }
167188 EscapeError :: NonAsciiCharInByteString => {
168189 assert ! ( mode. is_bytes( ) ) ;
0 commit comments