From a1f8a6ce80a340d51074071c0d9e30eb14f65d25 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Thu, 28 Jun 2018 10:45:57 +0200 Subject: [PATCH] Fix FileMap::line_begin_pos(). The method relied on the FileMap still being under construction in order for it to do what the name promises. It's now independent of the current state. --- src/libsyntax/parse/lexer/comments.rs | 6 ++++-- src/libsyntax_pos/lib.rs | 8 +++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 7da0d816d0f..3995a9b8689 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -240,9 +240,11 @@ fn read_block_comment(rdr: &mut StringReader, let mut lines: Vec = Vec::new(); // Count the number of chars since the start of the line by rescanning. - let mut src_index = rdr.src_index(rdr.filemap.line_begin_pos()); + let mut src_index = rdr.src_index(rdr.filemap.line_begin_pos(rdr.pos)); let end_src_index = rdr.src_index(rdr.pos); - assert!(src_index <= end_src_index); + assert!(src_index <= end_src_index, + "src_index={}, end_src_index={}, line_begin_pos={}", + src_index, end_src_index, rdr.filemap.line_begin_pos(rdr.pos).to_u32()); let mut n = 0; while src_index < end_src_index { let c = char_at(&rdr.src, src_index); diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 5502b30e488..55dec31511c 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -976,11 +976,9 @@ impl FileMap { } /// Return the BytePos of the beginning of the current line. - pub fn line_begin_pos(&self) -> BytePos { - match self.lines.last() { - Some(&line_pos) => line_pos, - None => self.start_pos, - } + pub fn line_begin_pos(&self, pos: BytePos) -> BytePos { + let line_index = self.lookup_line(pos).unwrap(); + self.lines[line_index] } /// Add externally loaded source.