Skip to content

Commit a76698b

Browse files
authored
Auto merge of #34045 - ollie27:rustdoc_stripped, r=brson
rustdoc: Don't generate empty files for stripped items We need to traverse stripped modules to generate redirect pages, but we shouldn't generate anything else for them. This now renders the file contents to a Vec before writing it to a file in one go. I think that's probably a better strategy anyway. Fixes: #34025
2 parents b1b7526 + cfb4ad2 commit a76698b

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/librustdoc/html/render.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,6 @@ impl Context {
12571257

12581258
info!("Recursing into {}", self.dst.display());
12591259

1260-
mkdir(&self.dst).unwrap();
12611260
let ret = f(self);
12621261

12631262
info!("Recursed; leaving {}", self.dst.display());
@@ -1301,7 +1300,7 @@ impl Context {
13011300
fn item<F>(&mut self, item: clean::Item, mut f: F) -> Result<(), Error> where
13021301
F: FnMut(&mut Context, clean::Item),
13031302
{
1304-
fn render(w: File, cx: &Context, it: &clean::Item,
1303+
fn render(writer: &mut io::Write, cx: &Context, it: &clean::Item,
13051304
pushname: bool) -> io::Result<()> {
13061305
// A little unfortunate that this is done like this, but it sure
13071306
// does make formatting *a lot* nicer.
@@ -1336,12 +1335,8 @@ impl Context {
13361335

13371336
reset_ids(true);
13381337

1339-
// We have a huge number of calls to write, so try to alleviate some
1340-
// of the pain by using a buffered writer instead of invoking the
1341-
// write syscall all the time.
1342-
let mut writer = BufWriter::new(w);
13431338
if !cx.render_redirect_pages {
1344-
layout::render(&mut writer, &cx.shared.layout, &page,
1339+
layout::render(writer, &cx.shared.layout, &page,
13451340
&Sidebar{ cx: cx, item: it },
13461341
&Item{ cx: cx, item: it },
13471342
cx.shared.css_file_extension.is_some())?;
@@ -1354,10 +1349,10 @@ impl Context {
13541349
url.push_str("/");
13551350
}
13561351
url.push_str(&item_path(it));
1357-
layout::redirect(&mut writer, &url)?;
1352+
layout::redirect(writer, &url)?;
13581353
}
13591354
}
1360-
writer.flush()
1355+
Ok(())
13611356
}
13621357

13631358
// Stripped modules survive the rustdoc passes (i.e. `strip-private`)
@@ -1378,9 +1373,16 @@ impl Context {
13781373
let mut item = Some(item);
13791374
self.recurse(name, |this| {
13801375
let item = item.take().unwrap();
1381-
let joint_dst = this.dst.join("index.html");
1382-
let dst = try_err!(File::create(&joint_dst), &joint_dst);
1383-
try_err!(render(dst, this, &item, false), &joint_dst);
1376+
1377+
let mut buf = Vec::new();
1378+
render(&mut buf, this, &item, false).unwrap();
1379+
// buf will be empty if the module is stripped and there is no redirect for it
1380+
if !buf.is_empty() {
1381+
let joint_dst = this.dst.join("index.html");
1382+
try_err!(fs::create_dir_all(&this.dst), &this.dst);
1383+
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
1384+
try_err!(dst.write_all(&buf), &joint_dst);
1385+
}
13841386

13851387
let m = match item.inner {
13861388
clean::StrippedItem(box clean::ModuleItem(m)) |
@@ -1389,7 +1391,7 @@ impl Context {
13891391
};
13901392

13911393
// render sidebar-items.js used throughout this module
1392-
{
1394+
if !this.render_redirect_pages {
13931395
let items = this.build_sidebar_items(&m);
13941396
let js_dst = this.dst.join("sidebar-items.js");
13951397
let mut js_out = BufWriter::new(try_err!(File::create(&js_dst), &js_dst));
@@ -1403,10 +1405,15 @@ impl Context {
14031405
Ok(())
14041406
})
14051407
} else if item.name.is_some() {
1406-
let joint_dst = self.dst.join(&item_path(&item));
1407-
1408-
let dst = try_err!(File::create(&joint_dst), &joint_dst);
1409-
try_err!(render(dst, self, &item, true), &joint_dst);
1408+
let mut buf = Vec::new();
1409+
render(&mut buf, self, &item, true).unwrap();
1410+
// buf will be empty if the item is stripped and there is no redirect for it
1411+
if !buf.is_empty() {
1412+
let joint_dst = self.dst.join(&item_path(&item));
1413+
try_err!(fs::create_dir_all(&self.dst), &self.dst);
1414+
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
1415+
try_err!(dst.write_all(&buf), &joint_dst);
1416+
}
14101417
Ok(())
14111418
} else {
14121419
Ok(())

src/test/rustdoc/issue-34025.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 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+
#![crate_name = "foo"]
12+
13+
// @!has 'foo/sys/index.html'
14+
// @!has 'foo/sys/sidebar-items.js'
15+
#[doc(hidden)]
16+
pub mod sys {
17+
extern "C" {
18+
// @!has 'foo/sys/fn.foo.html'
19+
#[doc(hidden)]
20+
pub fn foo();
21+
}
22+
}

0 commit comments

Comments
 (0)