Skip to content

Commit f47d20a

Browse files
committed
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
1 parent bf34187 commit f47d20a

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

src/librustdoc/clean/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,10 @@ impl Span {
19471947

19481948
impl Clean<Span> for syntax::codemap::Span {
19491949
fn clean(&self, cx: &DocContext) -> Span {
1950+
if *self == DUMMY_SP {
1951+
return Span::empty();
1952+
}
1953+
19501954
let cm = cx.sess().codemap();
19511955
let filename = cm.span_to_filename(*self);
19521956
let lo = cm.lookup_char_pos(self.lo);

src/libsyntax/codemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ impl CodeMap {
894894
FileMapAndBytePos {fm: fm, pos: offset}
895895
}
896896

897-
/// Converts an absolute BytePos to a CharPos relative to the filemap and above.
897+
/// Converts an absolute BytePos to a CharPos relative to the filemap.
898898
pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
899899
let idx = self.lookup_filemap_idx(bpos);
900900
let files = self.files.borrow();

src/libsyntax/parse/lexer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl<'a> StringReader<'a> {
231231
None => {
232232
if self.is_eof() {
233233
self.peek_tok = token::Eof;
234+
self.peek_span = codemap::mk_sp(self.filemap.end_pos, self.filemap.end_pos);
234235
} else {
235236
let start_bytepos = self.last_pos;
236237
self.peek_tok = self.next_token_inner();

src/libsyntax/parse/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! The main parser interface
1212
1313
use ast;
14-
use codemap::{Span, CodeMap, FileMap};
14+
use codemap::{self, Span, CodeMap, FileMap};
1515
use diagnostic::{SpanHandler, Handler, Auto, FatalError};
1616
use parse::attr::ParserAttr;
1717
use parse::parser::Parser;
@@ -203,7 +203,14 @@ pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess,
203203
pub fn filemap_to_parser<'a>(sess: &'a ParseSess,
204204
filemap: Rc<FileMap>,
205205
cfg: ast::CrateConfig) -> Parser<'a> {
206-
tts_to_parser(sess, filemap_to_tts(sess, filemap), cfg)
206+
let end_pos = filemap.end_pos;
207+
let mut parser = tts_to_parser(sess, filemap_to_tts(sess, filemap), cfg);
208+
209+
if parser.token == token::Eof && parser.span == codemap::DUMMY_SP {
210+
parser.span = codemap::mk_sp(end_pos, end_pos);
211+
}
212+
213+
parser
207214
}
208215

209216
// must preserve old name for now, because quote! from the *existing*

src/libsyntax/parse/parser.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -4824,8 +4824,14 @@ impl<'a> Parser<'a> {
48244824
return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
48254825
}
48264826

4827+
let hi = if self.span == codemap::DUMMY_SP {
4828+
inner_lo
4829+
} else {
4830+
self.span.lo
4831+
};
4832+
48274833
Ok(ast::Mod {
4828-
inner: mk_sp(inner_lo, self.span.lo),
4834+
inner: mk_sp(inner_lo, hi),
48294835
items: items
48304836
})
48314837
}
@@ -4869,8 +4875,7 @@ impl<'a> Parser<'a> {
48694875

48704876
fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
48714877
let default_path = self.id_to_interned_str(id);
4872-
let file_path = match ::attr::first_attr_value_str_by_name(attrs,
4873-
"path") {
4878+
let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
48744879
Some(d) => d,
48754880
None => default_path,
48764881
};
@@ -5003,13 +5008,12 @@ impl<'a> Parser<'a> {
50035008
included_mod_stack.push(path.clone());
50045009
drop(included_mod_stack);
50055010

5006-
let mut p0 =
5007-
new_sub_parser_from_file(self.sess,
5008-
self.cfg.clone(),
5009-
&path,
5010-
owns_directory,
5011-
Some(name),
5012-
id_sp);
5011+
let mut p0 = new_sub_parser_from_file(self.sess,
5012+
self.cfg.clone(),
5013+
&path,
5014+
owns_directory,
5015+
Some(name),
5016+
id_sp);
50135017
let mod_inner_lo = p0.span.lo;
50145018
let mod_attrs = p0.parse_inner_attributes();
50155019
let m0 = try!(p0.parse_mod_items(&token::Eof, mod_inner_lo));

0 commit comments

Comments
 (0)