Skip to content

Commit 7494bd9

Browse files
authored
Rollup merge of rust-lang#139364 - Kohei316:feat/doc-hidden-suggestion, r=nnethercote
Make the compiler suggest actual paths instead of visible paths if the visible paths are through any doc hidden path. close rust-lang#127011 Currently, when emitting a diagnostic about a valid trait, the compiler suggestes using visible paths of the trait even if they are through a doc hidden path. This PR updates the compiler to suggest actual paths in these cases.
2 parents 4d1a639 + d059182 commit 7494bd9

6 files changed

+59
-2
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_middle::bug;
2525
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
2626
use rustc_middle::ty::print::{
2727
PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths,
28+
with_no_visible_paths_if_doc_hidden,
2829
};
2930
use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
3031
use rustc_span::def_id::DefIdSet;
@@ -3328,15 +3329,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33283329
let path_strings = candidates.iter().map(|trait_did| {
33293330
format!(
33303331
"{prefix}{}{postfix}\n",
3331-
with_crate_prefix!(self.tcx.def_path_str(*trait_did)),
3332+
with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3333+
self.tcx.def_path_str(*trait_did)
3334+
)),
33323335
)
33333336
});
33343337

33353338
let glob_path_strings = globs.iter().map(|trait_did| {
33363339
let parent_did = parent_map.get(trait_did).unwrap();
33373340
format!(
33383341
"{prefix}{}::*{postfix} // trait {}\n",
3339-
with_crate_prefix!(self.tcx.def_path_str(*parent_did)),
3342+
with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3343+
self.tcx.def_path_str(*parent_did)
3344+
)),
33403345
self.tcx.item_name(*trait_did),
33413346
)
33423347
});

compiler/rustc_middle/src/ty/print/pretty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ thread_local! {
6363
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
6464
static REDUCED_QUERIES: Cell<bool> = const { Cell::new(false) };
6565
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
66+
static NO_VISIBLE_PATH_IF_DOC_HIDDEN: Cell<bool> = const { Cell::new(false) };
6667
static RTN_MODE: Cell<RtnMode> = const { Cell::new(RtnMode::ForDiagnostic) };
6768
}
6869

@@ -134,6 +135,8 @@ define_helper!(
134135
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
135136
/// visible (public) reexports of types as paths.
136137
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
138+
/// Prevent selection of visible paths if the paths are through a doc hidden path.
139+
fn with_no_visible_paths_if_doc_hidden(NoVisibleIfDocHiddenGuard, NO_VISIBLE_PATH_IF_DOC_HIDDEN);
137140
);
138141

139142
#[must_use]
@@ -569,6 +572,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
569572
return Ok(false);
570573
};
571574

575+
if self.tcx().is_doc_hidden(visible_parent) && with_no_visible_paths_if_doc_hidden() {
576+
return Ok(false);
577+
}
578+
572579
let actual_parent = self.tcx().opt_parent(def_id);
573580
debug!(
574581
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@ edition: 2021
2+
3+
pub trait Foo {
4+
fn foo();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ignore-tidy-linelength
2+
//@ edition: 2021
3+
//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs
4+
5+
pub struct Bar;
6+
7+
impl __DocHidden::Foo for Bar {
8+
fn foo() {}
9+
}
10+
11+
#[doc(hidden)]
12+
pub mod __DocHidden {
13+
pub use suggest_trait_reexported_as_not_doc_visible_a::Foo;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ignore-tidy-linelength
2+
//@ edition: 2021
3+
//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs
4+
//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_b=suggest-trait-reexported-as-not-doc-visible-b.rs
5+
6+
use suggest_trait_reexported_as_not_doc_visible_b::Bar;
7+
8+
fn main() {
9+
Bar::foo();
10+
//~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope
2+
--> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:9:10
3+
|
4+
LL | Bar::foo();
5+
| ^^^ function or associated item not found in `Bar`
6+
|
7+
= help: items from traits can only be used if the trait is in scope
8+
help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it
9+
|
10+
LL + use suggest_trait_reexported_as_not_doc_visible_a::Foo;
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)