Skip to content

Commit 8f935fb

Browse files
committed
Strip out function implementation when documenting.
This prevents compilation failure we want to document a platform-specific module. Every function is replaced by `loop {}` using the same construct as `--unpretty everybody_loops`. Note also a workaround to #43636 is included: `const fn` will retain their bodies, since the standard library has quite a number of them.
1 parent 57e720d commit 8f935fb

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

src/librustc_driver/pretty.rs

+32-30
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use std::io::{self, Write};
4444
use std::option;
4545
use std::path::Path;
4646
use std::str::FromStr;
47+
use std::mem;
4748

4849
use rustc::hir::map as hir_map;
4950
use rustc::hir::map::blocks;
@@ -618,52 +619,53 @@ impl UserIdentifiedItem {
618619
}
619620
}
620621

621-
struct ReplaceBodyWithLoop {
622+
// Note: Also used by librustdoc, see PR #43348. Consider moving this struct elsewhere.
623+
pub struct ReplaceBodyWithLoop {
622624
within_static_or_const: bool,
623625
}
624626

625627
impl ReplaceBodyWithLoop {
626-
fn new() -> ReplaceBodyWithLoop {
628+
pub fn new() -> ReplaceBodyWithLoop {
627629
ReplaceBodyWithLoop { within_static_or_const: false }
628630
}
631+
632+
fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, is_const: bool, action: F) -> R {
633+
let old_const = mem::replace(&mut self.within_static_or_const, is_const);
634+
let ret = action(self);
635+
self.within_static_or_const = old_const;
636+
ret
637+
}
629638
}
630639

631640
impl fold::Folder for ReplaceBodyWithLoop {
632641
fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
633-
match i {
634-
ast::ItemKind::Static(..) |
635-
ast::ItemKind::Const(..) => {
636-
self.within_static_or_const = true;
637-
let ret = fold::noop_fold_item_kind(i, self);
638-
self.within_static_or_const = false;
639-
return ret;
640-
}
641-
_ => fold::noop_fold_item_kind(i, self),
642-
}
642+
let is_const = match i {
643+
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
644+
ast::ItemKind::Fn(_, _, ref constness, _, _, _) =>
645+
constness.node == ast::Constness::Const,
646+
_ => false,
647+
};
648+
self.run(is_const, |s| fold::noop_fold_item_kind(i, s))
643649
}
644650

645651
fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector<ast::TraitItem> {
646-
match i.node {
647-
ast::TraitItemKind::Const(..) => {
648-
self.within_static_or_const = true;
649-
let ret = fold::noop_fold_trait_item(i, self);
650-
self.within_static_or_const = false;
651-
return ret;
652-
}
653-
_ => fold::noop_fold_trait_item(i, self),
654-
}
652+
let is_const = match i.node {
653+
ast::TraitItemKind::Const(..) => true,
654+
ast::TraitItemKind::Method(ast::MethodSig { ref constness, .. }, _) =>
655+
constness.node == ast::Constness::Const,
656+
_ => false,
657+
};
658+
self.run(is_const, |s| fold::noop_fold_trait_item(i, s))
655659
}
656660

657661
fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector<ast::ImplItem> {
658-
match i.node {
659-
ast::ImplItemKind::Const(..) => {
660-
self.within_static_or_const = true;
661-
let ret = fold::noop_fold_impl_item(i, self);
662-
self.within_static_or_const = false;
663-
return ret;
664-
}
665-
_ => fold::noop_fold_impl_item(i, self),
666-
}
662+
let is_const = match i.node {
663+
ast::ImplItemKind::Const(..) => true,
664+
ast::ImplItemKind::Method(ast::MethodSig { ref constness, .. }, _) =>
665+
constness.node == ast::Constness::Const,
666+
_ => false,
667+
};
668+
self.run(is_const, |s| fold::noop_fold_impl_item(i, s))
667669
}
668670

669671
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {

src/librustdoc/core.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use rustc_lint;
1212
use rustc_driver::{driver, target_features, abort_on_err};
13+
use rustc_driver::pretty::ReplaceBodyWithLoop;
1314
use rustc::dep_graph::DepGraph;
1415
use rustc::session::{self, config};
1516
use rustc::hir::def_id::DefId;
@@ -26,6 +27,7 @@ use rustc_metadata::cstore::CStore;
2627

2728
use syntax::{ast, codemap};
2829
use syntax::feature_gate::UnstableFeatures;
30+
use syntax::fold::Folder;
2931
use errors;
3032
use errors::emitter::ColorConfig;
3133

@@ -158,6 +160,7 @@ pub fn run_core(search_paths: SearchPaths,
158160
let krate = panictry!(driver::phase_1_parse_input(&driver::CompileController::basic(),
159161
&sess,
160162
&input));
163+
let krate = ReplaceBodyWithLoop::new().fold_crate(krate);
161164

162165
let name = link::find_crate_name(Some(&sess), &krate.attrs, &input);
163166

src/librustdoc/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ use rustc_back::dynamic_lib::DynamicLibrary;
3232
use rustc_back::tempdir::TempDir;
3333
use rustc_driver::{self, driver, Compilation};
3434
use rustc_driver::driver::phase_2_configure_and_expand;
35+
use rustc_driver::pretty::ReplaceBodyWithLoop;
3536
use rustc_metadata::cstore::CStore;
3637
use rustc_resolve::MakeGlobMap;
3738
use rustc_trans;
3839
use rustc_trans::back::link;
3940
use syntax::ast;
4041
use syntax::codemap::CodeMap;
4142
use syntax::feature_gate::UnstableFeatures;
43+
use syntax::fold::Folder;
4244
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span};
4345
use errors;
4446
use errors::emitter::ColorConfig;
@@ -72,6 +74,7 @@ pub fn run(input: &str,
7274
crate_types: vec![config::CrateTypeDylib],
7375
externs: externs.clone(),
7476
unstable_features: UnstableFeatures::from_environment(),
77+
lint_cap: Some(::rustc::lint::Level::Allow),
7578
actually_rustdoc: true,
7679
..config::basic_options().clone()
7780
};
@@ -94,6 +97,7 @@ pub fn run(input: &str,
9497
let krate = panictry!(driver::phase_1_parse_input(&driver::CompileController::basic(),
9598
&sess,
9699
&input));
100+
let krate = ReplaceBodyWithLoop::new().fold_crate(krate);
97101
let driver::ExpansionResult { defs, mut hir_forest, .. } = {
98102
phase_2_configure_and_expand(
99103
&sess, &cstore, krate, None, "rustdoc-test", None, MakeGlobMap::No, |_| Ok(())

0 commit comments

Comments
 (0)