Skip to content

Commit

Permalink
Use a span from the correct file for the inner span of a module
Browse files Browse the repository at this point in the history
This basically only affects modules which are empty (or only contain comments).

Closes #26755
  • Loading branch information
nrc committed Jul 21, 2015
1 parent bf34187 commit f47d20a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 9 additions & 2 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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*
Expand Down
24 changes: 14 additions & 10 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit f47d20a

Please sign in to comment.