From c3afb16e1608929a816d6c0e2a0118185199aef1 Mon Sep 17 00:00:00 2001 From: Tinco Andringa Date: Wed, 11 Jul 2018 15:19:32 +0200 Subject: Track whether module declarations are inline (fixes #12590) --- src/libsyntax/parse/parser.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f57fca2cfcf..12156522242 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6252,6 +6252,7 @@ impl<'a> Parser<'a> { Ok(ast::Mod { inner: inner_lo.to(hi), items, + inline: true }) } @@ -6287,8 +6288,10 @@ impl<'a> Parser<'a> { // This mod is in an external file. Let's go get it! let ModulePathSuccess { path, directory_ownership, warn } = self.submod_path(id, &outer_attrs, id_span)?; - let (module, mut attrs) = + let (mut module, mut attrs) = self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?; + // Record that we fetched the mod from an external file + module.inline = false; if warn { let attr = Attribute { id: attr::mk_attr_id(), @@ -6301,9 +6304,13 @@ impl<'a> Parser<'a> { attr::mark_known(&attr); attrs.push(attr); } - Ok((id, module, Some(attrs))) + Ok((id, ItemKind::Mod(module), Some(attrs))) } else { - let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() }; + let placeholder = ast::Mod { + inner: syntax_pos::DUMMY_SP, + items: Vec::new(), + inline: false + }; Ok((id, ItemKind::Mod(placeholder), None)) } } else { @@ -6503,7 +6510,7 @@ impl<'a> Parser<'a> { directory_ownership: DirectoryOwnership, name: String, id_sp: Span) - -> PResult<'a, (ast::ItemKind, Vec )> { + -> PResult<'a, (ast::Mod, Vec )> { let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut(); if let Some(i) = included_mod_stack.iter().position(|p| *p == path) { let mut err = String::from("circular modules: "); @@ -6525,7 +6532,7 @@ impl<'a> Parser<'a> { let mod_attrs = p0.parse_inner_attributes()?; let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?; self.sess.included_mod_stack.borrow_mut().pop(); - Ok((ast::ItemKind::Mod(m0), mod_attrs)) + Ok((m0, mod_attrs)) } /// Parse a function declaration from a foreign module -- cgit 1.4.1-3-g733a5 From 81a8ee8fc4822a651aaea722d7920c0f780e9041 Mon Sep 17 00:00:00 2001 From: Tinco Andringa Date: Thu, 12 Jul 2018 23:35:40 +0200 Subject: pretty=expanded should expand mod declarations --- src/libsyntax/parse/parser.rs | 6 +++--- src/libsyntax/print/pprust.rs | 13 ++++++++++--- src/test/pretty/issue_12590_c.pp | 24 ++++++++++++++++++++++++ src/test/pretty/issue_12590_c.rs | 18 ++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/test/pretty/issue_12590_c.pp create mode 100644 src/test/pretty/issue_12590_c.rs (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 12156522242..16f1a1ad846 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6288,10 +6288,9 @@ impl<'a> Parser<'a> { // This mod is in an external file. Let's go get it! let ModulePathSuccess { path, directory_ownership, warn } = self.submod_path(id, &outer_attrs, id_span)?; - let (mut module, mut attrs) = + let (module, mut attrs) = self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?; // Record that we fetched the mod from an external file - module.inline = false; if warn { let attr = Attribute { id: attr::mk_attr_id(), @@ -6530,7 +6529,8 @@ impl<'a> Parser<'a> { p0.cfg_mods = self.cfg_mods; let mod_inner_lo = p0.span; let mod_attrs = p0.parse_inner_attributes()?; - let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?; + let mut m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?; + m0.inline = false; self.sess.included_mod_stack.borrow_mut().pop(); Ok((m0, mod_attrs)) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index fb4000294ea..74ac57a634b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -61,6 +61,7 @@ pub struct State<'a> { cur_cmnt: usize, boxes: Vec, ann: &'a (dyn PpAnn+'a), + is_expanded: bool } fn rust_printer<'a>(writer: Box, ann: &'a dyn PpAnn) -> State<'a> { @@ -72,6 +73,7 @@ fn rust_printer<'a>(writer: Box, ann: &'a dyn PpAnn) -> State<'a> cur_cmnt: 0, boxes: Vec::new(), ann, + is_expanded: false } } @@ -133,14 +135,17 @@ impl<'a> State<'a> { // If the code is post expansion, don't use the table of // literals, since it doesn't correspond with the literals // in the AST anymore. - if is_expanded { None } else { Some(lits) }) + if is_expanded { None } else { Some(lits) }, + is_expanded + ) } pub fn new(cm: &'a SourceMap, out: Box, ann: &'a dyn PpAnn, comments: Option>, - literals: Option>) -> State<'a> { + literals: Option>, + is_expanded: bool) -> State<'a> { State { s: pp::mk_printer(out, DEFAULT_COLUMNS), cm: Some(cm), @@ -149,6 +154,7 @@ impl<'a> State<'a> { cur_cmnt: 0, boxes: Vec::new(), ann, + is_expanded: is_expanded } } } @@ -1261,7 +1267,8 @@ impl<'a> State<'a> { self.head(&visibility_qualified(&item.vis, "mod"))?; self.print_ident(item.ident)?; - if _mod.inline { + if _mod.inline || self.is_expanded { + println!("Going to print inline anyway"); self.nbsp()?; self.bopen()?; self.print_mod(_mod, &item.attrs)?; diff --git a/src/test/pretty/issue_12590_c.pp b/src/test/pretty/issue_12590_c.pp new file mode 100644 index 00000000000..359b55a735a --- /dev/null +++ b/src/test/pretty/issue_12590_c.pp @@ -0,0 +1,24 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// pp-exact:issue_12590_c.pp +// pretty-mode:expanded + +// The next line should be expanded + +mod issue_12590_b { + + + fn b() { } + + fn main() { } +} + +fn main() { } diff --git a/src/test/pretty/issue_12590_c.rs b/src/test/pretty/issue_12590_c.rs new file mode 100644 index 00000000000..a6b81fdefe6 --- /dev/null +++ b/src/test/pretty/issue_12590_c.rs @@ -0,0 +1,18 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// pp-exact:issue_12590_c.pp +// pretty-mode:expanded + +// The next line should be expanded + +mod issue_12590_b; + +fn main() { } -- cgit 1.4.1-3-g733a5