diagnostics: if AssocFn has self argument, describe as method

Discussed in
https://rust-lang.zulipchat.com/#narrow/stream/147480-t-compiler.2Fwg-diagnostics/topic/.22associated.20function.22.20vs.20.22method.22/near/329265515

This commit also changes the tooltips on rustdoc intra-doc links
targeting methods.
This commit is contained in:
Michael Howell 2023-02-21 14:05:32 -07:00
parent 3b4d6e0804
commit 3d056c3125
25 changed files with 91 additions and 58 deletions

View File

@ -2135,7 +2135,7 @@ fn report_escaping_data(
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
let tcx = self.infcx.tcx;
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id().to_def_id());
let escapes_from = tcx.def_descr(self.mir_def_id().to_def_id());
let mut err =
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);

View File

@ -660,10 +660,8 @@ fn report_escaping_data_error(
errci.outlived_fr,
);
let (_, escapes_from) = self
.infcx
.tcx
.article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
let escapes_from =
self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
// Revert to the normal error in these cases.
// Assignments aren't "escapes" in function items.
@ -757,8 +755,7 @@ fn report_general_error(
..
} = errci;
let (_, mir_def_name) =
self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
let mir_def_name = self.infcx.tcx.def_descr(self.mir_def_id().to_def_id());
let err = LifetimeOutliveErr { span: *span };
let mut diag = self.infcx.tcx.sess.create_err(err);

View File

@ -124,6 +124,11 @@ pub enum DefKind {
}
impl DefKind {
/// Get an English description for the item's kind.
///
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr` or
/// `TyCtxt::def_kind_descr` instead, because they give better
/// information for generators and associated functions.
pub fn descr(self, def_id: DefId) -> &'static str {
match self {
DefKind::Fn => "function",
@ -166,6 +171,10 @@ pub fn descr(self, def_id: DefId) -> &'static str {
}
/// Gets an English article for the definition.
///
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr_article` or
/// `TyCtxt::def_kind_descr_article` instead, because they give better
/// information for generators and associated functions.
pub fn article(&self) -> &'static str {
match *self {
DefKind::AssocTy

View File

@ -304,10 +304,7 @@ pub(crate) fn complain_about_inherent_assoc_type_not_found(
if let Some(did) = adt_did {
err.span_label(
tcx.def_span(did),
format!(
"associated item `{name}` not found for this {}",
tcx.def_kind(did).descr(did)
),
format!("associated item `{name}` not found for this {}", tcx.def_descr(did)),
);
}
};

View File

@ -1217,7 +1217,7 @@ fn add_predicates_for_ast_type_binding(
| (hir::def::DefKind::AssocConst, ty::TermKind::Const(_)) => (),
(_, _) => {
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
let expected = def_kind.descr(assoc_item_def_id);
let expected = tcx.def_descr(assoc_item_def_id);
let mut err = tcx.sess.struct_span_err(
binding.span,
&format!("expected {expected} bound, found {got}"),
@ -1552,7 +1552,7 @@ trait here instead: `trait NewTrait: {} {{}}`",
i.bottom().1,
E0038,
"the {} `{}` cannot be made into an object",
tcx.def_kind(def_id).descr(def_id),
tcx.def_descr(def_id),
tcx.item_name(def_id),
);
err.note(
@ -2174,7 +2174,7 @@ pub fn associated_path_to_ty(
"`{}` could{} refer to the {} defined here",
assoc_ident,
also,
kind.descr(def_id)
tcx.def_kind_descr(kind, def_id)
);
lint.span_note(tcx.def_span(def_id), &note_msg);
};
@ -2350,7 +2350,7 @@ fn check_assoc_ty(
let kind = DefKind::AssocTy;
if !tcx.visibility(item).is_accessible_from(def_scope, tcx) {
let kind = kind.descr(item);
let kind = tcx.def_kind_descr(kind, item);
let msg = format!("{kind} `{name}` is private");
let def_span = tcx.def_span(item);
tcx.sess

View File

@ -1460,7 +1460,7 @@ fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
span,
format!(
"{} captures itself here",
tcx.def_kind(closure_def_id).descr(closure_def_id)
tcx.def_descr(closure_def_id)
),
);
}

View File

@ -71,7 +71,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
let drop_impl_span = tcx.def_span(drop_impl_did);
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
let self_descr = tcx.def_descr(self_type_did);
let mut err =
struct_span_err!(tcx.sess, drop_impl_span, E0366, "`Drop` impls cannot be specialized");
match arg {
@ -217,7 +217,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
if !assumptions_in_impl_context.iter().copied().any(predicate_matches_closure) {
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did.to_def_id());
let self_descr = tcx.def_descr(self_type_did.to_def_id());
let reported = struct_span_err!(
tcx.sess,
predicate_sp,

View File

@ -531,7 +531,7 @@ fn lint_auto_trait_impl<'tcx>(
}),
|lint| {
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
let self_descr = tcx.def_descr(self_type_did);
match arg {
ty::util::NotUniqueParam::DuplicateParam(arg) => {
lint.note(&format!("`{}` is mentioned multiple times", arg));

View File

@ -439,7 +439,7 @@ fn get_unbound_associated_types(&self) -> Vec<String> {
fn create_error_message(&self) -> String {
let def_path = self.tcx.def_path_str(self.def_id);
let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
let def_kind = self.tcx.def_descr(self.def_id);
let (quantifier, bound) = self.get_quantifier_and_bound();
let kind = self.kind();
let provided_lt_args = self.num_provided_lifetime_args();
@ -990,7 +990,7 @@ fn show_definition(&self, err: &mut Diagnostic) {
};
let msg = {
let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
let def_kind = self.tcx.def_descr(self.def_id);
let (quantifier, bound) = self.get_quantifier_and_bound();
let params = if bound == 0 {

View File

@ -671,7 +671,7 @@ fn report_invalid_callee(
&& !self.type_is_sized_modulo_regions(self.param_env, output_ty, callee_expr.span)
{
let descr = match maybe_def {
DefIdOrName::DefId(def_id) => self.tcx.def_kind(def_id).descr(def_id),
DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
DefIdOrName::Name(name) => name,
};
err.span_label(

View File

@ -2448,7 +2448,7 @@ fn ban_private_field_access(
return_ty: Option<Ty<'tcx>>,
) {
let struct_path = self.tcx().def_path_str(base_did);
let kind_name = self.tcx().def_kind(base_did).descr(base_did);
let kind_name = self.tcx().def_descr(base_did);
let mut err = struct_span_err!(
self.tcx().sess,
field.span,

View File

@ -1938,8 +1938,7 @@ fn label_fn_like(
spans.push_span_label(param.span, "");
}
let def_kind = self.tcx.def_kind(def_id);
err.span_note(spans, &format!("{} defined here", def_kind.descr(def_id)));
err.span_note(spans, &format!("{} defined here", self.tcx.def_descr(def_id)));
} else if let Some(hir::Node::Expr(e)) = self.tcx.hir().get_if_local(def_id)
&& let hir::ExprKind::Closure(hir::Closure { body, .. }) = &e.kind
{
@ -1952,10 +1951,9 @@ fn label_fn_like(
};
err.span_note(span, &format!("{} defined here", kind));
} else {
let def_kind = self.tcx.def_kind(def_id);
err.span_note(
self.tcx.def_span(def_id),
&format!("{} defined here", def_kind.descr(def_id)),
&format!("{} defined here", self.tcx.def_descr(def_id)),
);
}
}

View File

@ -120,7 +120,7 @@ pub(crate) fn suggest_fn_call(
DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
DefKind::Ctor(CtorOf::Struct, _) => "construct this tuple struct".to_string(),
DefKind::Ctor(CtorOf::Variant, _) => "construct this tuple variant".to_string(),
kind => format!("call this {}", kind.descr(def_id)),
kind => format!("call this {}", self.tcx.def_kind_descr(kind, def_id)),
},
DefIdOrName::Name(name) => format!("call this {name}"),
};
@ -339,7 +339,7 @@ pub fn suggest_deref_ref_or_into(
CtorOf::Variant => "an enum variant",
}));
} else {
let descr = kind.descr(def_id);
let descr = self.tcx.def_kind_descr(kind, def_id);
err.span_label(sp, format!("{descr} `{name}` defined here"));
}
return true;

View File

@ -1366,8 +1366,8 @@ pub fn maybe_emit_unstable_name_collision_hint(
span,
format!(
"{} {} with this name may be added to the standard library in the future",
def_kind.article(),
def_kind.descr(self.item.def_id),
tcx.def_kind_descr_article(def_kind, self.item.def_id),
tcx.def_kind_descr(def_kind, self.item.def_id),
),
|lint| {
match (self.item.kind, self.item.container) {

View File

@ -160,7 +160,7 @@ pub fn report_method_error(
}
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
let kind = kind.descr(def_id);
let kind = self.tcx.def_kind_descr(kind, def_id);
let mut err = struct_span_err!(
self.tcx.sess,
item_name.span,
@ -1062,8 +1062,8 @@ trait bound{s}",
span,
&format!(
"there is {} {} with a similar name",
def_kind.article(),
def_kind.descr(similar_candidate.def_id),
self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id),
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
),
similar_candidate.name,
Applicability::MaybeIncorrect,
@ -1172,7 +1172,7 @@ fn note_candidates_on_method_error(
path,
ty,
item.kind,
item.def_id,
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
sugg_span,
idx,
self.tcx.sess.source_map(),
@ -1208,7 +1208,7 @@ fn note_candidates_on_method_error(
path,
rcvr_ty,
item.kind,
item.def_id,
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
sugg_span,
idx,
self.tcx.sess.source_map(),
@ -2853,7 +2853,7 @@ fn print_disambiguation_help<'tcx>(
trait_name: String,
rcvr_ty: Ty<'_>,
kind: ty::AssocKind,
def_id: DefId,
def_kind_descr: &'static str,
span: Span,
candidate: Option<usize>,
source_map: &source_map::SourceMap,
@ -2886,7 +2886,7 @@ fn print_disambiguation_help<'tcx>(
span,
&format!(
"disambiguate the {} for {}",
kind.as_def_kind().descr(def_id),
def_kind_descr,
if let Some(candidate) = candidate {
format!("candidate #{}", candidate)
} else {

View File

@ -122,7 +122,7 @@ fn for_parent_def_id(
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
Some(InferenceDiagnosticsParentData {
prefix: tcx.def_kind(parent_def_id).descr(parent_def_id),
prefix: tcx.def_descr(parent_def_id),
name: parent_name,
})
}

View File

@ -255,7 +255,7 @@ fn late_report_deprecation(
let method_span = method_span.unwrap_or(span);
tcx.struct_span_lint_hir(lint, hir_id, method_span, message, |diag| {
if let hir::Node::Expr(_) = tcx.hir().get(hir_id) {
let kind = tcx.def_kind(def_id).descr(def_id);
let kind = tcx.def_descr(def_id);
deprecation_suggestion(diag, kind, suggestion, method_span);
}
diag
@ -392,7 +392,7 @@ pub fn eval_stability_allow_unstable(
let lint = deprecation_lint(is_in_effect);
if self.lint_level_at_node(lint, id).0 != Level::Allow {
let def_path = with_no_trimmed_paths!(self.def_path_str(def_id));
let def_kind = self.def_kind(def_id).descr(def_id);
let def_kind = self.def_descr(def_id);
late_report_deprecation(
self,

View File

@ -1200,13 +1200,8 @@ pub fn caller_location_ty(self) -> Ty<'tcx> {
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
pub fn article_and_description(self, def_id: DefId) -> (&'static str, &'static str) {
match self.def_kind(def_id) {
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
rustc_hir::GeneratorKind::Async(..) => ("an", "async closure"),
rustc_hir::GeneratorKind::Gen => ("a", "generator"),
},
def_kind => (def_kind.article(), def_kind.descr(def_id)),
}
let kind = self.def_kind(def_id);
(self.def_kind_descr_article(kind, def_id), self.def_kind_descr(kind, def_id))
}
pub fn type_length_limit(self) -> Limit {

View File

@ -761,6 +761,40 @@ pub fn generator_layout_and_saved_local_names(
}
(generator_layout, generator_saved_local_names)
}
/// Query and get an English description for the item's kind.
pub fn def_descr(self, def_id: DefId) -> &'static str {
self.def_kind_descr(self.def_kind(def_id), def_id)
}
/// Get an English description for the item's kind.
pub fn def_kind_descr(self, def_kind: DefKind, def_id: DefId) -> &'static str {
match def_kind {
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
rustc_hir::GeneratorKind::Async(..) => "async closure",
rustc_hir::GeneratorKind::Gen => "generator",
},
_ => def_kind.descr(def_id),
}
}
/// Gets an English article for the [`TyCtxt::def_descr`].
pub fn def_descr_article(self, def_id: DefId) -> &'static str {
self.def_kind_descr_article(self.def_kind(def_id), def_id)
}
/// Gets an English article for the [`TyCtxt::def_kind_descr`].
pub fn def_kind_descr_article(self, def_kind: DefKind, def_id: DefId) -> &'static str {
match def_kind {
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
rustc_hir::GeneratorKind::Async(..) => "an",
rustc_hir::GeneratorKind::Gen => "a",
},
_ => def_kind.article(),
}
}
}
struct OpaqueTypeExpander<'tcx> {

View File

@ -691,7 +691,7 @@ fn warn_multiple_dead_codes(
})
.collect();
let descr = tcx.def_kind(first_id).descr(first_id.to_def_id());
let descr = tcx.def_descr(first_id.to_def_id());
let num = dead_codes.len();
let multiple = num > 6;
let name_list = names.into();
@ -703,7 +703,7 @@ fn warn_multiple_dead_codes(
};
let parent_info = if let Some(parent_item) = parent_item {
let parent_descr = tcx.def_kind(parent_item).descr(parent_item.to_def_id());
let parent_descr = tcx.def_descr(parent_item.to_def_id());
Some(ParentInfo {
num,
descr,

View File

@ -523,7 +523,7 @@ fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
&& stab.is_none()
&& self.effective_visibilities.is_reachable(def_id)
{
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
let descr = self.tcx.def_descr(def_id.to_def_id());
self.tcx.sess.emit_err(errors::MissingStabilityAttr { span, descr });
}
}
@ -551,7 +551,7 @@ fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
let is_reachable = self.effective_visibilities.is_reachable(def_id);
if is_const && is_stable && missing_const_stability_attribute && is_reachable {
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
let descr = self.tcx.def_descr(def_id.to_def_id());
self.tcx.sess.emit_err(errors::MissingConstStabAttr { span, descr });
}
}

View File

@ -1329,7 +1329,7 @@ fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId, span: S
hir::QPath::Resolved(_, path) => Some(self.tcx.def_path_str(path.res.def_id())),
hir::QPath::TypeRelative(_, segment) => Some(segment.ident.to_string()),
};
let kind = kind.descr(def_id);
let kind = self.tcx.def_descr(def_id);
let sess = self.tcx.sess;
let _ = match name {
Some(name) => {

View File

@ -189,7 +189,7 @@ pub(crate) fn report_conflict<'b>(
}
let container = match parent.kind {
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
ModuleKind::Def(kind, _, _) => self.tcx.def_kind_descr(kind, parent.def_id()),
ModuleKind::Block => "block",
};
@ -1804,7 +1804,7 @@ pub(crate) fn report_path_resolution_error(
found("module")
} else {
match binding.res() {
Res::Def(kind, id) => found(kind.descr(id)),
Res::Def(kind, id) => found(self.tcx.def_kind_descr(kind, id)),
_ => found(ns_to_try.descr()),
}
}

View File

@ -927,7 +927,10 @@ fn suggest_fn_call(
DefKind::Ctor(CtorOf::Variant, _) => {
"use parentheses to construct this tuple variant".to_string()
}
kind => format!("use parentheses to call this {}", kind.descr(def_id)),
kind => format!(
"use parentheses to call this {}",
self.tcx.def_kind_descr(kind, def_id)
),
},
DefIdOrName::Name(name) => format!("use parentheses to call this {name}"),
};
@ -2139,7 +2142,7 @@ fn suggest_fully_qualified_path(
err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \
accessed through a specific `impl`",
assoc_item.kind.as_def_kind().descr(item_def_id)
self.tcx.def_kind_descr(assoc_item.kind.as_def_kind(), item_def_id)
));
err.span_suggestion(
span,

View File

@ -775,7 +775,7 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
let fqp = fqp.iter().map(|sym| sym.as_str()).join("::");
if let &Some(UrlFragment::Item(id)) = fragment {
let name = cx.tcx().item_name(id);
let descr = cx.tcx().def_kind(id).descr(id);
let descr = cx.tcx().def_descr(id);
format!("{descr} {fqp}::{name}")
} else {
format!("{shortty} {fqp}")