Skip to content

Commit 5ab15cd

Browse files
committed
Track whether module declarations are inline (fixes rust-lang#12590)
1 parent 6cc42a4 commit 5ab15cd

File tree

9 files changed

+62
-10
lines changed

9 files changed

+62
-10
lines changed

src/libsyntax/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,8 @@ pub struct Mod {
18261826
/// to the last token in the external file.
18271827
pub inner: Span,
18281828
pub items: Vec<P<Item>>,
1829+
/// For `mod foo;` inline is false, for `mod foo { .. }` it is true.
1830+
pub inline: bool,
18291831
}
18301832

18311833
/// Foreign module declaration.

src/libsyntax/ext/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
11001100
ast::ItemKind::Mod(ast::Mod {
11011101
inner: inner_span,
11021102
items,
1103+
inline: true
11031104
})
11041105
)
11051106
}

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
288288
krate.module = ast::Mod {
289289
inner: orig_mod_span,
290290
items: vec![],
291+
inline: true,
291292
};
292293
},
293294
_ => unreachable!(),

src/libsyntax/fold.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,11 @@ pub fn noop_fold_fn_header<T: Folder>(mut header: FnHeader, folder: &mut T) -> F
10141014
header
10151015
}
10161016

1017-
pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod {
1017+
pub fn noop_fold_mod<T: Folder>(Mod {inner, items, inline}: Mod, folder: &mut T) -> Mod {
10181018
Mod {
10191019
inner: folder.new_span(inner),
10201020
items: items.move_flat_map(|x| folder.fold_item(x)),
1021+
inline: inline,
10211022
}
10221023
}
10231024

@@ -1047,6 +1048,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
10471048
None => (ast::Mod {
10481049
inner: span,
10491050
items: vec![],
1051+
inline: true,
10501052
}, vec![], span)
10511053
};
10521054

src/libsyntax/parse/parser.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6166,6 +6166,7 @@ impl<'a> Parser<'a> {
61666166
Ok(ast::Mod {
61676167
inner: inner_lo.to(hi),
61686168
items,
6169+
inline: true
61696170
})
61706171
}
61716172

@@ -6203,8 +6204,10 @@ impl<'a> Parser<'a> {
62036204
// This mod is in an external file. Let's go get it!
62046205
let ModulePathSuccess { path, directory_ownership, warn } =
62056206
self.submod_path(id, &outer_attrs, id_span)?;
6206-
let (module, mut attrs) =
6207+
let (mut module, mut attrs) =
62076208
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
6209+
// Record that we fetched the mod from an external file
6210+
module.inline = false;
62086211
if warn {
62096212
let attr = Attribute {
62106213
id: attr::mk_attr_id(),
@@ -6217,9 +6220,13 @@ impl<'a> Parser<'a> {
62176220
attr::mark_known(&attr);
62186221
attrs.push(attr);
62196222
}
6220-
Ok((id, module, Some(attrs)))
6223+
Ok((id, ItemKind::Mod(module), Some(attrs)))
62216224
} else {
6222-
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
6225+
let placeholder = ast::Mod {
6226+
inner: syntax_pos::DUMMY_SP,
6227+
items: Vec::new(),
6228+
inline: false
6229+
};
62236230
Ok((id, ItemKind::Mod(placeholder), None))
62246231
}
62256232
} else {
@@ -6419,7 +6426,7 @@ impl<'a> Parser<'a> {
64196426
directory_ownership: DirectoryOwnership,
64206427
name: String,
64216428
id_sp: Span)
6422-
-> PResult<'a, (ast::ItemKind, Vec<Attribute> )> {
6429+
-> PResult<'a, (ast::Mod, Vec<Attribute> )> {
64236430
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
64246431
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
64256432
let mut err = String::from("circular modules: ");
@@ -6441,7 +6448,7 @@ impl<'a> Parser<'a> {
64416448
let mod_attrs = p0.parse_inner_attributes()?;
64426449
let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
64436450
self.sess.included_mod_stack.borrow_mut().pop();
6444-
Ok((ast::ItemKind::Mod(m0), mod_attrs))
6451+
Ok((m0, mod_attrs))
64456452
}
64466453

64476454
/// Parse a function declaration from a foreign module

src/libsyntax/print/pprust.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,10 +1262,16 @@ impl<'a> State<'a> {
12621262
ast::ItemKind::Mod(ref _mod) => {
12631263
self.head(&visibility_qualified(&item.vis, "mod"))?;
12641264
self.print_ident(item.ident)?;
1265-
self.nbsp()?;
1266-
self.bopen()?;
1267-
self.print_mod(_mod, &item.attrs)?;
1268-
self.bclose(item.span)?;
1265+
1266+
if _mod.inline {
1267+
self.nbsp()?;
1268+
self.bopen()?;
1269+
self.print_mod(_mod, &item.attrs)?;
1270+
self.bclose(item.span)?;
1271+
} else {
1272+
self.s.word(";")?;
1273+
}
1274+
12691275
}
12701276
ast::ItemKind::ForeignMod(ref nmod) => {
12711277
self.head("extern")?;

src/libsyntax/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
259259
let reexport_mod = ast::Mod {
260260
inner: DUMMY_SP,
261261
items,
262+
inline: true,
262263
};
263264

264265
let sym = Ident::with_empty_ctxt(Symbol::gensym("__test_reexports"));
@@ -576,6 +577,7 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
576577
let testmod = ast::Mod {
577578
inner: DUMMY_SP,
578579
items: vec![import, mainfn, tests],
580+
inline: true,
579581
};
580582
let item_ = ast::ItemKind::Mod(testmod);
581583
let mod_ident = Ident::with_empty_ctxt(Symbol::gensym("__test"));

src/test/pretty/issue_12590_a.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// pp-exact
12+
13+
// The next line should not be expanded
14+
15+
mod issue_12590_b;
16+
17+
fn main() { }

src/test/pretty/issue_12590_b.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Second part of two file test
12+
fn b() { }
13+
14+
fn main() { }

0 commit comments

Comments
 (0)