Skip to content

Commit e913d69

Browse files
committed
Remove StringReader::col.
It only has a single use, within code handling indented block comments. We can replace that with the new `FileMap::col_pos()`, which computes the col position (BytePos instead of CharPos) based on the record of the last newline char (which we already record). This is actually an improvement, because `trim_whitespace_prefix_and_push_line()` was using `col`, which is a `CharPos`, as a slice index, which is a byte/char confusion.
1 parent 444b770 commit e913d69

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/libsyntax/parse/lexer/comments.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,19 @@ fn read_block_comment(rdr: &mut StringReader,
238238
debug!(">>> block comment");
239239
let p = rdr.pos;
240240
let mut lines: Vec<String> = Vec::new();
241-
let col = rdr.col;
241+
242+
// Count the number of chars since the start of the line by rescanning.
243+
let mut src_index = rdr.src_index(rdr.filemap.line_begin_pos());
244+
let end_src_index = rdr.src_index(rdr.pos);
245+
assert!(src_index <= end_src_index);
246+
let mut n = 0;
247+
while src_index < end_src_index {
248+
let c = char_at(&rdr.src, src_index);
249+
src_index += c.len_utf8();
250+
n += 1;
251+
}
252+
let col = CharPos(n);
253+
242254
rdr.bump();
243255
rdr.bump();
244256

src/libsyntax/parse/lexer/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ pub struct StringReader<'a> {
4444
pub next_pos: BytePos,
4545
/// The absolute offset within the codemap of the current character
4646
pub pos: BytePos,
47-
/// The column of the next character to read
48-
pub col: CharPos,
4947
/// The current character (which has been read from self.pos)
5048
pub ch: Option<char>,
5149
pub filemap: Lrc<syntax_pos::FileMap>,
@@ -175,7 +173,6 @@ impl<'a> StringReader<'a> {
175173
sess,
176174
next_pos: filemap.start_pos,
177175
pos: filemap.start_pos,
178-
col: CharPos(0),
179176
ch: Some('\n'),
180177
filemap,
181178
end_src_index: src.len(),
@@ -442,9 +439,6 @@ impl<'a> StringReader<'a> {
442439
if self.save_new_lines_and_multibyte {
443440
self.filemap.next_line(self.next_pos);
444441
}
445-
self.col = CharPos(0);
446-
} else {
447-
self.col = self.col + CharPos(1);
448442
}
449443
if next_ch_len > 1 {
450444
if self.save_new_lines_and_multibyte {

src/libsyntax_pos/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,15 @@ impl FileMap {
971971
lines.push(pos);
972972
}
973973

974+
/// Return the BytePos of the beginning of the current line.
975+
pub fn line_begin_pos(&self) -> BytePos {
976+
let lines = self.lines.borrow();
977+
match lines.last() {
978+
Some(&line_pos) => line_pos,
979+
None => self.start_pos,
980+
}
981+
}
982+
974983
/// Add externally loaded source.
975984
/// If the hash of the input doesn't match or no input is supplied via None,
976985
/// it is interpreted as an error and the corresponding enum variant is set.

0 commit comments

Comments
 (0)