diff options
| author | Kevin Atkinson <kevina@cs.utah.edu> | 2012-01-22 17:24:55 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-01-23 17:37:15 -0800 |
| commit | ad21d9c64a1b7b6fb996f37d697c5d4f43f13bae (patch) | |
| tree | 20c81c17a0ee2fd2624079984cf235d19722c763 /src/comp/syntax/parse | |
| parent | 355edf13e7d41ecdef750b166824422e76c4ef26 (diff) | |
| download | rust-ad21d9c64a1b7b6fb996f37d697c5d4f43f13bae.tar.gz rust-ad21d9c64a1b7b6fb996f37d697c5d4f43f13bae.zip | |
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).
Diffstat (limited to 'src/comp/syntax/parse')
| -rw-r--r-- | src/comp/syntax/parse/eval.rs | 15 | ||||
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 34 |
2 files changed, 29 insertions, 20 deletions
diff --git a/src/comp/syntax/parse/eval.rs b/src/comp/syntax/parse/eval.rs index 897cfe03e09..ca7af9e273d 100644 --- a/src/comp/syntax/parse/eval.rs +++ b/src/comp/syntax/parse/eval.rs @@ -14,8 +14,6 @@ export eval_crate_directives_to_mod; type ctx = @{p: parser, sess: parser::parse_sess, - mutable chpos: uint, - mutable byte_pos: uint, cfg: ast::crate_cfg}; 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>) if file_exists(modpath) { #debug("found companion mod"); let p0 = new_parser_from_file(cx.sess, cx.cfg, modpath, - cx.chpos, cx.byte_pos, SOURCE_FILE); + SOURCE_FILE); let inner_attrs = parse_inner_attrs_and_next(p0); let first_item_outer_attrs = inner_attrs.next; let m0 = parse_mod_items(p0, token::EOF, first_item_outer_attrs); - cx.chpos = p0.reader.chpos; - cx.byte_pos = p0.reader.pos; + cx.sess.chpos = p0.reader.chpos; + cx.sess.byte_pos = p0.reader.pos; ret (m0.view_items, m0.items, inner_attrs.inner); } else { ret ([], [], []); @@ -108,8 +106,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str, file_path } else { prefix + std::fs::path_sep() + file_path }; let p0 = - new_parser_from_file(cx.sess, cx.cfg, full_path, cx.chpos, - cx.byte_pos, SOURCE_FILE); + new_parser_from_file(cx.sess, cx.cfg, full_path, SOURCE_FILE); let inner_attrs = parse_inner_attrs_and_next(p0); let mod_attrs = attrs + inner_attrs.inner; let first_item_outer_attrs = inner_attrs.next; @@ -119,8 +116,8 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str, syntax::parse::parser::mk_item(p0, cdir.span.lo, cdir.span.hi, id, ast::item_mod(m0), mod_attrs); // Thread defids, chpos and byte_pos through the parsers - cx.chpos = p0.reader.chpos; - cx.byte_pos = p0.reader.pos; + cx.sess.chpos = p0.reader.chpos; + cx.sess.byte_pos = p0.reader.pos; items += [i]; } ast::cdir_dir_mod(id, cdirs, attrs) { diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index b18db178fcd..cb7e1144334 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -25,7 +25,10 @@ enum file_type { CRATE_FILE, SOURCE_FILE, } type parse_sess = @{ cm: codemap::codemap, mutable next_id: node_id, - diagnostic: diagnostic::handler + diagnostic: diagnostic::handler, + // these two must be kept up to date + mutable chpos: uint, + mutable byte_pos: uint }; fn next_node_id(sess: parse_sess) -> node_id { @@ -91,7 +94,7 @@ impl parser for parser { } fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str, - chpos: uint, byte_pos: uint, ftype: file_type) -> + ftype: file_type) -> parser { let src = alt io::read_whole_file_str(path) { result::ok(src) { @@ -102,7 +105,7 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str, sess.diagnostic.fatal(e) } }; - let filemap = codemap::new_filemap(path, chpos, byte_pos); + let filemap = codemap::new_filemap(path, sess.chpos, sess.byte_pos); sess.cm.files += [filemap]; let itr = @interner::mk(str::hash, str::eq); 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, fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, name: str, source: str) -> parser { let ftype = SOURCE_FILE; - let filemap = codemap::new_filemap(name, 0u, 0u); + let filemap = codemap::new_filemap(name, sess.chpos, sess.byte_pos); sess.cm.files += [filemap]; let itr = @interner::mk(str::hash, str::eq); let rdr = lexer::new_reader(sess.cm, sess.diagnostic, @@ -2482,21 +2485,30 @@ fn parse_native_view(p: parser) -> [@ast::view_item] { fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_parser_from_file(sess, cfg, input, 0u, 0u, SOURCE_FILE); - ret parse_crate_mod(p, cfg); + let p = new_parser_from_file(sess, cfg, input, SOURCE_FILE); + let r = parse_crate_mod(p, cfg); + sess.chpos = p.reader.chpos; + sess.byte_pos = p.reader.pos; + ret r; } fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::expr { let p = new_parser_from_source_str(sess, cfg, name, source); - ret parse_expr(p); + let r = parse_expr(p); + sess.chpos = p.reader.chpos; + sess.byte_pos = p.reader.pos; + ret r; } fn parse_crate_from_source_str(name: str, source: str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { let p = new_parser_from_source_str(sess, cfg, name, source); - ret parse_crate_mod(p, cfg); + let r = parse_crate_mod(p, cfg); + sess.chpos = p.reader.chpos; + sess.byte_pos = p.reader.pos; + ret r; } // Parses a source module as a crate @@ -2589,18 +2601,18 @@ fn parse_crate_directives(p: parser, term: token::token, fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_parser_from_file(sess, cfg, input, 0u, 0u, CRATE_FILE); + let p = new_parser_from_file(sess, cfg, input, CRATE_FILE); let lo = p.span.lo; let prefix = std::fs::dirname(p.reader.filemap.name); let leading_attrs = parse_inner_attrs_and_next(p); let crate_attrs = leading_attrs.inner; let first_cdir_attr = leading_attrs.next; let cdirs = parse_crate_directives(p, token::EOF, first_cdir_attr); + sess.chpos = p.reader.chpos; + sess.byte_pos = p.reader.pos; let cx = @{p: p, sess: sess, - mutable chpos: p.reader.chpos, - mutable byte_pos: p.reader.pos, cfg: p.cfg}; let (companionmod, _) = fs::splitext(fs::basename(input)); let (m, attrs) = eval::eval_crate_directives_to_mod( |
