Skip to content

Commit c4501a0

Browse files
committed
Auto merge of #52319 - tinco:issue_12590, r=pnkfelix
Track whether module declarations are inline (fixes #12590) To track whether module declarations are inline I added a field `inline: bool` to `ast::Mod`. The main use case is for pretty to know whether it should render the items associated with the module, but perhaps there are use cases for this information to not be forgotten in the AST.
2 parents e999ebd + b985e91 commit c4501a0

File tree

12 files changed

+143
-17
lines changed

12 files changed

+143
-17
lines changed

Diff for: src/libsyntax/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,8 @@ pub struct Mod {
18851885
/// to the last token in the external file.
18861886
pub inner: Span,
18871887
pub items: Vec<P<Item>>,
1888+
/// For `mod foo;` inline is false, for `mod foo { .. }` it is true.
1889+
pub inline: bool,
18881890
}
18891891

18901892
/// Foreign module declaration.

Diff for: src/libsyntax/ext/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
11011101
ast::ItemKind::Mod(ast::Mod {
11021102
inner: inner_span,
11031103
items,
1104+
inline: true
11041105
})
11051106
)
11061107
}

Diff for: src/libsyntax/ext/expand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
303303
krate.module = ast::Mod {
304304
inner: orig_mod_span,
305305
items: vec![],
306+
inline: true,
306307
};
307308
},
308309
_ => unreachable!(),

Diff for: src/libsyntax/fold.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,11 @@ pub fn noop_fold_fn_header<T: Folder>(mut header: FnHeader, folder: &mut T) -> F
10441044
header
10451045
}
10461046

1047-
pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod {
1047+
pub fn noop_fold_mod<T: Folder>(Mod {inner, items, inline}: Mod, folder: &mut T) -> Mod {
10481048
Mod {
10491049
inner: folder.new_span(inner),
10501050
items: items.move_flat_map(|x| folder.fold_item(x)),
1051+
inline: inline,
10511052
}
10521053
}
10531054

@@ -1077,6 +1078,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
10771078
None => (ast::Mod {
10781079
inner: span,
10791080
items: vec![],
1081+
inline: true,
10801082
}, vec![], span)
10811083
};
10821084

Diff for: src/libsyntax/parse/parser.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -6297,6 +6297,7 @@ impl<'a> Parser<'a> {
62976297
Ok(ast::Mod {
62986298
inner: inner_lo.to(hi),
62996299
items,
6300+
inline: true
63006301
})
63016302
}
63026303

@@ -6334,6 +6335,7 @@ impl<'a> Parser<'a> {
63346335
self.submod_path(id, &outer_attrs, id_span)?;
63356336
let (module, mut attrs) =
63366337
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
6338+
// Record that we fetched the mod from an external file
63376339
if warn {
63386340
let attr = Attribute {
63396341
id: attr::mk_attr_id(),
@@ -6346,9 +6348,13 @@ impl<'a> Parser<'a> {
63466348
attr::mark_known(&attr);
63476349
attrs.push(attr);
63486350
}
6349-
Ok((id, module, Some(attrs)))
6351+
Ok((id, ItemKind::Mod(module), Some(attrs)))
63506352
} else {
6351-
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
6353+
let placeholder = ast::Mod {
6354+
inner: syntax_pos::DUMMY_SP,
6355+
items: Vec::new(),
6356+
inline: false
6357+
};
63526358
Ok((id, ItemKind::Mod(placeholder), None))
63536359
}
63546360
} else {
@@ -6548,7 +6554,7 @@ impl<'a> Parser<'a> {
65486554
directory_ownership: DirectoryOwnership,
65496555
name: String,
65506556
id_sp: Span)
6551-
-> PResult<'a, (ast::ItemKind, Vec<Attribute> )> {
6557+
-> PResult<'a, (ast::Mod, Vec<Attribute> )> {
65526558
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
65536559
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
65546560
let mut err = String::from("circular modules: ");
@@ -6568,9 +6574,10 @@ impl<'a> Parser<'a> {
65686574
p0.cfg_mods = self.cfg_mods;
65696575
let mod_inner_lo = p0.span;
65706576
let mod_attrs = p0.parse_inner_attributes()?;
6571-
let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
6577+
let mut m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
6578+
m0.inline = false;
65726579
self.sess.included_mod_stack.borrow_mut().pop();
6573-
Ok((ast::ItemKind::Mod(m0), mod_attrs))
6580+
Ok((m0, mod_attrs))
65746581
}
65756582

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

Diff for: src/libsyntax/print/pprust.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub struct State<'a> {
6161
cur_cmnt: usize,
6262
boxes: Vec<pp::Breaks>,
6363
ann: &'a (dyn PpAnn+'a),
64+
is_expanded: bool
6465
}
6566

6667
fn rust_printer<'a>(writer: Box<dyn Write+'a>, ann: &'a dyn PpAnn) -> State<'a> {
@@ -72,6 +73,7 @@ fn rust_printer<'a>(writer: Box<dyn Write+'a>, ann: &'a dyn PpAnn) -> State<'a>
7273
cur_cmnt: 0,
7374
boxes: Vec::new(),
7475
ann,
76+
is_expanded: false
7577
}
7678
}
7779

@@ -133,14 +135,17 @@ impl<'a> State<'a> {
133135
// If the code is post expansion, don't use the table of
134136
// literals, since it doesn't correspond with the literals
135137
// in the AST anymore.
136-
if is_expanded { None } else { Some(lits) })
138+
if is_expanded { None } else { Some(lits) },
139+
is_expanded
140+
)
137141
}
138142

139143
pub fn new(cm: &'a SourceMap,
140144
out: Box<dyn Write+'a>,
141145
ann: &'a dyn PpAnn,
142146
comments: Option<Vec<comments::Comment>>,
143-
literals: Option<Vec<comments::Literal>>) -> State<'a> {
147+
literals: Option<Vec<comments::Literal>>,
148+
is_expanded: bool) -> State<'a> {
144149
State {
145150
s: pp::mk_printer(out, DEFAULT_COLUMNS),
146151
cm: Some(cm),
@@ -149,6 +154,7 @@ impl<'a> State<'a> {
149154
cur_cmnt: 0,
150155
boxes: Vec::new(),
151156
ann,
157+
is_expanded: is_expanded
152158
}
153159
}
154160
}
@@ -1260,10 +1266,18 @@ impl<'a> State<'a> {
12601266
ast::ItemKind::Mod(ref _mod) => {
12611267
self.head(&visibility_qualified(&item.vis, "mod"))?;
12621268
self.print_ident(item.ident)?;
1263-
self.nbsp()?;
1264-
self.bopen()?;
1265-
self.print_mod(_mod, &item.attrs)?;
1266-
self.bclose(item.span)?;
1269+
1270+
if _mod.inline || self.is_expanded {
1271+
self.nbsp()?;
1272+
self.bopen()?;
1273+
self.print_mod(_mod, &item.attrs)?;
1274+
self.bclose(item.span)?;
1275+
} else {
1276+
self.s.word(";")?;
1277+
self.end()?; // end inner head-block
1278+
self.end()?; // end outer head-block
1279+
}
1280+
12671281
}
12681282
ast::ItemKind::ForeignMod(ref nmod) => {
12691283
self.head("extern")?;

Diff for: src/libsyntax/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
237237
})).collect();
238238

239239
let reexport_mod = ast::Mod {
240+
inline: true,
240241
inner: DUMMY_SP,
241242
items,
242243
};

Diff for: src/test/pretty/issue_12590_a.rs

+17
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() { }

Diff for: src/test/pretty/issue_12590_b.rs

+14
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() { }

Diff for: src/test/pretty/issue_12590_c.pp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#[prelude_import]
4+
use ::std::prelude::v1::*;
5+
#[macro_use]
6+
extern crate std;
7+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
8+
// file at the top-level directory of this distribution and at
9+
// http://rust-lang.org/COPYRIGHT.
10+
//
11+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
12+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
13+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
14+
// option. This file may not be copied, modified, or distributed
15+
// except according to those terms.
16+
17+
// pretty-compare-only
18+
// pretty-mode:expanded
19+
// pp-exact:issue_12590_c.pp
20+
21+
// The next line should be expanded
22+
23+
mod issue_12590_b {
24+
25+
fn b() { }
26+
fn main() { }
27+
}
28+
fn main() { }

Diff for: src/test/pretty/issue_12590_c.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// pretty-compare-only
12+
// pretty-mode:expanded
13+
// pp-exact:issue_12590_c.pp
14+
15+
// The next line should be expanded
16+
17+
mod issue_12590_b;
18+
19+
fn main() { }

Diff for: src/tools/compiletest/src/runtest.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ struct DebuggerCommands {
240240
breakpoint_lines: Vec<usize>,
241241
}
242242

243+
enum ReadFrom {
244+
Path,
245+
Stdin(String),
246+
}
247+
243248
impl<'test> TestCx<'test> {
244249
/// Code executed for each revision in turn (or, if there are no
245250
/// revisions, exactly once, with revision == None).
@@ -450,8 +455,14 @@ impl<'test> TestCx<'test> {
450455
round, self.revision
451456
),
452457
);
453-
let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
458+
let read_from = if round == 0 {
459+
ReadFrom::Path
460+
} else {
461+
ReadFrom::Stdin(srcs[round].to_owned())
462+
};
454463

464+
let proc_res = self.print_source(read_from,
465+
&self.props.pretty_mode);
455466
if !proc_res.status.success() {
456467
self.fatal_proc_rec(
457468
&format!(
@@ -506,7 +517,7 @@ impl<'test> TestCx<'test> {
506517
}
507518

508519
// additionally, run `--pretty expanded` and try to build it.
509-
let proc_res = self.print_source(srcs[round].clone(), "expanded");
520+
let proc_res = self.print_source(ReadFrom::Path, "expanded");
510521
if !proc_res.status.success() {
511522
self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
512523
}
@@ -524,12 +535,16 @@ impl<'test> TestCx<'test> {
524535
}
525536
}
526537

527-
fn print_source(&self, src: String, pretty_type: &str) -> ProcRes {
538+
fn print_source(&self, read_from: ReadFrom, pretty_type: &str) -> ProcRes {
528539
let aux_dir = self.aux_output_dir_name();
540+
let input: &str = match read_from {
541+
ReadFrom::Stdin(_) => "-",
542+
ReadFrom::Path => self.testpaths.file.to_str().unwrap(),
543+
};
529544

530545
let mut rustc = Command::new(&self.config.rustc_path);
531546
rustc
532-
.arg("-")
547+
.arg(input)
533548
.args(&["-Z", &format!("unpretty={}", pretty_type)])
534549
.args(&["--target", &self.config.target])
535550
.arg("-L")
@@ -538,11 +553,16 @@ impl<'test> TestCx<'test> {
538553
.args(&self.props.compile_flags)
539554
.envs(self.props.exec_env.clone());
540555

556+
let src = match read_from {
557+
ReadFrom::Stdin(src) => Some(src),
558+
ReadFrom::Path => None
559+
};
560+
541561
self.compose_and_run(
542562
rustc,
543563
self.config.compile_lib_path.to_str().unwrap(),
544564
Some(aux_dir.to_str().unwrap()),
545-
Some(src),
565+
src,
546566
)
547567
}
548568

0 commit comments

Comments
 (0)