@@ -221,30 +221,16 @@ pub fn new_parser_from_tts<'a>(sess: &'a ParseSess,
221
221
/// Given a session and a path and an optional span (for error reporting),
222
222
/// add the path to the session's codemap and return the new filemap.
223
223
pub fn file_to_filemap ( sess : & ParseSess , path : & Path , spanopt : Option < Span > )
224
- -> Rc < FileMap > {
225
- let err = |msg : & str | {
224
+ -> Rc < FileMap > {
225
+ let mut contents = String :: new ( ) ;
226
+ if let Err ( e) = File :: open ( path) . and_then ( |mut f| f. read_to_string ( & mut contents) ) {
227
+ let msg = format ! ( "couldn't read {:?}: {}" , path. display( ) , e) ;
226
228
match spanopt {
227
- Some ( sp) => panic ! ( sess. span_diagnostic. span_fatal( sp, msg) ) ,
228
- None => sess. span_diagnostic . handler ( ) . fatal ( msg) ,
229
- }
230
- } ;
231
- let mut bytes = Vec :: new ( ) ;
232
- match File :: open ( path) . and_then ( |mut f| f. read_to_end ( & mut bytes) ) {
233
- Ok ( ..) => { }
234
- Err ( e) => {
235
- err ( & format ! ( "couldn't read {:?}: {}" , path. display( ) , e) ) ;
236
- unreachable ! ( ) ;
237
- }
238
- } ;
239
- match str:: from_utf8 ( & bytes[ ..] ) . ok ( ) {
240
- Some ( s) => {
241
- sess. codemap ( ) . new_filemap ( path. to_str ( ) . unwrap ( ) . to_string ( ) , s. to_string ( ) )
242
- }
243
- None => {
244
- err ( & format ! ( "{:?} is not UTF-8 encoded" , path. display( ) ) ) ;
245
- unreachable ! ( ) ;
229
+ Some ( sp) => panic ! ( sess. span_diagnostic. span_fatal( sp, & msg) ) ,
230
+ None => sess. span_diagnostic . handler ( ) . fatal ( & msg)
246
231
}
247
232
}
233
+ sess. codemap ( ) . new_filemap ( path. to_str ( ) . unwrap ( ) . to_string ( ) , contents)
248
234
}
249
235
250
236
/// Given a filemap, produce a sequence of token-trees
0 commit comments