Skip to content

Commit 0d50b04

Browse files
committed
syntax::parse: optimize file_to_filemap to read a string directly.
1 parent 6a59d18 commit 0d50b04

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,30 +221,16 @@ pub fn new_parser_from_tts<'a>(sess: &'a ParseSess,
221221
/// Given a session and a path and an optional span (for error reporting),
222222
/// add the path to the session's codemap and return the new filemap.
223223
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);
226228
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)
246231
}
247232
}
233+
sess.codemap().new_filemap(path.to_str().unwrap().to_string(), contents)
248234
}
249235

250236
/// Given a filemap, produce a sequence of token-trees

0 commit comments

Comments
 (0)