Skip to content

Commit ad808d9

Browse files
committed
Auto merge of #66680 - Centril:rollup-1ke3svj, r=Centril
Rollup of 5 pull requests Successful merges: - #61351 (Stabilize cfg(doc)) - #66539 (Point at type in `let` assignment on type errors) - #66655 (rustdoc: Mark `--extern-private` as unstable) - #66657 (rustdoc: Don't panic when failing to write .lock file) - #66673 (Move def collector from `rustc` to `rustc_resolve`) Failed merges: r? @ghost
2 parents 0c987c5 + c215de0 commit ad808d9

File tree

119 files changed

+768
-404
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+768
-404
lines changed

src/doc/rustdoc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
- [Documentation tests](documentation-tests.md)
88
- [Lints](lints.md)
99
- [Passes](passes.md)
10+
- [Advanced Features](advanced-features.md)
1011
- [Unstable features](unstable-features.md)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Advanced Features
2+
3+
The features listed on this page fall outside the rest of the main categories.
4+
5+
## `#[cfg(doc)]`: Documenting platform-/feature-specific information
6+
7+
For conditional compilation, Rustdoc treats your crate the same way the compiler does: Only things
8+
from the host target are available (or from the given `--target` if present), and everything else is
9+
"filtered out" from the crate. This can cause problems if your crate is providing different things
10+
on different targets and you want your documentation to reflect all the available items you
11+
provide.
12+
13+
If you want to make sure an item is seen by Rustdoc regardless of what platform it's targeting,
14+
you can apply `#[cfg(doc)]` to it. Rustdoc sets this whenever it's building documentation, so
15+
anything that uses that flag will make it into documentation it generates. To apply this to an item
16+
with other `#[cfg]` filters on it, you can write something like `#[cfg(any(windows, doc))]`.
17+
This will preserve the item either when built normally on Windows, or when being documented
18+
anywhere.
19+
20+
Please note that this feature is not passed to doctests.
21+
22+
Example:
23+
24+
```rust
25+
/// Token struct that can only be used on Windows.
26+
#[cfg(any(windows, doc))]
27+
pub struct WindowsToken;
28+
/// Token struct that can only be used on Unix.
29+
#[cfg(any(unix, doc))]
30+
pub struct UnixToken;
31+
```
32+
33+
Here, the respective tokens can only be used by dependent crates on their respective platforms, but
34+
they will both appear in documentation.

src/librustc/hir/map/definitions.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub struct Definitions {
105105
/// we know what parent node that fragment should be attached to thanks to this table.
106106
invocation_parents: FxHashMap<ExpnId, DefIndex>,
107107
/// Indices of unnamed struct or variant fields with unresolved attributes.
108-
pub(super) placeholder_field_indices: NodeMap<usize>,
108+
placeholder_field_indices: NodeMap<usize>,
109109
}
110110

111111
/// A unique identifier that we can use to lookup a definition
@@ -535,6 +535,15 @@ impl Definitions {
535535
let old_parent = self.invocation_parents.insert(invoc_id, parent);
536536
assert!(old_parent.is_none(), "parent `DefIndex` is reset for an invocation");
537537
}
538+
539+
pub fn placeholder_field_index(&self, node_id: ast::NodeId) -> usize {
540+
self.placeholder_field_indices[&node_id]
541+
}
542+
543+
pub fn set_placeholder_field_index(&mut self, node_id: ast::NodeId, index: usize) {
544+
let old_index = self.placeholder_field_indices.insert(node_id, index);
545+
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
546+
}
538547
}
539548

540549
impl DefPathData {

src/librustc/hir/map/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use self::collector::NodeCollector;
2-
pub use self::def_collector::DefCollector;
32
pub use self::definitions::{
43
Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData, DefPathHash
54
};
@@ -25,7 +24,6 @@ use syntax_pos::{Span, DUMMY_SP};
2524

2625
pub mod blocks;
2726
mod collector;
28-
mod def_collector;
2927
pub mod definitions;
3028
mod hir_id_validator;
3129

src/librustc_data_structures/flock.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,3 @@ cfg_if! {
298298
}
299299
}
300300
}
301-
302-
impl Lock {
303-
pub fn panicking_new(p: &Path,
304-
wait: bool,
305-
create: bool,
306-
exclusive: bool)
307-
-> Lock {
308-
Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
309-
panic!("could not lock `{}`: {}", p.display(), err);
310-
})
311-
}
312-
}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! unexpanded macros in the fragment are visited and registered.
66
//! Imports are also considered items and placed into modules here, but not resolved yet.
77
8+
use crate::def_collector::collect_definitions;
89
use crate::macros::{LegacyBinding, LegacyScope};
910
use crate::resolve_imports::ImportDirective;
1011
use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
@@ -16,7 +17,6 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
1617
use rustc::bug;
1718
use rustc::hir::def::{self, *};
1819
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
19-
use rustc::hir::map::DefCollector;
2020
use rustc::ty;
2121
use rustc::middle::cstore::CrateStore;
2222
use rustc_metadata::cstore::LoadedMacro;
@@ -167,8 +167,7 @@ impl<'a> Resolver<'a> {
167167
fragment: &AstFragment,
168168
parent_scope: ParentScope<'a>,
169169
) -> LegacyScope<'a> {
170-
let mut def_collector = DefCollector::new(&mut self.definitions, parent_scope.expansion);
171-
fragment.visit_with(&mut def_collector);
170+
collect_definitions(&mut self.definitions, fragment, parent_scope.expansion);
172171
let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope };
173172
fragment.visit_with(&mut visitor);
174173
visitor.parent_scope.legacy

src/librustc/hir/map/def_collector.rs renamed to src/librustc_resolve/def_collector.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
use crate::hir::map::definitions::*;
2-
use crate::hir::def_id::DefIndex;
3-
1+
use log::debug;
2+
use rustc::hir::map::definitions::*;
3+
use rustc::hir::def_id::DefIndex;
44
use syntax::ast::*;
55
use syntax::visit;
66
use syntax::symbol::{kw, sym};
77
use syntax::token::{self, Token};
8+
use syntax_expand::expand::AstFragment;
89
use syntax_pos::hygiene::ExpnId;
910
use syntax_pos::Span;
1011

12+
crate fn collect_definitions(
13+
definitions: &mut Definitions,
14+
fragment: &AstFragment,
15+
expansion: ExpnId,
16+
) {
17+
let parent_def = definitions.invocation_parent(expansion);
18+
fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion });
19+
}
20+
1121
/// Creates `DefId`s for nodes in the AST.
12-
pub struct DefCollector<'a> {
22+
struct DefCollector<'a> {
1323
definitions: &'a mut Definitions,
1424
parent_def: DefIndex,
1525
expansion: ExpnId,
1626
}
1727

1828
impl<'a> DefCollector<'a> {
19-
pub fn new(definitions: &'a mut Definitions, expansion: ExpnId) -> Self {
20-
let parent_def = definitions.invocation_parent(expansion);
21-
DefCollector { definitions, parent_def, expansion }
22-
}
23-
2429
fn create_def(&mut self,
2530
node_id: NodeId,
2631
data: DefPathData,
@@ -82,7 +87,7 @@ impl<'a> DefCollector<'a> {
8287
.or_else(|| index.map(sym::integer))
8388
.unwrap_or_else(|| {
8489
let node_id = NodeId::placeholder_from_expn_id(self.expansion);
85-
sym::integer(self.definitions.placeholder_field_indices[&node_id])
90+
sym::integer(self.definitions.placeholder_field_index(node_id))
8691
});
8792
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
8893
self.with_parent(def, |this| visit::walk_struct_field(this, field));
@@ -186,7 +191,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
186191
for (index, field) in data.fields().iter().enumerate() {
187192
self.collect_field(field, Some(index));
188193
if field.is_placeholder && field.ident.is_none() {
189-
self.definitions.placeholder_field_indices.insert(field.id, index);
194+
self.definitions.set_placeholder_field_index(field.id, index);
190195
}
191196
}
192197
}

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use rustc_error_codes::*;
6868

6969
type Res = def::Res<NodeId>;
7070

71+
mod def_collector;
7172
mod diagnostics;
7273
mod late;
7374
mod macros;

src/librustc_typeck/check/demand.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102
// N.B., this code relies on `self.diverges` to be accurate. In
103103
// particular, assignments to `!` will be permitted if the
104104
// diverges flag is currently "always".
105-
pub fn demand_coerce_diag(&self,
106-
expr: &hir::Expr,
107-
checked_ty: Ty<'tcx>,
108-
expected: Ty<'tcx>,
109-
allow_two_phase: AllowTwoPhase)
110-
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
105+
pub fn demand_coerce_diag(
106+
&self,
107+
expr: &hir::Expr,
108+
checked_ty: Ty<'tcx>,
109+
expected: Ty<'tcx>,
110+
allow_two_phase: AllowTwoPhase,
111+
) -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111112
let expected = self.resolve_vars_with_obligations(expected);
112113

113114
let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
@@ -126,6 +127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
126127
return (expected, None)
127128
}
128129

130+
self.annotate_expected_due_to_let_ty(&mut err, expr);
129131
self.suggest_compatible_variants(&mut err, expr, expected, expr_ty);
130132
self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
131133
self.suggest_boxing_when_appropriate(&mut err, expr, expected, expr_ty);
@@ -134,6 +136,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
134136
(expected, Some(err))
135137
}
136138

139+
fn annotate_expected_due_to_let_ty(&self, err: &mut DiagnosticBuilder<'_>, expr: &hir::Expr) {
140+
let parent = self.tcx.hir().get_parent_node(expr.hir_id);
141+
if let Some(hir::Node::Local(hir::Local {
142+
ty: Some(ty),
143+
init: Some(init),
144+
..
145+
})) = self.tcx.hir().find(parent) {
146+
if init.hir_id == expr.hir_id {
147+
// Point at `let` assignment type.
148+
err.span_label(ty.span, "expected due to this");
149+
}
150+
}
151+
}
152+
137153
/// Returns whether the expected type is `bool` and the expression is `x = y`.
138154
pub fn is_assign_to_bool(&self, expr: &hir::Expr, expected: Ty<'tcx>) -> bool {
139155
if let hir::ExprKind::Assign(..) = expr.kind {

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
250250

251251
let extern_names: Vec<String> = externs.iter().map(|(s,_)| s).cloned().collect();
252252

253-
// Add the rustdoc cfg into the doc build.
253+
// Add the doc cfg into the doc build.
254254
cfgs.push("doc".to_string());
255255

256256
let cpath = Some(input.clone());

0 commit comments

Comments
 (0)