Skip to content

Introduce a span for module contents #13791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn fold_mod(cx: &mut Context, m: &ast::Mod) -> ast::Mod {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect();
ast::Mod {
inner: m.inner,
view_items: filtered_view_items,
items: flattened_items
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
}

let mod_nomain = ast::Mod {
inner: m.inner,
view_items: m.view_items.clone(),
items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(),
};
Expand Down Expand Up @@ -335,6 +336,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::Item {
)).unwrap();

let testmod = ast::Mod {
inner: DUMMY_SP,
view_items: view_items,
items: vec!(mainfn, tests),
};
Expand Down
19 changes: 18 additions & 1 deletion src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,27 @@ impl Clean<Item> for doctree::Module {
self.view_items.clean().move_iter().collect(),
self.macros.clean().move_iter().collect()
);

// determine if we should display the inner contents or
// the outer `mod` item for the source code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment could be expanded to at least mention that this is handling the difference between mod ...; and mod ... {} explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added additional comments that show that difference.

let where = {
let ctxt = local_data::get(super::ctxtkey, |x| *x.unwrap());
let cm = ctxt.sess().codemap();
let outer = cm.lookup_char_pos(self.where_outer.lo);
let inner = cm.lookup_char_pos(self.where_inner.lo);
if outer.file.start_pos == inner.file.start_pos {
// mod foo { ... }
self.where_outer
} else {
// mod foo; (and a separate FileMap for the contents)
self.where_inner
}
};

Item {
name: Some(name),
attrs: self.attrs.clean(),
source: self.where.clean(),
source: where.clean(),
visibility: self.vis.clean(),
id: self.id,
inner: ModuleItem(Module {
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use syntax::ast::{Ident, NodeId};
pub struct Module {
pub name: Option<Ident>,
pub attrs: Vec<ast::Attribute>,
pub where: Span,
pub where_outer: Span,
pub where_inner: Span,
pub structs: Vec<Struct>,
pub enums: Vec<Enum>,
pub fns: Vec<Function>,
Expand All @@ -42,7 +43,8 @@ impl Module {
name : name,
id: 0,
vis: ast::Inherited,
where: syntax::codemap::DUMMY_SP,
where_outer: syntax::codemap::DUMMY_SP,
where_inner: syntax::codemap::DUMMY_SP,
attrs : Vec::new(),
structs : Vec::new(),
enums : Vec::new(),
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ impl<'a> RustdocVisitor<'a> {
for item in m.view_items.iter() {
self.visit_view_item(item, &mut om);
}
om.where = span;
om.where_outer = span;
om.where_inner = m.inner;
om.attrs = attrs;
om.vis = vis;
om.id = id;
Expand Down
18 changes: 15 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,12 @@ pub struct Method {

#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
pub struct Mod {
pub view_items: Vec<ViewItem> ,
pub items: Vec<@Item> ,
/// A span from the first token past `{` to the last token until `}`.
/// For `mod foo;`, the inner span ranges from the first token
/// to the last token in the external file.
pub inner: Span,
pub view_items: Vec<ViewItem>,
pub items: Vec<@Item>,
}

#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
Expand Down Expand Up @@ -1165,7 +1169,15 @@ mod test {
fn check_asts_encodable() {
use std::io;
let e = Crate {
module: Mod {view_items: Vec::new(), items: Vec::new()},
module: Mod {
inner: Span {
lo: BytePos(11),
hi: BytePos(19),
expn_info: None,
},
view_items: Vec::new(),
items: Vec::new(),
},
attrs: Vec::new(),
config: Vec::new(),
span: Span {
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub trait AstBuilder {
generics: Generics) -> @ast::Item;
fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> @ast::Item;

fn item_mod(&self, span: Span,
fn item_mod(&self, span: Span, inner_span: Span,
name: Ident, attrs: Vec<ast::Attribute> ,
vi: Vec<ast::ViewItem> , items: Vec<@ast::Item> ) -> @ast::Item;

Expand Down Expand Up @@ -898,7 +898,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
self.item(span, name, Vec::new(), ast::ItemStruct(@struct_def, generics))
}

fn item_mod(&self, span: Span, name: Ident,
fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
attrs: Vec<ast::Attribute> ,
vi: Vec<ast::ViewItem> ,
items: Vec<@ast::Item> ) -> @ast::Item {
Expand All @@ -907,6 +907,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
name,
attrs,
ast::ItemMod(ast::Mod {
inner: inner_span,
view_items: vi,
items: items,
})
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMeth

pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
ast::Mod {
inner: folder.new_span(m.inner),
view_items: m.view_items
.iter()
.map(|x| folder.fold_view_item(x)).collect(),
Expand Down
17 changes: 12 additions & 5 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4036,7 +4036,8 @@ impl<'a> Parser<'a> {
// attributes (of length 0 or 1), parse all of the items in a module
fn parse_mod_items(&mut self,
term: token::Token,
first_item_attrs: Vec<Attribute> )
first_item_attrs: Vec<Attribute>,
inner_lo: BytePos)
-> Mod {
// parse all of the items up to closing or an attribute.
// view items are legal here.
Expand Down Expand Up @@ -4081,7 +4082,11 @@ impl<'a> Parser<'a> {
self.span_err(self.last_span, "expected item after attributes");
}

ast::Mod { view_items: view_items, items: items }
ast::Mod {
inner: mk_sp(inner_lo, self.span.lo),
view_items: view_items,
items: items
}
}

fn parse_item_const(&mut self) -> ItemInfo {
Expand All @@ -4107,8 +4112,9 @@ impl<'a> Parser<'a> {
} else {
self.push_mod_path(id, outer_attrs);
self.expect(&token::LBRACE);
let mod_inner_lo = self.span.lo;
let (inner, next) = self.parse_inner_attrs_and_next();
let m = self.parse_mod_items(token::RBRACE, next);
let m = self.parse_mod_items(token::RBRACE, next, mod_inner_lo);
self.expect(&token::RBRACE);
self.pop_mod_path();
(id, ItemMod(m), Some(inner))
Expand Down Expand Up @@ -4197,10 +4203,11 @@ impl<'a> Parser<'a> {
self.cfg.clone(),
&path,
id_sp);
let mod_inner_lo = p0.span.lo;
let (inner, next) = p0.parse_inner_attrs_and_next();
let mod_attrs = outer_attrs.append(inner.as_slice());
let first_item_outer_attrs = next;
let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs, mod_inner_lo);
self.sess.included_mod_stack.borrow_mut().pop();
return (ast::ItemMod(m0), mod_attrs);
}
Expand Down Expand Up @@ -5061,7 +5068,7 @@ impl<'a> Parser<'a> {
let (inner, next) = self.parse_inner_attrs_and_next();
let first_item_outer_attrs = next;
// parse the items inside the crate:
let m = self.parse_mod_items(token::EOF, first_item_outer_attrs);
let m = self.parse_mod_items(token::EOF, first_item_outer_attrs, lo);

ast::Crate {
module: m,
Expand Down