Use a span from the correct file for the inner span of a module

This basically only affects modules which are empty (or only contain comments).

Closes #26755
This commit is contained in:
Nick Cameron 2015-07-06 14:13:19 +12:00
parent bf34187a2f
commit f47d20aecd
5 changed files with 29 additions and 13 deletions

View File

@ -1947,6 +1947,10 @@ impl Span {
impl Clean<Span> for syntax::codemap::Span {
fn clean(&self, cx: &DocContext) -> Span {
if *self == DUMMY_SP {
return Span::empty();
}
let cm = cx.sess().codemap();
let filename = cm.span_to_filename(*self);
let lo = cm.lookup_char_pos(self.lo);

View File

@ -894,7 +894,7 @@ impl CodeMap {
FileMapAndBytePos {fm: fm, pos: offset}
}
/// Converts an absolute BytePos to a CharPos relative to the filemap and above.
/// Converts an absolute BytePos to a CharPos relative to the filemap.
pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
let idx = self.lookup_filemap_idx(bpos);
let files = self.files.borrow();

View File

@ -231,6 +231,7 @@ impl<'a> StringReader<'a> {
None => {
if self.is_eof() {
self.peek_tok = token::Eof;
self.peek_span = codemap::mk_sp(self.filemap.end_pos, self.filemap.end_pos);
} else {
let start_bytepos = self.last_pos;
self.peek_tok = self.next_token_inner();

View File

@ -11,7 +11,7 @@
//! The main parser interface
use ast;
use codemap::{Span, CodeMap, FileMap};
use codemap::{self, Span, CodeMap, FileMap};
use diagnostic::{SpanHandler, Handler, Auto, FatalError};
use parse::attr::ParserAttr;
use parse::parser::Parser;
@ -203,7 +203,14 @@ pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess,
pub fn filemap_to_parser<'a>(sess: &'a ParseSess,
filemap: Rc<FileMap>,
cfg: ast::CrateConfig) -> Parser<'a> {
tts_to_parser(sess, filemap_to_tts(sess, filemap), cfg)
let end_pos = filemap.end_pos;
let mut parser = tts_to_parser(sess, filemap_to_tts(sess, filemap), cfg);
if parser.token == token::Eof && parser.span == codemap::DUMMY_SP {
parser.span = codemap::mk_sp(end_pos, end_pos);
}
parser
}
// must preserve old name for now, because quote! from the *existing*

View File

@ -4824,8 +4824,14 @@ impl<'a> Parser<'a> {
return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
}
let hi = if self.span == codemap::DUMMY_SP {
inner_lo
} else {
self.span.lo
};
Ok(ast::Mod {
inner: mk_sp(inner_lo, self.span.lo),
inner: mk_sp(inner_lo, hi),
items: items
})
}
@ -4869,8 +4875,7 @@ impl<'a> Parser<'a> {
fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
let default_path = self.id_to_interned_str(id);
let file_path = match ::attr::first_attr_value_str_by_name(attrs,
"path") {
let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
Some(d) => d,
None => default_path,
};
@ -5003,13 +5008,12 @@ impl<'a> Parser<'a> {
included_mod_stack.push(path.clone());
drop(included_mod_stack);
let mut p0 =
new_sub_parser_from_file(self.sess,
self.cfg.clone(),
&path,
owns_directory,
Some(name),
id_sp);
let mut p0 = new_sub_parser_from_file(self.sess,
self.cfg.clone(),
&path,
owns_directory,
Some(name),
id_sp);
let mod_inner_lo = p0.span.lo;
let mod_attrs = p0.parse_inner_attributes();
let m0 = try!(p0.parse_mod_items(&token::Eof, mod_inner_lo));