Skip to content

Make FileMap::{lines, multibyte_chars, non_narrow_chars} non-mutable. #50997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 28, 2018
Prev Previous commit
Next Next commit
Use u32 instead of usize of encoding byte count of multi-byte chars.
  • Loading branch information
michaelwoerister committed Jun 27, 2018
commit 3497138634bf58a7c29ef35f1f677dbde0633af8
6 changes: 3 additions & 3 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,14 @@ impl CodeMap {
total_extra_bytes += mbc.bytes - 1;
// We should never see a byte position in the middle of a
// character
assert!(bpos.to_usize() >= mbc.pos.to_usize() + mbc.bytes);
assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes);
} else {
break;
}
}

assert!(map.start_pos.to_usize() + total_extra_bytes <= bpos.to_usize());
CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes)
assert!(map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32());
CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes as usize)
}

// Return the index of the filemap (in self.files) which contains pos.
Expand Down
20 changes: 17 additions & 3 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ pub struct MultiByteChar {
/// The absolute offset of the character in the CodeMap
pub pos: BytePos,
/// The number of bytes, >=2
pub bytes: usize,
pub bytes: u32,
}

/// Identifies an offset of a non-narrow character in a FileMap
Expand Down Expand Up @@ -1174,6 +1174,8 @@ fn remove_bom(src: &mut String) {
pub trait Pos {
fn from_usize(n: usize) -> Self;
fn to_usize(&self) -> usize;
fn from_u32(n: u32) -> Self;
fn to_u32(&self) -> u32;
}

/// A byte offset. Keep this small (currently 32-bits), as AST contains
Expand All @@ -1195,7 +1197,13 @@ impl Pos for BytePos {
fn from_usize(n: usize) -> BytePos { BytePos(n as u32) }

#[inline(always)]
fn to_usize(&self) -> usize { let BytePos(n) = *self; n as usize }
fn to_usize(&self) -> usize { self.0 as usize }

#[inline(always)]
fn from_u32(n: u32) -> BytePos { BytePos(n) }

#[inline(always)]
fn to_u32(&self) -> u32 { self.0 }
}

impl Add for BytePos {
Expand Down Expand Up @@ -1233,7 +1241,13 @@ impl Pos for CharPos {
fn from_usize(n: usize) -> CharPos { CharPos(n) }

#[inline(always)]
fn to_usize(&self) -> usize { let CharPos(n) = *self; n }
fn to_usize(&self) -> usize { self.0 }

#[inline(always)]
fn from_u32(n: u32) -> CharPos { CharPos(n as usize) }

#[inline(always)]
fn to_u32(&self) -> u32 { self.0 as u32}
}

impl Add for CharPos {
Expand Down