From 89c4b25a5b33edc7baf275f9a2ac07a2b74baccf Mon Sep 17 00:00:00 2001 From: morine0122 Date: Sat, 22 Mar 2025 21:57:45 +0900 Subject: [PATCH 01/13] add debug --- compiler/rustc_hir_typeck/src/method/suggest.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 908c3ee2eb8e3..5b7a6e7f85e6c 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3291,6 +3291,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + #[instrument(level = "debug", skip(self))] fn suggest_use_candidates(&self, candidates: Vec, handle_candidates: F) where F: FnOnce(Vec, Vec, Span), @@ -3329,6 +3330,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let prefix = if visible { "use " } else { "" }; let postfix = if visible { ";" } else { "" }; let path_strings = candidates.iter().map(|trait_did| { + debug!( + "trait_did={:?} with_crate_prefix={:?}", + trait_did, + with_crate_prefix!(self.tcx.def_path_str(*trait_did)) + ); format!( "{prefix}{}{postfix}\n", with_crate_prefix!(self.tcx.def_path_str(*trait_did)), From c57a72110fe936b5a1b6ce4148d9258ce1131718 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Sun, 23 Mar 2025 16:04:56 +0900 Subject: [PATCH 02/13] debug --- .../rustc_hir_typeck/src/method/suggest.rs | 2 +- compiler/rustc_middle/src/ty/print/mod.rs | 13 ++++---- compiler/rustc_middle/src/ty/print/pretty.rs | 31 ++++++++++++------- tests/ui/issues/auxiliary/issue-127011-a-2.rs | 5 +++ tests/ui/issues/auxiliary/issue-127011-a.rs | 5 +++ tests/ui/issues/auxiliary/issue-127011-b-2.rs | 10 ++++++ tests/ui/issues/auxiliary/issue-127011-b.rs | 10 ++++++ tests/ui/issues/issue-127011-2.rs | 10 ++++++ tests/ui/issues/issue-127011-2.stderr | 15 +++++++++ tests/ui/issues/issue-127011.rs | 10 ++++++ tests/ui/issues/issue-127011.stderr | 15 +++++++++ 11 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 tests/ui/issues/auxiliary/issue-127011-a-2.rs create mode 100644 tests/ui/issues/auxiliary/issue-127011-a.rs create mode 100644 tests/ui/issues/auxiliary/issue-127011-b-2.rs create mode 100644 tests/ui/issues/auxiliary/issue-127011-b.rs create mode 100644 tests/ui/issues/issue-127011-2.rs create mode 100644 tests/ui/issues/issue-127011-2.stderr create mode 100644 tests/ui/issues/issue-127011.rs create mode 100644 tests/ui/issues/issue-127011.stderr diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 5b7a6e7f85e6c..5d93bbf2219c8 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3291,7 +3291,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - #[instrument(level = "debug", skip(self))] + #[instrument(level = "debug", skip(self, handle_candidates))] fn suggest_use_candidates(&self, candidates: Vec, handle_candidates: F) where F: FnOnce(Vec, Vec, Span), diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index dc2040aa5cf85..148af7bfc4126 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -6,6 +6,7 @@ use rustc_data_structures::sso::SsoHashSet; use rustc_hir as hir; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; +#[allow(unused_imports)] use tracing::{debug, instrument, trace}; use crate::ty::{self, GenericArg, ShortInstance, Ty, TyCtxt}; @@ -111,14 +112,14 @@ pub trait Printer<'tcx>: Sized { // Defaults (should not be overridden): - #[instrument(skip(self), level = "debug")] + // #[instrument(skip(self), level = "debug")] fn default_print_def_path( &mut self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], ) -> Result<(), PrintError> { let key = self.tcx().def_key(def_id); - debug!(?key); + // debug!(?key); match key.disambiguated_data.data { DefPathData::CrateRoot => { @@ -207,10 +208,10 @@ pub trait Printer<'tcx>: Sized { self_ty: Ty<'tcx>, impl_trait_ref: Option>, ) -> Result<(), PrintError> { - debug!( - "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}", - impl_def_id, self_ty, impl_trait_ref - ); + // debug!( + // "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}", + // impl_def_id, self_ty, impl_trait_ref + // ); let key = self.tcx().def_key(impl_def_id); let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id }; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 3ef8ecc59e402..27a64a2bfd14d 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -370,6 +370,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { /// from at least one local module, and returns `true`. If the crate defining `def_id` is /// declared with an `extern crate`, the path is guaranteed to use the `extern crate`. fn try_print_visible_def_path(&mut self, def_id: DefId) -> Result { + debug!("try_print_visible_def_path: def_id={:?}", def_id); if with_no_visible_paths() { return Ok(false); } @@ -488,7 +489,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { def_id: DefId, callers: &mut Vec, ) -> Result { - debug!("try_print_visible_def_path: def_id={:?}", def_id); + // debug!("try_print_visible_def_path: def_id={:?}", def_id); // If `def_id` is a direct or injected extern crate, return the // path to the crate followed by the path to the item within the crate. @@ -551,7 +552,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { let visible_parent_map = self.tcx().visible_parent_map(()); let mut cur_def_key = self.tcx().def_key(def_id); - debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); + // debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); // For a constructor, we want the name of its parent rather than . if let DefPathData::Ctor = cur_def_key.disambiguated_data.data { @@ -566,20 +567,21 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } let Some(visible_parent) = visible_parent_map.get(&def_id).cloned() else { + debug!("try_print_visible_def_path: no visible parent for {:?}", def_id); return Ok(false); }; let actual_parent = self.tcx().opt_parent(def_id); - debug!( - "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}", - visible_parent, actual_parent, - ); + // debug!( + // "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}", + // visible_parent, actual_parent, + // ); let mut data = cur_def_key.disambiguated_data.data; - debug!( - "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}", - data, visible_parent, actual_parent, - ); + // debug!( + // "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}", + // data, visible_parent, actual_parent, + // ); match data { // In order to output a path that could actually be imported (valid and visible), @@ -616,6 +618,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { DefPathData::TypeNs(Some(ref mut name)) if Some(visible_parent) != actual_parent => { // Item might be re-exported several times, but filter for the one // that's public and whose identifier isn't `_`. + debug!( + "try_print_visible_def_path: def_id={:?}, visible_parent={:?}, actual_parent={:?}", + def_id, visible_parent, actual_parent + ); let reexport = self .tcx() // FIXME(typed_def_id): Further propagate ModDefId @@ -624,6 +630,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) .map(|child| child.ident.name); + debug!("try_print_visible_def_path: reexport={:?}", reexport); if let Some(new_name) = reexport { *name = new_name; @@ -638,7 +645,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } _ => {} } - debug!("try_print_visible_def_path: data={:?}", data); + // debug!("try_print_visible_def_path: data={:?}", data); if callers.contains(&visible_parent) { return Ok(false); @@ -2185,7 +2192,7 @@ impl<'t> TyCtxt<'t> { ) -> String { let def_id = def_id.into_query_param(); let ns = guess_def_namespace(self, def_id); - debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); + // debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); FmtPrinter::print_string(self, ns, |cx| cx.print_def_path(def_id, args)).unwrap() } diff --git a/tests/ui/issues/auxiliary/issue-127011-a-2.rs b/tests/ui/issues/auxiliary/issue-127011-a-2.rs new file mode 100644 index 0000000000000..87d83663626b5 --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-a-2.rs @@ -0,0 +1,5 @@ +//@ edition: 2021 + +pub trait Foo { + fn foo(); +} diff --git a/tests/ui/issues/auxiliary/issue-127011-a.rs b/tests/ui/issues/auxiliary/issue-127011-a.rs new file mode 100644 index 0000000000000..87d83663626b5 --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-a.rs @@ -0,0 +1,5 @@ +//@ edition: 2021 + +pub trait Foo { + fn foo(); +} diff --git a/tests/ui/issues/auxiliary/issue-127011-b-2.rs b/tests/ui/issues/auxiliary/issue-127011-b-2.rs new file mode 100644 index 0000000000000..d9e202e9724a6 --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-b-2.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a_2=issue-127011-a-2.rs + +use issue_127011_a_2::Foo; + +pub struct Bar; + +impl Foo for Bar { + fn foo() {} +} diff --git a/tests/ui/issues/auxiliary/issue-127011-b.rs b/tests/ui/issues/auxiliary/issue-127011-b.rs new file mode 100644 index 0000000000000..d6b11e30f518d --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-b.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a=issue-127011-a.rs + +pub use issue_127011_a::Foo; + +pub struct Bar; + +impl Foo for Bar { + fn foo() {} +} diff --git a/tests/ui/issues/issue-127011-2.rs b/tests/ui/issues/issue-127011-2.rs new file mode 100644 index 0000000000000..60879e5c07d6c --- /dev/null +++ b/tests/ui/issues/issue-127011-2.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a_2=issue-127011-a-2.rs +//@ aux-crate:issue_127011_b_2=issue-127011-b-2.rs + +use issue_127011_b_2::Bar; + +fn main() { + Bar::foo(); + //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] +} diff --git a/tests/ui/issues/issue-127011-2.stderr b/tests/ui/issues/issue-127011-2.stderr new file mode 100644 index 0000000000000..3428f66b354c7 --- /dev/null +++ b/tests/ui/issues/issue-127011-2.stderr @@ -0,0 +1,15 @@ +error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope + --> $DIR/issue-127011-2.rs:8:10 + | +LL | Bar::foo(); + | ^^^ function or associated item not found in `Bar` + | + = help: items from traits can only be used if the trait is in scope +help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it + | +LL + use issue_127011_a_2::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/issues/issue-127011.rs b/tests/ui/issues/issue-127011.rs new file mode 100644 index 0000000000000..90e8a5968ab95 --- /dev/null +++ b/tests/ui/issues/issue-127011.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a=issue-127011-a.rs +//@ aux-crate:issue_127011_b=issue-127011-b.rs + +use issue_127011_b::Bar; + +fn main() { + Bar::foo(); + //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] +} diff --git a/tests/ui/issues/issue-127011.stderr b/tests/ui/issues/issue-127011.stderr new file mode 100644 index 0000000000000..c3f2cd395488b --- /dev/null +++ b/tests/ui/issues/issue-127011.stderr @@ -0,0 +1,15 @@ +error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope + --> $DIR/issue-127011.rs:8:10 + | +LL | Bar::foo(); + | ^^^ function or associated item not found in `Bar` + | + = help: items from traits can only be used if the trait is in scope +help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it + | +LL + use issue_127011_a::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0599`. From 31a42393a9a67e9b6f9581faf1c9a01ac667cdd5 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Sun, 23 Mar 2025 16:51:11 +0900 Subject: [PATCH 03/13] debug --- compiler/rustc_hir_typeck/src/method/suggest.rs | 10 +++++----- compiler/rustc_middle/src/ty/print/pretty.rs | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 5d93bbf2219c8..b4ab83a09004a 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3330,11 +3330,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let prefix = if visible { "use " } else { "" }; let postfix = if visible { ";" } else { "" }; let path_strings = candidates.iter().map(|trait_did| { - debug!( - "trait_did={:?} with_crate_prefix={:?}", - trait_did, - with_crate_prefix!(self.tcx.def_path_str(*trait_did)) - ); + // debug!( + // "trait_did={:?} with_crate_prefix={:?}", + // trait_did, + // with_crate_prefix!(self.tcx.def_path_str(*trait_did)) + // ); format!( "{prefix}{}{postfix}\n", with_crate_prefix!(self.tcx.def_path_str(*trait_did)), diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 27a64a2bfd14d..2ea4307f5ebed 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -372,6 +372,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { fn try_print_visible_def_path(&mut self, def_id: DefId) -> Result { debug!("try_print_visible_def_path: def_id={:?}", def_id); if with_no_visible_paths() { + debug!("with no visible path: def_id={:?}", def_id); return Ok(false); } @@ -2227,6 +2228,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { def_id: DefId, args: &'tcx [GenericArg<'tcx>], ) -> Result<(), PrintError> { + debug!("print_def_path: def_id={:?}, args={:?}", def_id, args); if args.is_empty() { match self.try_print_trimmed_def_path(def_id)? { true => return Ok(()), @@ -2240,6 +2242,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { } let key = self.tcx.def_key(def_id); + debug!("print_def_path: def_id={:?}, key={:?}", def_id, key); if let DefPathData::Impl = key.disambiguated_data.data { // Always use types for non-local impls, where types are always // available, and filename/line-number is mostly uninteresting. From be500b08c739bacd995a7f931787083a2877b5fc Mon Sep 17 00:00:00 2001 From: morine0122 Date: Sun, 23 Mar 2025 17:04:24 +0900 Subject: [PATCH 04/13] add new test --- tests/ui/issues/auxiliary/issue-127011-a-1.rs | 3 +++ tests/ui/issues/auxiliary/issue-127011-b-1.rs | 4 ++++ tests/ui/issues/issue-127011-1.rs | 10 ++++++++++ tests/ui/issues/issue-127011-1.stderr | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 tests/ui/issues/auxiliary/issue-127011-a-1.rs create mode 100644 tests/ui/issues/auxiliary/issue-127011-b-1.rs create mode 100644 tests/ui/issues/issue-127011-1.rs create mode 100644 tests/ui/issues/issue-127011-1.stderr diff --git a/tests/ui/issues/auxiliary/issue-127011-a-1.rs b/tests/ui/issues/auxiliary/issue-127011-a-1.rs new file mode 100644 index 0000000000000..42718ce421609 --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-a-1.rs @@ -0,0 +1,3 @@ +//@ edition: 2021 + +pub fn foo() {} diff --git a/tests/ui/issues/auxiliary/issue-127011-b-1.rs b/tests/ui/issues/auxiliary/issue-127011-b-1.rs new file mode 100644 index 0000000000000..8f8a4b7ea1b14 --- /dev/null +++ b/tests/ui/issues/auxiliary/issue-127011-b-1.rs @@ -0,0 +1,4 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a_1=issue-127011-a-1.rs + +pub use issue_127011_a_1::foo; diff --git a/tests/ui/issues/issue-127011-1.rs b/tests/ui/issues/issue-127011-1.rs new file mode 100644 index 0000000000000..9bc2f4cdb370d --- /dev/null +++ b/tests/ui/issues/issue-127011-1.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:issue_127011_a_1=issue-127011-a-1.rs +//@ aux-crate:issue_127011_b_1=issue-127011-b-1.rs + +use issue_127011_b_1; + +fn main() { + foo(); + //~^ ERROR: cannot find function `foo` in this scope [E0425] +} diff --git a/tests/ui/issues/issue-127011-1.stderr b/tests/ui/issues/issue-127011-1.stderr new file mode 100644 index 0000000000000..689709bae9d4d --- /dev/null +++ b/tests/ui/issues/issue-127011-1.stderr @@ -0,0 +1,18 @@ +error[E0425]: cannot find function `foo` in this scope + --> $DIR/issue-127011-1.rs:8:5 + | +LL | foo(); + | ^^^ not found in this scope + | +help: consider importing one of these functions + | +LL + use crate::issue_127011_b_1::foo; + | +LL + use issue_127011_a_1::foo; + | +LL + use issue_127011_b_1::foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`. From f6a0951f298367249d0a2b02e2c20c0781afe47a Mon Sep 17 00:00:00 2001 From: morine0122 Date: Tue, 1 Apr 2025 03:07:39 +0900 Subject: [PATCH 05/13] add with no visible path if doc hidden --- compiler/rustc_hir_typeck/src/method/suggest.rs | 12 +++++++++--- compiler/rustc_middle/src/ty/print/pretty.rs | 5 +++++ tests/ui/issues/issue-127011.stderr | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index b4ab83a09004a..8f10acbeb431f 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -23,8 +23,10 @@ use rustc_hir::{self as hir, ExprKind, HirId, Node, PathSegment, QPath}; use rustc_infer::infer::{self, RegionVariableOrigin}; use rustc_middle::bug; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type}; +#[allow(unused_imports)] use rustc_middle::ty::print::{ - PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths, + PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths, with_no_trimmed_paths, + with_no_visible_paths, with_no_visible_paths_if_doc_hidden, }; use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; use rustc_span::def_id::DefIdSet; @@ -3337,7 +3339,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // ); format!( "{prefix}{}{postfix}\n", - with_crate_prefix!(self.tcx.def_path_str(*trait_did)), + with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( + self.tcx.def_path_str(*trait_did) + )), ) }); @@ -3345,7 +3349,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let parent_did = parent_map.get(trait_did).unwrap(); format!( "{prefix}{}::*{postfix} // trait {}\n", - with_crate_prefix!(self.tcx.def_path_str(*parent_did)), + with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( + self.tcx.def_path_str(*parent_did) + )), self.tcx.item_name(*trait_did), ) }); diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 2ea4307f5ebed..d99516f72f976 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -63,6 +63,7 @@ thread_local! { static FORCE_TRIMMED_PATH: Cell = const { Cell::new(false) }; static REDUCED_QUERIES: Cell = const { Cell::new(false) }; static NO_VISIBLE_PATH: Cell = const { Cell::new(false) }; + static NO_VISIBLE_PATH_IF_DOC_HIDDEN: Cell = const { Cell::new(false) }; static RTN_MODE: Cell = const { Cell::new(RtnMode::ForDiagnostic) }; } @@ -134,6 +135,7 @@ define_helper!( /// Prevent selection of visible paths. `Display` impl of DefId will prefer /// visible (public) reexports of types as paths. fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH); + fn with_no_visible_paths_if_doc_hidden(NoVisibleIfDocHiddenGuard, NO_VISIBLE_PATH_IF_DOC_HIDDEN); ); #[must_use] @@ -491,6 +493,9 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { callers: &mut Vec, ) -> Result { // debug!("try_print_visible_def_path: def_id={:?}", def_id); + if self.tcx().is_doc_hidden(def_id) && with_no_visible_paths_if_doc_hidden() { + return Ok(false); + } // If `def_id` is a direct or injected extern crate, return the // path to the crate followed by the path to the item within the crate. diff --git a/tests/ui/issues/issue-127011.stderr b/tests/ui/issues/issue-127011.stderr index c3f2cd395488b..8867f6b0f471e 100644 --- a/tests/ui/issues/issue-127011.stderr +++ b/tests/ui/issues/issue-127011.stderr @@ -7,7 +7,7 @@ LL | Bar::foo(); = help: items from traits can only be used if the trait is in scope help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it | -LL + use issue_127011_a::Foo; +LL + use issue_127011_b::Foo; | error: aborting due to 1 previous error From 8f71112593ad1f859aaa6bd2107f58df004ebac2 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Wed, 2 Apr 2025 02:58:54 +0900 Subject: [PATCH 06/13] add appropriate --- compiler/rustc_hir_typeck/src/method/suggest.rs | 10 +++++++--- compiler/rustc_middle/src/ty/print/pretty.rs | 7 ++++--- ...ggest-trait-reexported-as-not-doc-visible-a.rs | 5 +++++ ...ggest-trait-reexported-as-not-doc-visible-b.rs | 13 +++++++++++++ ...suggest-trait-reexported-as-not-doc-visible.rs | 10 ++++++++++ ...est-trait-reexported-as-not-doc-visible.stderr | 15 +++++++++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs create mode 100644 tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs create mode 100644 tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs create mode 100644 tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 8f10acbeb431f..4128d88d6db9f 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3337,11 +3337,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // trait_did, // with_crate_prefix!(self.tcx.def_path_str(*trait_did)) // ); + // format!( + // "{prefix}{}{postfix}\n", + // with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( + // self.tcx.def_path_str(*trait_did) + // )), + // ) format!( "{prefix}{}{postfix}\n", - with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( - self.tcx.def_path_str(*trait_did) - )), + with_crate_prefix!(self.tcx.def_path_str(*trait_did)), ) }); diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d99516f72f976..1432533177690 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -493,9 +493,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { callers: &mut Vec, ) -> Result { // debug!("try_print_visible_def_path: def_id={:?}", def_id); - if self.tcx().is_doc_hidden(def_id) && with_no_visible_paths_if_doc_hidden() { - return Ok(false); - } // If `def_id` is a direct or injected extern crate, return the // path to the crate followed by the path to the item within the crate. @@ -577,6 +574,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { return Ok(false); }; + if self.tcx().is_doc_hidden(visible_parent) && with_no_visible_paths_if_doc_hidden() { + return Ok(false); + } + let actual_parent = self.tcx().opt_parent(def_id); // debug!( // "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}", diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs new file mode 100644 index 0000000000000..87d83663626b5 --- /dev/null +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs @@ -0,0 +1,5 @@ +//@ edition: 2021 + +pub trait Foo { + fn foo(); +} diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs new file mode 100644 index 0000000000000..58a49c3c2b296 --- /dev/null +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs @@ -0,0 +1,13 @@ +//@ edition: 2021 +//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs + +pub struct Bar; + +impl __DocHidden::Foo for Bar { + fn foo() {} +} + +#[doc(hidden)] +pub mod __DocHidden { + pub use suggest_trait_reexported_as_not_doc_visible_a::Foo; +} diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs new file mode 100644 index 0000000000000..8339ce8310178 --- /dev/null +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs @@ -0,0 +1,10 @@ +//@ edition: 2021 +//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs +//@ aux-crate:suggest_trait_reexported_as_not_doc_visible_b=suggest-trait-reexported-as-not-doc-visible-b.rs + +use suggest_trait_reexported_as_not_doc_visible_b::Bar; + +fn main() { + Bar::foo(); + //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] +} diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr new file mode 100644 index 0000000000000..30a8cc2b2cdc5 --- /dev/null +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr @@ -0,0 +1,15 @@ +error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope + --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:8:10 + | +LL | Bar::foo(); + | ^^^ function or associated item not found in `Bar` + | + = help: items from traits can only be used if the trait is in scope +help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it + | +LL + use suggest_trait_reexported_as_not_doc_visible_b::__DocHidden::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0599`. From bc74d2b1434a07d7a8cebb0243a312254400e9ee Mon Sep 17 00:00:00 2001 From: morine0122 Date: Wed, 2 Apr 2025 03:35:44 +0900 Subject: [PATCH 07/13] pass test --- .../rustc_hir_typeck/src/method/suggest.rs | 14 +++++++------- tests/ui/issues/auxiliary/issue-127011-a-1.rs | 3 --- tests/ui/issues/auxiliary/issue-127011-a-2.rs | 5 ----- tests/ui/issues/auxiliary/issue-127011-a.rs | 5 ----- tests/ui/issues/auxiliary/issue-127011-b-1.rs | 4 ---- tests/ui/issues/auxiliary/issue-127011-b-2.rs | 10 ---------- tests/ui/issues/auxiliary/issue-127011-b.rs | 10 ---------- tests/ui/issues/issue-127011-1.rs | 10 ---------- tests/ui/issues/issue-127011-1.stderr | 18 ------------------ tests/ui/issues/issue-127011-2.rs | 10 ---------- tests/ui/issues/issue-127011-2.stderr | 15 --------------- tests/ui/issues/issue-127011.rs | 10 ---------- tests/ui/issues/issue-127011.stderr | 15 --------------- ...st-trait-reexported-as-not-doc-visible-b.rs | 1 + ...gest-trait-reexported-as-not-doc-visible.rs | 1 + ...-trait-reexported-as-not-doc-visible.stderr | 4 ++-- 16 files changed, 11 insertions(+), 124 deletions(-) delete mode 100644 tests/ui/issues/auxiliary/issue-127011-a-1.rs delete mode 100644 tests/ui/issues/auxiliary/issue-127011-a-2.rs delete mode 100644 tests/ui/issues/auxiliary/issue-127011-a.rs delete mode 100644 tests/ui/issues/auxiliary/issue-127011-b-1.rs delete mode 100644 tests/ui/issues/auxiliary/issue-127011-b-2.rs delete mode 100644 tests/ui/issues/auxiliary/issue-127011-b.rs delete mode 100644 tests/ui/issues/issue-127011-1.rs delete mode 100644 tests/ui/issues/issue-127011-1.stderr delete mode 100644 tests/ui/issues/issue-127011-2.rs delete mode 100644 tests/ui/issues/issue-127011-2.stderr delete mode 100644 tests/ui/issues/issue-127011.rs delete mode 100644 tests/ui/issues/issue-127011.stderr diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 4128d88d6db9f..d8534604e6e54 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3337,16 +3337,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // trait_did, // with_crate_prefix!(self.tcx.def_path_str(*trait_did)) // ); - // format!( - // "{prefix}{}{postfix}\n", - // with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( - // self.tcx.def_path_str(*trait_did) - // )), - // ) format!( "{prefix}{}{postfix}\n", - with_crate_prefix!(self.tcx.def_path_str(*trait_did)), + with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( + self.tcx.def_path_str(*trait_did) + )), ) + // format!( + // "{prefix}{}{postfix}\n", + // with_crate_prefix!(self.tcx.def_path_str(*trait_did)), + // ) }); let glob_path_strings = globs.iter().map(|trait_did| { diff --git a/tests/ui/issues/auxiliary/issue-127011-a-1.rs b/tests/ui/issues/auxiliary/issue-127011-a-1.rs deleted file mode 100644 index 42718ce421609..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-a-1.rs +++ /dev/null @@ -1,3 +0,0 @@ -//@ edition: 2021 - -pub fn foo() {} diff --git a/tests/ui/issues/auxiliary/issue-127011-a-2.rs b/tests/ui/issues/auxiliary/issue-127011-a-2.rs deleted file mode 100644 index 87d83663626b5..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-a-2.rs +++ /dev/null @@ -1,5 +0,0 @@ -//@ edition: 2021 - -pub trait Foo { - fn foo(); -} diff --git a/tests/ui/issues/auxiliary/issue-127011-a.rs b/tests/ui/issues/auxiliary/issue-127011-a.rs deleted file mode 100644 index 87d83663626b5..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-a.rs +++ /dev/null @@ -1,5 +0,0 @@ -//@ edition: 2021 - -pub trait Foo { - fn foo(); -} diff --git a/tests/ui/issues/auxiliary/issue-127011-b-1.rs b/tests/ui/issues/auxiliary/issue-127011-b-1.rs deleted file mode 100644 index 8f8a4b7ea1b14..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-b-1.rs +++ /dev/null @@ -1,4 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a_1=issue-127011-a-1.rs - -pub use issue_127011_a_1::foo; diff --git a/tests/ui/issues/auxiliary/issue-127011-b-2.rs b/tests/ui/issues/auxiliary/issue-127011-b-2.rs deleted file mode 100644 index d9e202e9724a6..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-b-2.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a_2=issue-127011-a-2.rs - -use issue_127011_a_2::Foo; - -pub struct Bar; - -impl Foo for Bar { - fn foo() {} -} diff --git a/tests/ui/issues/auxiliary/issue-127011-b.rs b/tests/ui/issues/auxiliary/issue-127011-b.rs deleted file mode 100644 index d6b11e30f518d..0000000000000 --- a/tests/ui/issues/auxiliary/issue-127011-b.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a=issue-127011-a.rs - -pub use issue_127011_a::Foo; - -pub struct Bar; - -impl Foo for Bar { - fn foo() {} -} diff --git a/tests/ui/issues/issue-127011-1.rs b/tests/ui/issues/issue-127011-1.rs deleted file mode 100644 index 9bc2f4cdb370d..0000000000000 --- a/tests/ui/issues/issue-127011-1.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a_1=issue-127011-a-1.rs -//@ aux-crate:issue_127011_b_1=issue-127011-b-1.rs - -use issue_127011_b_1; - -fn main() { - foo(); - //~^ ERROR: cannot find function `foo` in this scope [E0425] -} diff --git a/tests/ui/issues/issue-127011-1.stderr b/tests/ui/issues/issue-127011-1.stderr deleted file mode 100644 index 689709bae9d4d..0000000000000 --- a/tests/ui/issues/issue-127011-1.stderr +++ /dev/null @@ -1,18 +0,0 @@ -error[E0425]: cannot find function `foo` in this scope - --> $DIR/issue-127011-1.rs:8:5 - | -LL | foo(); - | ^^^ not found in this scope - | -help: consider importing one of these functions - | -LL + use crate::issue_127011_b_1::foo; - | -LL + use issue_127011_a_1::foo; - | -LL + use issue_127011_b_1::foo; - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/issues/issue-127011-2.rs b/tests/ui/issues/issue-127011-2.rs deleted file mode 100644 index 60879e5c07d6c..0000000000000 --- a/tests/ui/issues/issue-127011-2.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a_2=issue-127011-a-2.rs -//@ aux-crate:issue_127011_b_2=issue-127011-b-2.rs - -use issue_127011_b_2::Bar; - -fn main() { - Bar::foo(); - //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] -} diff --git a/tests/ui/issues/issue-127011-2.stderr b/tests/ui/issues/issue-127011-2.stderr deleted file mode 100644 index 3428f66b354c7..0000000000000 --- a/tests/ui/issues/issue-127011-2.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope - --> $DIR/issue-127011-2.rs:8:10 - | -LL | Bar::foo(); - | ^^^ function or associated item not found in `Bar` - | - = help: items from traits can only be used if the trait is in scope -help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it - | -LL + use issue_127011_a_2::Foo; - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/issues/issue-127011.rs b/tests/ui/issues/issue-127011.rs deleted file mode 100644 index 90e8a5968ab95..0000000000000 --- a/tests/ui/issues/issue-127011.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ edition: 2021 -//@ aux-crate:issue_127011_a=issue-127011-a.rs -//@ aux-crate:issue_127011_b=issue-127011-b.rs - -use issue_127011_b::Bar; - -fn main() { - Bar::foo(); - //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] -} diff --git a/tests/ui/issues/issue-127011.stderr b/tests/ui/issues/issue-127011.stderr deleted file mode 100644 index 8867f6b0f471e..0000000000000 --- a/tests/ui/issues/issue-127011.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope - --> $DIR/issue-127011.rs:8:10 - | -LL | Bar::foo(); - | ^^^ function or associated item not found in `Bar` - | - = help: items from traits can only be used if the trait is in scope -help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it - | -LL + use issue_127011_b::Foo; - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs index 58a49c3c2b296..9e90e0f0b356c 100644 --- a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs @@ -1,3 +1,4 @@ +// ignore-tidy-linelength //@ edition: 2021 //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs index 8339ce8310178..3cb775e85ac6b 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs @@ -1,3 +1,4 @@ +// ignore-tidy-linelength //@ edition: 2021 //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_b=suggest-trait-reexported-as-not-doc-visible-b.rs diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr index 30a8cc2b2cdc5..9128ee6844469 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr @@ -1,5 +1,5 @@ error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope - --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:8:10 + --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:9:10 | LL | Bar::foo(); | ^^^ function or associated item not found in `Bar` @@ -7,7 +7,7 @@ LL | Bar::foo(); = help: items from traits can only be used if the trait is in scope help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it | -LL + use suggest_trait_reexported_as_not_doc_visible_b::__DocHidden::Foo; +LL + use suggest_trait_reexported_as_not_doc_visible_a::Foo; | error: aborting due to 1 previous error From 2bebdf0212d17815b0a8302f5ab39ed4214bdfdf Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 05:27:35 +0900 Subject: [PATCH 08/13] add test --- ...t-trait-reexported-as-not-doc-visible-a.rs | 12 ++++- ...t-trait-reexported-as-not-doc-visible-b.rs | 27 +++++++--- ...est-trait-reexported-as-not-doc-visible.rs | 8 +-- ...trait-reexported-as-not-doc-visible.stderr | 51 ++++++++++++++++--- 4 files changed, 81 insertions(+), 17 deletions(-) diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs index 87d83663626b5..84e7734e5fd2b 100644 --- a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs @@ -1,5 +1,13 @@ //@ edition: 2021 -pub trait Foo { - fn foo(); +pub trait TraitA { + fn a(); +} + +pub trait TraitB { + fn b(); +} + +pub trait TraitC { + fn c(); } diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs index 9e90e0f0b356c..f78bda4b7cfed 100644 --- a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs @@ -2,13 +2,28 @@ //@ edition: 2021 //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs -pub struct Bar; - -impl __DocHidden::Foo for Bar { - fn foo() {} -} +#[doc(hidden)] +pub use suggest_trait_reexported_as_not_doc_visible_a::TraitA; #[doc(hidden)] pub mod __DocHidden { - pub use suggest_trait_reexported_as_not_doc_visible_a::Foo; + pub use suggest_trait_reexported_as_not_doc_visible_a::TraitB; + + pub mod Inner { + pub use suggest_trait_reexported_as_not_doc_visible_a::TraitC; + } +} + +impl suggest_trait_reexported_as_not_doc_visible_a::TraitA for Foo { + fn a() {} } + +impl __DocHidden::TraitB for Foo { + fn b() {} +} + +impl __DocHidden::Inner::TraitC for Foo { + fn c() {} +} + +pub struct Foo; diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs index 3cb775e85ac6b..861769257b1fd 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs @@ -3,9 +3,11 @@ //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_b=suggest-trait-reexported-as-not-doc-visible-b.rs -use suggest_trait_reexported_as_not_doc_visible_b::Bar; +use suggest_trait_reexported_as_not_doc_visible_b::Foo; fn main() { - Bar::foo(); - //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] + Foo::a(); + //~ no function or associated item named `a` found for struct `Foo` in the current scope [E0599] + Foo::b(); + Foo::c(); } diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr index 9128ee6844469..8ac46eda7aae9 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr @@ -1,15 +1,54 @@ -error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope +error[E0599]: no function or associated item named `a` found for struct `Foo` in the current scope --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:9:10 | -LL | Bar::foo(); - | ^^^ function or associated item not found in `Bar` +LL | Foo::a(); + | ^ function or associated item not found in `Foo` | = help: items from traits can only be used if the trait is in scope -help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it +help: trait `TraitA` which provides `a` is implemented but not in scope; perhaps you want to import it | -LL + use suggest_trait_reexported_as_not_doc_visible_a::Foo; +LL + use suggest_trait_reexported_as_not_doc_visible_b::TraitA; + | +help: there is an associated function `b` with a similar name + | +LL - Foo::a(); +LL + Foo::b(); + | + +error[E0599]: no function or associated item named `b` found for struct `Foo` in the current scope + --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:10:10 + | +LL | Foo::b(); + | ^ function or associated item not found in `Foo` + | + = help: items from traits can only be used if the trait is in scope +help: trait `TraitB` which provides `b` is implemented but not in scope; perhaps you want to import it + | +LL + use suggest_trait_reexported_as_not_doc_visible_a::TraitB; + | +help: there is an associated function `a` with a similar name + | +LL - Foo::b(); +LL + Foo::a(); + | + +error[E0599]: no function or associated item named `c` found for struct `Foo` in the current scope + --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:11:10 + | +LL | Foo::c(); + | ^ function or associated item not found in `Foo` + | + = help: items from traits can only be used if the trait is in scope +help: trait `TraitC` which provides `c` is implemented but not in scope; perhaps you want to import it + | +LL + use suggest_trait_reexported_as_not_doc_visible_a::TraitC; + | +help: there is an associated function `a` with a similar name + | +LL - Foo::c(); +LL + Foo::a(); | -error: aborting due to 1 previous error +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0599`. From 6e5c64e116488421766f9efd2947aee074c2d5c0 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 22:07:14 +0900 Subject: [PATCH 09/13] revert --- ...t-trait-reexported-as-not-doc-visible-a.rs | 12 +---- ...t-trait-reexported-as-not-doc-visible-b.rs | 27 +++------- ...est-trait-reexported-as-not-doc-visible.rs | 8 ++- ...trait-reexported-as-not-doc-visible.stderr | 51 +++---------------- 4 files changed, 17 insertions(+), 81 deletions(-) diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs index 84e7734e5fd2b..87d83663626b5 100644 --- a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-a.rs @@ -1,13 +1,5 @@ //@ edition: 2021 -pub trait TraitA { - fn a(); -} - -pub trait TraitB { - fn b(); -} - -pub trait TraitC { - fn c(); +pub trait Foo { + fn foo(); } diff --git a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs index f78bda4b7cfed..9e90e0f0b356c 100644 --- a/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs +++ b/tests/ui/typeck/auxiliary/suggest-trait-reexported-as-not-doc-visible-b.rs @@ -2,28 +2,13 @@ //@ edition: 2021 //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs -#[doc(hidden)] -pub use suggest_trait_reexported_as_not_doc_visible_a::TraitA; - -#[doc(hidden)] -pub mod __DocHidden { - pub use suggest_trait_reexported_as_not_doc_visible_a::TraitB; - - pub mod Inner { - pub use suggest_trait_reexported_as_not_doc_visible_a::TraitC; - } -} +pub struct Bar; -impl suggest_trait_reexported_as_not_doc_visible_a::TraitA for Foo { - fn a() {} +impl __DocHidden::Foo for Bar { + fn foo() {} } -impl __DocHidden::TraitB for Foo { - fn b() {} -} - -impl __DocHidden::Inner::TraitC for Foo { - fn c() {} +#[doc(hidden)] +pub mod __DocHidden { + pub use suggest_trait_reexported_as_not_doc_visible_a::Foo; } - -pub struct Foo; diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs index 861769257b1fd..3cb775e85ac6b 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs @@ -3,11 +3,9 @@ //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_a=suggest-trait-reexported-as-not-doc-visible-a.rs //@ aux-crate:suggest_trait_reexported_as_not_doc_visible_b=suggest-trait-reexported-as-not-doc-visible-b.rs -use suggest_trait_reexported_as_not_doc_visible_b::Foo; +use suggest_trait_reexported_as_not_doc_visible_b::Bar; fn main() { - Foo::a(); - //~ no function or associated item named `a` found for struct `Foo` in the current scope [E0599] - Foo::b(); - Foo::c(); + Bar::foo(); + //~^ ERROR: no function or associated item named `foo` found for struct `Bar` in the current scope [E0599] } diff --git a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr index 8ac46eda7aae9..9128ee6844469 100644 --- a/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr +++ b/tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr @@ -1,54 +1,15 @@ -error[E0599]: no function or associated item named `a` found for struct `Foo` in the current scope +error[E0599]: no function or associated item named `foo` found for struct `Bar` in the current scope --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:9:10 | -LL | Foo::a(); - | ^ function or associated item not found in `Foo` +LL | Bar::foo(); + | ^^^ function or associated item not found in `Bar` | = help: items from traits can only be used if the trait is in scope -help: trait `TraitA` which provides `a` is implemented but not in scope; perhaps you want to import it +help: trait `Foo` which provides `foo` is implemented but not in scope; perhaps you want to import it | -LL + use suggest_trait_reexported_as_not_doc_visible_b::TraitA; - | -help: there is an associated function `b` with a similar name - | -LL - Foo::a(); -LL + Foo::b(); - | - -error[E0599]: no function or associated item named `b` found for struct `Foo` in the current scope - --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:10:10 - | -LL | Foo::b(); - | ^ function or associated item not found in `Foo` - | - = help: items from traits can only be used if the trait is in scope -help: trait `TraitB` which provides `b` is implemented but not in scope; perhaps you want to import it - | -LL + use suggest_trait_reexported_as_not_doc_visible_a::TraitB; - | -help: there is an associated function `a` with a similar name - | -LL - Foo::b(); -LL + Foo::a(); - | - -error[E0599]: no function or associated item named `c` found for struct `Foo` in the current scope - --> $DIR/suggest-trait-reexported-as-not-doc-visible.rs:11:10 - | -LL | Foo::c(); - | ^ function or associated item not found in `Foo` - | - = help: items from traits can only be used if the trait is in scope -help: trait `TraitC` which provides `c` is implemented but not in scope; perhaps you want to import it - | -LL + use suggest_trait_reexported_as_not_doc_visible_a::TraitC; - | -help: there is an associated function `a` with a similar name - | -LL - Foo::c(); -LL + Foo::a(); +LL + use suggest_trait_reexported_as_not_doc_visible_a::Foo; | -error: aborting due to 3 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0599`. From e0bce71e88701bb26efbef0903460c579fe151d7 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 23:06:18 +0900 Subject: [PATCH 10/13] refactor --- compiler/rustc_hir_typeck/src/method/suggest.rs | 5 ++--- compiler/rustc_middle/src/ty/print/pretty.rs | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index d8534604e6e54..d408171737ed4 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -23,10 +23,9 @@ use rustc_hir::{self as hir, ExprKind, HirId, Node, PathSegment, QPath}; use rustc_infer::infer::{self, RegionVariableOrigin}; use rustc_middle::bug; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type}; -#[allow(unused_imports)] use rustc_middle::ty::print::{ - PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths, with_no_trimmed_paths, - with_no_visible_paths, with_no_visible_paths_if_doc_hidden, + PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths, + with_no_visible_paths_if_doc_hidden, }; use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; use rustc_span::def_id::DefIdSet; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 1432533177690..d5dff4bb375a4 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -135,6 +135,7 @@ define_helper!( /// Prevent selection of visible paths. `Display` impl of DefId will prefer /// visible (public) reexports of types as paths. fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH); + /// Prevent selection of visible paths if the paths are through any doc hidden path. fn with_no_visible_paths_if_doc_hidden(NoVisibleIfDocHiddenGuard, NO_VISIBLE_PATH_IF_DOC_HIDDEN); ); From 6f8306881cde238b8a007a5e3063403872276402 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 23:18:51 +0900 Subject: [PATCH 11/13] refactor --- compiler/rustc_middle/src/ty/print/mod.rs | 13 ++++++------ compiler/rustc_middle/src/ty/print/pretty.rs | 22 ++------------------ 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 148af7bfc4126..dc2040aa5cf85 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -6,7 +6,6 @@ use rustc_data_structures::sso::SsoHashSet; use rustc_hir as hir; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; -#[allow(unused_imports)] use tracing::{debug, instrument, trace}; use crate::ty::{self, GenericArg, ShortInstance, Ty, TyCtxt}; @@ -112,14 +111,14 @@ pub trait Printer<'tcx>: Sized { // Defaults (should not be overridden): - // #[instrument(skip(self), level = "debug")] + #[instrument(skip(self), level = "debug")] fn default_print_def_path( &mut self, def_id: DefId, args: &'tcx [GenericArg<'tcx>], ) -> Result<(), PrintError> { let key = self.tcx().def_key(def_id); - // debug!(?key); + debug!(?key); match key.disambiguated_data.data { DefPathData::CrateRoot => { @@ -208,10 +207,10 @@ pub trait Printer<'tcx>: Sized { self_ty: Ty<'tcx>, impl_trait_ref: Option>, ) -> Result<(), PrintError> { - // debug!( - // "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}", - // impl_def_id, self_ty, impl_trait_ref - // ); + debug!( + "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}", + impl_def_id, self_ty, impl_trait_ref + ); let key = self.tcx().def_key(impl_def_id); let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id }; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d5dff4bb375a4..277c9b8c79064 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -373,9 +373,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { /// from at least one local module, and returns `true`. If the crate defining `def_id` is /// declared with an `extern crate`, the path is guaranteed to use the `extern crate`. fn try_print_visible_def_path(&mut self, def_id: DefId) -> Result { - debug!("try_print_visible_def_path: def_id={:?}", def_id); if with_no_visible_paths() { - debug!("with no visible path: def_id={:?}", def_id); return Ok(false); } @@ -493,7 +491,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { def_id: DefId, callers: &mut Vec, ) -> Result { - // debug!("try_print_visible_def_path: def_id={:?}", def_id); + debug!("try_print_visible_def_path: def_id={:?}", def_id); // If `def_id` is a direct or injected extern crate, return the // path to the crate followed by the path to the item within the crate. @@ -556,7 +554,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { let visible_parent_map = self.tcx().visible_parent_map(()); let mut cur_def_key = self.tcx().def_key(def_id); - // debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); + debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); // For a constructor, we want the name of its parent rather than . if let DefPathData::Ctor = cur_def_key.disambiguated_data.data { @@ -571,7 +569,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } let Some(visible_parent) = visible_parent_map.get(&def_id).cloned() else { - debug!("try_print_visible_def_path: no visible parent for {:?}", def_id); return Ok(false); }; @@ -580,16 +577,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } let actual_parent = self.tcx().opt_parent(def_id); - // debug!( - // "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}", - // visible_parent, actual_parent, - // ); let mut data = cur_def_key.disambiguated_data.data; - // debug!( - // "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}", - // data, visible_parent, actual_parent, - // ); match data { // In order to output a path that could actually be imported (valid and visible), @@ -626,10 +615,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { DefPathData::TypeNs(Some(ref mut name)) if Some(visible_parent) != actual_parent => { // Item might be re-exported several times, but filter for the one // that's public and whose identifier isn't `_`. - debug!( - "try_print_visible_def_path: def_id={:?}, visible_parent={:?}, actual_parent={:?}", - def_id, visible_parent, actual_parent - ); let reexport = self .tcx() // FIXME(typed_def_id): Further propagate ModDefId @@ -638,7 +623,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) .map(|child| child.ident.name); - debug!("try_print_visible_def_path: reexport={:?}", reexport); if let Some(new_name) = reexport { *name = new_name; @@ -653,7 +637,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } _ => {} } - // debug!("try_print_visible_def_path: data={:?}", data); if callers.contains(&visible_parent) { return Ok(false); @@ -2200,7 +2183,6 @@ impl<'t> TyCtxt<'t> { ) -> String { let def_id = def_id.into_query_param(); let ns = guess_def_namespace(self, def_id); - // debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); FmtPrinter::print_string(self, ns, |cx| cx.print_def_path(def_id, args)).unwrap() } From 3a2536f7f5936a30e508129d165094feebc81ea1 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 23:20:47 +0900 Subject: [PATCH 12/13] refactor --- compiler/rustc_hir_typeck/src/method/suggest.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index d408171737ed4..13855d3fe27a0 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3292,7 +3292,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - #[instrument(level = "debug", skip(self, handle_candidates))] fn suggest_use_candidates(&self, candidates: Vec, handle_candidates: F) where F: FnOnce(Vec, Vec, Span), @@ -3331,21 +3330,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let prefix = if visible { "use " } else { "" }; let postfix = if visible { ";" } else { "" }; let path_strings = candidates.iter().map(|trait_did| { - // debug!( - // "trait_did={:?} with_crate_prefix={:?}", - // trait_did, - // with_crate_prefix!(self.tcx.def_path_str(*trait_did)) - // ); format!( "{prefix}{}{postfix}\n", with_no_visible_paths_if_doc_hidden!(with_crate_prefix!( self.tcx.def_path_str(*trait_did) )), ) - // format!( - // "{prefix}{}{postfix}\n", - // with_crate_prefix!(self.tcx.def_path_str(*trait_did)), - // ) }); let glob_path_strings = globs.iter().map(|trait_did| { From 2c01f51de72f1548e0996fb4831960e35615a082 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Thu, 3 Apr 2025 23:25:08 +0900 Subject: [PATCH 13/13] refactor --- compiler/rustc_middle/src/ty/print/pretty.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 277c9b8c79064..5b503ce6e2634 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -577,8 +577,16 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } let actual_parent = self.tcx().opt_parent(def_id); + debug!( + "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}", + visible_parent, actual_parent, + ); let mut data = cur_def_key.disambiguated_data.data; + debug!( + "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}", + data, visible_parent, actual_parent, + ); match data { // In order to output a path that could actually be imported (valid and visible), @@ -637,6 +645,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } _ => {} } + debug!("try_print_visible_def_path: data={:?}", data); if callers.contains(&visible_parent) { return Ok(false); @@ -2183,6 +2192,7 @@ impl<'t> TyCtxt<'t> { ) -> String { let def_id = def_id.into_query_param(); let ns = guess_def_namespace(self, def_id); + debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); FmtPrinter::print_string(self, ns, |cx| cx.print_def_path(def_id, args)).unwrap() } @@ -2217,7 +2227,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { def_id: DefId, args: &'tcx [GenericArg<'tcx>], ) -> Result<(), PrintError> { - debug!("print_def_path: def_id={:?}, args={:?}", def_id, args); if args.is_empty() { match self.try_print_trimmed_def_path(def_id)? { true => return Ok(()), @@ -2231,7 +2240,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { } let key = self.tcx.def_key(def_id); - debug!("print_def_path: def_id={:?}, key={:?}", def_id, key); if let DefPathData::Impl = key.disambiguated_data.data { // Always use types for non-local impls, where types are always // available, and filename/line-number is mostly uninteresting.