Skip to content

Commit ad21d9c

Browse files
kevinabrson
authored andcommitted
Don't reset the chpos/byte_pos to 0 in new_parser_from_source_str.
This correctly fixes issue #1362. chpos/byte_pos are now the offsets within a particular file, but rather the offsets within a virtual file with is formed by combing all of the modules within a crate. Thus, resetting them to 0 causes an overlap and hence, bogus source locations. Fix #1362 by moving chpos/byte_pos to parse_sess so that new_parser_from_source_str has access to them and hence can chose an initial value that is not already been used in the crate. Note that the trigger for bug 1361 was that syntax/ext/expand.rs calls parse_expr_from_source_str (which calls new_parser_from_source_str) using the same codemap as the current crate (and hence causing overlap with files in the crate as new_parser_from_source_str resets the chpos/byte_pos to 0).
1 parent 355edf1 commit ad21d9c

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

src/cargo/cargo.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ fn load_pkg(filename: str) -> option::t<pkg> {
105105
let sess = @{
106106
cm: cm,
107107
mutable next_id: 1,
108-
diagnostic: diagnostic::mk_handler(cm, none)
108+
diagnostic: diagnostic::mk_handler(cm, none),
109+
mutable chpos: 0u,
110+
mutable byte_pos: 0u
109111
};
110112
let c = parser::parse_crate_from_crate_file(filename, [], sess);
111113

src/comp/driver/driver.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ fn build_session(sopts: @session::options, input: str,
488488
parse_sess: @{
489489
cm: codemap,
490490
mutable next_id: 1,
491-
diagnostic: diagnostic_handler
491+
diagnostic: diagnostic_handler,
492+
mutable chpos: 0u,
493+
mutable byte_pos: 0u
492494
},
493495
codemap: codemap,
494496
// For a library crate, this is always none

src/comp/syntax/parse/eval.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export eval_crate_directives_to_mod;
1414
type ctx =
1515
@{p: parser,
1616
sess: parser::parse_sess,
17-
mutable chpos: uint,
18-
mutable byte_pos: uint,
1917
cfg: ast::crate_cfg};
2018

2119
fn eval_crate_directives(cx: ctx, cdirs: [@ast::crate_directive], prefix: str,
@@ -76,12 +74,12 @@ fn parse_companion_mod(cx: ctx, prefix: str, suffix: option::t<str>)
7674
if file_exists(modpath) {
7775
#debug("found companion mod");
7876
let p0 = new_parser_from_file(cx.sess, cx.cfg, modpath,
79-
cx.chpos, cx.byte_pos, SOURCE_FILE);
77+
SOURCE_FILE);
8078
let inner_attrs = parse_inner_attrs_and_next(p0);
8179
let first_item_outer_attrs = inner_attrs.next;
8280
let m0 = parse_mod_items(p0, token::EOF, first_item_outer_attrs);
83-
cx.chpos = p0.reader.chpos;
84-
cx.byte_pos = p0.reader.pos;
81+
cx.sess.chpos = p0.reader.chpos;
82+
cx.sess.byte_pos = p0.reader.pos;
8583
ret (m0.view_items, m0.items, inner_attrs.inner);
8684
} else {
8785
ret ([], [], []);
@@ -108,8 +106,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
108106
file_path
109107
} else { prefix + std::fs::path_sep() + file_path };
110108
let p0 =
111-
new_parser_from_file(cx.sess, cx.cfg, full_path, cx.chpos,
112-
cx.byte_pos, SOURCE_FILE);
109+
new_parser_from_file(cx.sess, cx.cfg, full_path, SOURCE_FILE);
113110
let inner_attrs = parse_inner_attrs_and_next(p0);
114111
let mod_attrs = attrs + inner_attrs.inner;
115112
let first_item_outer_attrs = inner_attrs.next;
@@ -119,8 +116,8 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
119116
syntax::parse::parser::mk_item(p0, cdir.span.lo, cdir.span.hi, id,
120117
ast::item_mod(m0), mod_attrs);
121118
// Thread defids, chpos and byte_pos through the parsers
122-
cx.chpos = p0.reader.chpos;
123-
cx.byte_pos = p0.reader.pos;
119+
cx.sess.chpos = p0.reader.chpos;
120+
cx.sess.byte_pos = p0.reader.pos;
124121
items += [i];
125122
}
126123
ast::cdir_dir_mod(id, cdirs, attrs) {

src/comp/syntax/parse/parser.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ enum file_type { CRATE_FILE, SOURCE_FILE, }
2525
type parse_sess = @{
2626
cm: codemap::codemap,
2727
mutable next_id: node_id,
28-
diagnostic: diagnostic::handler
28+
diagnostic: diagnostic::handler,
29+
// these two must be kept up to date
30+
mutable chpos: uint,
31+
mutable byte_pos: uint
2932
};
3033

3134
fn next_node_id(sess: parse_sess) -> node_id {
@@ -91,7 +94,7 @@ impl parser for parser {
9194
}
9295

9396
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
94-
chpos: uint, byte_pos: uint, ftype: file_type) ->
97+
ftype: file_type) ->
9598
parser {
9699
let src = alt io::read_whole_file_str(path) {
97100
result::ok(src) {
@@ -102,7 +105,7 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
102105
sess.diagnostic.fatal(e)
103106
}
104107
};
105-
let filemap = codemap::new_filemap(path, chpos, byte_pos);
108+
let filemap = codemap::new_filemap(path, sess.chpos, sess.byte_pos);
106109
sess.cm.files += [filemap];
107110
let itr = @interner::mk(str::hash, str::eq);
108111
let rdr = lexer::new_reader(sess.cm, sess.diagnostic,
@@ -113,7 +116,7 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
113116
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
114117
name: str, source: str) -> parser {
115118
let ftype = SOURCE_FILE;
116-
let filemap = codemap::new_filemap(name, 0u, 0u);
119+
let filemap = codemap::new_filemap(name, sess.chpos, sess.byte_pos);
117120
sess.cm.files += [filemap];
118121
let itr = @interner::mk(str::hash, str::eq);
119122
let rdr = lexer::new_reader(sess.cm, sess.diagnostic,
@@ -2482,21 +2485,30 @@ fn parse_native_view(p: parser) -> [@ast::view_item] {
24822485

24832486
fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
24842487
sess: parse_sess) -> @ast::crate {
2485-
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, SOURCE_FILE);
2486-
ret parse_crate_mod(p, cfg);
2488+
let p = new_parser_from_file(sess, cfg, input, SOURCE_FILE);
2489+
let r = parse_crate_mod(p, cfg);
2490+
sess.chpos = p.reader.chpos;
2491+
sess.byte_pos = p.reader.pos;
2492+
ret r;
24872493
}
24882494

24892495

24902496
fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
24912497
sess: parse_sess) -> @ast::expr {
24922498
let p = new_parser_from_source_str(sess, cfg, name, source);
2493-
ret parse_expr(p);
2499+
let r = parse_expr(p);
2500+
sess.chpos = p.reader.chpos;
2501+
sess.byte_pos = p.reader.pos;
2502+
ret r;
24942503
}
24952504

24962505
fn parse_crate_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
24972506
sess: parse_sess) -> @ast::crate {
24982507
let p = new_parser_from_source_str(sess, cfg, name, source);
2499-
ret parse_crate_mod(p, cfg);
2508+
let r = parse_crate_mod(p, cfg);
2509+
sess.chpos = p.reader.chpos;
2510+
sess.byte_pos = p.reader.pos;
2511+
ret r;
25002512
}
25012513

25022514
// Parses a source module as a crate
@@ -2589,18 +2601,18 @@ fn parse_crate_directives(p: parser, term: token::token,
25892601

25902602
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
25912603
sess: parse_sess) -> @ast::crate {
2592-
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, CRATE_FILE);
2604+
let p = new_parser_from_file(sess, cfg, input, CRATE_FILE);
25932605
let lo = p.span.lo;
25942606
let prefix = std::fs::dirname(p.reader.filemap.name);
25952607
let leading_attrs = parse_inner_attrs_and_next(p);
25962608
let crate_attrs = leading_attrs.inner;
25972609
let first_cdir_attr = leading_attrs.next;
25982610
let cdirs = parse_crate_directives(p, token::EOF, first_cdir_attr);
2611+
sess.chpos = p.reader.chpos;
2612+
sess.byte_pos = p.reader.pos;
25992613
let cx =
26002614
@{p: p,
26012615
sess: sess,
2602-
mutable chpos: p.reader.chpos,
2603-
mutable byte_pos: p.reader.pos,
26042616
cfg: p.cfg};
26052617
let (companionmod, _) = fs::splitext(fs::basename(input));
26062618
let (m, attrs) = eval::eval_crate_directives_to_mod(

src/fuzzer/fuzzer.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,9 @@ fn parse_and_print(code: str) -> str {
419419
let sess = @{
420420
cm: cm,
421421
mutable next_id: 0,
422-
diagnostic: diagnostic::mk_handler(cm, none)
422+
diagnostic: diagnostic::mk_handler(cm, none),
423+
mutable chpos: 0u,
424+
mutable byte_pos: 0u
423425
};
424426
write_file(filename, code);
425427
let crate = parser::parse_crate_from_source_str(
@@ -566,7 +568,9 @@ fn check_variants(files: [str], cx: context) {
566568
let sess = @{
567569
cm: cm,
568570
mutable next_id: 0,
569-
diagnostic: diagnostic::mk_handler(cm, none)
571+
diagnostic: diagnostic::mk_handler(cm, none),
572+
mutable chpos: 0u,
573+
mutable byte_pos: 0u
570574
};
571575
let crate =
572576
parser::parse_crate_from_source_str(

src/rustdoc/attr_parser.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ mod test {
289289
let parse_sess = @{
290290
cm: cm,
291291
mutable next_id: 0,
292-
diagnostic: diagnostic::mk_handler(cm, none)
292+
diagnostic: diagnostic::mk_handler(cm, none),
293+
mutable chpos: 0u,
294+
mutable byte_pos: 0u
293295
};
294296
let parser = parser::new_parser_from_source_str(
295297
parse_sess, [], "-", source);

src/rustdoc/parse.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ fn new_parse_sess() -> parser::parse_sess {
1212
let sess = @{
1313
cm: cm,
1414
mutable next_id: 1,
15-
diagnostic: diagnostic::mk_handler(cm, none)
15+
diagnostic: diagnostic::mk_handler(cm, none),
16+
mutable chpos: 0u,
17+
mutable byte_pos: 0u
1618
};
1719
ret sess;
1820
}

0 commit comments

Comments
 (0)