diff options
| author | bors <bors@rust-lang.org> | 2014-04-28 05:21:46 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-04-28 05:21:46 -0700 |
| commit | a1ad41b93d133aa4f3bda71475f8e41d9dfe704d (patch) | |
| tree | a8cdfdaf2359e39d80de4045ca7f1143f7767b43 /src/libsyntax | |
| parent | 7a19a82d119ca51ed872ed207bed396cdf4a3283 (diff) | |
| parent | c8a29c4c595e76b71372a2e40d359ac1ddd8aec8 (diff) | |
| download | rust-a1ad41b93d133aa4f3bda71475f8e41d9dfe704d.tar.gz rust-a1ad41b93d133aa4f3bda71475f8e41d9dfe704d.zip | |
auto merge of #13791 : lifthrasiir/rust/mod-inner-span, r=huonw
This PR is primarily motivated by (and fixes) #12926.
We currently only have a span for the individual item itself and not for the referred contents. This normally does not cause a problem since both are located in the same file; it *is* possible that the contained statement or item is located in the other file (the syntax extension can do that), but even in that case the syntax extension should be located in the same file as the item. The module item (i.e. `mod foo;`) is the only exception here, and thus warrants a special treatment.
Rustdoc would now distinguish `mod foo;` from `mod foo {...}` by checking if the span for the module item and module contents is in different files. If it's the case, we'd prefer module contents over module item. There are alternative strategies, but as noted above we will have some corner cases if we don't record the contents span explicitly.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 18 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 17 |
4 files changed, 31 insertions, 10 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 9c9560e6dea..45f753d0e98 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -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)] @@ -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 { diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index dbf3c75401c..457cb4b79bf 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -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; @@ -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 { @@ -907,6 +907,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { name, attrs, ast::ItemMod(ast::Mod { + inner: inner_span, view_items: vi, items: items, }) diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index a4219d152a9..47ef23b82d2 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -652,6 +652,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(), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 88110a87e6c..8e6d6719bb9 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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. @@ -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 { @@ -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)) @@ -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); } @@ -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, |
