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

+1
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)
+34
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

+10-1
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

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

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

+2-3
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 src/librustc_resolve/def_collector.rs

+16-11
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

+1
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

+22-6
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

+1-1
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());

src/librustdoc/html/render.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ fn write_shared(
546546
// Write out the shared files. Note that these are shared among all rustdoc
547547
// docs placed in the output directory, so this needs to be a synchronized
548548
// operation with respect to all other rustdocs running around.
549-
let _lock = flock::Lock::panicking_new(&cx.dst.join(".lock"), true, true, true);
549+
let lock_file = cx.dst.join(".lock");
550+
let _lock = try_err!(flock::Lock::new(&lock_file, true, true, true), &lock_file);
550551

551552
// Add all the static files. These may already exist, but we just
552553
// overwrite them anyway to make sure that they're fresh and up-to-date.

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn opts() -> Vec<RustcOptGroup> {
144144
stable("extern", |o| {
145145
o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")
146146
}),
147-
stable("extern-private", |o| {
147+
unstable("extern-private", |o| {
148148
o.optmulti("", "extern-private",
149149
"pass an --extern to rustc (compatibility only)", "NAME=PATH")
150150
}),

src/libsyntax/feature_gate/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[
3030
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
3131
(sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3232
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
33-
(sym::doc, sym::doc_cfg, cfg_fn!(doc_cfg)),
3433
];
3534

3635
#[derive(Debug)]

src/libsyntax_pos/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ symbols! {
627627
rustc_test_marker,
628628
rustc_then_this_would_need,
629629
rustc_variance,
630-
rustdoc,
631630
rustfmt,
632631
rust_eh_personality,
633632
rust_eh_unwind_resume,

src/test/rustdoc-ui/failed-doctest-missing-codes.stdout

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ error[E0308]: mismatched types
99
--> $DIR/failed-doctest-missing-codes.rs:9:13
1010
|
1111
LL | let x: () = 5i32;
12-
| ^^^^ expected `()`, found `i32`
12+
| -- ^^^^ expected `()`, found `i32`
13+
| |
14+
| expected due to this
1315

1416
error: aborting due to previous error
1517

src/test/rustdoc/issue-66159.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// aux-build:issue-66159-1.rs
2+
// compile-flags:-Z unstable-options
23
// extern-private:issue_66159_1
34

45
// The issue was an ICE which meant that we never actually generated the docs

src/test/ui/array-not-vector.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error[E0308]: mismatched types
22
--> $DIR/array-not-vector.rs:2:19
33
|
44
LL | let _x: i32 = [1, 2, 3];
5-
| ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
5+
| --- ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
6+
| |
7+
| expected due to this
68

79
error[E0308]: mismatched types
810
--> $DIR/array-not-vector.rs:7:20
911
|
1012
LL | let _y: &i32 = x;
11-
| ^ expected `i32`, found slice `[i32]`
13+
| ---- ^ expected `i32`, found slice `[i32]`
14+
| |
15+
| expected due to this
1216
|
1317
= note: expected reference `&i32`
1418
found reference `&[i32]`

src/test/ui/associated-types/associated-types-eq-3.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/associated-types-eq-3.rs:23:18
33
|
44
LL | let _: Bar = x.boo();
5-
| ^^^^^^^ expected struct `Bar`, found associated type
5+
| --- ^^^^^^^ expected struct `Bar`, found associated type
6+
| |
7+
| expected due to this
68
|
79
= note: expected struct `Bar`
810
found associated type `<I as Foo>::A`

src/test/ui/associated-types/associated-types-path-2.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ error[E0308]: mismatched types
4343
--> $DIR/associated-types-path-2.rs:41:18
4444
|
4545
LL | let _: i32 = f2(2i32);
46-
| ^^^^^^^^ expected `i32`, found `u32`
46+
| --- ^^^^^^^^ expected `i32`, found `u32`
47+
| |
48+
| expected due to this
4749
|
4850
help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit
4951
|

src/test/ui/c-variadic/variadic-ffi-1.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ error[E0308]: mismatched types
2626
--> $DIR/variadic-ffi-1.rs:19:56
2727
|
2828
LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
29-
| ^^^ expected non-variadic fn, found variadic function
29+
| ------------------------------------- ^^^ expected non-variadic fn, found variadic function
30+
| |
31+
| expected due to this
3032
|
3133
= note: expected fn pointer `unsafe extern "C" fn(isize, u8)`
3234
found fn item `unsafe extern "C" fn(isize, u8, ...) {foo}`
@@ -35,7 +37,9 @@ error[E0308]: mismatched types
3537
--> $DIR/variadic-ffi-1.rs:20:54
3638
|
3739
LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
38-
| ^^^ expected variadic fn, found non-variadic function
40+
| ----------------------------------- ^^^ expected variadic fn, found non-variadic function
41+
| |
42+
| expected due to this
3943
|
4044
= note: expected fn pointer `extern "C" fn(isize, u8, ...)`
4145
found fn item `extern "C" fn(isize, u8) {bar}`

src/test/ui/cfg-rustdoc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[cfg(doc)]
2+
pub struct Foo;
3+
4+
fn main() {
5+
let f = Foo; //~ ERROR
6+
}

src/test/ui/cfg-rustdoc.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find value `Foo` in this scope
2+
--> $DIR/cfg-rustdoc.rs:5:13
3+
|
4+
LL | let f = Foo;
5+
| ^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/closures/closure-no-fn-1.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/closure-no-fn-1.rs:6:29
33
|
44
LL | let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
5-
| ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
5+
| ------------ ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
6+
| |
7+
| expected due to this
68
|
79
= note: expected fn pointer `fn(u8) -> u8`
810
found closure `[closure@$DIR/closure-no-fn-1.rs:6:29: 6:50 a:_]`

0 commit comments

Comments
 (0)