Skip to content

Commit 52c5d77

Browse files
Hide foreign #[doc(hidden)] paths in import suggestions
1 parent f704f3b commit 52c5d77

File tree

8 files changed

+126
-33
lines changed

8 files changed

+126
-33
lines changed

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
14761476
val.fold_with(&mut visitor)
14771477
}
14781478

1479-
/// Determines whether an item is annotated with `doc(hidden)`.
1479+
/// Determines whether an item is directly annotated with `doc(hidden)`.
14801480
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
14811481
tcx.get_attrs(def_id, sym::doc)
14821482
.filter_map(|attr| attr.meta_item_list())

compiler/rustc_resolve/src/diagnostics.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub(crate) struct ImportSuggestion {
9898
pub descr: &'static str,
9999
pub path: Path,
100100
pub accessible: bool,
101+
// false if the path traverses a foreign `#[doc(hidden)]` item.
102+
pub doc_visible: bool,
101103
pub via_import: bool,
102104
/// An extra note that should be issued if this item is suggested
103105
pub note: Option<String>,
@@ -1153,10 +1155,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11531155
{
11541156
let mut candidates = Vec::new();
11551157
let mut seen_modules = FxHashSet::default();
1156-
let mut worklist = vec![(start_module, ThinVec::<ast::PathSegment>::new(), true)];
1158+
let start_did = start_module.def_id();
1159+
let mut worklist = vec![(
1160+
start_module,
1161+
ThinVec::<ast::PathSegment>::new(),
1162+
true,
1163+
start_did.is_local() || !self.tcx.is_doc_hidden(start_did),
1164+
)];
11571165
let mut worklist_via_import = vec![];
11581166

1159-
while let Some((in_module, path_segments, accessible)) = match worklist.pop() {
1167+
while let Some((in_module, path_segments, accessible, doc_visible)) = match worklist.pop() {
11601168
None => worklist_via_import.pop(),
11611169
Some(x) => Some(x),
11621170
} {
@@ -1199,6 +1207,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11991207
}
12001208
}
12011209

1210+
let res = name_binding.res();
1211+
let did = match res {
1212+
Res::Def(DefKind::Ctor(..), did) => this.tcx.opt_parent(did),
1213+
_ => res.opt_def_id(),
1214+
};
1215+
let child_doc_visible = doc_visible
1216+
&& (did.map_or(true, |did| did.is_local() || !this.tcx.is_doc_hidden(did)));
1217+
12021218
// collect results based on the filter function
12031219
// avoid suggesting anything from the same module in which we are resolving
12041220
// avoid suggesting anything with a hygienic name
@@ -1207,7 +1223,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12071223
&& in_module != parent_scope.module
12081224
&& !ident.span.normalize_to_macros_2_0().from_expansion()
12091225
{
1210-
let res = name_binding.res();
12111226
if filter_fn(res) {
12121227
// create the path
12131228
let mut segms = if lookup_ident.span.at_least_rust_2018() {
@@ -1221,10 +1236,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12211236

12221237
segms.push(ast::PathSegment::from_ident(ident));
12231238
let path = Path { span: name_binding.span, segments: segms, tokens: None };
1224-
let did = match res {
1225-
Res::Def(DefKind::Ctor(..), did) => this.tcx.opt_parent(did),
1226-
_ => res.opt_def_id(),
1227-
};
12281239

12291240
if child_accessible {
12301241
// Remove invisible match if exists
@@ -1264,6 +1275,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12641275
descr: res.descr(),
12651276
path,
12661277
accessible: child_accessible,
1278+
doc_visible: child_doc_visible,
12671279
note,
12681280
via_import,
12691281
});
@@ -1284,7 +1296,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12841296
// add the module to the lookup
12851297
if seen_modules.insert(module.def_id()) {
12861298
if via_import { &mut worklist_via_import } else { &mut worklist }
1287-
.push((module, path_segments, child_accessible));
1299+
.push((module, path_segments, child_accessible, child_doc_visible));
12881300
}
12891301
}
12901302
}
@@ -2694,8 +2706,26 @@ fn show_candidates(
26942706
Vec::new();
26952707

26962708
candidates.iter().for_each(|c| {
2697-
(if c.accessible { &mut accessible_path_strings } else { &mut inaccessible_path_strings })
2698-
.push((pprust::path_to_string(&c.path), c.descr, c.did, &c.note, c.via_import))
2709+
if c.accessible {
2710+
// Don't suggest `#[doc(hidden)]` items from other crates
2711+
if c.doc_visible {
2712+
accessible_path_strings.push((
2713+
pprust::path_to_string(&c.path),
2714+
c.descr,
2715+
c.did,
2716+
&c.note,
2717+
c.via_import,
2718+
))
2719+
}
2720+
} else {
2721+
inaccessible_path_strings.push((
2722+
pprust::path_to_string(&c.path),
2723+
c.descr,
2724+
c.did,
2725+
&c.note,
2726+
c.via_import,
2727+
))
2728+
}
26992729
});
27002730

27012731
// we want consistent results across executions, but candidates are produced
@@ -2794,9 +2824,7 @@ fn show_candidates(
27942824
err.help(msg);
27952825
}
27962826
true
2797-
} else if !matches!(mode, DiagnosticMode::Import) {
2798-
assert!(!inaccessible_path_strings.is_empty());
2799-
2827+
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagnosticMode::Import)) {
28002828
let prefix = if let DiagnosticMode::Pattern = mode {
28012829
"you might have meant to match on "
28022830
} else {

compiler/rustc_resolve/src/late/diagnostics.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2194,15 +2194,20 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21942194
fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {
21952195
let mut result = None;
21962196
let mut seen_modules = FxHashSet::default();
2197-
let mut worklist = vec![(self.r.graph_root, ThinVec::new())];
2198-
2199-
while let Some((in_module, path_segments)) = worklist.pop() {
2197+
let root_did = self.r.graph_root.def_id();
2198+
let mut worklist = vec![(
2199+
self.r.graph_root,
2200+
ThinVec::new(),
2201+
root_did.is_local() || !self.r.tcx.is_doc_hidden(root_did),
2202+
)];
2203+
2204+
while let Some((in_module, path_segments, doc_visible)) = worklist.pop() {
22002205
// abort if the module is already found
22012206
if result.is_some() {
22022207
break;
22032208
}
22042209

2205-
in_module.for_each_child(self.r, |_, ident, _, name_binding| {
2210+
in_module.for_each_child(self.r, |r, ident, _, name_binding| {
22062211
// abort if the module is already found or if name_binding is private external
22072212
if result.is_some() || !name_binding.vis.is_visible_locally() {
22082213
return;
@@ -2212,6 +2217,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
22122217
let mut path_segments = path_segments.clone();
22132218
path_segments.push(ast::PathSegment::from_ident(ident));
22142219
let module_def_id = module.def_id();
2220+
let doc_visible = doc_visible
2221+
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
22152222
if module_def_id == def_id {
22162223
let path =
22172224
Path { span: name_binding.span, segments: path_segments, tokens: None };
@@ -2222,14 +2229,15 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
22222229
descr: "module",
22232230
path,
22242231
accessible: true,
2232+
doc_visible,
22252233
note: None,
22262234
via_import: false,
22272235
},
22282236
));
22292237
} else {
22302238
// add the module to the lookup
22312239
if seen_modules.insert(module_def_id) {
2232-
worklist.push((module, path_segments));
2240+
worklist.push((module, path_segments, doc_visible));
22332241
}
22342242
}
22352243
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[doc(hidden)]
2+
pub mod hidden {
3+
pub struct Foo;
4+
}
5+
6+
pub mod hidden1 {
7+
#[doc(hidden)]
8+
pub struct Foo;
9+
}
10+
11+
12+
#[doc(hidden)]
13+
pub(crate) mod hidden2 {
14+
pub struct Bar;
15+
}
16+
17+
pub use hidden2::Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aux-build:hidden-struct.rs
2+
// compile-flags: --crate-type lib
3+
4+
extern crate hidden_struct;
5+
6+
#[doc(hidden)]
7+
mod local {
8+
pub struct Foo;
9+
}
10+
11+
pub fn test(_: Foo) {}
12+
//~^ ERROR cannot find type `Foo` in this scope
13+
14+
pub fn test2(_: Bar) {}
15+
//~^ ERROR cannot find type `Bar` in this scope
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0412]: cannot find type `Foo` in this scope
2+
--> $DIR/dont-suggest-foreign-doc-hidden.rs:11:16
3+
|
4+
LL | pub fn test(_: Foo) {}
5+
| ^^^ not found in this scope
6+
|
7+
help: consider importing this struct
8+
|
9+
LL + use local::Foo;
10+
|
11+
12+
error[E0412]: cannot find type `Bar` in this scope
13+
--> $DIR/dont-suggest-foreign-doc-hidden.rs:14:17
14+
|
15+
LL | pub fn test2(_: Bar) {}
16+
| ^^^ not found in this scope
17+
|
18+
help: consider importing this struct
19+
|
20+
LL + use hidden_struct::Bar;
21+
|
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0412`.

tests/ui/type-alias-impl-trait/nested-impl-trait-in-tait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![feature(type_alias_impl_trait)]
22

3-
pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
3+
pub type Tait = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
44
//~^ ERROR use of undeclared lifetime name `'db`
5-
//~| ERROR cannot find type `Key` in this scope
5+
//~| ERROR cannot find type `LocalKey` in this scope
66
//~| ERROR unconstrained opaque type
77
//~| ERROR unconstrained opaque type
88

tests/ui/type-alias-impl-trait/nested-impl-trait-in-tait.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
error[E0261]: use of undeclared lifetime name `'db`
22
--> $DIR/nested-impl-trait-in-tait.rs:3:40
33
|
4-
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
4+
LL | pub type Tait = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
55
| ^^^ undeclared lifetime
66
|
77
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
88
help: consider making the bound lifetime-generic with a new `'db` lifetime
99
|
10-
LL | pub type Tait = impl for<'db> Iterator<Item = (&'db Key, impl Iterator)>;
10+
LL | pub type Tait = impl for<'db> Iterator<Item = (&'db LocalKey, impl Iterator)>;
1111
| ++++++++
1212
help: consider introducing lifetime `'db` here
1313
|
14-
LL | pub type Tait<'db> = impl Iterator<Item = (&'db Key, impl Iterator)>;
14+
LL | pub type Tait<'db> = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
1515
| +++++
1616

17-
error[E0412]: cannot find type `Key` in this scope
17+
error[E0412]: cannot find type `LocalKey` in this scope
1818
--> $DIR/nested-impl-trait-in-tait.rs:3:44
1919
|
20-
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
21-
| ^^^ not found in this scope
20+
LL | pub type Tait = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
21+
| ^^^^^^^^ not found in this scope
2222
|
2323
help: consider importing this struct
2424
|
25-
LL + use std::thread::local_impl::Key;
25+
LL + use std::thread::LocalKey;
2626
|
2727

2828
error: unconstrained opaque type
2929
--> $DIR/nested-impl-trait-in-tait.rs:3:17
3030
|
31-
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
LL | pub type Tait = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
|
3434
= note: `Tait` must be used in combination with a concrete type within the same module
3535

3636
error: unconstrained opaque type
37-
--> $DIR/nested-impl-trait-in-tait.rs:3:49
37+
--> $DIR/nested-impl-trait-in-tait.rs:3:54
3838
|
39-
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
40-
| ^^^^^^^^^^^^^
39+
LL | pub type Tait = impl Iterator<Item = (&'db LocalKey, impl Iterator)>;
40+
| ^^^^^^^^^^^^^
4141
|
4242
= note: `Tait` must be used in combination with a concrete type within the same module
4343

0 commit comments

Comments
 (0)