Skip to content

Commit 0978711

Browse files
committed
Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk
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. For anyone not sure why this is being done, see the Reference definitions of these terms in <https://doc.rust-lang.org/1.67.1/reference/items/associated-items.html#methods> > Associated functions whose first parameter is named `self` are called methods and may be invoked using the [method call operator](https://doc.rust-lang.org/1.67.1/reference/expressions/method-call-expr.html), for example, `x.foo()`, as well as the usual function call notation. In particular, while this means it's technically correct for rustc to refer to a method as an associated function (and there are a few cases where it'll still do so), rustc *must never* use the term "method" to refer to an associated function that does not have a `self` parameter.
2 parents da439d9 + bf9b980 commit 0978711

File tree

160 files changed

+755
-700
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+755
-700
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
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

+3-6
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

+9
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

+1-4
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

+4-4
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ fn opaque_type_cycle_error(
14611461
span,
14621462
format!(
14631463
"{} captures itself here",
1464-
tcx.def_kind(closure_def_id).descr(closure_def_id)
1464+
tcx.def_descr(closure_def_id)
14651465
),
14661466
);
14671467
}

compiler/rustc_hir_analysis/src/check/dropck.rs

+2-2
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

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

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+2-2
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

+1-1
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(

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2448,7 +2448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24482448
return_ty: Option<Ty<'tcx>>,
24492449
) {
24502450
let struct_path = self.tcx().def_path_str(base_did);
2451-
let kind_name = self.tcx().def_kind(base_did).descr(base_did);
2451+
let kind_name = self.tcx().def_descr(base_did);
24522452
let mut err = struct_span_err!(
24532453
self.tcx().sess,
24542454
field.span,

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1938,8 +1938,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19381938
spans.push_span_label(param.span, "");
19391939
}
19401940

1941-
let def_kind = self.tcx.def_kind(def_id);
1942-
err.span_note(spans, &format!("{} defined here", def_kind.descr(def_id)));
1941+
err.span_note(spans, &format!("{} defined here", self.tcx.def_descr(def_id)));
19431942
} else if let Some(hir::Node::Expr(e)) = self.tcx.hir().get_if_local(def_id)
19441943
&& let hir::ExprKind::Closure(hir::Closure { body, .. }) = &e.kind
19451944
{
@@ -1952,10 +1951,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19521951
};
19531952
err.span_note(span, &format!("{} defined here", kind));
19541953
} else {
1955-
let def_kind = self.tcx.def_kind(def_id);
19561954
err.span_note(
19571955
self.tcx.def_span(def_id),
1958-
&format!("{} defined here", def_kind.descr(def_id)),
1956+
&format!("{} defined here", self.tcx.def_descr(def_id)),
19591957
);
19601958
}
19611959
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
121121
DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
122122
DefKind::Ctor(CtorOf::Struct, _) => "construct this tuple struct".to_string(),
123123
DefKind::Ctor(CtorOf::Variant, _) => "construct this tuple variant".to_string(),
124-
kind => format!("call this {}", kind.descr(def_id)),
124+
kind => format!("call this {}", self.tcx.def_kind_descr(kind, def_id)),
125125
},
126126
DefIdOrName::Name(name) => format!("call this {name}"),
127127
};
@@ -340,7 +340,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
340340
CtorOf::Variant => "an enum variant",
341341
}));
342342
} else {
343-
let descr = kind.descr(def_id);
343+
let descr = self.tcx.def_kind_descr(kind, def_id);
344344
err.span_label(sp, format!("{descr} `{name}` defined here"));
345345
}
346346
return true;

compiler/rustc_hir_typeck/src/method/probe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ impl<'tcx> Pick<'tcx> {
13661366
span,
13671367
format!(
13681368
"{} {} with this name may be added to the standard library in the future",
1369-
def_kind.article(),
1370-
def_kind.descr(self.item.def_id),
1369+
tcx.def_kind_descr_article(def_kind, self.item.def_id),
1370+
tcx.def_kind_descr(def_kind, self.item.def_id),
13711371
),
13721372
|lint| {
13731373
match (self.item.kind, self.item.container) {

compiler/rustc_hir_typeck/src/method/suggest.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
160160
}
161161

162162
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
163-
let kind = kind.descr(def_id);
163+
let kind = self.tcx.def_kind_descr(kind, def_id);
164164
let mut err = struct_span_err!(
165165
self.tcx.sess,
166166
item_name.span,
@@ -1062,8 +1062,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10621062
span,
10631063
&format!(
10641064
"there is {} {} with a similar name",
1065-
def_kind.article(),
1066-
def_kind.descr(similar_candidate.def_id),
1065+
self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id),
1066+
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
10671067
),
10681068
similar_candidate.name,
10691069
Applicability::MaybeIncorrect,
@@ -1172,7 +1172,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11721172
path,
11731173
ty,
11741174
item.kind,
1175-
item.def_id,
1175+
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
11761176
sugg_span,
11771177
idx,
11781178
self.tcx.sess.source_map(),
@@ -1208,7 +1208,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12081208
path,
12091209
rcvr_ty,
12101210
item.kind,
1211-
item.def_id,
1211+
self.tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id),
12121212
sugg_span,
12131213
idx,
12141214
self.tcx.sess.source_map(),
@@ -2853,7 +2853,7 @@ fn print_disambiguation_help<'tcx>(
28532853
trait_name: String,
28542854
rcvr_ty: Ty<'_>,
28552855
kind: ty::AssocKind,
2856-
def_id: DefId,
2856+
def_kind_descr: &'static str,
28572857
span: Span,
28582858
candidate: Option<usize>,
28592859
source_map: &source_map::SourceMap,
@@ -2886,7 +2886,7 @@ fn print_disambiguation_help<'tcx>(
28862886
span,
28872887
&format!(
28882888
"disambiguate the {} for {}",
2889-
kind.as_def_kind().descr(def_id),
2889+
def_kind_descr,
28902890
if let Some(candidate) = candidate {
28912891
format!("candidate #{}", candidate)
28922892
} else {

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl InferenceDiagnosticsParentData {
122122
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
123123

124124
Some(InferenceDiagnosticsParentData {
125-
prefix: tcx.def_kind(parent_def_id).descr(parent_def_id),
125+
prefix: tcx.def_descr(parent_def_id),
126126
name: parent_name,
127127
})
128128
}

compiler/rustc_middle/src/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn late_report_deprecation(
255255
let method_span = method_span.unwrap_or(span);
256256
tcx.struct_span_lint_hir(lint, hir_id, method_span, message, |diag| {
257257
if let hir::Node::Expr(_) = tcx.hir().get(hir_id) {
258-
let kind = tcx.def_kind(def_id).descr(def_id);
258+
let kind = tcx.def_descr(def_id);
259259
deprecation_suggestion(diag, kind, suggestion, method_span);
260260
}
261261
diag
@@ -392,7 +392,7 @@ impl<'tcx> TyCtxt<'tcx> {
392392
let lint = deprecation_lint(is_in_effect);
393393
if self.lint_level_at_node(lint, id).0 != Level::Allow {
394394
let def_path = with_no_trimmed_paths!(self.def_path_str(def_id));
395-
let def_kind = self.def_kind(def_id).descr(def_id);
395+
let def_kind = self.def_descr(def_id);
396396

397397
late_report_deprecation(
398398
self,

compiler/rustc_middle/src/ty/context.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,8 @@ impl<'tcx> TyCtxt<'tcx> {
12001200

12011201
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
12021202
pub fn article_and_description(self, def_id: DefId) -> (&'static str, &'static str) {
1203-
match self.def_kind(def_id) {
1204-
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
1205-
rustc_hir::GeneratorKind::Async(..) => ("an", "async closure"),
1206-
rustc_hir::GeneratorKind::Gen => ("a", "generator"),
1207-
},
1208-
def_kind => (def_kind.article(), def_kind.descr(def_id)),
1209-
}
1203+
let kind = self.def_kind(def_id);
1204+
(self.def_kind_descr_article(kind, def_id), self.def_kind_descr(kind, def_id))
12101205
}
12111206

12121207
pub fn type_length_limit(self) -> Limit {

compiler/rustc_middle/src/ty/util.rs

+34
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,40 @@ impl<'tcx> TyCtxt<'tcx> {
761761
}
762762
(generator_layout, generator_saved_local_names)
763763
}
764+
765+
/// Query and get an English description for the item's kind.
766+
pub fn def_descr(self, def_id: DefId) -> &'static str {
767+
self.def_kind_descr(self.def_kind(def_id), def_id)
768+
}
769+
770+
/// Get an English description for the item's kind.
771+
pub fn def_kind_descr(self, def_kind: DefKind, def_id: DefId) -> &'static str {
772+
match def_kind {
773+
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
774+
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
775+
rustc_hir::GeneratorKind::Async(..) => "async closure",
776+
rustc_hir::GeneratorKind::Gen => "generator",
777+
},
778+
_ => def_kind.descr(def_id),
779+
}
780+
}
781+
782+
/// Gets an English article for the [`TyCtxt::def_descr`].
783+
pub fn def_descr_article(self, def_id: DefId) -> &'static str {
784+
self.def_kind_descr_article(self.def_kind(def_id), def_id)
785+
}
786+
787+
/// Gets an English article for the [`TyCtxt::def_kind_descr`].
788+
pub fn def_kind_descr_article(self, def_kind: DefKind, def_id: DefId) -> &'static str {
789+
match def_kind {
790+
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
791+
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
792+
rustc_hir::GeneratorKind::Async(..) => "an",
793+
rustc_hir::GeneratorKind::Gen => "a",
794+
},
795+
_ => def_kind.article(),
796+
}
797+
}
764798
}
765799

766800
struct OpaqueTypeExpander<'tcx> {

compiler/rustc_passes/src/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ impl<'tcx> DeadVisitor<'tcx> {
694694
})
695695
.collect();
696696

697-
let descr = tcx.def_kind(first_id).descr(first_id.to_def_id());
697+
let descr = tcx.def_descr(first_id.to_def_id());
698698
let num = dead_codes.len();
699699
let multiple = num > 6;
700700
let name_list = names.into();
@@ -706,7 +706,7 @@ impl<'tcx> DeadVisitor<'tcx> {
706706
};
707707

708708
let parent_info = if let Some(parent_item) = parent_item {
709-
let parent_descr = tcx.def_kind(parent_item).descr(parent_item.to_def_id());
709+
let parent_descr = tcx.def_descr(parent_item.to_def_id());
710710
Some(ParentInfo {
711711
num,
712712
descr,

0 commit comments

Comments
 (0)