Skip to content

Commit 0ed03e5

Browse files
committed
Auto merge of #43348 - kennytm:fix-24658-doc-every-platform, r=alexcrichton
Expose all OS-specific modules in libstd doc. 1. Uses the special `--cfg dox` configuration passed by rustbuild when running `rustdoc`. Changes the `#[cfg(platform)]` into `#[cfg(any(dox, platform))]` so that platform-specific API are visible to rustdoc. 2. Since platform-specific implementations often won't compile correctly on other platforms, `rustdoc` is changed to apply `everybody_loops` to the functions during documentation and doc-test harness. 3. Since platform-specific code are documented on all platforms now, it could confuse users who found a useful API but is non-portable. Also, their examples will be doc-tested, so must be excluded when not testing on the native platform. An undocumented attribute `#[doc(cfg(...))]` is introduced to serve the above purposed. Fixes #24658 (Does _not_ fully implement #1998).
2 parents 14fb329 + 3093bb8 commit 0ed03e5

File tree

23 files changed

+1261
-75
lines changed

23 files changed

+1261
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# `doc_cfg`
2+
3+
The tracking issue for this feature is: [#43781]
4+
5+
------
6+
7+
The `doc_cfg` feature allows an API be documented as only available in some specific platforms.
8+
This attribute has two effects:
9+
10+
1. In the annotated item's documentation, there will be a message saying "This is supported on
11+
(platform) only".
12+
13+
2. The item's doc-tests will only run on the specific platform.
14+
15+
This feature was introduced as part of PR [#43348] to allow the platform-specific parts of the
16+
standard library be documented.
17+
18+
```rust
19+
#![feature(doc_cfg)]
20+
21+
#[cfg(any(windows, feature = "documentation"))]
22+
#[doc(cfg(windows))]
23+
/// The application's icon in the notification area (a.k.a. system tray).
24+
///
25+
/// # Examples
26+
///
27+
/// ```no_run
28+
/// extern crate my_awesome_ui_library;
29+
/// use my_awesome_ui_library::current_app;
30+
/// use my_awesome_ui_library::windows::notification;
31+
///
32+
/// let icon = current_app().get::<notification::Icon>();
33+
/// icon.show();
34+
/// icon.show_message("Hello");
35+
/// ```
36+
pub struct Icon {
37+
// ...
38+
}
39+
```
40+
41+
[#43781]: https://github.com/rust-lang/rust/issues/43781
42+
[#43348]: https://github.com/rust-lang/rust/issues/43348

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> {

0 commit comments

Comments
 (0)