1
Fork 0

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 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.
This commit is contained in:
bors 2023-02-23 00:19:12 +00:00
commit 0978711950
160 changed files with 761 additions and 706 deletions

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 @@ impl<'tcx> TyCtxt<'tcx> {
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 @@ impl<'tcx> TyCtxt<'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 @@ impl<'tcx> TyCtxt<'tcx> {
}
(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> {