Skip to content
Closed

Dev2 #139325

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc_middle::bug;
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
use rustc_middle::ty::print::{
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;
Expand Down Expand Up @@ -3291,6 +3292,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

#[instrument(level = "debug", skip(self, handle_candidates))]
fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
where
F: FnOnce(Vec<String>, Vec<String>, Span),
Expand Down Expand Up @@ -3329,17 +3331,30 @@ 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)),
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| {
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),
)
});
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -207,10 +208,10 @@ pub trait Printer<'tcx>: Sized {
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> 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 };
Expand Down
41 changes: 29 additions & 12 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ thread_local! {
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
static REDUCED_QUERIES: Cell<bool> = const { Cell::new(false) };
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
static NO_VISIBLE_PATH_IF_DOC_HIDDEN: Cell<bool> = const { Cell::new(false) };
static RTN_MODE: Cell<RtnMode> = const { Cell::new(RtnMode::ForDiagnostic) };
}

Expand Down Expand Up @@ -134,6 +135,8 @@ 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);
);

#[must_use]
Expand Down Expand Up @@ -370,7 +373,9 @@ 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<bool, PrintError> {
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);
}

Expand Down Expand Up @@ -488,7 +493,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
def_id: DefId,
callers: &mut Vec<DefId>,
) -> Result<bool, PrintError> {
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.
Expand Down Expand Up @@ -551,7 +556,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 <unnamed>.
if let DefPathData::Ctor = cur_def_key.disambiguated_data.data {
Expand All @@ -566,20 +571,25 @@ 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);
};

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={:?}",
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),
Expand Down Expand Up @@ -616,6 +626,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
Expand All @@ -624,6 +638,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;
Expand All @@ -638,7 +653,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);
Expand Down Expand Up @@ -2185,7 +2200,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()
}
Expand Down Expand Up @@ -2220,6 +2235,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(()),
Expand All @@ -2233,6 +2249,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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@ edition: 2021

pub trait Foo {
fn foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ignore-tidy-linelength
//@ 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;
}
11 changes: 11 additions & 0 deletions tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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

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]
}
15 changes: 15 additions & 0 deletions tests/ui/typeck/suggest-trait-reexported-as-not-doc-visible.stderr
Original file line number Diff line number Diff line change
@@ -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:9: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_a::Foo;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Loading