Skip to content

Commit 3d056c3

Browse files
committed
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.
1 parent 3b4d6e0 commit 3d056c3

File tree

25 files changed

+91
-58
lines changed

25 files changed

+91
-58
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21352135
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
21362136
let tcx = self.infcx.tcx;
21372137

2138-
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id().to_def_id());
2138+
let escapes_from = tcx.def_descr(self.mir_def_id().to_def_id());
21392139

21402140
let mut err =
21412141
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
660660
errci.outlived_fr,
661661
);
662662

663-
let (_, escapes_from) = self
664-
.infcx
665-
.tcx
666-
.article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
663+
let escapes_from =
664+
self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
667665

668666
// Revert to the normal error in these cases.
669667
// Assignments aren't "escapes" in function items.
@@ -757,8 +755,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
757755
..
758756
} = errci;
759757

760-
let (_, mir_def_name) =
761-
self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
758+
let mir_def_name = self.infcx.tcx.def_descr(self.mir_def_id().to_def_id());
762759

763760
let err = LifetimeOutliveErr { span: *span };
764761
let mut diag = self.infcx.tcx.sess.create_err(err);

compiler/rustc_hir/src/def.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ pub enum DefKind {
124124
}
125125

126126
impl DefKind {
127+
/// Get an English description for the item's kind.
128+
///
129+
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr` or
130+
/// `TyCtxt::def_kind_descr` instead, because they give better
131+
/// information for generators and associated functions.
127132
pub fn descr(self, def_id: DefId) -> &'static str {
128133
match self {
129134
DefKind::Fn => "function",
@@ -166,6 +171,10 @@ impl DefKind {
166171
}
167172

168173
/// Gets an English article for the definition.
174+
///
175+
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr_article` or
176+
/// `TyCtxt::def_kind_descr_article` instead, because they give better
177+
/// information for generators and associated functions.
169178
pub fn article(&self) -> &'static str {
170179
match *self {
171180
DefKind::AssocTy

compiler/rustc_hir_analysis/src/astconv/errors.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
304304
if let Some(did) = adt_did {
305305
err.span_label(
306306
tcx.def_span(did),
307-
format!(
308-
"associated item `{name}` not found for this {}",
309-
tcx.def_kind(did).descr(did)
310-
),
307+
format!("associated item `{name}` not found for this {}", tcx.def_descr(did)),
311308
);
312309
}
313310
};

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12171217
| (hir::def::DefKind::AssocConst, ty::TermKind::Const(_)) => (),
12181218
(_, _) => {
12191219
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
1220-
let expected = def_kind.descr(assoc_item_def_id);
1220+
let expected = tcx.def_descr(assoc_item_def_id);
12211221
let mut err = tcx.sess.struct_span_err(
12221222
binding.span,
12231223
&format!("expected {expected} bound, found {got}"),
@@ -1552,7 +1552,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15521552
i.bottom().1,
15531553
E0038,
15541554
"the {} `{}` cannot be made into an object",
1555-
tcx.def_kind(def_id).descr(def_id),
1555+
tcx.def_descr(def_id),
15561556
tcx.item_name(def_id),
15571557
);
15581558
err.note(
@@ -2174,7 +2174,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21742174
"`{}` could{} refer to the {} defined here",
21752175
assoc_ident,
21762176
also,
2177-
kind.descr(def_id)
2177+
tcx.def_kind_descr(kind, def_id)
21782178
);
21792179
lint.span_note(tcx.def_span(def_id), &note_msg);
21802180
};
@@ -2350,7 +2350,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23502350
let kind = DefKind::AssocTy;
23512351

23522352
if !tcx.visibility(item).is_accessible_from(def_scope, tcx) {
2353-
let kind = kind.descr(item);
2353+
let kind = tcx.def_kind_descr(kind, item);
23542354
let msg = format!("{kind} `{name}` is private");
23552355
let def_span = tcx.def_span(item);
23562356
tcx.sess

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ fn opaque_type_cycle_error(
14601460
span,
14611461
format!(
14621462
"{} captures itself here",
1463-
tcx.def_kind(closure_def_id).descr(closure_def_id)
1463+
tcx.def_descr(closure_def_id)
14641464
),
14651465
);
14661466
}

compiler/rustc_hir_analysis/src/check/dropck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
7171

7272
let drop_impl_span = tcx.def_span(drop_impl_did);
7373
let item_span = tcx.def_span(self_type_did);
74-
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
74+
let self_descr = tcx.def_descr(self_type_did);
7575
let mut err =
7676
struct_span_err!(tcx.sess, drop_impl_span, E0366, "`Drop` impls cannot be specialized");
7777
match arg {
@@ -217,7 +217,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
217217

218218
if !assumptions_in_impl_context.iter().copied().any(predicate_matches_closure) {
219219
let item_span = tcx.def_span(self_type_did);
220-
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did.to_def_id());
220+
let self_descr = tcx.def_descr(self_type_did.to_def_id());
221221
let reported = struct_span_err!(
222222
tcx.sess,
223223
predicate_sp,

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ fn lint_auto_trait_impl<'tcx>(
531531
}),
532532
|lint| {
533533
let item_span = tcx.def_span(self_type_did);
534-
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
534+
let self_descr = tcx.def_descr(self_type_did);
535535
match arg {
536536
ty::util::NotUniqueParam::DuplicateParam(arg) => {
537537
lint.note(&format!("`{}` is mentioned multiple times", arg));

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
439439

440440
fn create_error_message(&self) -> String {
441441
let def_path = self.tcx.def_path_str(self.def_id);
442-
let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
442+
let def_kind = self.tcx.def_descr(self.def_id);
443443
let (quantifier, bound) = self.get_quantifier_and_bound();
444444
let kind = self.kind();
445445
let provided_lt_args = self.num_provided_lifetime_args();
@@ -990,7 +990,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
990990
};
991991

992992
let msg = {
993-
let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
993+
let def_kind = self.tcx.def_descr(self.def_id);
994994
let (quantifier, bound) = self.get_quantifier_and_bound();
995995

996996
let params = if bound == 0 {

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
671671
&& !self.type_is_sized_modulo_regions(self.param_env, output_ty, callee_expr.span)
672672
{
673673
let descr = match maybe_def {
674-
DefIdOrName::DefId(def_id) => self.tcx.def_kind(def_id).descr(def_id),
674+
DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
675675
DefIdOrName::Name(name) => name,
676676
};
677677
err.span_label(

0 commit comments

Comments
 (0)