Skip to content

Make BytePos 32-bit #10567

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 2 commits into from
Nov 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use syntax::parse::token;
use syntax::parse::token::{ident_interner, interner_get};
use syntax::parse::token::special_idents;
use syntax::print::pprust::path_to_str;
use syntax::codemap::{Span, dummy_sp, BytePos};
use syntax::codemap::{Span, dummy_sp, Pos};
use syntax::opt_vec::OptVec;
use syntax::visit;
use syntax::visit::Visitor;
Expand Down Expand Up @@ -2624,7 +2624,7 @@ impl Resolver {
if "???" == module_name {
let span = Span {
lo: span.lo,
hi: span.lo + BytePos(segment_name.len()),
hi: span.lo + Pos::from_uint(segment_name.len()),
expn_info: span.expn_info,
};
self.resolve_error(span,
Expand Down
12 changes: 7 additions & 5 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ pub trait Pos {
fn to_uint(&self) -> uint;
}

/// A byte offset
/// A byte offset. Keep this small (currently 32-bits), as AST contains
/// a lot of them.
#[deriving(Clone, Eq, IterBytes, Ord)]
pub struct BytePos(uint);
pub struct BytePos(u32);

/// A character offset. Because of multibyte utf8 characters, a byte offset
/// is not equivalent to a character offset. The CodeMap will convert BytePos
/// values to CharPos values as necessary.
Expand All @@ -42,8 +44,8 @@ pub struct CharPos(uint);
// have been unsuccessful

impl Pos for BytePos {
fn from_uint(n: uint) -> BytePos { BytePos(n) }
fn to_uint(&self) -> uint { **self }
fn from_uint(n: uint) -> BytePos { BytePos(n as u32) }
fn to_uint(&self) -> uint { **self as uint }
}

impl Add<BytePos, BytePos> for BytePos {
Expand Down Expand Up @@ -278,7 +280,7 @@ impl CodeMap {

let filemap = @FileMap {
name: filename, substr: substr, src: src,
start_pos: BytePos(start_pos),
start_pos: Pos::from_uint(start_pos),
lines: @mut ~[],
multibyte_chars: @mut ~[],
};
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn bump(rdr: &mut StringReader) {
let last_char = rdr.curr;
let next = rdr.src.char_range_at(current_byte_offset);
let byte_offset_diff = next.next - current_byte_offset;
rdr.pos = rdr.pos + BytePos(byte_offset_diff);
rdr.pos = rdr.pos + Pos::from_uint(byte_offset_diff);
rdr.curr = next.ch;
rdr.col = rdr.col + CharPos(1u);
if last_char == '\n' {
Expand All @@ -251,7 +251,7 @@ pub fn bump(rdr: &mut StringReader) {

if byte_offset_diff > 1 {
rdr.filemap.record_multibyte_char(
BytePos(current_byte_offset), byte_offset_diff);
Pos::from_uint(current_byte_offset), byte_offset_diff);
}
} else {
rdr.curr = unsafe { transmute(-1u32) }; // FIXME: #8971: unsound
Expand Down Expand Up @@ -327,7 +327,7 @@ fn consume_any_line_comment(rdr: @mut StringReader)
bump(rdr);
// line comments starting with "///" or "//!" are doc-comments
if rdr.curr == '/' || rdr.curr == '!' {
let start_bpos = rdr.pos - BytePos(3u);
let start_bpos = rdr.pos - BytePos(3);
while rdr.curr != '\n' && !is_eof(rdr) {
bump(rdr);
}
Expand Down Expand Up @@ -381,7 +381,7 @@ fn consume_block_comment(rdr: @mut StringReader)
-> Option<TokenAndSpan> {
// block comments starting with "/**" or "/*!" are doc-comments
let is_doc_comment = rdr.curr == '*' || rdr.curr == '!';
let start_bpos = rdr.pos - BytePos(if is_doc_comment {3u} else {2u});
let start_bpos = rdr.pos - BytePos(if is_doc_comment {3} else {2});

let mut level: int = 1;
while level > 0 {
Expand Down Expand Up @@ -809,7 +809,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
// Byte offsetting here is okay because the
// character before position `start` is an
// ascii single quote.
start - BytePos(1u),
start - BytePos(1),
rdr.last_pos,
~"unterminated character constant");
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod test {
}

// produce a codemap::span
fn sp (a: uint, b: uint) -> Span {
fn sp(a: u32, b: u32) -> Span {
Span{lo:BytePos(a),hi:BytePos(b),expn_info:None}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ impl Parser {
token::GT => self.bump(),
token::BINOP(token::SHR) => self.replace_token(
token::GT,
self.span.lo + BytePos(1u),
self.span.lo + BytePos(1),
self.span.hi
),
_ => self.fatal(format!("expected `{}`, found `{}`",
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ pub fn maybe_print_trailing_comment(s: @ps, span: codemap::Span,
if (*cmnt).style != comments::trailing { return; }
let span_line = cm.lookup_char_pos(span.hi);
let comment_line = cm.lookup_char_pos((*cmnt).pos);
let mut next = (*cmnt).pos + BytePos(1u);
let mut next = (*cmnt).pos + BytePos(1);
match next_pos { None => (), Some(p) => next = p }
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
span_line.line == comment_line.line {
Expand Down