Skip to content

Commit 27cc3d2

Browse files
committed
libsyntax: De-@mut FileMap::lines
1 parent ada9150 commit 27cc3d2

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/libsyntax/codemap.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ source code snippets, etc.
2121
2222
*/
2323

24+
use std::cell::RefCell;
2425
use std::cmp;
2526
use extra::serialize::{Encodable, Decodable, Encoder, Decoder};
2627

@@ -220,7 +221,7 @@ pub struct FileMap {
220221
/// The start position of this source in the CodeMap
221222
start_pos: BytePos,
222223
/// Locations of lines beginnings in the source code
223-
lines: @mut ~[BytePos],
224+
lines: RefCell<~[BytePos]>,
224225
/// Locations of multi-byte characters in the source code
225226
multibyte_chars: @mut ~[MultiByteChar],
226227
}
@@ -233,14 +234,16 @@ impl FileMap {
233234
// about what ends a line between this file and parse.rs
234235
pub fn next_line(&self, pos: BytePos) {
235236
// the new charpos must be > the last one (or it's the first one).
236-
let lines = &mut *self.lines;
237-
assert!((lines.len() == 0) || (lines[lines.len() - 1] < pos))
238-
lines.push(pos);
237+
let mut lines = self.lines.borrow_mut();;
238+
let line_len = lines.get().len();
239+
assert!(line_len == 0 || (lines.get()[line_len - 1] < pos))
240+
lines.get().push(pos);
239241
}
240242

241243
// get a line from the list of pre-computed line-beginnings
242244
pub fn get_line(&self, line: int) -> ~str {
243-
let begin: BytePos = self.lines[line] - self.start_pos;
245+
let mut lines = self.lines.borrow_mut();
246+
let begin: BytePos = lines.get()[line] - self.start_pos;
244247
let begin = begin.to_uint();
245248
let slice = self.src.slice_from(begin);
246249
match slice.find('\n') {
@@ -296,7 +299,7 @@ impl CodeMap {
296299
let filemap = @FileMap {
297300
name: filename, substr: substr, src: src,
298301
start_pos: Pos::from_uint(start_pos),
299-
lines: @mut ~[],
302+
lines: RefCell::new(~[]),
300303
multibyte_chars: @mut ~[],
301304
};
302305

@@ -421,11 +424,11 @@ impl CodeMap {
421424
let idx = self.lookup_filemap_idx(pos);
422425
let f = self.files[idx];
423426
let mut a = 0u;
424-
let lines = &*f.lines;
425-
let mut b = lines.len();
427+
let mut lines = f.lines.borrow_mut();
428+
let mut b = lines.get().len();
426429
while b - a > 1u {
427430
let m = (a + b) / 2u;
428-
if lines[m] > pos { b = m; } else { a = m; }
431+
if lines.get()[m] > pos { b = m; } else { a = m; }
429432
}
430433
return FileMapAndLine {fm: f, line: a};
431434
}
@@ -434,7 +437,8 @@ impl CodeMap {
434437
let FileMapAndLine {fm: f, line: a} = self.lookup_line(pos);
435438
let line = a + 1u; // Line numbers start at 1
436439
let chpos = self.bytepos_to_local_charpos(pos);
437-
let linebpos = f.lines[a];
440+
let mut lines = f.lines.borrow_mut();
441+
let linebpos = lines.get()[a];
438442
let linechpos = self.bytepos_to_local_charpos(linebpos);
439443
debug!("codemap: byte pos {:?} is on the line at byte pos {:?}",
440444
pos, linebpos);

src/libsyntax/ext/source_util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use parse;
1919
use parse::token::{get_ident_interner};
2020
use print::pprust;
2121

22+
use std::cell::RefCell;
2223
use std::io;
2324
use std::io::File;
2425
use std::str;
@@ -113,7 +114,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
113114
substr: codemap::FssNone,
114115
src: s,
115116
start_pos: codemap::BytePos(0),
116-
lines: @mut ~[],
117+
lines: RefCell::new(~[]),
117118
multibyte_chars: @mut ~[],
118119
});
119120
base::MRExpr(cx.expr_str(sp, s))

0 commit comments

Comments
 (0)