1111#![ feature( if_let_guard) ]
1212#![ feature( iter_intersperse) ]
1313#![ feature( let_chains) ]
14+ #![ feature( string_from_utf8_lossy_owned) ]
1415#![ warn( unreachable_pub) ]
1516// tidy-alphabetical-end
1617
17- use std:: path:: Path ;
18+ use std:: path:: { Path , PathBuf } ;
19+ use std:: str:: Utf8Error ;
1820
1921use rustc_ast as ast;
2022use rustc_ast:: tokenstream:: TokenStream ;
2123use rustc_ast:: { AttrItem , Attribute , MetaItemInner , token} ;
2224use rustc_ast_pretty:: pprust;
2325use rustc_data_structures:: sync:: Lrc ;
24- use rustc_errors:: { Diag , FatalError , PResult } ;
26+ use rustc_errors:: { Diag , EmissionGuarantee , FatalError , PResult , pluralize } ;
2527use rustc_session:: parse:: ParseSess ;
28+ use rustc_span:: source_map:: SourceMap ;
2629use rustc_span:: { FileName , SourceFile , Span } ;
2730pub use unicode_normalization:: UNICODE_VERSION as UNICODE_NORMALIZATION_VERSION ;
2831
@@ -73,9 +76,22 @@ pub fn new_parser_from_file<'a>(
7376 path : & Path ,
7477 sp : Option < Span > ,
7578) -> Result < Parser < ' a > , Vec < Diag < ' a > > > {
76- let source_file = psess. source_map ( ) . load_file ( path) . unwrap_or_else ( |e| {
77- let msg = format ! ( "couldn't read {}: {}" , path. display( ) , e) ;
79+ let sm = psess. source_map ( ) ;
80+ let source_file = sm. load_file ( path) . unwrap_or_else ( |e| {
81+ let msg = format ! ( "couldn't read `{}`: {}" , path. display( ) , e) ;
7882 let mut err = psess. dcx ( ) . struct_fatal ( msg) ;
83+ if let Ok ( contents) = std:: fs:: read ( path)
84+ && let Err ( utf8err) = String :: from_utf8 ( contents. clone ( ) )
85+ {
86+ utf8_error (
87+ sm,
88+ & path. display ( ) . to_string ( ) ,
89+ sp,
90+ & mut err,
91+ utf8err. utf8_error ( ) ,
92+ & contents,
93+ ) ;
94+ }
7995 if let Some ( sp) = sp {
8096 err. span ( sp) ;
8197 }
@@ -84,6 +100,49 @@ pub fn new_parser_from_file<'a>(
84100 new_parser_from_source_file ( psess, source_file)
85101}
86102
103+ pub fn utf8_error < E : EmissionGuarantee > (
104+ sm : & SourceMap ,
105+ path : & str ,
106+ sp : Option < Span > ,
107+ err : & mut Diag < ' _ , E > ,
108+ utf8err : Utf8Error ,
109+ contents : & [ u8 ] ,
110+ ) {
111+ // The file exists, but it wasn't valid UTF-8.
112+ let start = utf8err. valid_up_to ( ) ;
113+ let note = format ! ( "invalid utf-8 at byte `{start}`" ) ;
114+ let msg = if let Some ( len) = utf8err. error_len ( ) {
115+ format ! (
116+ "byte{s} `{bytes}` {are} not valid utf-8" ,
117+ bytes = if len == 1 {
118+ format!( "{:?}" , contents[ start] )
119+ } else {
120+ format!( "{:?}" , & contents[ start..start + len] )
121+ } ,
122+ s = pluralize!( len) ,
123+ are = if len == 1 { "is" } else { "are" } ,
124+ )
125+ } else {
126+ note. clone ( )
127+ } ;
128+ let contents = String :: from_utf8_lossy ( contents) . to_string ( ) ;
129+ let source = sm. new_source_file ( PathBuf :: from ( path) . into ( ) , contents) ;
130+ let span = Span :: with_root_ctxt (
131+ source. normalized_byte_pos ( start as u32 ) ,
132+ source. normalized_byte_pos ( start as u32 ) ,
133+ ) ;
134+ if span. is_dummy ( ) {
135+ err. note ( note) ;
136+ } else {
137+ if sp. is_some ( ) {
138+ err. span_note ( span, msg) ;
139+ } else {
140+ err. span ( span) ;
141+ err. span_label ( span, msg) ;
142+ }
143+ }
144+ }
145+
87146/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
88147/// the initial token stream.
89148fn new_parser_from_source_file (
0 commit comments