1use core::ops::ControlFlow;
7use std::borrow::Cow;
8use std::path::PathBuf;
9
10use hir::Expr;
11use rustc_ast::ast::Mutability;
12use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
13use rustc_data_structures::sorted_map::SortedMap;
14use rustc_data_structures::unord::UnordSet;
15use rustc_errors::codes::*;
16use rustc_errors::{
17 Applicability, Diag, MultiSpan, StashKey, StringPart, listify, pluralize, struct_span_code_err,
18};
19use rustc_hir::attrs::diagnostic::OnUnimplementedNote;
20use rustc_hir::def::{CtorKind, DefKind, Res};
21use rustc_hir::def_id::DefId;
22use rustc_hir::intravisit::{self, Visitor};
23use rustc_hir::lang_items::LangItem;
24use rustc_hir::{
25 self as hir, ExprKind, HirId, Node, PathSegment, QPath, find_attr, is_range_literal,
26};
27use rustc_infer::infer::{BoundRegionConversionTime, RegionVariableOrigin};
28use rustc_middle::bug;
29use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
30use rustc_middle::ty::print::{
31 PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths,
32 with_no_visible_paths_if_doc_hidden,
33};
34use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
35use rustc_span::def_id::DefIdSet;
36use rustc_span::{
37 DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol, edit_distance,
38 kw, sym,
39};
40use rustc_trait_selection::error_reporting::traits::DefIdOrName;
41use rustc_trait_selection::infer::InferCtxtExt;
42use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
43use rustc_trait_selection::traits::{
44 FulfillmentError, Obligation, ObligationCauseCode, supertraits,
45};
46use tracing::{debug, info, instrument};
47
48use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
49use super::{CandidateSource, MethodError, NoMatchData};
50use crate::errors::{self, CandidateTraitNote, NoAssociatedItem};
51use crate::method::probe::UnsatisfiedPredicates;
52use crate::{Expectation, FnCtxt};
53
54struct TraitBoundDuplicateTracker {
58 trait_def_ids: FxIndexSet<DefId>,
59 seen_ref: FxIndexSet<DefId>,
60 seen_non_ref: FxIndexSet<DefId>,
61 has_ref_dupes: bool,
62}
63
64impl TraitBoundDuplicateTracker {
65 fn new() -> Self {
66 Self {
67 trait_def_ids: FxIndexSet::default(),
68 seen_ref: FxIndexSet::default(),
69 seen_non_ref: FxIndexSet::default(),
70 has_ref_dupes: false,
71 }
72 }
73
74 fn track(&mut self, def_id: DefId, is_ref: bool) {
76 self.trait_def_ids.insert(def_id);
77 if is_ref {
78 if self.seen_non_ref.contains(&def_id) {
79 self.has_ref_dupes = true;
80 }
81 self.seen_ref.insert(def_id);
82 } else {
83 if self.seen_ref.contains(&def_id) {
84 self.has_ref_dupes = true;
85 }
86 self.seen_non_ref.insert(def_id);
87 }
88 }
89
90 fn has_ref_dupes(&self) -> bool {
91 self.has_ref_dupes
92 }
93
94 fn into_trait_def_ids(self) -> FxIndexSet<DefId> {
95 self.trait_def_ids
96 }
97}
98
99impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
100 fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
101 self.autoderef(span, ty)
102 .silence_errors()
103 .any(|(ty, _)| #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Slice(..) | ty::Array(..) => true,
_ => false,
}matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
104 }
105
106 fn impl_into_iterator_should_be_iterator(
107 &self,
108 ty: Ty<'tcx>,
109 span: Span,
110 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
111 ) -> bool {
112 fn predicate_bounds_generic_param<'tcx>(
113 predicate: ty::Predicate<'_>,
114 generics: &'tcx ty::Generics,
115 generic_param: &ty::GenericParamDef,
116 tcx: TyCtxt<'tcx>,
117 ) -> bool {
118 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
119 predicate.kind().as_ref().skip_binder()
120 {
121 let ty::TraitPredicate { trait_ref: ty::TraitRef { args, .. }, .. } = trait_pred;
122 if args.is_empty() {
123 return false;
124 }
125 let Some(arg_ty) = args[0].as_type() else {
126 return false;
127 };
128 let ty::Param(param) = *arg_ty.kind() else {
129 return false;
130 };
131 generic_param.index == generics.type_param(param, tcx).index
133 } else {
134 false
135 }
136 }
137
138 let is_iterator_predicate = |predicate: ty::Predicate<'tcx>| -> bool {
139 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
140 predicate.kind().as_ref().skip_binder()
141 {
142 self.tcx.is_diagnostic_item(sym::Iterator, trait_pred.trait_ref.def_id)
143 && trait_pred.trait_ref.self_ty() == ty
145 } else {
146 false
147 }
148 };
149
150 let Some(into_iterator_trait) = self.tcx.get_diagnostic_item(sym::IntoIterator) else {
152 return false;
153 };
154 let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]);
155 let obligation = Obligation::new(self.tcx, self.misc(span), self.param_env, trait_ref);
156 if !self.predicate_must_hold_modulo_regions(&obligation) {
157 return false;
158 }
159
160 match *ty.peel_refs().kind() {
161 ty::Param(param) => {
162 let generics = self.tcx.generics_of(self.body_id);
163 let generic_param = generics.type_param(param, self.tcx);
164 for unsatisfied in unsatisfied_predicates.iter() {
165 if predicate_bounds_generic_param(
168 unsatisfied.0,
169 generics,
170 generic_param,
171 self.tcx,
172 ) && is_iterator_predicate(unsatisfied.0)
173 {
174 return true;
175 }
176 }
177 }
178 ty::Slice(..) | ty::Adt(..) | ty::Alias(ty::Opaque, _) => {
179 for unsatisfied in unsatisfied_predicates.iter() {
180 if is_iterator_predicate(unsatisfied.0) {
181 return true;
182 }
183 }
184 }
185 _ => return false,
186 }
187 false
188 }
189
190 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("report_method_error",
"rustc_hir_typeck::method::suggest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(190u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["call_id", "rcvr_ty",
"error", "expected", "trait_missing_method"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&call_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&rcvr_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&trait_missing_method
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: ErrorGuaranteed = loop {};
return __tracing_attr_fake_return;
}
{
for &import_id in
self.tcx.in_scope_traits(call_id).into_iter().flatten().flat_map(|c|
c.import_ids) {
self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
}
let (span, expr_span, source, item_name, args) =
match self.tcx.hir_node(call_id) {
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
span, .. }) => {
(segment.ident.span, span, SelfSource::MethodCall(rcvr),
segment.ident, Some(args))
}
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::Path(QPath::TypeRelative(rcvr,
segment)),
span, .. }) |
hir::Node::PatExpr(&hir::PatExpr {
kind: hir::PatExprKind::Path(QPath::TypeRelative(rcvr,
segment)),
span, .. }) |
hir::Node::Pat(&hir::Pat {
kind: hir::PatKind::Struct(QPath::TypeRelative(rcvr,
segment), ..) |
hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr,
segment), ..),
span, .. }) => {
let args =
match self.tcx.parent_hir_node(call_id) {
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::Call(callee, args), .. }) if
callee.hir_id == call_id => Some(args),
_ => None,
};
(segment.ident.span, span, SelfSource::QPath(rcvr),
segment.ident, args)
}
node => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("{0:?}", node)));
}
};
let within_macro_span =
span.within_macro(expr_span, self.tcx.sess.source_map());
if let Err(guar) = rcvr_ty.error_reported() { return guar; }
match error {
MethodError::NoMatch(mut no_match_data) =>
self.report_no_match_method_error(span, rcvr_ty, item_name,
call_id, source, args, expr_span, &mut no_match_data,
expected, trait_missing_method, within_macro_span),
MethodError::Ambiguity(mut sources) => {
let mut err =
{
self.dcx().struct_span_err(item_name.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("multiple applicable items in scope"))
})).with_code(E0034)
};
err.span_label(item_name.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("multiple `{0}` found",
item_name))
}));
if let Some(within_macro_span) = within_macro_span {
err.span_label(within_macro_span,
"due to this macro variable");
}
self.note_candidates_on_method_error(rcvr_ty, item_name,
source, args, span, &mut err, &mut sources,
Some(expr_span));
err.emit()
}
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits)
=> {
let kind = self.tcx.def_kind_descr(kind, def_id);
let mut err =
{
self.dcx().struct_span_err(item_name.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` is private",
kind, item_name))
})).with_code(E0624)
};
err.span_label(item_name.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("private {0}", kind))
}));
let sp =
self.tcx.hir_span_if_local(def_id).unwrap_or_else(||
self.tcx.def_span(def_id));
err.span_label(sp,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("private {0} defined here",
kind))
}));
if let Some(within_macro_span) = within_macro_span {
err.span_label(within_macro_span,
"due to this macro variable");
}
self.suggest_valid_traits(&mut err, item_name,
out_of_scope_traits, true);
self.suggest_unwrapping_inner_self(&mut err, source,
rcvr_ty, item_name);
err.emit()
}
MethodError::IllegalSizedBound {
candidates, needs_mut, bound_span, self_expr } => {
let msg =
if needs_mut {
{
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the `{0}` method cannot be invoked on `{1}`",
item_name, rcvr_ty))
})
}
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the `{0}` method cannot be invoked on a trait object",
item_name))
})
};
let mut err = self.dcx().struct_span_err(span, msg);
if !needs_mut {
err.span_label(bound_span,
"this has a `Sized` requirement");
}
if let Some(within_macro_span) = within_macro_span {
err.span_label(within_macro_span,
"due to this macro variable");
}
if !candidates.is_empty() {
let help =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}other candidate{1} {2} found in the following trait{1}",
if candidates.len() == 1 { "an" } else { "" },
if candidates.len() == 1 { "" } else { "s" },
if candidates.len() == 1 { "was" } else { "were" }))
});
self.suggest_use_candidates(candidates,
|accessible_sugg, inaccessible_sugg, span|
{
let suggest_for_access =
|err: &mut Diag<'_>, mut msg: String, sugg: Vec<_>|
{
msg +=
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", perhaps add a `use` for {0}:",
if sugg.len() == 1 { "it" } else { "one_of_them" }))
});
err.span_suggestions(span, msg, sugg,
Applicability::MaybeIncorrect);
};
let suggest_for_privacy =
|err: &mut Diag<'_>, mut msg: String, suggs: Vec<String>|
{
if let [sugg] = suggs.as_slice() {
err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("trait `{0}` provides `{1}` is implemented but not reachable",
sugg.trim(), item_name))
}));
} else {
msg +=
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" but {0} not reachable",
if suggs.len() == 1 { "is" } else { "are" }))
});
err.span_suggestions(span, msg, suggs,
Applicability::MaybeIncorrect);
}
};
if accessible_sugg.is_empty() {
suggest_for_privacy(&mut err, help, inaccessible_sugg);
} else if inaccessible_sugg.is_empty() {
suggest_for_access(&mut err, help, accessible_sugg);
} else {
suggest_for_access(&mut err, help.clone(), accessible_sugg);
suggest_for_privacy(&mut err, help, inaccessible_sugg);
}
});
}
if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind()
{
if needs_mut {
let trait_type =
Ty::new_ref(self.tcx, *region, *t_type,
mutability.invert());
let msg =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you need `{0}` instead of `{1}`",
trait_type, rcvr_ty))
});
let mut kind = &self_expr.kind;
while let hir::ExprKind::AddrOf(_, _, expr) |
hir::ExprKind::Unary(hir::UnOp::Deref, expr) = kind {
kind = &expr.kind;
}
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path))
= kind && let hir::def::Res::Local(hir_id) = path.res &&
let hir::Node::Pat(b) = self.tcx.hir_node(hir_id) &&
let hir::Node::Param(p) = self.tcx.parent_hir_node(b.hir_id)
&&
let Some(decl) =
self.tcx.parent_hir_node(p.hir_id).fn_decl() &&
let Some(ty) =
decl.inputs.iter().find(|ty| ty.span == p.ty_span) &&
let hir::TyKind::Ref(_, mut_ty) = &ty.kind &&
let hir::Mutability::Not = mut_ty.mutbl {
err.span_suggestion_verbose(mut_ty.ty.span.shrink_to_lo(),
msg, "mut ", Applicability::MachineApplicable);
} else { err.help(msg); }
}
}
err.emit()
}
MethodError::ErrorReported(guar) => guar,
MethodError::BadReturnType =>
::rustc_middle::util::bug::bug_fmt(format_args!("no return type expectations but got BadReturnType")),
}
}
}
}#[instrument(level = "debug", skip(self))]
191 pub(crate) fn report_method_error(
192 &self,
193 call_id: HirId,
194 rcvr_ty: Ty<'tcx>,
195 error: MethodError<'tcx>,
196 expected: Expectation<'tcx>,
197 trait_missing_method: bool,
198 ) -> ErrorGuaranteed {
199 for &import_id in
202 self.tcx.in_scope_traits(call_id).into_iter().flatten().flat_map(|c| c.import_ids)
203 {
204 self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
205 }
206
207 let (span, expr_span, source, item_name, args) = match self.tcx.hir_node(call_id) {
208 hir::Node::Expr(&hir::Expr {
209 kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
210 span,
211 ..
212 }) => {
213 (segment.ident.span, span, SelfSource::MethodCall(rcvr), segment.ident, Some(args))
214 }
215 hir::Node::Expr(&hir::Expr {
216 kind: hir::ExprKind::Path(QPath::TypeRelative(rcvr, segment)),
217 span,
218 ..
219 })
220 | hir::Node::PatExpr(&hir::PatExpr {
221 kind: hir::PatExprKind::Path(QPath::TypeRelative(rcvr, segment)),
222 span,
223 ..
224 })
225 | hir::Node::Pat(&hir::Pat {
226 kind:
227 hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
228 | hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..),
229 span,
230 ..
231 }) => {
232 let args = match self.tcx.parent_hir_node(call_id) {
233 hir::Node::Expr(&hir::Expr {
234 kind: hir::ExprKind::Call(callee, args), ..
235 }) if callee.hir_id == call_id => Some(args),
236 _ => None,
237 };
238 (segment.ident.span, span, SelfSource::QPath(rcvr), segment.ident, args)
239 }
240 node => unreachable!("{node:?}"),
241 };
242
243 let within_macro_span = span.within_macro(expr_span, self.tcx.sess.source_map());
246
247 if let Err(guar) = rcvr_ty.error_reported() {
249 return guar;
250 }
251
252 match error {
253 MethodError::NoMatch(mut no_match_data) => self.report_no_match_method_error(
254 span,
255 rcvr_ty,
256 item_name,
257 call_id,
258 source,
259 args,
260 expr_span,
261 &mut no_match_data,
262 expected,
263 trait_missing_method,
264 within_macro_span,
265 ),
266
267 MethodError::Ambiguity(mut sources) => {
268 let mut err = struct_span_code_err!(
269 self.dcx(),
270 item_name.span,
271 E0034,
272 "multiple applicable items in scope"
273 );
274 err.span_label(item_name.span, format!("multiple `{item_name}` found"));
275 if let Some(within_macro_span) = within_macro_span {
276 err.span_label(within_macro_span, "due to this macro variable");
277 }
278
279 self.note_candidates_on_method_error(
280 rcvr_ty,
281 item_name,
282 source,
283 args,
284 span,
285 &mut err,
286 &mut sources,
287 Some(expr_span),
288 );
289 err.emit()
290 }
291
292 MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
293 let kind = self.tcx.def_kind_descr(kind, def_id);
294 let mut err = struct_span_code_err!(
295 self.dcx(),
296 item_name.span,
297 E0624,
298 "{} `{}` is private",
299 kind,
300 item_name
301 );
302 err.span_label(item_name.span, format!("private {kind}"));
303 let sp =
304 self.tcx.hir_span_if_local(def_id).unwrap_or_else(|| self.tcx.def_span(def_id));
305 err.span_label(sp, format!("private {kind} defined here"));
306 if let Some(within_macro_span) = within_macro_span {
307 err.span_label(within_macro_span, "due to this macro variable");
308 }
309 self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
310 self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
311 err.emit()
312 }
313
314 MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
315 let msg = if needs_mut {
316 with_forced_trimmed_paths!(format!(
317 "the `{item_name}` method cannot be invoked on `{rcvr_ty}`"
318 ))
319 } else {
320 format!("the `{item_name}` method cannot be invoked on a trait object")
321 };
322 let mut err = self.dcx().struct_span_err(span, msg);
323 if !needs_mut {
324 err.span_label(bound_span, "this has a `Sized` requirement");
325 }
326 if let Some(within_macro_span) = within_macro_span {
327 err.span_label(within_macro_span, "due to this macro variable");
328 }
329 if !candidates.is_empty() {
330 let help = format!(
331 "{an}other candidate{s} {were} found in the following trait{s}",
332 an = if candidates.len() == 1 { "an" } else { "" },
333 s = pluralize!(candidates.len()),
334 were = pluralize!("was", candidates.len()),
335 );
336 self.suggest_use_candidates(
337 candidates,
338 |accessible_sugg, inaccessible_sugg, span| {
339 let suggest_for_access =
340 |err: &mut Diag<'_>, mut msg: String, sugg: Vec<_>| {
341 msg += &format!(
342 ", perhaps add a `use` for {one_of_them}:",
343 one_of_them =
344 if sugg.len() == 1 { "it" } else { "one_of_them" },
345 );
346 err.span_suggestions(
347 span,
348 msg,
349 sugg,
350 Applicability::MaybeIncorrect,
351 );
352 };
353 let suggest_for_privacy =
354 |err: &mut Diag<'_>, mut msg: String, suggs: Vec<String>| {
355 if let [sugg] = suggs.as_slice() {
356 err.help(format!("\
357 trait `{}` provides `{item_name}` is implemented but not reachable",
358 sugg.trim(),
359 ));
360 } else {
361 msg += &format!(" but {} not reachable", pluralize!("is", suggs.len()));
362 err.span_suggestions(
363 span,
364 msg,
365 suggs,
366 Applicability::MaybeIncorrect,
367 );
368 }
369 };
370 if accessible_sugg.is_empty() {
371 suggest_for_privacy(&mut err, help, inaccessible_sugg);
373 } else if inaccessible_sugg.is_empty() {
374 suggest_for_access(&mut err, help, accessible_sugg);
375 } else {
376 suggest_for_access(&mut err, help.clone(), accessible_sugg);
377 suggest_for_privacy(&mut err, help, inaccessible_sugg);
378 }
379 },
380 );
381 }
382 if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind() {
383 if needs_mut {
384 let trait_type =
385 Ty::new_ref(self.tcx, *region, *t_type, mutability.invert());
386 let msg = format!("you need `{trait_type}` instead of `{rcvr_ty}`");
387 let mut kind = &self_expr.kind;
388 while let hir::ExprKind::AddrOf(_, _, expr)
389 | hir::ExprKind::Unary(hir::UnOp::Deref, expr) = kind
390 {
391 kind = &expr.kind;
392 }
393 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = kind
394 && let hir::def::Res::Local(hir_id) = path.res
395 && let hir::Node::Pat(b) = self.tcx.hir_node(hir_id)
396 && let hir::Node::Param(p) = self.tcx.parent_hir_node(b.hir_id)
397 && let Some(decl) = self.tcx.parent_hir_node(p.hir_id).fn_decl()
398 && let Some(ty) = decl.inputs.iter().find(|ty| ty.span == p.ty_span)
399 && let hir::TyKind::Ref(_, mut_ty) = &ty.kind
400 && let hir::Mutability::Not = mut_ty.mutbl
401 {
402 err.span_suggestion_verbose(
403 mut_ty.ty.span.shrink_to_lo(),
404 msg,
405 "mut ",
406 Applicability::MachineApplicable,
407 );
408 } else {
409 err.help(msg);
410 }
411 }
412 }
413 err.emit()
414 }
415
416 MethodError::ErrorReported(guar) => guar,
417
418 MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
419 }
420 }
421
422 fn create_missing_writer_err(
423 &self,
424 rcvr_ty: Ty<'tcx>,
425 rcvr_expr: &hir::Expr<'tcx>,
426 mut long_ty_path: Option<PathBuf>,
427 ) -> Diag<'_> {
428 let mut err = {
self.dcx().struct_span_err(rcvr_expr.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot write into `{0}`",
self.tcx.short_string(rcvr_ty, &mut long_ty_path)))
})).with_code(E0599)
}struct_span_code_err!(
429 self.dcx(),
430 rcvr_expr.span,
431 E0599,
432 "cannot write into `{}`",
433 self.tcx.short_string(rcvr_ty, &mut long_ty_path),
434 );
435 *err.long_ty_path() = long_ty_path;
436 err.span_note(
437 rcvr_expr.span,
438 "must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method",
439 );
440 if let ExprKind::Lit(_) = rcvr_expr.kind {
441 err.span_help(
442 rcvr_expr.span.shrink_to_lo(),
443 "a writer is needed before this format string",
444 );
445 };
446 err
447 }
448
449 fn create_no_assoc_err(
450 &self,
451 rcvr_ty: Ty<'tcx>,
452 item_ident: Ident,
453 item_kind: &'static str,
454 trait_missing_method: bool,
455 source: SelfSource<'tcx>,
456 is_method: bool,
457 sugg_span: Span,
458 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
459 ) -> Diag<'_> {
460 let mut ty = rcvr_ty;
463 let span = item_ident.span;
464 if let ty::Adt(def, generics) = rcvr_ty.kind() {
465 if generics.len() > 0 {
466 let mut autoderef = self.autoderef(span, rcvr_ty).silence_errors();
467 let candidate_found = autoderef.any(|(ty, _)| {
468 if let ty::Adt(adt_def, _) = ty.kind() {
469 self.tcx
470 .inherent_impls(adt_def.did())
471 .into_iter()
472 .any(|def_id| self.associated_value(*def_id, item_ident).is_some())
473 } else {
474 false
475 }
476 });
477 let has_deref = autoderef.step_count() > 0;
478 if !candidate_found && !has_deref && unsatisfied_predicates.is_empty() {
479 ty = self.tcx.at(span).type_of(def.did()).instantiate_identity();
480 }
481 }
482 }
483
484 let mut err = self.dcx().create_err(NoAssociatedItem {
485 span,
486 item_kind,
487 item_ident,
488 ty_prefix: if trait_missing_method {
489 Cow::from("trait")
491 } else {
492 rcvr_ty.prefix_string(self.tcx)
493 },
494 ty,
495 trait_missing_method,
496 });
497
498 if is_method {
499 self.suggest_use_shadowed_binding_with_method(source, item_ident, rcvr_ty, &mut err);
500 }
501
502 let tcx = self.tcx;
503 if let SelfSource::QPath(ty) = source
505 && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
506 && let Res::SelfTyAlias { alias_to: impl_def_id, .. } = path.res
507 && let DefKind::Impl { .. } = self.tcx.def_kind(impl_def_id)
508 && let Some(candidate) = tcx.associated_items(impl_def_id).find_by_ident_and_kind(
509 self.tcx,
510 item_ident,
511 ty::AssocTag::Type,
512 impl_def_id,
513 )
514 && let Some(adt_def) = tcx.type_of(candidate.def_id).skip_binder().ty_adt_def()
515 && adt_def.is_struct()
516 && adt_def.non_enum_variant().ctor_kind() == Some(CtorKind::Fn)
517 {
518 let def_path = tcx.def_path_str(adt_def.did());
519 err.span_suggestion(
520 sugg_span,
521 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to construct a value of type `{0}`, use the explicit path",
def_path))
})format!("to construct a value of type `{}`, use the explicit path", def_path),
522 def_path,
523 Applicability::MachineApplicable,
524 );
525 }
526
527 err
528 }
529
530 fn suggest_use_shadowed_binding_with_method(
531 &self,
532 self_source: SelfSource<'tcx>,
533 method_name: Ident,
534 ty: Ty<'tcx>,
535 err: &mut Diag<'_>,
536 ) {
537 #[derive(#[automatically_derived]
impl ::core::fmt::Debug for LetStmt {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f, "LetStmt",
"ty_hir_id_opt", &self.ty_hir_id_opt, "binding_id",
&self.binding_id, "span", &self.span, "init_hir_id",
&&self.init_hir_id)
}
}Debug)]
538 struct LetStmt {
539 ty_hir_id_opt: Option<hir::HirId>,
540 binding_id: hir::HirId,
541 span: Span,
542 init_hir_id: hir::HirId,
543 }
544
545 struct LetVisitor<'a, 'tcx> {
555 binding_name: Symbol,
557 binding_id: hir::HirId,
558 fcx: &'a FnCtxt<'a, 'tcx>,
560 call_expr: &'tcx Expr<'tcx>,
561 method_name: Ident,
562 sugg_let: Option<LetStmt>,
564 }
565
566 impl<'a, 'tcx> LetVisitor<'a, 'tcx> {
567 fn is_sub_scope(&self, sub_id: hir::ItemLocalId, super_id: hir::ItemLocalId) -> bool {
569 let scope_tree = self.fcx.tcx.region_scope_tree(self.fcx.body_id);
570 if let Some(sub_var_scope) = scope_tree.var_scope(sub_id)
571 && let Some(super_var_scope) = scope_tree.var_scope(super_id)
572 && scope_tree.is_subscope_of(sub_var_scope, super_var_scope)
573 {
574 return true;
575 }
576 false
577 }
578
579 fn check_and_add_sugg_binding(&mut self, binding: LetStmt) -> bool {
582 if !self.is_sub_scope(self.binding_id.local_id, binding.binding_id.local_id) {
583 return false;
584 }
585
586 if let Some(ty_hir_id) = binding.ty_hir_id_opt
588 && let Some(tyck_ty) = self.fcx.node_ty_opt(ty_hir_id)
589 {
590 if self
591 .fcx
592 .lookup_probe_for_diagnostic(
593 self.method_name,
594 tyck_ty,
595 self.call_expr,
596 ProbeScope::TraitsInScope,
597 None,
598 )
599 .is_ok()
600 {
601 self.sugg_let = Some(binding);
602 return true;
603 } else {
604 return false;
605 }
606 }
607
608 if let Some(self_ty) = self.fcx.node_ty_opt(binding.init_hir_id)
613 && self
614 .fcx
615 .lookup_probe_for_diagnostic(
616 self.method_name,
617 self_ty,
618 self.call_expr,
619 ProbeScope::TraitsInScope,
620 None,
621 )
622 .is_ok()
623 {
624 self.sugg_let = Some(binding);
625 return true;
626 }
627 return false;
628 }
629 }
630
631 impl<'v> Visitor<'v> for LetVisitor<'_, '_> {
632 type Result = ControlFlow<()>;
633 fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
634 if let hir::StmtKind::Let(&hir::LetStmt { pat, ty, init, .. }) = ex.kind
635 && let hir::PatKind::Binding(_, binding_id, binding_name, ..) = pat.kind
636 && let Some(init) = init
637 && binding_name.name == self.binding_name
638 && binding_id != self.binding_id
639 {
640 if self.check_and_add_sugg_binding(LetStmt {
641 ty_hir_id_opt: ty.map(|ty| ty.hir_id),
642 binding_id,
643 span: pat.span,
644 init_hir_id: init.hir_id,
645 }) {
646 return ControlFlow::Break(());
647 }
648 ControlFlow::Continue(())
649 } else {
650 hir::intravisit::walk_stmt(self, ex)
651 }
652 }
653
654 fn visit_pat(&mut self, p: &'v hir::Pat<'v>) -> Self::Result {
658 match p.kind {
659 hir::PatKind::Binding(_, binding_id, binding_name, _) => {
660 if binding_name.name == self.binding_name && binding_id == self.binding_id {
661 return ControlFlow::Break(());
662 }
663 }
664 _ => {
665 let _ = intravisit::walk_pat(self, p);
666 }
667 }
668 ControlFlow::Continue(())
669 }
670 }
671
672 if let SelfSource::MethodCall(rcvr) = self_source
673 && let hir::ExprKind::Path(QPath::Resolved(_, path)) = rcvr.kind
674 && let hir::def::Res::Local(recv_id) = path.res
675 && let Some(segment) = path.segments.first()
676 {
677 let body = self.tcx.hir_body_owned_by(self.body_id);
678
679 if let Node::Expr(call_expr) = self.tcx.parent_hir_node(rcvr.hir_id) {
680 let mut let_visitor = LetVisitor {
681 fcx: self,
682 call_expr,
683 binding_name: segment.ident.name,
684 binding_id: recv_id,
685 method_name,
686 sugg_let: None,
687 };
688 let _ = let_visitor.visit_body(&body);
689 if let Some(sugg_let) = let_visitor.sugg_let
690 && let Some(self_ty) = self.node_ty_opt(sugg_let.init_hir_id)
691 {
692 let _sm = self.infcx.tcx.sess.source_map();
693 let rcvr_name = segment.ident.name;
694 let mut span = MultiSpan::from_span(sugg_let.span);
695 span.push_span_label(sugg_let.span,
696 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` of type `{1}` that has method `{2}` defined earlier here",
rcvr_name, self_ty, method_name))
})format!("`{rcvr_name}` of type `{self_ty}` that has method `{method_name}` defined earlier here"));
697
698 let ty = self.tcx.short_string(ty, err.long_ty_path());
699 span.push_span_label(
700 self.tcx.hir_span(recv_id),
701 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("earlier `{0}` shadowed here with type `{1}`",
rcvr_name, ty))
})format!("earlier `{rcvr_name}` shadowed here with type `{ty}`"),
702 );
703 err.span_note(
704 span,
705 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there\'s an earlier shadowed binding `{0}` of type `{1}` that has method `{2}` available",
rcvr_name, self_ty, method_name))
})format!(
706 "there's an earlier shadowed binding `{rcvr_name}` of type `{self_ty}` \
707 that has method `{method_name}` available"
708 ),
709 );
710 }
711 }
712 }
713 }
714
715 fn suggest_method_call_annotation(
716 &self,
717 err: &mut Diag<'_>,
718 span: Span,
719 rcvr_ty: Ty<'tcx>,
720 item_ident: Ident,
721 mode: Mode,
722 source: SelfSource<'tcx>,
723 expected: Expectation<'tcx>,
724 ) {
725 if let Mode::MethodCall = mode
726 && let SelfSource::MethodCall(cal) = source
727 {
728 self.suggest_await_before_method(
729 err,
730 item_ident,
731 rcvr_ty,
732 cal,
733 span,
734 expected.only_has_type(self),
735 );
736 }
737
738 self.suggest_on_pointer_type(err, source, rcvr_ty, item_ident);
739
740 if let SelfSource::MethodCall(rcvr_expr) = source {
741 self.suggest_fn_call(err, rcvr_expr, rcvr_ty, |output_ty| {
742 let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id));
743 let probe = self.lookup_probe_for_diagnostic(
744 item_ident,
745 output_ty,
746 call_expr,
747 ProbeScope::AllTraits,
748 expected.only_has_type(self),
749 );
750 probe.is_ok()
751 });
752 self.note_internal_mutation_in_method(
753 err,
754 rcvr_expr,
755 expected.to_option(self),
756 rcvr_ty,
757 );
758 }
759 }
760
761 fn suggest_static_method_candidates(
762 &self,
763 err: &mut Diag<'_>,
764 span: Span,
765 rcvr_ty: Ty<'tcx>,
766 item_ident: Ident,
767 source: SelfSource<'tcx>,
768 args: Option<&'tcx [hir::Expr<'tcx>]>,
769 sugg_span: Span,
770 no_match_data: &NoMatchData<'tcx>,
771 ) -> Vec<CandidateSource> {
772 let mut static_candidates = no_match_data.static_candidates.clone();
773
774 static_candidates.dedup();
778
779 if !static_candidates.is_empty() {
780 err.note(
781 "found the following associated functions; to be used as methods, \
782 functions must have a `self` parameter",
783 );
784 err.span_label(span, "this is an associated function, not a method");
785 }
786 if static_candidates.len() == 1 {
787 self.suggest_associated_call_syntax(
788 err,
789 &static_candidates,
790 rcvr_ty,
791 source,
792 item_ident,
793 args,
794 sugg_span,
795 );
796 self.note_candidates_on_method_error(
797 rcvr_ty,
798 item_ident,
799 source,
800 args,
801 span,
802 err,
803 &mut static_candidates,
804 None,
805 );
806 } else if static_candidates.len() > 1 {
807 self.note_candidates_on_method_error(
808 rcvr_ty,
809 item_ident,
810 source,
811 args,
812 span,
813 err,
814 &mut static_candidates,
815 Some(sugg_span),
816 );
817 }
818 static_candidates
819 }
820
821 fn suggest_unsatisfied_ty_or_trait(
822 &self,
823 err: &mut Diag<'_>,
824 span: Span,
825 rcvr_ty: Ty<'tcx>,
826 item_ident: Ident,
827 item_kind: &str,
828 source: SelfSource<'tcx>,
829 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
830 static_candidates: &[CandidateSource],
831 ) -> Result<(bool, bool, bool, bool, SortedMap<Span, Vec<String>>), ()> {
832 let mut restrict_type_params = false;
833 let mut suggested_derive = false;
834 let mut unsatisfied_bounds = false;
835 let mut custom_span_label = !static_candidates.is_empty();
836 let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
837 let tcx = self.tcx;
838
839 if item_ident.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
840 let msg = "consider using `len` instead";
841 if let SelfSource::MethodCall(_expr) = source {
842 err.span_suggestion_short(span, msg, "len", Applicability::MachineApplicable);
843 } else {
844 err.span_label(span, msg);
845 }
846 if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
847 let iterator_trait = self.tcx.def_path_str(iterator_trait);
848 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`count` is defined on `{0}`, which `{1}` does not implement",
iterator_trait, rcvr_ty))
})format!(
849 "`count` is defined on `{iterator_trait}`, which `{rcvr_ty}` does not implement"
850 ));
851 }
852 } else if self.impl_into_iterator_should_be_iterator(rcvr_ty, span, unsatisfied_predicates)
853 {
854 err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is not an iterator",
rcvr_ty))
})format!("`{rcvr_ty}` is not an iterator"));
855 if !span.in_external_macro(self.tcx.sess.source_map()) {
856 err.multipart_suggestion(
857 "call `.into_iter()` first",
858 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("into_iter()."))
}))]))vec![(span.shrink_to_lo(), format!("into_iter()."))],
859 Applicability::MaybeIncorrect,
860 );
861 }
862 return Err(());
864 } else if !unsatisfied_predicates.is_empty() {
865 if #[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.kind() {
ty::Param(_) => true,
_ => false,
}matches!(rcvr_ty.kind(), ty::Param(_)) {
866 } else {
877 self.handle_unsatisfied_predicates(
878 err,
879 rcvr_ty,
880 item_ident,
881 item_kind,
882 span,
883 unsatisfied_predicates,
884 &mut restrict_type_params,
885 &mut suggested_derive,
886 &mut unsatisfied_bounds,
887 &mut custom_span_label,
888 &mut bound_spans,
889 );
890 }
891 } else if let ty::Adt(def, targs) = rcvr_ty.kind()
892 && let SelfSource::MethodCall(rcvr_expr) = source
893 {
894 if targs.len() == 1 {
898 let mut item_segment = hir::PathSegment::invalid();
899 item_segment.ident = item_ident;
900 for t in [Ty::new_mut_ref, Ty::new_imm_ref, |_, _, t| t] {
901 let new_args =
902 tcx.mk_args_from_iter(targs.iter().map(|arg| match arg.as_type() {
903 Some(ty) => ty::GenericArg::from(t(
904 tcx,
905 tcx.lifetimes.re_erased,
906 ty.peel_refs(),
907 )),
908 _ => arg,
909 }));
910 let rcvr_ty = Ty::new_adt(tcx, *def, new_args);
911 if let Ok(method) = self.lookup_method_for_diagnostic(
912 rcvr_ty,
913 &item_segment,
914 span,
915 tcx.parent_hir_node(rcvr_expr.hir_id).expect_expr(),
916 rcvr_expr,
917 ) {
918 err.span_note(
919 tcx.def_span(method.def_id),
920 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is available for `{1}`",
item_kind, rcvr_ty))
})format!("{item_kind} is available for `{rcvr_ty}`"),
921 );
922 }
923 }
924 }
925 }
926 Ok((
927 restrict_type_params,
928 suggested_derive,
929 unsatisfied_bounds,
930 custom_span_label,
931 bound_spans,
932 ))
933 }
934
935 fn suggest_surround_method_call(
936 &self,
937 err: &mut Diag<'_>,
938 span: Span,
939 rcvr_ty: Ty<'tcx>,
940 item_ident: Ident,
941 source: SelfSource<'tcx>,
942 similar_candidate: &Option<ty::AssocItem>,
943 ) -> bool {
944 match source {
945 SelfSource::MethodCall(expr) => {
948 !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, err)
949 && similar_candidate.is_none()
950 }
951 _ => true,
952 }
953 }
954
955 fn find_possible_candidates_for_method(
956 &self,
957 err: &mut Diag<'_>,
958 span: Span,
959 rcvr_ty: Ty<'tcx>,
960 item_ident: Ident,
961 item_kind: &str,
962 mode: Mode,
963 source: SelfSource<'tcx>,
964 no_match_data: &NoMatchData<'tcx>,
965 expected: Expectation<'tcx>,
966 should_label_not_found: bool,
967 custom_span_label: bool,
968 ) {
969 let mut find_candidate_for_method = false;
970 let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
971
972 if should_label_not_found && !custom_span_label {
973 self.set_not_found_span_label(
974 err,
975 rcvr_ty,
976 item_ident,
977 item_kind,
978 mode,
979 source,
980 span,
981 unsatisfied_predicates,
982 &mut find_candidate_for_method,
983 );
984 }
985 if !find_candidate_for_method {
986 self.lookup_segments_chain_for_no_match_method(
987 err,
988 item_ident,
989 item_kind,
990 source,
991 no_match_data,
992 );
993 }
994
995 if unsatisfied_predicates.is_empty() {
998 self.suggest_calling_method_on_field(
999 err,
1000 source,
1001 span,
1002 rcvr_ty,
1003 item_ident,
1004 expected.only_has_type(self),
1005 );
1006 }
1007 }
1008
1009 fn suggest_confusable_or_similarly_named_method(
1010 &self,
1011 err: &mut Diag<'_>,
1012 span: Span,
1013 rcvr_ty: Ty<'tcx>,
1014 item_ident: Ident,
1015 mode: Mode,
1016 args: Option<&'tcx [hir::Expr<'tcx>]>,
1017 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1018 similar_candidate: Option<ty::AssocItem>,
1019 ) {
1020 let confusable_suggested = self.confusable_method_name(
1021 err,
1022 rcvr_ty,
1023 item_ident,
1024 args.map(|args| {
1025 args.iter()
1026 .map(|expr| {
1027 self.node_ty_opt(expr.hir_id).unwrap_or_else(|| self.next_ty_var(expr.span))
1028 })
1029 .collect()
1030 }),
1031 );
1032 if let Some(similar_candidate) = similar_candidate {
1033 if unsatisfied_predicates.is_empty()
1036 && Some(similar_candidate.name()) != confusable_suggested
1038 && !span.from_expansion()
1040 {
1041 self.find_likely_intended_associated_item(err, similar_candidate, span, args, mode);
1042 }
1043 }
1044 }
1045
1046 fn suggest_method_not_found_because_of_unsatisfied_bounds(
1047 &self,
1048 err: &mut Diag<'_>,
1049 rcvr_ty: Ty<'tcx>,
1050 item_ident: Ident,
1051 item_kind: &str,
1052 bound_spans: SortedMap<Span, Vec<String>>,
1053 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1054 ) {
1055 let mut ty_span = match rcvr_ty.kind() {
1056 ty::Param(param_type) => {
1057 Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id()))
1058 }
1059 ty::Adt(def, _) if def.did().is_local() => Some(self.tcx.def_span(def.did())),
1060 _ => None,
1061 };
1062 let rcvr_ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1063 let mut tracker = TraitBoundDuplicateTracker::new();
1064 for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
1065 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1066 predicate.kind().skip_binder()
1067 && let self_ty = pred.trait_ref.self_ty()
1068 && self_ty.peel_refs() == rcvr_ty
1069 {
1070 let is_ref = #[allow(non_exhaustive_omitted_patterns)] match self_ty.kind() {
ty::Ref(..) => true,
_ => false,
}matches!(self_ty.kind(), ty::Ref(..));
1071 tracker.track(pred.trait_ref.def_id, is_ref);
1072 }
1073 }
1074 let has_ref_dupes = tracker.has_ref_dupes();
1075 let mut missing_trait_names = tracker
1076 .into_trait_def_ids()
1077 .into_iter()
1078 .map(|def_id| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`",
self.tcx.def_path_str(def_id)))
})format!("`{}`", self.tcx.def_path_str(def_id)))
1079 .collect::<Vec<_>>();
1080 missing_trait_names.sort();
1081 let should_condense =
1082 has_ref_dupes && missing_trait_names.len() > 1 && #[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.kind() {
ty::Adt(..) => true,
_ => false,
}matches!(rcvr_ty.kind(), ty::Adt(..));
1083 let missing_trait_list = if should_condense {
1084 Some(match missing_trait_names.as_slice() {
1085 [only] => only.clone(),
1086 [first, second] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} or {1}", first, second))
})format!("{first} or {second}"),
1087 [rest @ .., last] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} or {1}", rest.join(", "),
last))
})format!("{} or {last}", rest.join(", ")),
1088 [] => String::new(),
1089 })
1090 } else {
1091 None
1092 };
1093 for (span, mut bounds) in bound_spans {
1094 if !self.tcx.sess.source_map().is_span_accessible(span) {
1095 continue;
1096 }
1097 bounds.sort();
1098 bounds.dedup();
1099 let is_ty_span = Some(span) == ty_span;
1100 if is_ty_span && should_condense {
1101 ty_span.take();
1102 let label = if let Some(missing_trait_list) = &missing_trait_list {
1103 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} `{2}` not found for this {0} because `{3}` doesn\'t implement {4}",
rcvr_ty.prefix_string(self.tcx), item_kind, item_ident,
rcvr_ty_str, missing_trait_list))
})format!(
1104 "{item_kind} `{item_ident}` not found for this {} because `{rcvr_ty_str}` doesn't implement {missing_trait_list}",
1105 rcvr_ty.prefix_string(self.tcx)
1106 )
1107 } else {
1108 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} `{2}` not found for this {0}",
rcvr_ty.prefix_string(self.tcx), item_kind, item_ident))
})format!(
1109 "{item_kind} `{item_ident}` not found for this {}",
1110 rcvr_ty.prefix_string(self.tcx)
1111 )
1112 };
1113 err.span_label(span, label);
1114 continue;
1115 }
1116 let pre = if is_ty_span {
1117 ty_span.take();
1118 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} `{2}` not found for this {0} because it ",
rcvr_ty.prefix_string(self.tcx), item_kind, item_ident))
})format!(
1119 "{item_kind} `{item_ident}` not found for this {} because it ",
1120 rcvr_ty.prefix_string(self.tcx)
1121 )
1122 } else {
1123 String::new()
1124 };
1125 let msg = match &bounds[..] {
1126 [bound] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}doesn\'t satisfy {1}", pre,
bound))
})format!("{pre}doesn't satisfy {bound}"),
1127 bounds if bounds.len() > 4 => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("doesn\'t satisfy {0} bounds",
bounds.len()))
})format!("doesn't satisfy {} bounds", bounds.len()),
1128 [bounds @ .., last] => {
1129 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}doesn\'t satisfy {0} or {2}",
bounds.join(", "), pre, last))
})format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
1130 }
1131 [] => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1132 };
1133 err.span_label(span, msg);
1134 }
1135 if let Some(span) = ty_span {
1136 err.span_label(
1137 span,
1138 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} `{2}` not found for this {0}",
rcvr_ty.prefix_string(self.tcx), item_kind, item_ident))
})format!(
1139 "{item_kind} `{item_ident}` not found for this {}",
1140 rcvr_ty.prefix_string(self.tcx)
1141 ),
1142 );
1143 }
1144 }
1145
1146 fn report_no_match_method_error(
1147 &self,
1148 span: Span,
1149 rcvr_ty: Ty<'tcx>,
1150 item_ident: Ident,
1151 expr_id: hir::HirId,
1152 source: SelfSource<'tcx>,
1153 args: Option<&'tcx [hir::Expr<'tcx>]>,
1154 sugg_span: Span,
1155 no_match_data: &mut NoMatchData<'tcx>,
1156 expected: Expectation<'tcx>,
1157 trait_missing_method: bool,
1158 within_macro_span: Option<Span>,
1159 ) -> ErrorGuaranteed {
1160 let tcx = self.tcx;
1161 let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
1162
1163 if let Err(guar) = rcvr_ty.error_reported() {
1164 return guar;
1165 }
1166
1167 if let Err(guar) =
1170 self.report_failed_method_call_on_range_end(tcx, rcvr_ty, source, span, item_ident)
1171 {
1172 return guar;
1173 }
1174
1175 let mut ty_file = None;
1176 let mode = no_match_data.mode;
1177 let is_method = mode == Mode::MethodCall;
1178 let item_kind = if is_method {
1179 "method"
1180 } else if rcvr_ty.is_enum() || rcvr_ty.is_fresh_ty() {
1181 "variant, associated function, or constant"
1182 } else {
1183 "associated function or constant"
1184 };
1185
1186 if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
1187 tcx,
1188 rcvr_ty,
1189 source,
1190 span,
1191 item_kind,
1192 item_ident,
1193 &mut ty_file,
1194 ) {
1195 return guar;
1196 }
1197
1198 let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
1199 let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
1200 tcx.is_diagnostic_item(sym::write_macro, def_id)
1201 || tcx.is_diagnostic_item(sym::writeln_macro, def_id)
1202 }) && item_ident.name == sym::write_fmt;
1203 let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source {
1204 self.create_missing_writer_err(rcvr_ty, rcvr_expr, ty_file)
1205 } else {
1206 self.create_no_assoc_err(
1207 rcvr_ty,
1208 item_ident,
1209 item_kind,
1210 trait_missing_method,
1211 source,
1212 is_method,
1213 sugg_span,
1214 unsatisfied_predicates,
1215 )
1216 };
1217
1218 self.set_label_for_method_error(
1219 &mut err,
1220 source,
1221 rcvr_ty,
1222 item_ident,
1223 expr_id,
1224 item_ident.span,
1225 sugg_span,
1226 within_macro_span,
1227 args,
1228 );
1229
1230 self.suggest_method_call_annotation(
1231 &mut err,
1232 item_ident.span,
1233 rcvr_ty,
1234 item_ident,
1235 mode,
1236 source,
1237 expected,
1238 );
1239
1240 let static_candidates = self.suggest_static_method_candidates(
1241 &mut err,
1242 item_ident.span,
1243 rcvr_ty,
1244 item_ident,
1245 source,
1246 args,
1247 sugg_span,
1248 &no_match_data,
1249 );
1250
1251 let Ok((
1252 restrict_type_params,
1253 suggested_derive,
1254 unsatisfied_bounds,
1255 custom_span_label,
1256 bound_spans,
1257 )) = self.suggest_unsatisfied_ty_or_trait(
1258 &mut err,
1259 item_ident.span,
1260 rcvr_ty,
1261 item_ident,
1262 item_kind,
1263 source,
1264 unsatisfied_predicates,
1265 &static_candidates,
1266 )
1267 else {
1268 return err.emit();
1269 };
1270
1271 let similar_candidate = no_match_data.similar_candidate;
1272 let should_label_not_found = self.suggest_surround_method_call(
1273 &mut err,
1274 item_ident.span,
1275 rcvr_ty,
1276 item_ident,
1277 source,
1278 &similar_candidate,
1279 );
1280
1281 self.find_possible_candidates_for_method(
1282 &mut err,
1283 item_ident.span,
1284 rcvr_ty,
1285 item_ident,
1286 item_kind,
1287 mode,
1288 source,
1289 no_match_data,
1290 expected,
1291 should_label_not_found,
1292 custom_span_label,
1293 );
1294
1295 self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident);
1296
1297 if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params || suggested_derive {
1298 } else {
1300 self.suggest_traits_to_import(
1301 &mut err,
1302 item_ident.span,
1303 rcvr_ty,
1304 item_ident,
1305 args.map(|args| args.len() + 1),
1306 source,
1307 no_match_data.out_of_scope_traits.clone(),
1308 &static_candidates,
1309 unsatisfied_bounds,
1310 expected.only_has_type(self),
1311 trait_missing_method,
1312 );
1313 }
1314
1315 self.suggest_enum_variant_for_method_call(
1316 &mut err,
1317 rcvr_ty,
1318 item_ident,
1319 item_ident.span,
1320 source,
1321 unsatisfied_predicates,
1322 );
1323
1324 self.suggest_confusable_or_similarly_named_method(
1325 &mut err,
1326 item_ident.span,
1327 rcvr_ty,
1328 item_ident,
1329 mode,
1330 args,
1331 unsatisfied_predicates,
1332 similar_candidate,
1333 );
1334
1335 self.suggest_method_not_found_because_of_unsatisfied_bounds(
1336 &mut err,
1337 rcvr_ty,
1338 item_ident,
1339 item_kind,
1340 bound_spans,
1341 unsatisfied_predicates,
1342 );
1343
1344 self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_ident, expected);
1345 self.suggest_bounds_for_range_to_method(&mut err, source, item_ident);
1346 err.emit()
1347 }
1348
1349 fn set_not_found_span_label(
1350 &self,
1351 err: &mut Diag<'_>,
1352 rcvr_ty: Ty<'tcx>,
1353 item_ident: Ident,
1354 item_kind: &str,
1355 mode: Mode,
1356 source: SelfSource<'tcx>,
1357 span: Span,
1358 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1359 find_candidate_for_method: &mut bool,
1360 ) {
1361 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1362 if unsatisfied_predicates.is_empty() {
1363 err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} not found in `{1}`", item_kind,
ty_str))
})format!("{item_kind} not found in `{ty_str}`"));
1364 let is_string_or_ref_str = match rcvr_ty.kind() {
1365 ty::Ref(_, ty, _) => {
1366 ty.is_str()
1367 || #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String) =>
true,
_ => false,
}matches!(
1368 ty.kind(),
1369 ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String)
1370 )
1371 }
1372 ty::Adt(adt, _) => self.tcx.is_lang_item(adt.did(), LangItem::String),
1373 _ => false,
1374 };
1375 if is_string_or_ref_str && item_ident.name == sym::iter {
1376 err.span_suggestion_verbose(
1377 item_ident.span,
1378 "because of the in-memory representation of `&str`, to obtain \
1379 an `Iterator` over each of its codepoint use method `chars`",
1380 "chars",
1381 Applicability::MachineApplicable,
1382 );
1383 }
1384 if let ty::Adt(adt, _) = rcvr_ty.kind() {
1385 let mut inherent_impls_candidate = self
1386 .tcx
1387 .inherent_impls(adt.did())
1388 .into_iter()
1389 .copied()
1390 .filter(|def_id| {
1391 if let Some(assoc) = self.associated_value(*def_id, item_ident) {
1392 match (mode, assoc.is_method(), source) {
1395 (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
1396 self.tcx.at(span).type_of(*def_id).instantiate_identity()
1401 != rcvr_ty
1402 }
1403 (Mode::Path, false, _) => true,
1404 _ => false,
1405 }
1406 } else {
1407 false
1408 }
1409 })
1410 .collect::<Vec<_>>();
1411 inherent_impls_candidate.sort_by_key(|&id| self.tcx.def_path_str(id));
1412 inherent_impls_candidate.dedup();
1413 let msg = match &inherent_impls_candidate[..] {
1414 [] => return,
1415 [only] => {
1416 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the {0} was found for `",
item_kind))
})),
StringPart::highlighted(self.tcx.at(span).type_of(*only).instantiate_identity().to_string()),
StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`"))
}))]))vec![
1417 StringPart::normal(format!("the {item_kind} was found for `")),
1418 StringPart::highlighted(
1419 self.tcx.at(span).type_of(*only).instantiate_identity().to_string(),
1420 ),
1421 StringPart::normal(format!("`")),
1422 ]
1423 }
1424 candidates => {
1425 let limit = if candidates.len() == 5 { 5 } else { 4 };
1427 let type_candidates = candidates
1428 .iter()
1429 .take(limit)
1430 .map(|impl_item| {
1431 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("- `{0}`",
self.tcx.at(span).type_of(*impl_item).instantiate_identity()))
})format!(
1432 "- `{}`",
1433 self.tcx.at(span).type_of(*impl_item).instantiate_identity()
1434 )
1435 })
1436 .collect::<Vec<_>>()
1437 .join("\n");
1438 let additional_types = if candidates.len() > limit {
1439 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\nand {0} more types",
candidates.len() - limit))
})format!("\nand {} more types", candidates.len() - limit)
1440 } else {
1441 "".to_string()
1442 };
1443 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the {0} was found for\n{1}{2}",
item_kind, type_candidates, additional_types))
}))]))vec![StringPart::normal(format!(
1444 "the {item_kind} was found for\n{type_candidates}{additional_types}"
1445 ))]
1446 }
1447 };
1448 err.highlighted_note(msg);
1449 *find_candidate_for_method = mode == Mode::MethodCall;
1450 }
1451 } else {
1452 let ty_str = if ty_str.len() > 50 { String::new() } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("on `{0}` ", ty_str))
})format!("on `{ty_str}` ") };
1453 err.span_label(
1454 span,
1455 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} cannot be called {1}due to unsatisfied trait bounds",
item_kind, ty_str))
})format!("{item_kind} cannot be called {ty_str}due to unsatisfied trait bounds"),
1456 );
1457 }
1458 }
1459
1460 fn suggest_enum_variant_for_method_call(
1462 &self,
1463 err: &mut Diag<'_>,
1464 rcvr_ty: Ty<'tcx>,
1465 item_ident: Ident,
1466 span: Span,
1467 source: SelfSource<'tcx>,
1468 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1469 ) {
1470 if !unsatisfied_predicates.is_empty() || !rcvr_ty.is_enum() {
1472 return;
1473 }
1474
1475 let tcx = self.tcx;
1476 let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
1477 if let Some(var_name) = edit_distance::find_best_match_for_name(
1478 &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
1479 item_ident.name,
1480 None,
1481 ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
1482 {
1483 let mut suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span, var_name.to_string())]))vec![(span, var_name.to_string())];
1484 if let SelfSource::QPath(ty) = source
1485 && let hir::Node::Expr(ref path_expr) = tcx.parent_hir_node(ty.hir_id)
1486 && let hir::ExprKind::Path(_) = path_expr.kind
1487 && let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(parent), .. })
1488 | hir::Node::Expr(parent) = tcx.parent_hir_node(path_expr.hir_id)
1489 {
1490 let replacement_span = match parent.kind {
1492 hir::ExprKind::Call(callee, _) if callee.hir_id == path_expr.hir_id => {
1493 span.with_hi(parent.span.hi())
1494 }
1495 hir::ExprKind::Struct(..) => span.with_hi(parent.span.hi()),
1496 _ => span,
1497 };
1498 match (variant.ctor, parent.kind) {
1499 (None, hir::ExprKind::Struct(..)) => {
1500 suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span, var_name.to_string())]))vec![(span, var_name.to_string())];
1503 }
1504 (None, _) => {
1505 suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(replacement_span,
if variant.fields.is_empty() {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {{}}", var_name))
})
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} {{ {0} }}",
variant.fields.iter().map(|f|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: /* value */",
f.name))
})).collect::<Vec<_>>().join(", "), var_name))
})
})]))vec![(
1507 replacement_span,
1508 if variant.fields.is_empty() {
1509 format!("{var_name} {{}}")
1510 } else {
1511 format!(
1512 "{var_name} {{ {} }}",
1513 variant
1514 .fields
1515 .iter()
1516 .map(|f| format!("{}: /* value */", f.name))
1517 .collect::<Vec<_>>()
1518 .join(", ")
1519 )
1520 },
1521 )];
1522 }
1523 (Some((hir::def::CtorKind::Const, _)), _) => {
1524 suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(replacement_span, var_name.to_string())]))vec![(replacement_span, var_name.to_string())];
1526 }
1527 (Some((hir::def::CtorKind::Fn, def_id)), hir::ExprKind::Call(rcvr, args)) => {
1528 let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1529 let inputs = fn_sig.inputs().skip_binder();
1530 match (inputs, args) {
1533 (inputs, []) => {
1534 suggestion.push((
1536 rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
1537 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})",
inputs.iter().map(|i|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})).collect::<Vec<String>>().join(", ")))
})format!(
1538 "({})",
1539 inputs
1540 .iter()
1541 .map(|i| format!("/* {i} */"))
1542 .collect::<Vec<String>>()
1543 .join(", ")
1544 ),
1545 ));
1546 }
1547 (_, [arg]) if inputs.len() != args.len() => {
1548 suggestion.push((
1550 arg.span,
1551 inputs
1552 .iter()
1553 .map(|i| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})format!("/* {i} */"))
1554 .collect::<Vec<String>>()
1555 .join(", "),
1556 ));
1557 }
1558 (_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
1559 suggestion.push((
1561 arg_start.span.to(arg_end.span),
1562 inputs
1563 .iter()
1564 .map(|i| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})format!("/* {i} */"))
1565 .collect::<Vec<String>>()
1566 .join(", "),
1567 ));
1568 }
1569 _ => {}
1571 }
1572 }
1573 (Some((hir::def::CtorKind::Fn, def_id)), _) => {
1574 let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1575 let inputs = fn_sig.inputs().skip_binder();
1576 suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(replacement_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}({0})",
inputs.iter().map(|i|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})).collect::<Vec<String>>().join(", "), var_name))
}))]))vec![(
1577 replacement_span,
1578 format!(
1579 "{var_name}({})",
1580 inputs
1581 .iter()
1582 .map(|i| format!("/* {i} */"))
1583 .collect::<Vec<String>>()
1584 .join(", ")
1585 ),
1586 )];
1587 }
1588 }
1589 }
1590 err.multipart_suggestion(
1591 "there is a variant with a similar name",
1592 suggestion,
1593 Applicability::HasPlaceholders,
1594 );
1595 }
1596 }
1597
1598 fn handle_unsatisfied_predicates(
1599 &self,
1600 err: &mut Diag<'_>,
1601 rcvr_ty: Ty<'tcx>,
1602 item_ident: Ident,
1603 item_kind: &str,
1604 span: Span,
1605 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1606 restrict_type_params: &mut bool,
1607 suggested_derive: &mut bool,
1608 unsatisfied_bounds: &mut bool,
1609 custom_span_label: &mut bool,
1610 bound_spans: &mut SortedMap<Span, Vec<String>>,
1611 ) {
1612 let tcx = self.tcx;
1613 let rcvr_ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1614 let mut type_params = FxIndexMap::default();
1615
1616 let mut unimplemented_traits = FxIndexMap::default();
1619
1620 let mut unimplemented_traits_only = true;
1621 for (predicate, _parent_pred, cause) in unsatisfied_predicates {
1622 if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
1623 (predicate.kind().skip_binder(), cause.as_ref())
1624 {
1625 if p.trait_ref.self_ty() != rcvr_ty {
1626 continue;
1630 }
1631 unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
1632 predicate.kind().rebind(p),
1633 Obligation {
1634 cause: cause.clone(),
1635 param_env: self.param_env,
1636 predicate: *predicate,
1637 recursion_depth: 0,
1638 },
1639 ));
1640 }
1641 }
1642
1643 for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
1648 match predicate.kind().skip_binder() {
1649 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))
1650 if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
1651 _ => {
1652 unimplemented_traits_only = false;
1653 break;
1654 }
1655 }
1656 }
1657
1658 let mut collect_type_param_suggestions =
1659 |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
1660 if let (ty::Param(_), ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))) =
1662 (self_ty.kind(), parent_pred.kind().skip_binder())
1663 {
1664 let node = match p.trait_ref.self_ty().kind() {
1665 ty::Param(_) => {
1666 Some(self.tcx.hir_node_by_def_id(self.body_id))
1669 }
1670 ty::Adt(def, _) => {
1671 def.did().as_local().map(|def_id| self.tcx.hir_node_by_def_id(def_id))
1672 }
1673 _ => None,
1674 };
1675 if let Some(hir::Node::Item(hir::Item { kind, .. })) = node
1676 && let Some(g) = kind.generics()
1677 {
1678 let key = (
1679 g.tail_span_for_predicate_suggestion(),
1680 g.add_where_or_trailing_comma(),
1681 );
1682 type_params
1683 .entry(key)
1684 .or_insert_with(UnordSet::default)
1685 .insert(obligation.to_owned());
1686 return true;
1687 }
1688 }
1689 false
1690 };
1691 let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
1692 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`",
if obligation.len() > 50 { quiet } else { obligation }))
})format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
1693 match self_ty.kind() {
1694 ty::Adt(def, _) => {
1696 bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
1697 }
1698 ty::Dynamic(preds, _) => {
1700 for pred in preds.iter() {
1701 match pred.skip_binder() {
1702 ty::ExistentialPredicate::Trait(tr) => {
1703 bound_spans
1704 .get_mut_or_insert_default(tcx.def_span(tr.def_id))
1705 .push(msg.clone());
1706 }
1707 ty::ExistentialPredicate::Projection(_)
1708 | ty::ExistentialPredicate::AutoTrait(_) => {}
1709 }
1710 }
1711 }
1712 ty::Closure(def_id, _) => {
1714 bound_spans
1715 .get_mut_or_insert_default(tcx.def_span(*def_id))
1716 .push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", quiet))
})format!("`{quiet}`"));
1717 }
1718 _ => {}
1719 }
1720 };
1721
1722 let mut format_pred = |pred: ty::Predicate<'tcx>| {
1723 let bound_predicate = pred.kind();
1724 match bound_predicate.skip_binder() {
1725 ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
1726 let pred = bound_predicate.rebind(pred);
1727 let projection_term = pred.skip_binder().projection_term;
1729 let quiet_projection_term = projection_term
1730 .with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
1731
1732 let term = pred.skip_binder().term;
1733
1734 let obligation = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} = {1}", projection_term, term))
})format!("{projection_term} = {term}");
1735 let quiet =
1736 {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} = {1}",
quiet_projection_term, term))
})
}with_forced_trimmed_paths!(format!("{} = {}", quiet_projection_term, term));
1737
1738 bound_span_label(projection_term.self_ty(), &obligation, &quiet);
1739 Some((obligation, projection_term.self_ty()))
1740 }
1741 ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
1742 let p = poly_trait_ref.trait_ref;
1743 let self_ty = p.self_ty();
1744 let path = p.print_only_trait_path();
1745 let obligation = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", self_ty, path))
})format!("{self_ty}: {path}");
1746 let quiet = {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("_: {0}", path))
})
}with_forced_trimmed_paths!(format!("_: {}", path));
1747 bound_span_label(self_ty, &obligation, &quiet);
1748 Some((obligation, self_ty))
1749 }
1750 _ => None,
1751 }
1752 };
1753
1754 let mut skip_list: UnordSet<_> = Default::default();
1756 let mut spanned_predicates = FxIndexMap::default();
1757 let mut manually_impl = false;
1758 for (p, parent_p, cause) in unsatisfied_predicates {
1759 let (item_def_id, cause_span, cause_msg) =
1762 match cause.as_ref().map(|cause| cause.code()) {
1763 Some(ObligationCauseCode::ImplDerived(data)) => {
1764 let msg = if let DefKind::Impl { of_trait: true } =
1765 self.tcx.def_kind(data.impl_or_alias_def_id)
1766 {
1767 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type parameter would need to implement `{0}`",
self.tcx.item_name(self.tcx.impl_trait_id(data.impl_or_alias_def_id))))
})format!(
1768 "type parameter would need to implement `{}`",
1769 self.tcx
1770 .item_name(self.tcx.impl_trait_id(data.impl_or_alias_def_id))
1771 )
1772 } else {
1773 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unsatisfied bound `{0}` introduced here",
p))
})format!("unsatisfied bound `{p}` introduced here")
1774 };
1775 (data.impl_or_alias_def_id, data.span, msg)
1776 }
1777 Some(
1778 ObligationCauseCode::WhereClauseInExpr(def_id, span, _, _)
1779 | ObligationCauseCode::WhereClause(def_id, span),
1780 ) if !span.is_dummy() => {
1781 (*def_id, *span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unsatisfied bound `{0}` introduced here",
p))
})format!("unsatisfied bound `{p}` introduced here"))
1782 }
1783 _ => continue,
1784 };
1785
1786 if !#[allow(non_exhaustive_omitted_patterns)] match p.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Projection(..) |
ty::ClauseKind::Trait(..)) => true,
_ => false,
}matches!(
1788 p.kind().skip_binder(),
1789 ty::PredicateKind::Clause(
1790 ty::ClauseKind::Projection(..) | ty::ClauseKind::Trait(..)
1791 )
1792 ) {
1793 continue;
1794 }
1795
1796 match self.tcx.hir_get_if_local(item_def_id) {
1797 Some(Node::Item(hir::Item {
1800 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
1801 ..
1802 })) if #[allow(non_exhaustive_omitted_patterns)] match self_ty.span.ctxt().outer_expn_data().kind
{
ExpnKind::Macro(MacroKind::Derive, _) => true,
_ => false,
}matches!(
1803 self_ty.span.ctxt().outer_expn_data().kind,
1804 ExpnKind::Macro(MacroKind::Derive, _)
1805 ) || #[allow(non_exhaustive_omitted_patterns)] match of_trait.map(|t|
t.trait_ref.path.span.ctxt().outer_expn_data().kind) {
Some(ExpnKind::Macro(MacroKind::Derive, _)) => true,
_ => false,
}matches!(
1806 of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
1807 Some(ExpnKind::Macro(MacroKind::Derive, _))
1808 ) =>
1809 {
1810 let span = self_ty.span.ctxt().outer_expn_data().call_site;
1811 let entry = spanned_predicates.entry(span);
1812 let entry = entry.or_insert_with(|| {
1813 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1814 });
1815 entry.0.insert(cause_span);
1816 entry.1.insert((
1817 cause_span,
1818 cause_msg,
1819 ));
1820 entry.2.push(p);
1821 skip_list.insert(p);
1822 manually_impl = true;
1823 }
1824
1825 Some(Node::Item(hir::Item {
1827 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
1828 span: item_span,
1829 ..
1830 })) => {
1831 let sized_pred =
1832 unsatisfied_predicates.iter().any(|(pred, _, _)| {
1833 match pred.kind().skip_binder() {
1834 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1835 self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
1836 && pred.polarity == ty::PredicatePolarity::Positive
1837 }
1838 _ => false,
1839 }
1840 });
1841 for param in generics.params {
1842 if param.span == cause_span && sized_pred {
1843 let (sp, sugg) = match param.colon_span {
1844 Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
1845 None => (param.span.shrink_to_hi(), ": ?Sized"),
1846 };
1847 err.span_suggestion_verbose(
1848 sp,
1849 "consider relaxing the type parameter's implicit `Sized` bound",
1850 sugg,
1851 Applicability::MachineApplicable,
1852 );
1853 }
1854 }
1855 if let Some(pred) = parent_p {
1856 let _ = format_pred(*pred);
1858 }
1859 skip_list.insert(p);
1860 let entry = spanned_predicates.entry(self_ty.span);
1861 let entry = entry.or_insert_with(|| {
1862 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1863 });
1864 entry.2.push(p);
1865 if cause_span != *item_span {
1866 entry.0.insert(cause_span);
1867 entry.1.insert((cause_span, "unsatisfied trait bound introduced here".to_string()));
1868 } else {
1869 if let Some(of_trait) = of_trait {
1870 entry.0.insert(of_trait.trait_ref.path.span);
1871 }
1872 entry.0.insert(self_ty.span);
1873 };
1874 if let Some(of_trait) = of_trait {
1875 entry.1.insert((of_trait.trait_ref.path.span, String::new()));
1876 }
1877 entry.1.insert((self_ty.span, String::new()));
1878 }
1879 Some(Node::Item(hir::Item {
1880 kind: hir::ItemKind::Trait(_, rustc_ast::ast::IsAuto::Yes, ..),
1881 span: item_span,
1882 ..
1883 })) => {
1884 self.dcx().span_delayed_bug(
1885 *item_span,
1886 "auto trait is invoked with no method error, but no error reported?",
1887 );
1888 }
1889 Some(
1890 Node::Item(hir::Item {
1891 kind:
1892 hir::ItemKind::Trait(_, _, _, ident, ..)
1893 | hir::ItemKind::TraitAlias(_, ident, ..),
1894 ..
1895 })
1896 | Node::TraitItem(hir::TraitItem { ident, .. })
1898 | Node::ImplItem(hir::ImplItem { ident, .. })
1899 ) => {
1900 skip_list.insert(p);
1901 let entry = spanned_predicates.entry(ident.span);
1902 let entry = entry.or_insert_with(|| {
1903 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1904 });
1905 entry.0.insert(cause_span);
1906 entry.1.insert((ident.span, String::new()));
1907 entry.1.insert((cause_span, "unsatisfied trait bound introduced here".to_string()));
1908 entry.2.push(p);
1909 }
1910 _ => {
1911 }
1916 }
1917 }
1918 let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
1919 spanned_predicates.sort_by_key(|(span, _)| *span);
1920 for (_, (primary_spans, span_labels, predicates)) in spanned_predicates {
1921 let mut tracker = TraitBoundDuplicateTracker::new();
1922 let mut all_trait_bounds_for_rcvr = true;
1923 for pred in &predicates {
1924 match pred.kind().skip_binder() {
1925 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1926 let self_ty = pred.trait_ref.self_ty();
1927 if self_ty.peel_refs() != rcvr_ty {
1928 all_trait_bounds_for_rcvr = false;
1929 break;
1930 }
1931 let is_ref = #[allow(non_exhaustive_omitted_patterns)] match self_ty.kind() {
ty::Ref(..) => true,
_ => false,
}matches!(self_ty.kind(), ty::Ref(..));
1932 tracker.track(pred.trait_ref.def_id, is_ref);
1933 }
1934 _ => {
1935 all_trait_bounds_for_rcvr = false;
1936 break;
1937 }
1938 }
1939 }
1940 let has_ref_dupes = tracker.has_ref_dupes();
1941 let trait_def_ids = tracker.into_trait_def_ids();
1942 let mut preds: Vec<_> = predicates
1943 .iter()
1944 .filter_map(|pred| format_pred(**pred))
1945 .map(|(p, _)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"))
1946 .collect();
1947 preds.sort();
1948 preds.dedup();
1949 let availability_note = if all_trait_bounds_for_rcvr
1950 && has_ref_dupes
1951 && trait_def_ids.len() > 1
1952 && #[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.kind() {
ty::Adt(..) => true,
_ => false,
}matches!(rcvr_ty.kind(), ty::Adt(..))
1953 {
1954 let mut trait_names = trait_def_ids
1955 .into_iter()
1956 .map(|def_id| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", tcx.def_path_str(def_id)))
})format!("`{}`", tcx.def_path_str(def_id)))
1957 .collect::<Vec<_>>();
1958 trait_names.sort();
1959 listify(&trait_names, |name| name.to_string()).map(|traits| {
1960 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("for `{0}` to be available, `{1}` must implement {2}",
item_ident, rcvr_ty_str, traits))
})format!(
1961 "for `{item_ident}` to be available, `{rcvr_ty_str}` must implement {traits}"
1962 )
1963 })
1964 } else {
1965 None
1966 };
1967 let msg = if let Some(availability_note) = availability_note {
1968 availability_note
1969 } else if let [pred] = &preds[..] {
1970 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("trait bound {0} was not satisfied",
pred))
})format!("trait bound {pred} was not satisfied")
1971 } else {
1972 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following trait bounds were not satisfied:\n{0}",
preds.join("\n")))
})format!("the following trait bounds were not satisfied:\n{}", preds.join("\n"),)
1973 };
1974 let mut span: MultiSpan = primary_spans.into_iter().collect::<Vec<_>>().into();
1975 for (sp, label) in span_labels {
1976 span.push_span_label(sp, label);
1977 }
1978 err.span_note(span, msg);
1979 *unsatisfied_bounds = true;
1980 }
1981
1982 let mut suggested_bounds = UnordSet::default();
1983 let mut bound_list = unsatisfied_predicates
1985 .iter()
1986 .filter_map(|(pred, parent_pred, _cause)| {
1987 let mut suggested = false;
1988 format_pred(*pred).map(|(p, self_ty)| {
1989 if let Some(parent) = parent_pred
1990 && suggested_bounds.contains(parent)
1991 {
1992 } else if !suggested_bounds.contains(pred)
1994 && collect_type_param_suggestions(self_ty, *pred, &p)
1995 {
1996 suggested = true;
1997 suggested_bounds.insert(pred);
1998 }
1999 (
2000 match parent_pred {
2001 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"),
2002 Some(parent_pred) => match format_pred(*parent_pred) {
2003 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"),
2004 Some((parent_p, _)) => {
2005 if !suggested
2006 && !suggested_bounds.contains(pred)
2007 && !suggested_bounds.contains(parent_pred)
2008 && collect_type_param_suggestions(self_ty, *parent_pred, &p)
2009 {
2010 suggested_bounds.insert(pred);
2011 }
2012 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`\nwhich is required by `{1}`",
p, parent_p))
})format!("`{p}`\nwhich is required by `{parent_p}`")
2013 }
2014 },
2015 },
2016 *pred,
2017 )
2018 })
2019 })
2020 .filter(|(_, pred)| !skip_list.contains(&pred))
2021 .map(|(t, _)| t)
2022 .enumerate()
2023 .collect::<Vec<(usize, String)>>();
2024
2025 if !#[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.peel_refs().kind() {
ty::Param(_) => true,
_ => false,
}matches!(rcvr_ty.peel_refs().kind(), ty::Param(_)) {
2026 for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
2027 *restrict_type_params = true;
2028 let obligations = obligations.into_sorted_stable_ord();
2030 err.span_suggestion_verbose(
2031 span,
2032 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider restricting the type parameter{0} to satisfy the trait bound{0}",
if obligations.len() == 1 { "" } else { "s" }))
})format!(
2033 "consider restricting the type parameter{s} to satisfy the trait \
2034 bound{s}",
2035 s = pluralize!(obligations.len())
2036 ),
2037 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", add_where_or_comma,
obligations.join(", ")))
})format!("{} {}", add_where_or_comma, obligations.join(", ")),
2038 Applicability::MaybeIncorrect,
2039 );
2040 }
2041 }
2042
2043 bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); bound_list.dedup_by(|(_, a), (_, b)| a == b); bound_list.sort_by_key(|(pos, _)| *pos); if !bound_list.is_empty() || !skip_list.is_empty() {
2048 let bound_list =
2049 bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
2050 let actual_prefix = rcvr_ty.prefix_string(self.tcx);
2051 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/suggest.rs:2051",
"rustc_hir_typeck::method::suggest", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(2051u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("unimplemented_traits.len() == {0}",
unimplemented_traits.len()) as &dyn Value))])
});
} else { ; }
};info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
2052 let (primary_message, label, notes) = if unimplemented_traits.len() == 1
2053 && unimplemented_traits_only
2054 {
2055 unimplemented_traits
2056 .into_iter()
2057 .next()
2058 .map(|(_, (trait_ref, obligation))| {
2059 if trait_ref.self_ty().references_error() || rcvr_ty.references_error() {
2060 return (None, None, Vec::new());
2062 }
2063 let OnUnimplementedNote { message, label, notes, .. } = self
2064 .err_ctxt()
2065 .on_unimplemented_note(trait_ref, &obligation, err.long_ty_path());
2066 (message, label, notes)
2067 })
2068 .unwrap()
2069 } else {
2070 (None, None, Vec::new())
2071 };
2072 let primary_message = primary_message.unwrap_or_else(|| {
2073 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
2074 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the {0} `{1}` exists for {2} `{3}`, but its trait bounds were not satisfied",
item_kind, item_ident, actual_prefix, ty_str))
})format!(
2075 "the {item_kind} `{item_ident}` exists for {actual_prefix} `{ty_str}`, \
2076 but its trait bounds were not satisfied"
2077 )
2078 });
2079 err.primary_message(primary_message);
2080 if let Some(label) = label {
2081 *custom_span_label = true;
2082 err.span_label(span, label);
2083 }
2084 if !bound_list.is_empty() {
2085 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following trait bounds were not satisfied:\n{0}",
bound_list))
})format!("the following trait bounds were not satisfied:\n{bound_list}"));
2086 }
2087 for note in notes {
2088 err.note(note);
2089 }
2090
2091 if let ty::Adt(adt_def, _) = rcvr_ty.kind() {
2092 unsatisfied_predicates.iter().find(|(pred, _parent, _cause)| {
2093 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
2094 pred.kind().skip_binder()
2095 {
2096 self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(
2097 err, &pred, *adt_def,
2098 )
2099 } else {
2100 false
2101 }
2102 });
2103 }
2104
2105 *suggested_derive = self.suggest_derive(err, unsatisfied_predicates);
2106 *unsatisfied_bounds = true;
2107 }
2108 if manually_impl {
2109 err.help("consider manually implementing the trait to avoid undesired bounds");
2110 }
2111 }
2112
2113 fn lookup_segments_chain_for_no_match_method(
2115 &self,
2116 err: &mut Diag<'_>,
2117 item_name: Ident,
2118 item_kind: &str,
2119 source: SelfSource<'tcx>,
2120 no_match_data: &NoMatchData<'tcx>,
2121 ) {
2122 if no_match_data.unsatisfied_predicates.is_empty()
2123 && let Mode::MethodCall = no_match_data.mode
2124 && let SelfSource::MethodCall(mut source_expr) = source
2125 {
2126 let mut stack_methods = ::alloc::vec::Vec::new()vec![];
2127 while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, method_span) =
2128 source_expr.kind
2129 {
2130 if let Some(prev_match) = stack_methods.pop() {
2132 err.span_label(
2133 method_span,
2134 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` is available on `{2}`",
item_kind, item_name, prev_match))
})format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
2135 );
2136 }
2137 let rcvr_ty = self.resolve_vars_if_possible(
2138 self.typeck_results
2139 .borrow()
2140 .expr_ty_adjusted_opt(rcvr_expr)
2141 .unwrap_or(Ty::new_misc_error(self.tcx)),
2142 );
2143
2144 let Ok(candidates) = self.probe_for_name_many(
2145 Mode::MethodCall,
2146 item_name,
2147 None,
2148 IsSuggestion(true),
2149 rcvr_ty,
2150 source_expr.hir_id,
2151 ProbeScope::TraitsInScope,
2152 ) else {
2153 return;
2154 };
2155
2156 for _matched_method in candidates {
2160 stack_methods.push(rcvr_ty);
2162 }
2163 source_expr = rcvr_expr;
2164 }
2165 if let Some(prev_match) = stack_methods.pop() {
2167 err.span_label(
2168 source_expr.span,
2169 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` is available on `{2}`",
item_kind, item_name, prev_match))
})format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
2170 );
2171 }
2172 }
2173 }
2174
2175 fn find_likely_intended_associated_item(
2176 &self,
2177 err: &mut Diag<'_>,
2178 similar_candidate: ty::AssocItem,
2179 span: Span,
2180 args: Option<&'tcx [hir::Expr<'tcx>]>,
2181 mode: Mode,
2182 ) {
2183 let tcx = self.tcx;
2184 let def_kind = similar_candidate.as_def_kind();
2185 let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
2186 let similar_candidate_name = similar_candidate.name();
2187 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there is {2} {0} `{1}` with a similar name",
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
similar_candidate_name, an))
})format!(
2188 "there is {an} {} `{}` with a similar name",
2189 self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
2190 similar_candidate_name,
2191 );
2192 if def_kind == DefKind::AssocFn {
2197 let ty_args = self.infcx.fresh_args_for_item(span, similar_candidate.def_id);
2198 let fn_sig = tcx.fn_sig(similar_candidate.def_id).instantiate(tcx, ty_args);
2199 let fn_sig = self.instantiate_binder_with_fresh_vars(
2200 span,
2201 BoundRegionConversionTime::FnCall,
2202 fn_sig,
2203 );
2204 if similar_candidate.is_method() {
2205 if let Some(args) = args
2206 && fn_sig.inputs()[1..].len() == args.len()
2207 {
2208 err.span_suggestion_verbose(
2211 span,
2212 msg,
2213 similar_candidate_name,
2214 Applicability::MaybeIncorrect,
2215 );
2216 } else {
2217 err.span_help(
2220 tcx.def_span(similar_candidate.def_id),
2221 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}",
if let None = args {
""
} else { ", but with different arguments" }, msg))
})format!(
2222 "{msg}{}",
2223 if let None = args { "" } else { ", but with different arguments" },
2224 ),
2225 );
2226 }
2227 } else if let Some(args) = args
2228 && fn_sig.inputs().len() == args.len()
2229 {
2230 err.span_suggestion_verbose(
2233 span,
2234 msg,
2235 similar_candidate_name,
2236 Applicability::MaybeIncorrect,
2237 );
2238 } else {
2239 err.span_help(tcx.def_span(similar_candidate.def_id), msg);
2240 }
2241 } else if let Mode::Path = mode
2242 && args.unwrap_or(&[]).is_empty()
2243 {
2244 err.span_suggestion_verbose(
2246 span,
2247 msg,
2248 similar_candidate_name,
2249 Applicability::MaybeIncorrect,
2250 );
2251 } else {
2252 err.span_help(tcx.def_span(similar_candidate.def_id), msg);
2255 }
2256 }
2257
2258 pub(crate) fn confusable_method_name(
2259 &self,
2260 err: &mut Diag<'_>,
2261 rcvr_ty: Ty<'tcx>,
2262 item_name: Ident,
2263 call_args: Option<Vec<Ty<'tcx>>>,
2264 ) -> Option<Symbol> {
2265 if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
2266 for &inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter() {
2267 for inherent_method in
2268 self.tcx.associated_items(inherent_impl_did).in_definition_order()
2269 {
2270 if let Some(candidates) = {
{
'done:
{
for i in
::rustc_hir::attrs::HasAttrs::get_attrs(inherent_method.def_id,
&self.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcConfusables { symbols, ..
}) => {
break 'done Some(symbols);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(self.tcx, inherent_method.def_id, RustcConfusables{symbols, ..} => symbols)
2271 && candidates.contains(&item_name.name)
2272 && inherent_method.is_fn()
2273 {
2274 let args =
2275 ty::GenericArgs::identity_for_item(self.tcx, inherent_method.def_id)
2276 .rebase_onto(
2277 self.tcx,
2278 inherent_method.container_id(self.tcx),
2279 adt_args,
2280 );
2281 let fn_sig =
2282 self.tcx.fn_sig(inherent_method.def_id).instantiate(self.tcx, args);
2283 let fn_sig = self.instantiate_binder_with_fresh_vars(
2284 item_name.span,
2285 BoundRegionConversionTime::FnCall,
2286 fn_sig,
2287 );
2288 let name = inherent_method.name();
2289 let inputs = fn_sig.inputs();
2290 let expected_inputs =
2291 if inherent_method.is_method() { &inputs[1..] } else { inputs };
2292 if let Some(ref args) = call_args
2293 && expected_inputs
2294 .iter()
2295 .eq_by(args, |expected, found| self.may_coerce(*expected, *found))
2296 {
2297 err.span_suggestion_verbose(
2298 item_name.span,
2299 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use `{0}`",
name))
})format!("you might have meant to use `{}`", name),
2300 name,
2301 Applicability::MaybeIncorrect,
2302 );
2303 return Some(name);
2304 } else if let None = call_args {
2305 err.span_note(
2306 self.tcx.def_span(inherent_method.def_id),
2307 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use method `{0}`",
name))
})format!("you might have meant to use method `{}`", name),
2308 );
2309 return Some(name);
2310 }
2311 }
2312 }
2313 }
2314 }
2315 None
2316 }
2317 fn note_candidates_on_method_error(
2318 &self,
2319 rcvr_ty: Ty<'tcx>,
2320 item_name: Ident,
2321 self_source: SelfSource<'tcx>,
2322 args: Option<&'tcx [hir::Expr<'tcx>]>,
2323 span: Span,
2324 err: &mut Diag<'_>,
2325 sources: &mut Vec<CandidateSource>,
2326 sugg_span: Option<Span>,
2327 ) {
2328 sources.sort_by_key(|source| match *source {
2329 CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
2330 CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
2331 });
2332 sources.dedup();
2333 let limit = if sources.len() == 5 { 5 } else { 4 };
2335
2336 let mut suggs = ::alloc::vec::Vec::new()vec![];
2337 for (idx, source) in sources.iter().take(limit).enumerate() {
2338 match *source {
2339 CandidateSource::Impl(impl_did) => {
2340 let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
2343 let impl_trait_id = self.tcx.impl_opt_trait_id(impl_did)?;
2344 self.associated_value(impl_trait_id, item_name)
2345 }) else {
2346 continue;
2347 };
2348
2349 let note_span = if item.def_id.is_local() {
2350 Some(self.tcx.def_span(item.def_id))
2351 } else if impl_did.is_local() {
2352 Some(self.tcx.def_span(impl_did))
2353 } else {
2354 None
2355 };
2356
2357 let impl_ty = self.tcx.at(span).type_of(impl_did).instantiate_identity();
2358
2359 let insertion = match self.tcx.impl_opt_trait_ref(impl_did) {
2360 None => String::new(),
2361 Some(trait_ref) => {
2362 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" of the trait `{0}`",
self.tcx.def_path_str(trait_ref.skip_binder().def_id)))
})format!(
2363 " of the trait `{}`",
2364 self.tcx.def_path_str(trait_ref.skip_binder().def_id)
2365 )
2366 }
2367 };
2368
2369 let (note_str, idx) = if sources.len() > 1 {
2370 (
2371 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("candidate #{0} is defined in an impl{1} for the type `{2}`",
idx + 1, insertion, impl_ty))
})format!(
2372 "candidate #{} is defined in an impl{} for the type `{}`",
2373 idx + 1,
2374 insertion,
2375 impl_ty,
2376 ),
2377 Some(idx + 1),
2378 )
2379 } else {
2380 (
2381 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the candidate is defined in an impl{0} for the type `{1}`",
insertion, impl_ty))
})format!(
2382 "the candidate is defined in an impl{insertion} for the type `{impl_ty}`",
2383 ),
2384 None,
2385 )
2386 };
2387 if let Some(note_span) = note_span {
2388 err.span_note(note_span, note_str);
2390 } else {
2391 err.note(note_str);
2392 }
2393 if let Some(sugg_span) = sugg_span
2394 && let Some(trait_ref) = self.tcx.impl_opt_trait_ref(impl_did)
2395 && let Some(sugg) = print_disambiguation_help(
2396 self.tcx,
2397 err,
2398 self_source,
2399 args,
2400 trait_ref
2401 .instantiate(
2402 self.tcx,
2403 self.fresh_args_for_item(sugg_span, impl_did),
2404 )
2405 .with_replaced_self_ty(self.tcx, rcvr_ty),
2406 idx,
2407 sugg_span,
2408 item,
2409 )
2410 {
2411 suggs.push(sugg);
2412 }
2413 }
2414 CandidateSource::Trait(trait_did) => {
2415 let Some(item) = self.associated_value(trait_did, item_name) else { continue };
2416 let item_span = self.tcx.def_span(item.def_id);
2417 let idx = if sources.len() > 1 {
2418 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("candidate #{0} is defined in the trait `{1}`",
idx + 1, self.tcx.def_path_str(trait_did)))
})format!(
2419 "candidate #{} is defined in the trait `{}`",
2420 idx + 1,
2421 self.tcx.def_path_str(trait_did)
2422 );
2423 err.span_note(item_span, msg);
2424 Some(idx + 1)
2425 } else {
2426 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the candidate is defined in the trait `{0}`",
self.tcx.def_path_str(trait_did)))
})format!(
2427 "the candidate is defined in the trait `{}`",
2428 self.tcx.def_path_str(trait_did)
2429 );
2430 err.span_note(item_span, msg);
2431 None
2432 };
2433 if let Some(sugg_span) = sugg_span
2434 && let Some(sugg) = print_disambiguation_help(
2435 self.tcx,
2436 err,
2437 self_source,
2438 args,
2439 ty::TraitRef::new_from_args(
2440 self.tcx,
2441 trait_did,
2442 self.fresh_args_for_item(sugg_span, trait_did),
2443 )
2444 .with_replaced_self_ty(self.tcx, rcvr_ty),
2445 idx,
2446 sugg_span,
2447 item,
2448 )
2449 {
2450 suggs.push(sugg);
2451 }
2452 }
2453 }
2454 }
2455 if !suggs.is_empty()
2456 && let Some(span) = sugg_span
2457 {
2458 suggs.sort();
2459 err.span_suggestions(
2460 span.with_hi(item_name.span.lo()),
2461 "use fully-qualified syntax to disambiguate",
2462 suggs,
2463 Applicability::MachineApplicable,
2464 );
2465 }
2466 if sources.len() > limit {
2467 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("and {0} others",
sources.len() - limit))
})format!("and {} others", sources.len() - limit));
2468 }
2469 }
2470
2471 fn find_builder_fn(&self, err: &mut Diag<'_>, rcvr_ty: Ty<'tcx>, expr_id: hir::HirId) {
2474 let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
2475 return;
2476 };
2477 let mut items = self
2478 .tcx
2479 .inherent_impls(adt_def.did())
2480 .iter()
2481 .flat_map(|&i| self.tcx.associated_items(i).in_definition_order())
2482 .filter(|item| {
2485 #[allow(non_exhaustive_omitted_patterns)] match item.kind {
ty::AssocKind::Fn { has_self: false, .. } => true,
_ => false,
}matches!(item.kind, ty::AssocKind::Fn { has_self: false, .. })
2486 && self
2487 .probe_for_name(
2488 Mode::Path,
2489 item.ident(self.tcx),
2490 None,
2491 IsSuggestion(true),
2492 rcvr_ty,
2493 expr_id,
2494 ProbeScope::TraitsInScope,
2495 )
2496 .is_ok()
2497 })
2498 .filter_map(|item| {
2499 let ret_ty = self
2501 .tcx
2502 .fn_sig(item.def_id)
2503 .instantiate(self.tcx, self.fresh_args_for_item(DUMMY_SP, item.def_id))
2504 .output();
2505 let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
2506 let ty::Adt(def, args) = ret_ty.kind() else {
2507 return None;
2508 };
2509 if self.can_eq(self.param_env, ret_ty, rcvr_ty) {
2511 return Some((item.def_id, ret_ty));
2512 }
2513 if ![self.tcx.lang_items().option_type(), self.tcx.get_diagnostic_item(sym::Result)]
2515 .contains(&Some(def.did()))
2516 {
2517 return None;
2518 }
2519 let arg = args.get(0)?.expect_ty();
2520 if self.can_eq(self.param_env, rcvr_ty, arg) {
2521 Some((item.def_id, ret_ty))
2522 } else {
2523 None
2524 }
2525 })
2526 .collect::<Vec<_>>();
2527 let post = if items.len() > 5 {
2528 let items_len = items.len();
2529 items.truncate(4);
2530 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\nand {0} others", items_len - 4))
})format!("\nand {} others", items_len - 4)
2531 } else {
2532 String::new()
2533 };
2534 match items[..] {
2535 [] => {}
2536 [(def_id, ret_ty)] => {
2537 err.span_note(
2538 self.tcx.def_span(def_id),
2539 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if you\'re trying to build a new `{1}`, consider using `{0}` which returns `{2}`",
self.tcx.def_path_str(def_id), rcvr_ty, ret_ty))
})format!(
2540 "if you're trying to build a new `{rcvr_ty}`, consider using `{}` which \
2541 returns `{ret_ty}`",
2542 self.tcx.def_path_str(def_id),
2543 ),
2544 );
2545 }
2546 _ => {
2547 let span: MultiSpan = items
2548 .iter()
2549 .map(|&(def_id, _)| self.tcx.def_span(def_id))
2550 .collect::<Vec<Span>>()
2551 .into();
2552 err.span_note(
2553 span,
2554 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if you\'re trying to build a new `{1}` consider using one of the following associated functions:\n{0}{2}",
items.iter().map(|&(def_id, _ret_ty)|
self.tcx.def_path_str(def_id)).collect::<Vec<String>>().join("\n"),
rcvr_ty, post))
})format!(
2555 "if you're trying to build a new `{rcvr_ty}` consider using one of the \
2556 following associated functions:\n{}{post}",
2557 items
2558 .iter()
2559 .map(|&(def_id, _ret_ty)| self.tcx.def_path_str(def_id))
2560 .collect::<Vec<String>>()
2561 .join("\n")
2562 ),
2563 );
2564 }
2565 }
2566 }
2567
2568 fn suggest_associated_call_syntax(
2571 &self,
2572 err: &mut Diag<'_>,
2573 static_candidates: &[CandidateSource],
2574 rcvr_ty: Ty<'tcx>,
2575 source: SelfSource<'tcx>,
2576 item_name: Ident,
2577 args: Option<&'tcx [hir::Expr<'tcx>]>,
2578 sugg_span: Span,
2579 ) {
2580 let mut has_unsuggestable_args = false;
2581 let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
2582 let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
2586 let target_ty = self
2587 .autoderef(sugg_span, rcvr_ty)
2588 .silence_errors()
2589 .find(|(rcvr_ty, _)| {
2590 DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
2591 })
2592 .map_or(impl_ty, |(ty, _)| ty)
2593 .peel_refs();
2594 if let ty::Adt(def, args) = target_ty.kind() {
2595 let infer_args = self.tcx.mk_args_from_iter(args.into_iter().map(|arg| {
2598 if !arg.is_suggestable(self.tcx, true) {
2599 has_unsuggestable_args = true;
2600 match arg.kind() {
2601 GenericArgKind::Lifetime(_) => {
2602 self.next_region_var(RegionVariableOrigin::Misc(DUMMY_SP)).into()
2603 }
2604 GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
2605 GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
2606 }
2607 } else {
2608 arg
2609 }
2610 }));
2611
2612 self.tcx.value_path_str_with_args(def.did(), infer_args)
2613 } else {
2614 self.ty_to_value_string(target_ty)
2615 }
2616 } else {
2617 self.ty_to_value_string(rcvr_ty.peel_refs())
2618 };
2619 if let SelfSource::MethodCall(_) = source {
2620 let first_arg = static_candidates.get(0).and_then(|candidate_source| {
2621 let (assoc_did, self_ty) = match candidate_source {
2622 CandidateSource::Impl(impl_did) => {
2623 (*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
2624 }
2625 CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
2626 };
2627
2628 let assoc = self.associated_value(assoc_did, item_name)?;
2629 if !assoc.is_fn() {
2630 return None;
2631 }
2632
2633 let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
2636 sig.inputs().skip_binder().get(0).and_then(|first| {
2637 let first_ty = first.peel_refs();
2639 if first_ty == self_ty || first_ty == self.tcx.types.self_param {
2640 Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
2641 } else {
2642 None
2643 }
2644 })
2645 });
2646
2647 let mut applicability = Applicability::MachineApplicable;
2648 let args = if let SelfSource::MethodCall(receiver) = source
2649 && let Some(args) = args
2650 {
2651 let explicit_args = if first_arg.is_some() {
2653 std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
2654 } else {
2655 if has_unsuggestable_args {
2657 applicability = Applicability::HasPlaceholders;
2658 }
2659 args.iter().collect()
2660 };
2661 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}{1})", first_arg.unwrap_or(""),
explicit_args.iter().map(|arg|
self.tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_|
{
applicability = Applicability::HasPlaceholders;
"_".to_owned()
})).collect::<Vec<_>>().join(", ")))
})format!(
2662 "({}{})",
2663 first_arg.unwrap_or(""),
2664 explicit_args
2665 .iter()
2666 .map(|arg| self
2667 .tcx
2668 .sess
2669 .source_map()
2670 .span_to_snippet(arg.span)
2671 .unwrap_or_else(|_| {
2672 applicability = Applicability::HasPlaceholders;
2673 "_".to_owned()
2674 }))
2675 .collect::<Vec<_>>()
2676 .join(", "),
2677 )
2678 } else {
2679 applicability = Applicability::HasPlaceholders;
2680 "(...)".to_owned()
2681 };
2682 err.span_suggestion_verbose(
2683 sugg_span,
2684 "use associated function syntax instead",
2685 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}{2}", ty_str, item_name,
args))
})format!("{ty_str}::{item_name}{args}"),
2686 applicability,
2687 );
2688 } else {
2689 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try with `{0}::{1}`", ty_str,
item_name))
})format!("try with `{ty_str}::{item_name}`",));
2690 }
2691 }
2692
2693 fn suggest_calling_field_as_fn(
2696 &self,
2697 span: Span,
2698 rcvr_ty: Ty<'tcx>,
2699 expr: &hir::Expr<'_>,
2700 item_name: Ident,
2701 err: &mut Diag<'_>,
2702 ) -> bool {
2703 let tcx = self.tcx;
2704 let field_receiver =
2705 self.autoderef(span, rcvr_ty).silence_errors().find_map(|(ty, _)| match ty.kind() {
2706 ty::Adt(def, args) if !def.is_enum() => {
2707 let variant = &def.non_enum_variant();
2708 tcx.find_field_index(item_name, variant).map(|index| {
2709 let field = &variant.fields[index];
2710 let field_ty = field.ty(tcx, args);
2711 (field, field_ty)
2712 })
2713 }
2714 _ => None,
2715 });
2716 if let Some((field, field_ty)) = field_receiver {
2717 let scope = tcx.parent_module_from_def_id(self.body_id);
2718 let is_accessible = field.vis.is_accessible_from(scope, tcx);
2719
2720 if is_accessible {
2721 if let Some((what, _, _)) = self.extract_callable_info(field_ty) {
2722 let what = match what {
2723 DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
2724 DefIdOrName::Name(what) => what,
2725 };
2726 let expr_span = expr.span.to(item_name.span);
2727 err.multipart_suggestion(
2728 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to call the {0} stored in `{1}`, surround the field access with parentheses",
what, item_name))
})format!(
2729 "to call the {what} stored in `{item_name}`, \
2730 surround the field access with parentheses",
2731 ),
2732 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(expr_span.shrink_to_lo(), '('.to_string()),
(expr_span.shrink_to_hi(), ')'.to_string())]))vec![
2733 (expr_span.shrink_to_lo(), '('.to_string()),
2734 (expr_span.shrink_to_hi(), ')'.to_string()),
2735 ],
2736 Applicability::MachineApplicable,
2737 );
2738 } else {
2739 let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2740
2741 if let Some(span) = call_expr.span.trim_start(item_name.span) {
2742 err.span_suggestion(
2743 span,
2744 "remove the arguments",
2745 "",
2746 Applicability::MaybeIncorrect,
2747 );
2748 }
2749 }
2750 }
2751
2752 let field_kind = if is_accessible { "field" } else { "private field" };
2753 err.span_label(item_name.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}, not a method", field_kind))
})format!("{field_kind}, not a method"));
2754 return true;
2755 }
2756 false
2757 }
2758
2759 fn report_failed_method_call_on_range_end(
2762 &self,
2763 tcx: TyCtxt<'tcx>,
2764 actual: Ty<'tcx>,
2765 source: SelfSource<'tcx>,
2766 span: Span,
2767 item_name: Ident,
2768 ) -> Result<(), ErrorGuaranteed> {
2769 if let SelfSource::MethodCall(expr) = source {
2770 for (_, parent) in tcx.hir_parent_iter(expr.hir_id).take(5) {
2771 if let Node::Expr(parent_expr) = parent {
2772 if !is_range_literal(parent_expr) {
2773 continue;
2774 }
2775 let lang_item = match parent_expr.kind {
2776 ExprKind::Struct(qpath, _, _) => match tcx.qpath_lang_item(*qpath) {
2777 Some(
2778 lang_item @ (LangItem::Range
2779 | LangItem::RangeCopy
2780 | LangItem::RangeInclusiveCopy
2781 | LangItem::RangeTo
2782 | LangItem::RangeToInclusive),
2783 ) => Some(lang_item),
2784 _ => None,
2785 },
2786 ExprKind::Call(func, _) => match func.kind {
2787 ExprKind::Path(qpath)
2789 if tcx.qpath_is_lang_item(qpath, LangItem::RangeInclusiveNew) =>
2790 {
2791 Some(LangItem::RangeInclusiveStruct)
2792 }
2793 _ => None,
2794 },
2795 _ => None,
2796 };
2797
2798 if lang_item.is_none() {
2799 continue;
2800 }
2801
2802 let span_included = match parent_expr.kind {
2803 hir::ExprKind::Struct(_, eps, _) => {
2804 eps.last().is_some_and(|ep| ep.span.contains(span))
2805 }
2806 hir::ExprKind::Call(func, ..) => func.span.contains(span),
2808 _ => false,
2809 };
2810
2811 if !span_included {
2812 continue;
2813 }
2814
2815 let Some(range_def_id) =
2816 lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item))
2817 else {
2818 continue;
2819 };
2820 let range_ty =
2821 self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]);
2822
2823 let pick = self.lookup_probe_for_diagnostic(
2824 item_name,
2825 range_ty,
2826 expr,
2827 ProbeScope::AllTraits,
2828 None,
2829 );
2830 if pick.is_ok() {
2831 let range_span = parent_expr.span.with_hi(expr.span.hi());
2832 return Err(self.dcx().emit_err(errors::MissingParenthesesInRange {
2833 span,
2834 ty: actual,
2835 method_name: item_name.as_str().to_string(),
2836 add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
2837 func_name: item_name.name.as_str().to_string(),
2838 left: range_span.shrink_to_lo(),
2839 right: range_span.shrink_to_hi(),
2840 }),
2841 }));
2842 }
2843 }
2844 }
2845 }
2846 Ok(())
2847 }
2848
2849 fn report_failed_method_call_on_numerical_infer_var(
2850 &self,
2851 tcx: TyCtxt<'tcx>,
2852 actual: Ty<'tcx>,
2853 source: SelfSource<'_>,
2854 span: Span,
2855 item_kind: &str,
2856 item_name: Ident,
2857 long_ty_path: &mut Option<PathBuf>,
2858 ) -> Result<(), ErrorGuaranteed> {
2859 let found_candidate = all_traits(self.tcx)
2860 .into_iter()
2861 .any(|info| self.associated_value(info.def_id, item_name).is_some());
2862 let found_assoc = |ty: Ty<'tcx>| {
2863 simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
2864 .and_then(|simp| {
2865 tcx.incoherent_impls(simp)
2866 .iter()
2867 .find_map(|&id| self.associated_value(id, item_name))
2868 })
2869 .is_some()
2870 };
2871 let found_candidate = found_candidate
2872 || found_assoc(tcx.types.i8)
2873 || found_assoc(tcx.types.i16)
2874 || found_assoc(tcx.types.i32)
2875 || found_assoc(tcx.types.i64)
2876 || found_assoc(tcx.types.i128)
2877 || found_assoc(tcx.types.u8)
2878 || found_assoc(tcx.types.u16)
2879 || found_assoc(tcx.types.u32)
2880 || found_assoc(tcx.types.u64)
2881 || found_assoc(tcx.types.u128)
2882 || found_assoc(tcx.types.f32)
2883 || found_assoc(tcx.types.f64);
2884 if found_candidate
2885 && actual.is_numeric()
2886 && !actual.has_concrete_skeleton()
2887 && let SelfSource::MethodCall(expr) = source
2888 {
2889 let ty_str = self.tcx.short_string(actual, long_ty_path);
2890 let mut err = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("can\'t call {0} `{1}` on ambiguous numeric type `{2}`",
item_kind, item_name, ty_str))
})).with_code(E0689)
}struct_span_code_err!(
2891 self.dcx(),
2892 span,
2893 E0689,
2894 "can't call {item_kind} `{item_name}` on ambiguous numeric type `{ty_str}`"
2895 );
2896 *err.long_ty_path() = long_ty_path.take();
2897 let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
2898 match expr.kind {
2899 ExprKind::Lit(lit) => {
2900 let snippet = tcx
2902 .sess
2903 .source_map()
2904 .span_to_snippet(lit.span)
2905 .unwrap_or_else(|_| "<numeric literal>".to_owned());
2906
2907 let snippet = snippet.trim_suffix('.');
2910 err.span_suggestion(
2911 lit.span,
2912 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you must specify a concrete type for this numeric value, like `{0}`",
concrete_type))
})format!(
2913 "you must specify a concrete type for this numeric value, \
2914 like `{concrete_type}`"
2915 ),
2916 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_{1}", snippet, concrete_type))
})format!("{snippet}_{concrete_type}"),
2917 Applicability::MaybeIncorrect,
2918 );
2919 }
2920 ExprKind::Path(QPath::Resolved(_, path)) => {
2921 if let hir::def::Res::Local(hir_id) = path.res {
2923 let span = tcx.hir_span(hir_id);
2924 let filename = tcx.sess.source_map().span_to_filename(span);
2925
2926 let parent_node = self.tcx.parent_hir_node(hir_id);
2927 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you must specify a type for this binding, like `{0}`",
concrete_type))
})format!(
2928 "you must specify a type for this binding, like `{concrete_type}`",
2929 );
2930
2931 match (filename, parent_node) {
2934 (
2935 FileName::Real(_),
2936 Node::LetStmt(hir::LetStmt {
2937 source: hir::LocalSource::Normal,
2938 ty,
2939 ..
2940 }),
2941 ) => {
2942 let type_span = ty
2943 .map(|ty| ty.span.with_lo(span.hi()))
2944 .unwrap_or(span.shrink_to_hi());
2945 err.span_suggestion(
2946 type_span,
2949 msg,
2950 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}", concrete_type))
})format!(": {concrete_type}"),
2951 Applicability::MaybeIncorrect,
2952 );
2953 }
2954 (FileName::Real(_), Node::Pat(pat))
2957 if let Node::Pat(binding_pat) = self.tcx.hir_node(hir_id)
2958 && let hir::PatKind::Binding(..) = binding_pat.kind
2959 && let Node::Pat(parent_pat) = parent_node
2960 && #[allow(non_exhaustive_omitted_patterns)] match parent_pat.kind {
hir::PatKind::Ref(..) => true,
_ => false,
}matches!(parent_pat.kind, hir::PatKind::Ref(..)) =>
2961 {
2962 err.span_label(span, "you must specify a type for this binding");
2963
2964 let mut ref_muts = Vec::new();
2965 let mut current_node = parent_node;
2966
2967 while let Node::Pat(parent_pat) = current_node {
2968 if let hir::PatKind::Ref(_, _, mutability) = parent_pat.kind {
2969 ref_muts.push(mutability);
2970 current_node = self.tcx.parent_hir_node(parent_pat.hir_id);
2971 } else {
2972 break;
2973 }
2974 }
2975
2976 let mut type_annotation = String::new();
2977 for mutability in ref_muts.iter().rev() {
2978 match mutability {
2979 hir::Mutability::Mut => type_annotation.push_str("&mut "),
2980 hir::Mutability::Not => type_annotation.push('&'),
2981 }
2982 }
2983 type_annotation.push_str(&concrete_type);
2984
2985 err.span_suggestion_verbose(
2986 pat.span.shrink_to_hi(),
2987 "specify the type in the closure argument list",
2988 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}", type_annotation))
})format!(": {type_annotation}"),
2989 Applicability::MaybeIncorrect,
2990 );
2991 }
2992 _ => {
2993 err.span_label(span, msg);
2994 }
2995 }
2996 }
2997 }
2998 _ => {}
2999 }
3000 return Err(err.emit());
3001 }
3002 Ok(())
3003 }
3004
3005 pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
3009 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/suggest.rs:3009",
"rustc_hir_typeck::method::suggest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(3009u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("suggest_assoc_method_call segs: {0:?}",
segs) as &dyn Value))])
});
} else { ; }
};debug!("suggest_assoc_method_call segs: {:?}", segs);
3010 let [seg1, seg2] = segs else {
3011 return;
3012 };
3013 self.dcx().try_steal_modify_and_emit_err(
3014 seg1.ident.span,
3015 StashKey::CallAssocMethod,
3016 |err| {
3017 let body = self.tcx.hir_body_owned_by(self.body_id);
3018 struct LetVisitor {
3019 ident_name: Symbol,
3020 }
3021
3022 impl<'v> Visitor<'v> for LetVisitor {
3024 type Result = ControlFlow<Option<&'v hir::Expr<'v>>>;
3025 fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
3026 if let hir::StmtKind::Let(&hir::LetStmt { pat, init, .. }) = ex.kind
3027 && let hir::PatKind::Binding(_, _, ident, ..) = pat.kind
3028 && ident.name == self.ident_name
3029 {
3030 ControlFlow::Break(init)
3031 } else {
3032 hir::intravisit::walk_stmt(self, ex)
3033 }
3034 }
3035 }
3036
3037 if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
3038 && let ControlFlow::Break(Some(expr)) =
3039 (LetVisitor { ident_name: seg1.ident.name }).visit_body(body)
3040 && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
3041 {
3042 let probe = self.lookup_probe_for_diagnostic(
3043 seg2.ident,
3044 self_ty,
3045 call_expr,
3046 ProbeScope::TraitsInScope,
3047 None,
3048 );
3049 if probe.is_ok() {
3050 let sm = self.infcx.tcx.sess.source_map();
3051 err.span_suggestion_verbose(
3052 sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':')
3053 .unwrap(),
3054 "you may have meant to call an instance method",
3055 ".",
3056 Applicability::MaybeIncorrect,
3057 );
3058 }
3059 }
3060 },
3061 );
3062 }
3063
3064 fn suggest_calling_method_on_field(
3066 &self,
3067 err: &mut Diag<'_>,
3068 source: SelfSource<'tcx>,
3069 span: Span,
3070 actual: Ty<'tcx>,
3071 item_name: Ident,
3072 return_type: Option<Ty<'tcx>>,
3073 ) {
3074 if let SelfSource::MethodCall(expr) = source {
3075 let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
3076 for fields in self.get_field_candidates_considering_privacy_for_diag(
3077 span,
3078 actual,
3079 mod_id,
3080 expr.hir_id,
3081 ) {
3082 let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(expr.hir_id));
3083
3084 let lang_items = self.tcx.lang_items();
3085 let never_mention_traits = [
3086 lang_items.clone_trait(),
3087 lang_items.deref_trait(),
3088 lang_items.deref_mut_trait(),
3089 self.tcx.get_diagnostic_item(sym::AsRef),
3090 self.tcx.get_diagnostic_item(sym::AsMut),
3091 self.tcx.get_diagnostic_item(sym::Borrow),
3092 self.tcx.get_diagnostic_item(sym::BorrowMut),
3093 ];
3094 let mut candidate_fields: Vec<_> = fields
3095 .into_iter()
3096 .filter_map(|candidate_field| {
3097 self.check_for_nested_field_satisfying_condition_for_diag(
3098 span,
3099 &|_, field_ty| {
3100 self.lookup_probe_for_diagnostic(
3101 item_name,
3102 field_ty,
3103 call_expr,
3104 ProbeScope::TraitsInScope,
3105 return_type,
3106 )
3107 .is_ok_and(|pick| {
3108 !never_mention_traits
3109 .iter()
3110 .flatten()
3111 .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
3112 })
3113 },
3114 candidate_field,
3115 ::alloc::vec::Vec::new()vec![],
3116 mod_id,
3117 expr.hir_id,
3118 )
3119 })
3120 .map(|field_path| {
3121 field_path
3122 .iter()
3123 .map(|id| id.to_string())
3124 .collect::<Vec<String>>()
3125 .join(".")
3126 })
3127 .collect();
3128 candidate_fields.sort();
3129
3130 let len = candidate_fields.len();
3131 if len > 0 {
3132 err.span_suggestions(
3133 item_name.span.shrink_to_lo(),
3134 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} of the expressions\' fields {1} a method of the same name",
if len > 1 { "some" } else { "one" },
if len > 1 { "have" } else { "has" }))
})format!(
3135 "{} of the expressions' fields {} a method of the same name",
3136 if len > 1 { "some" } else { "one" },
3137 if len > 1 { "have" } else { "has" },
3138 ),
3139 candidate_fields.iter().map(|path| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.", path))
})format!("{path}.")),
3140 Applicability::MaybeIncorrect,
3141 );
3142 }
3143 }
3144 }
3145 }
3146
3147 fn suggest_unwrapping_inner_self(
3148 &self,
3149 err: &mut Diag<'_>,
3150 source: SelfSource<'tcx>,
3151 actual: Ty<'tcx>,
3152 item_name: Ident,
3153 ) {
3154 let tcx = self.tcx;
3155 let SelfSource::MethodCall(expr) = source else {
3156 return;
3157 };
3158 let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
3159
3160 let ty::Adt(kind, args) = actual.kind() else {
3161 return;
3162 };
3163 match kind.adt_kind() {
3164 ty::AdtKind::Enum => {
3165 let matching_variants: Vec<_> = kind
3166 .variants()
3167 .iter()
3168 .flat_map(|variant| {
3169 let [field] = &variant.fields.raw[..] else {
3170 return None;
3171 };
3172 let field_ty = field.ty(tcx, args);
3173
3174 if self.resolve_vars_if_possible(field_ty).is_ty_var() {
3176 return None;
3177 }
3178
3179 self.lookup_probe_for_diagnostic(
3180 item_name,
3181 field_ty,
3182 call_expr,
3183 ProbeScope::TraitsInScope,
3184 None,
3185 )
3186 .ok()
3187 .map(|pick| (variant, field, pick))
3188 })
3189 .collect();
3190
3191 let ret_ty_matches = |diagnostic_item| {
3192 if let Some(ret_ty) = self
3193 .ret_coercion
3194 .as_ref()
3195 .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
3196 && let ty::Adt(kind, _) = ret_ty.kind()
3197 && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
3198 {
3199 true
3200 } else {
3201 false
3202 }
3203 };
3204
3205 match &matching_variants[..] {
3206 [(_, field, pick)] => {
3207 let self_ty = field.ty(tcx, args);
3208 err.span_note(
3209 tcx.def_span(pick.item.def_id),
3210 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the method `{0}` exists on the type `{1}`",
item_name, self_ty))
})format!("the method `{item_name}` exists on the type `{self_ty}`"),
3211 );
3212 let (article, kind, variant, question) = if tcx.is_diagnostic_item(sym::Result, kind.did())
3213 && !tcx.hir_is_inside_const_context(expr.hir_id)
3215 {
3216 ("a", "Result", "Err", ret_ty_matches(sym::Result))
3217 } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
3218 ("an", "Option", "None", ret_ty_matches(sym::Option))
3219 } else {
3220 return;
3221 };
3222 if question {
3223 err.span_suggestion_verbose(
3224 expr.span.shrink_to_hi(),
3225 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use the `?` operator to extract the `{0}` value, propagating {1} `{2}::{3}` value to the caller",
self_ty, article, kind, variant))
})format!(
3226 "use the `?` operator to extract the `{self_ty}` value, propagating \
3227 {article} `{kind}::{variant}` value to the caller"
3228 ),
3229 "?",
3230 Applicability::MachineApplicable,
3231 );
3232 } else {
3233 err.span_suggestion_verbose(
3234 expr.span.shrink_to_hi(),
3235 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider using `{0}::expect` to unwrap the `{1}` value, panicking if the value is {2} `{0}::{3}`",
kind, self_ty, article, variant))
})format!(
3236 "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
3237 panicking if the value is {article} `{kind}::{variant}`"
3238 ),
3239 ".expect(\"REASON\")",
3240 Applicability::HasPlaceholders,
3241 );
3242 }
3243 }
3244 _ => {}
3246 }
3247 }
3248 ty::AdtKind::Struct | ty::AdtKind::Union => {
3251 let [first] = ***args else {
3252 return;
3253 };
3254 let ty::GenericArgKind::Type(ty) = first.kind() else {
3255 return;
3256 };
3257 let Ok(pick) = self.lookup_probe_for_diagnostic(
3258 item_name,
3259 ty,
3260 call_expr,
3261 ProbeScope::TraitsInScope,
3262 None,
3263 ) else {
3264 return;
3265 };
3266
3267 let name = self.ty_to_value_string(actual);
3268 let inner_id = kind.did();
3269 let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
3270 pick.autoref_or_ptr_adjustment
3271 {
3272 Some(mutbl)
3273 } else {
3274 None
3275 };
3276
3277 if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
3278 err.help("use `with` or `try_with` to access thread local storage");
3279 } else if tcx.is_lang_item(kind.did(), LangItem::MaybeUninit) {
3280 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if this `{0}` has been initialized, use one of the `assume_init` methods to access the inner value",
name))
})format!(
3281 "if this `{name}` has been initialized, \
3282 use one of the `assume_init` methods to access the inner value"
3283 ));
3284 } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
3285 let (suggestion, borrow_kind, panic_if) = match mutable {
3286 Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
3287 Some(Mutability::Mut) => {
3288 (".borrow_mut()", "mutably borrow", "any borrows exist")
3289 }
3290 None => return,
3291 };
3292 err.span_suggestion_verbose(
3293 expr.span.shrink_to_hi(),
3294 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use `{0}` to {1} the `{2}`, panicking if {3}",
suggestion, borrow_kind, ty, panic_if))
})format!(
3295 "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3296 panicking if {panic_if}"
3297 ),
3298 suggestion,
3299 Applicability::MaybeIncorrect,
3300 );
3301 } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
3302 err.span_suggestion_verbose(
3303 expr.span.shrink_to_hi(),
3304 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use `.lock().unwrap()` to borrow the `{0}`, blocking the current thread until it can be acquired",
ty))
})format!(
3305 "use `.lock().unwrap()` to borrow the `{ty}`, \
3306 blocking the current thread until it can be acquired"
3307 ),
3308 ".lock().unwrap()",
3309 Applicability::MaybeIncorrect,
3310 );
3311 } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
3312 let (suggestion, borrow_kind) = match mutable {
3313 Some(Mutability::Not) => (".read().unwrap()", "borrow"),
3314 Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
3315 None => return,
3316 };
3317 err.span_suggestion_verbose(
3318 expr.span.shrink_to_hi(),
3319 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use `{0}` to {1} the `{2}`, blocking the current thread until it can be acquired",
suggestion, borrow_kind, ty))
})format!(
3320 "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3321 blocking the current thread until it can be acquired"
3322 ),
3323 suggestion,
3324 Applicability::MaybeIncorrect,
3325 );
3326 } else {
3327 return;
3328 };
3329
3330 err.span_note(
3331 tcx.def_span(pick.item.def_id),
3332 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the method `{0}` exists on the type `{1}`",
item_name, ty))
})format!("the method `{item_name}` exists on the type `{ty}`"),
3333 );
3334 }
3335 }
3336 }
3337
3338 pub(crate) fn note_unmet_impls_on_type(
3339 &self,
3340 err: &mut Diag<'_>,
3341 errors: &[FulfillmentError<'tcx>],
3342 suggest_derive: bool,
3343 ) {
3344 let preds: Vec<_> = errors
3345 .iter()
3346 .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
3347 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
3348 match pred.self_ty().kind() {
3349 ty::Adt(_, _) => Some((e.root_obligation.predicate, pred)),
3350 _ => None,
3351 }
3352 }
3353 _ => None,
3354 })
3355 .collect();
3356
3357 let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
3359 preds.iter().partition(|&(_, pred)| {
3360 if let ty::Adt(def, _) = pred.self_ty().kind() {
3361 def.did().is_local()
3362 } else {
3363 false
3364 }
3365 });
3366
3367 local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
3368 let local_def_ids = local_preds
3369 .iter()
3370 .filter_map(|(_, pred)| match pred.self_ty().kind() {
3371 ty::Adt(def, _) => Some(def.did()),
3372 _ => None,
3373 })
3374 .collect::<FxIndexSet<_>>();
3375 let mut local_spans: MultiSpan = local_def_ids
3376 .iter()
3377 .filter_map(|def_id| {
3378 let span = self.tcx.def_span(*def_id);
3379 if span.is_dummy() { None } else { Some(span) }
3380 })
3381 .collect::<Vec<_>>()
3382 .into();
3383 for (_, pred) in &local_preds {
3384 if let ty::Adt(def, _) = pred.self_ty().kind() {
3385 local_spans.push_span_label(
3386 self.tcx.def_span(def.did()),
3387 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("must implement `{0}`",
pred.trait_ref.print_trait_sugared()))
})format!("must implement `{}`", pred.trait_ref.print_trait_sugared()),
3388 );
3389 }
3390 }
3391 if local_spans.primary_span().is_some() {
3392 let msg = if let [(_, local_pred)] = local_preds.as_slice() {
3393 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("an implementation of `{0}` might be missing for `{1}`",
local_pred.trait_ref.print_trait_sugared(),
local_pred.self_ty()))
})format!(
3394 "an implementation of `{}` might be missing for `{}`",
3395 local_pred.trait_ref.print_trait_sugared(),
3396 local_pred.self_ty()
3397 )
3398 } else {
3399 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following type{0} would have to `impl` {1} required trait{2} for this operation to be valid",
if local_def_ids.len() == 1 { "" } else { "s" },
if local_def_ids.len() == 1 { "its" } else { "their" },
if local_preds.len() == 1 { "" } else { "s" }))
})format!(
3400 "the following type{} would have to `impl` {} required trait{} for this \
3401 operation to be valid",
3402 pluralize!(local_def_ids.len()),
3403 if local_def_ids.len() == 1 { "its" } else { "their" },
3404 pluralize!(local_preds.len()),
3405 )
3406 };
3407 err.span_note(local_spans, msg);
3408 }
3409
3410 foreign_preds
3411 .sort_by_key(|(_, pred): &(_, ty::TraitPredicate<'_>)| pred.trait_ref.to_string());
3412
3413 for (_, pred) in &foreign_preds {
3414 let ty = pred.self_ty();
3415 let ty::Adt(def, _) = ty.kind() else { continue };
3416 let span = self.tcx.def_span(def.did());
3417 if span.is_dummy() {
3418 continue;
3419 }
3420 let mut mspan: MultiSpan = span.into();
3421 mspan.push_span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is defined in another crate",
ty))
})format!("`{ty}` is defined in another crate"));
3422 err.span_note(
3423 mspan,
3424 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{1}` does not implement `{0}`",
pred.trait_ref.print_trait_sugared(), ty))
})format!("`{ty}` does not implement `{}`", pred.trait_ref.print_trait_sugared()),
3425 );
3426
3427 foreign_preds.iter().find(|&(root_pred, pred)| {
3428 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred)) =
3429 root_pred.kind().skip_binder()
3430 && let Some(root_adt) = root_pred.self_ty().ty_adt_def()
3431 {
3432 self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(err, pred, root_adt)
3433 } else {
3434 false
3435 }
3436 });
3437 }
3438
3439 let preds: Vec<_> = errors
3440 .iter()
3441 .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
3442 .collect();
3443 if suggest_derive {
3444 self.suggest_derive(err, &preds);
3445 } else {
3446 let _ = self.note_predicate_source_and_get_derives(err, &preds);
3448 }
3449 }
3450
3451 fn consider_suggesting_derives_for_ty(
3454 &self,
3455 trait_pred: ty::TraitPredicate<'tcx>,
3456 adt: ty::AdtDef<'tcx>,
3457 ) -> Option<Vec<(String, Span, Symbol)>> {
3458 let diagnostic_name = self.tcx.get_diagnostic_name(trait_pred.def_id())?;
3459
3460 let can_derive = match diagnostic_name {
3461 sym::Copy | sym::Clone => true,
3462 _ if adt.is_union() => false,
3463 sym::Default
3464 | sym::Eq
3465 | sym::PartialEq
3466 | sym::Ord
3467 | sym::PartialOrd
3468 | sym::Hash
3469 | sym::Debug => true,
3470 _ => false,
3471 };
3472
3473 if !can_derive {
3474 return None;
3475 }
3476
3477 let trait_def_id = trait_pred.def_id();
3478 let self_ty = trait_pred.self_ty();
3479
3480 if self.tcx.non_blanket_impls_for_ty(trait_def_id, self_ty).any(|impl_def_id| {
3483 self.tcx
3484 .type_of(impl_def_id)
3485 .instantiate_identity()
3486 .ty_adt_def()
3487 .is_some_and(|def| def.did() == adt.did())
3488 }) {
3489 return None;
3490 }
3491
3492 let mut derives = Vec::new();
3493 let self_name = self_ty.to_string();
3494 let self_span = self.tcx.def_span(adt.did());
3495
3496 for super_trait in supertraits(self.tcx, ty::Binder::dummy(trait_pred.trait_ref)) {
3497 if let Some(parent_diagnostic_name) = self.tcx.get_diagnostic_name(super_trait.def_id())
3498 {
3499 derives.push((self_name.clone(), self_span, parent_diagnostic_name));
3500 }
3501 }
3502
3503 derives.push((self_name, self_span, diagnostic_name));
3504
3505 Some(derives)
3506 }
3507
3508 fn note_predicate_source_and_get_derives(
3509 &self,
3510 err: &mut Diag<'_>,
3511 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3512 ) -> Vec<(String, Span, Symbol)> {
3513 let mut derives = Vec::new();
3514 let mut traits = Vec::new();
3515 for (pred, _, _) in unsatisfied_predicates {
3516 let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) =
3517 pred.kind().no_bound_vars()
3518 else {
3519 continue;
3520 };
3521 let adt = match trait_pred.self_ty().ty_adt_def() {
3522 Some(adt) if adt.did().is_local() => adt,
3523 _ => continue,
3524 };
3525 if let Some(new_derives) = self.consider_suggesting_derives_for_ty(trait_pred, adt) {
3526 derives.extend(new_derives);
3527 } else {
3528 traits.push(trait_pred.def_id());
3529 }
3530 }
3531 traits.sort_by_key(|&id| self.tcx.def_path_str(id));
3532 traits.dedup();
3533
3534 let len = traits.len();
3535 if len > 0 {
3536 let span =
3537 MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
3538 let mut names = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`",
self.tcx.def_path_str(traits[0])))
})format!("`{}`", self.tcx.def_path_str(traits[0]));
3539 for (i, &did) in traits.iter().enumerate().skip(1) {
3540 if len > 2 {
3541 names.push_str(", ");
3542 }
3543 if i == len - 1 {
3544 names.push_str(" and ");
3545 }
3546 names.push('`');
3547 names.push_str(&self.tcx.def_path_str(did));
3548 names.push('`');
3549 }
3550 err.span_note(
3551 span,
3552 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait{0} {1} must be implemented",
if len == 1 { "" } else { "s" }, names))
})format!("the trait{} {} must be implemented", pluralize!(len), names),
3553 );
3554 }
3555
3556 derives
3557 }
3558
3559 pub(crate) fn suggest_derive(
3560 &self,
3561 err: &mut Diag<'_>,
3562 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3563 ) -> bool {
3564 let mut derives = self.note_predicate_source_and_get_derives(err, unsatisfied_predicates);
3565 derives.sort();
3566 derives.dedup();
3567
3568 let mut derives_grouped = Vec::<(String, Span, String)>::new();
3569 for (self_name, self_span, trait_name) in derives.into_iter() {
3570 if let Some((last_self_name, _, last_trait_names)) = derives_grouped.last_mut() {
3571 if last_self_name == &self_name {
3572 last_trait_names.push_str(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}", trait_name))
})format!(", {trait_name}").as_str());
3573 continue;
3574 }
3575 }
3576 derives_grouped.push((self_name, self_span, trait_name.to_string()));
3577 }
3578
3579 for (self_name, self_span, traits) in &derives_grouped {
3580 err.span_suggestion_verbose(
3581 self_span.shrink_to_lo(),
3582 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider annotating `{0}` with `#[derive({1})]`",
self_name, traits))
})format!("consider annotating `{self_name}` with `#[derive({traits})]`"),
3583 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#[derive({0})]\n", traits))
})format!("#[derive({traits})]\n"),
3584 Applicability::MaybeIncorrect,
3585 );
3586 }
3587 !derives_grouped.is_empty()
3588 }
3589
3590 fn note_derefed_ty_has_method(
3591 &self,
3592 err: &mut Diag<'_>,
3593 self_source: SelfSource<'tcx>,
3594 rcvr_ty: Ty<'tcx>,
3595 item_name: Ident,
3596 expected: Expectation<'tcx>,
3597 ) {
3598 let SelfSource::QPath(ty) = self_source else {
3599 return;
3600 };
3601 for (deref_ty, _) in self.autoderef(DUMMY_SP, rcvr_ty).silence_errors().skip(1) {
3602 if let Ok(pick) = self.probe_for_name(
3603 Mode::Path,
3604 item_name,
3605 expected.only_has_type(self),
3606 IsSuggestion(true),
3607 deref_ty,
3608 ty.hir_id,
3609 ProbeScope::TraitsInScope,
3610 ) {
3611 if deref_ty.is_suggestable(self.tcx, true)
3612 && pick.item.is_method()
3616 && let Some(self_ty) =
3617 self.tcx.fn_sig(pick.item.def_id).instantiate_identity().inputs().skip_binder().get(0)
3618 && self_ty.is_ref()
3619 {
3620 let suggested_path = match deref_ty.kind() {
3621 ty::Bool
3622 | ty::Char
3623 | ty::Int(_)
3624 | ty::Uint(_)
3625 | ty::Float(_)
3626 | ty::Adt(_, _)
3627 | ty::Str
3628 | ty::Alias(ty::Projection | ty::Inherent, _)
3629 | ty::Param(_) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", deref_ty))
})format!("{deref_ty}"),
3630 _ if self
3636 .tcx
3637 .sess
3638 .source_map()
3639 .span_wrapped_by_angle_or_parentheses(ty.span) =>
3640 {
3641 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", deref_ty))
})format!("{deref_ty}")
3642 }
3643 _ => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", deref_ty))
})format!("<{deref_ty}>"),
3644 };
3645 err.span_suggestion_verbose(
3646 ty.span,
3647 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the function `{0}` is implemented on `{1}`",
item_name, deref_ty))
})format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3648 suggested_path,
3649 Applicability::MaybeIncorrect,
3650 );
3651 } else {
3652 err.span_note(
3653 ty.span,
3654 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the function `{0}` is implemented on `{1}`",
item_name, deref_ty))
})format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3655 );
3656 }
3657 return;
3658 }
3659 }
3660 }
3661
3662 fn suggest_bounds_for_range_to_method(
3663 &self,
3664 err: &mut Diag<'_>,
3665 source: SelfSource<'tcx>,
3666 item_ident: Ident,
3667 ) {
3668 let SelfSource::MethodCall(rcvr_expr) = source else { return };
3669 let hir::ExprKind::Struct(qpath, fields, _) = rcvr_expr.kind else { return };
3670 let Some(lang_item) = self.tcx.qpath_lang_item(*qpath) else {
3671 return;
3672 };
3673 let is_inclusive = match lang_item {
3674 hir::LangItem::RangeTo => false,
3675 hir::LangItem::RangeToInclusive | hir::LangItem::RangeInclusiveCopy => true,
3676 _ => return,
3677 };
3678
3679 let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) else { return };
3680 let Some(_) = self
3681 .tcx
3682 .associated_items(iterator_trait)
3683 .filter_by_name_unhygienic(item_ident.name)
3684 .next()
3685 else {
3686 return;
3687 };
3688
3689 let source_map = self.tcx.sess.source_map();
3690 let range_type = if is_inclusive { "RangeInclusive" } else { "Range" };
3691 let Some(end_field) = fields.iter().find(|f| f.ident.name == rustc_span::sym::end) else {
3692 return;
3693 };
3694
3695 let element_ty = self.typeck_results.borrow().expr_ty_opt(end_field.expr);
3696 let is_integral = element_ty.is_some_and(|ty| ty.is_integral());
3697 let end_is_negative = is_integral
3698 && #[allow(non_exhaustive_omitted_patterns)] match end_field.expr.kind {
hir::ExprKind::Unary(rustc_ast::UnOp::Neg, _) => true,
_ => false,
}matches!(end_field.expr.kind, hir::ExprKind::Unary(rustc_ast::UnOp::Neg, _));
3699
3700 let Ok(snippet) = source_map.span_to_snippet(rcvr_expr.span) else { return };
3701
3702 let offset = snippet
3703 .chars()
3704 .take_while(|&c| c == '(' || c.is_whitespace())
3705 .map(|c| c.len_utf8())
3706 .sum::<usize>();
3707
3708 let insert_span = rcvr_expr
3709 .span
3710 .with_lo(rcvr_expr.span.lo() + rustc_span::BytePos(offset as u32))
3711 .shrink_to_lo();
3712
3713 let (value, appl) = if is_integral && !end_is_negative {
3714 ("0", Applicability::MachineApplicable)
3715 } else {
3716 ("/* start */", Applicability::HasPlaceholders)
3717 };
3718
3719 err.span_suggestion_verbose(
3720 insert_span,
3721 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider using a bounded `{0}` by adding a concrete starting value",
range_type))
})format!("consider using a bounded `{range_type}` by adding a concrete starting value"),
3722 value,
3723 appl,
3724 );
3725 }
3726
3727 fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
3729 match ty.kind() {
3730 ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
3731 _ => self.ty_to_string(ty),
3732 }
3733 }
3734
3735 fn suggest_await_before_method(
3736 &self,
3737 err: &mut Diag<'_>,
3738 item_name: Ident,
3739 ty: Ty<'tcx>,
3740 call: &hir::Expr<'_>,
3741 span: Span,
3742 return_type: Option<Ty<'tcx>>,
3743 ) {
3744 let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else { return };
3745 let output_ty = self.resolve_vars_if_possible(output_ty);
3746 let method_exists =
3747 self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
3748 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/suggest.rs:3748",
"rustc_hir_typeck::method::suggest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(3748u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("suggest_await_before_method: is_method_exist={0}",
method_exists) as &dyn Value))])
});
} else { ; }
};debug!("suggest_await_before_method: is_method_exist={}", method_exists);
3749 if method_exists {
3750 err.span_suggestion_verbose(
3751 span.shrink_to_lo(),
3752 "consider `await`ing on the `Future` and calling the method on its `Output`",
3753 "await.",
3754 Applicability::MaybeIncorrect,
3755 );
3756 }
3757 }
3758
3759 fn set_label_for_method_error(
3760 &self,
3761 err: &mut Diag<'_>,
3762 source: SelfSource<'tcx>,
3763 rcvr_ty: Ty<'tcx>,
3764 item_ident: Ident,
3765 expr_id: hir::HirId,
3766 span: Span,
3767 sugg_span: Span,
3768 within_macro_span: Option<Span>,
3769 args: Option<&'tcx [hir::Expr<'tcx>]>,
3770 ) {
3771 let tcx = self.tcx;
3772 if tcx.sess.source_map().is_multiline(sugg_span) {
3773 err.span_label(sugg_span.with_hi(span.lo()), "");
3774 }
3775 if let Some(within_macro_span) = within_macro_span {
3776 err.span_label(within_macro_span, "due to this macro variable");
3777 }
3778
3779 if #[allow(non_exhaustive_omitted_patterns)] match source {
SelfSource::QPath(_) => true,
_ => false,
}matches!(source, SelfSource::QPath(_)) && args.is_some() {
3780 self.find_builder_fn(err, rcvr_ty, expr_id);
3781 }
3782
3783 if tcx.ty_is_opaque_future(rcvr_ty) && item_ident.name == sym::poll {
3784 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
3785 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("method `poll` found on `Pin<&mut {0}>`, see documentation for `std::pin::Pin`",
ty_str))
})format!(
3786 "method `poll` found on `Pin<&mut {ty_str}>`, \
3787 see documentation for `std::pin::Pin`"
3788 ));
3789 err.help(
3790 "self type must be pinned to call `Future::poll`, \
3791 see https://rust-lang.github.io/async-book/part-reference/pinning.html",
3792 );
3793 }
3794
3795 if let Some(span) =
3796 tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
3797 {
3798 err.span_suggestion(
3799 span.shrink_to_lo(),
3800 "you are looking for the module in `std`, not the primitive type",
3801 "std::",
3802 Applicability::MachineApplicable,
3803 );
3804 }
3805 }
3806
3807 fn suggest_on_pointer_type(
3808 &self,
3809 err: &mut Diag<'_>,
3810 source: SelfSource<'tcx>,
3811 rcvr_ty: Ty<'tcx>,
3812 item_ident: Ident,
3813 ) {
3814 let tcx = self.tcx;
3815 if let SelfSource::MethodCall(rcvr_expr) = source
3817 && let ty::RawPtr(ty, ptr_mutbl) = *rcvr_ty.kind()
3818 && let Ok(pick) = self.lookup_probe_for_diagnostic(
3819 item_ident,
3820 Ty::new_ref(tcx, ty::Region::new_error_misc(tcx), ty, ptr_mutbl),
3821 self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)),
3822 ProbeScope::TraitsInScope,
3823 None,
3824 )
3825 && let ty::Ref(_, _, sugg_mutbl) = *pick.self_ty.kind()
3826 && (sugg_mutbl.is_not() || ptr_mutbl.is_mut())
3827 {
3828 let (method, method_anchor) = match sugg_mutbl {
3829 Mutability::Not => {
3830 let method_anchor = match ptr_mutbl {
3831 Mutability::Not => "as_ref",
3832 Mutability::Mut => "as_ref-1",
3833 };
3834 ("as_ref", method_anchor)
3835 }
3836 Mutability::Mut => ("as_mut", "as_mut"),
3837 };
3838 err.span_note(
3839 tcx.def_span(pick.item.def_id),
3840 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the method `{1}` exists on the type `{0}`",
pick.self_ty, item_ident))
})format!("the method `{item_ident}` exists on the type `{ty}`", ty = pick.self_ty),
3841 );
3842 let mut_str = ptr_mutbl.ptr_str();
3843 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might want to use the unsafe method `<*{0} T>::{1}` to get an optional reference to the value behind the pointer",
mut_str, method))
})format!(
3844 "you might want to use the unsafe method `<*{mut_str} T>::{method}` to get \
3845 an optional reference to the value behind the pointer"
3846 ));
3847 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("read the documentation for `<*{0} T>::{1}` and ensure you satisfy its safety preconditions before calling it to avoid undefined behavior: https://doc.rust-lang.org/std/primitive.pointer.html#method.{2}",
mut_str, method, method_anchor))
})format!(
3848 "read the documentation for `<*{mut_str} T>::{method}` and ensure you satisfy its \
3849 safety preconditions before calling it to avoid undefined behavior: \
3850 https://doc.rust-lang.org/std/primitive.pointer.html#method.{method_anchor}"
3851 ));
3852 }
3853 }
3854
3855 fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
3856 where
3857 F: FnOnce(Vec<String>, Vec<String>, Span),
3858 {
3859 let parent_map = self.tcx.visible_parent_map(());
3860
3861 let scope = self.tcx.parent_module_from_def_id(self.body_id);
3862 let (accessible_candidates, inaccessible_candidates): (Vec<_>, Vec<_>) =
3863 candidates.into_iter().partition(|id| {
3864 let vis = self.tcx.visibility(*id);
3865 vis.is_accessible_from(scope, self.tcx)
3866 });
3867
3868 let sugg = |candidates: Vec<_>, visible| {
3869 let (candidates, globs): (Vec<_>, Vec<_>) =
3872 candidates.into_iter().partition(|trait_did| {
3873 if let Some(parent_did) = parent_map.get(trait_did) {
3874 if *parent_did != self.tcx.parent(*trait_did)
3876 && self
3877 .tcx
3878 .module_children(*parent_did)
3879 .iter()
3880 .filter(|child| child.res.opt_def_id() == Some(*trait_did))
3881 .all(|child| child.ident.name == kw::Underscore)
3882 {
3883 return false;
3884 }
3885 }
3886
3887 true
3888 });
3889
3890 let prefix = if visible { "use " } else { "" };
3891 let postfix = if visible { ";" } else { "" };
3892 let path_strings = candidates.iter().map(|trait_did| {
3893 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}{2}\n",
{
let _guard = NoVisibleIfDocHiddenGuard::new();
{
let _guard = CratePrefixGuard::new();
self.tcx.def_path_str(*trait_did)
}
}, prefix, postfix))
})format!(
3894 "{prefix}{}{postfix}\n",
3895 with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3896 self.tcx.def_path_str(*trait_did)
3897 )),
3898 )
3899 });
3900
3901 let glob_path_strings = globs.iter().map(|trait_did| {
3902 let parent_did = parent_map.get(trait_did).unwrap();
3903 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2}{0}::*{3} // trait {1}\n",
{
let _guard = NoVisibleIfDocHiddenGuard::new();
{
let _guard = CratePrefixGuard::new();
self.tcx.def_path_str(*parent_did)
}
}, self.tcx.item_name(*trait_did), prefix, postfix))
})format!(
3904 "{prefix}{}::*{postfix} // trait {}\n",
3905 with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3906 self.tcx.def_path_str(*parent_did)
3907 )),
3908 self.tcx.item_name(*trait_did),
3909 )
3910 });
3911 let mut sugg: Vec<_> = path_strings.chain(glob_path_strings).collect();
3912 sugg.sort();
3913 sugg
3914 };
3915
3916 let accessible_sugg = sugg(accessible_candidates, true);
3917 let inaccessible_sugg = sugg(inaccessible_candidates, false);
3918
3919 let (module, _, _) = self.tcx.hir_get_module(scope);
3920 let span = module.spans.inject_use_span;
3921 handle_candidates(accessible_sugg, inaccessible_sugg, span);
3922 }
3923
3924 fn suggest_valid_traits(
3925 &self,
3926 err: &mut Diag<'_>,
3927 item_name: Ident,
3928 mut valid_out_of_scope_traits: Vec<DefId>,
3929 explain: bool,
3930 ) -> bool {
3931 valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
3932 if !valid_out_of_scope_traits.is_empty() {
3933 let mut candidates = valid_out_of_scope_traits;
3934 candidates.sort_by_key(|&id| self.tcx.def_path_str(id));
3935 candidates.dedup();
3936
3937 let edition_fix = candidates
3939 .iter()
3940 .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
3941 .copied();
3942
3943 if explain {
3944 err.help("items from traits can only be used if the trait is in scope");
3945 }
3946
3947 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} implemented but not in scope",
if candidates.len() == 1 {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("trait `{0}` which provides `{1}` is",
self.tcx.item_name(candidates[0]), item_name))
})
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following traits which provide `{0}` are",
item_name))
})
}))
})format!(
3948 "{this_trait_is} implemented but not in scope",
3949 this_trait_is = if candidates.len() == 1 {
3950 format!(
3951 "trait `{}` which provides `{item_name}` is",
3952 self.tcx.item_name(candidates[0]),
3953 )
3954 } else {
3955 format!("the following traits which provide `{item_name}` are")
3956 }
3957 );
3958
3959 self.suggest_use_candidates(candidates, |accessible_sugg, inaccessible_sugg, span| {
3960 let suggest_for_access = |err: &mut Diag<'_>, mut msg: String, suggs: Vec<_>| {
3961 msg += &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("; perhaps you want to import {0}",
if suggs.len() == 1 { "it" } else { "one of them" }))
})format!(
3962 "; perhaps you want to import {one_of}",
3963 one_of = if suggs.len() == 1 { "it" } else { "one of them" },
3964 );
3965 err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3966 };
3967 let suggest_for_privacy = |err: &mut Diag<'_>, suggs: Vec<String>| {
3968 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} implemented but not reachable",
if let [sugg] = suggs.as_slice() {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("trait `{0}` which provides `{1}` is",
sugg.trim(), item_name))
})
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following traits which provide `{0}` are",
item_name))
})
}))
})format!(
3969 "{this_trait_is} implemented but not reachable",
3970 this_trait_is = if let [sugg] = suggs.as_slice() {
3971 format!("trait `{}` which provides `{item_name}` is", sugg.trim())
3972 } else {
3973 format!("the following traits which provide `{item_name}` are")
3974 }
3975 );
3976 if suggs.len() == 1 {
3977 err.help(msg);
3978 } else {
3979 err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3980 }
3981 };
3982 if accessible_sugg.is_empty() {
3983 suggest_for_privacy(err, inaccessible_sugg);
3985 } else if inaccessible_sugg.is_empty() {
3986 suggest_for_access(err, msg, accessible_sugg);
3987 } else {
3988 suggest_for_access(err, msg, accessible_sugg);
3989 suggest_for_privacy(err, inaccessible_sugg);
3990 }
3991 });
3992
3993 if let Some(did) = edition_fix {
3994 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0}\' is included in the prelude starting in Edition 2021",
{
let _guard = CratePrefixGuard::new();
self.tcx.def_path_str(did)
}))
})format!(
3995 "'{}' is included in the prelude starting in Edition 2021",
3996 with_crate_prefix!(self.tcx.def_path_str(did))
3997 ));
3998 }
3999
4000 true
4001 } else {
4002 false
4003 }
4004 }
4005
4006 fn suggest_traits_to_import(
4007 &self,
4008 err: &mut Diag<'_>,
4009 span: Span,
4010 rcvr_ty: Ty<'tcx>,
4011 item_name: Ident,
4012 inputs_len: Option<usize>,
4013 source: SelfSource<'tcx>,
4014 valid_out_of_scope_traits: Vec<DefId>,
4015 static_candidates: &[CandidateSource],
4016 unsatisfied_bounds: bool,
4017 return_type: Option<Ty<'tcx>>,
4018 trait_missing_method: bool,
4019 ) {
4020 let mut alt_rcvr_sugg = false;
4021 let mut trait_in_other_version_found = false;
4022 if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
4023 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/suggest.rs:4023",
"rustc_hir_typeck::method::suggest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(4023u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("suggest_traits_to_import: span={0:?}, item_name={1:?}, rcvr_ty={2:?}, rcvr={3:?}",
span, item_name, rcvr_ty, rcvr) as &dyn Value))])
});
} else { ; }
};debug!(
4024 "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
4025 span, item_name, rcvr_ty, rcvr
4026 );
4027 let skippable = [
4028 self.tcx.lang_items().clone_trait(),
4029 self.tcx.lang_items().deref_trait(),
4030 self.tcx.lang_items().deref_mut_trait(),
4031 self.tcx.lang_items().drop_trait(),
4032 self.tcx.get_diagnostic_item(sym::AsRef),
4033 ];
4034 for (rcvr_ty, post, pin_call) in &[
4038 (rcvr_ty, "", None),
4039 (
4040 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
4041 "&mut ",
4042 Some("as_mut"),
4043 ),
4044 (
4045 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
4046 "&",
4047 Some("as_ref"),
4048 ),
4049 ] {
4050 match self.lookup_probe_for_diagnostic(
4051 item_name,
4052 *rcvr_ty,
4053 rcvr,
4054 ProbeScope::AllTraits,
4055 return_type,
4056 ) {
4057 Ok(pick) => {
4058 let did = Some(pick.item.container_id(self.tcx));
4063 if skippable.contains(&did) {
4064 continue;
4065 }
4066 trait_in_other_version_found = self
4067 .detect_and_explain_multiple_crate_versions_of_trait_item(
4068 err,
4069 pick.item.def_id,
4070 rcvr.hir_id,
4071 Some(*rcvr_ty),
4072 );
4073 if pick.autoderefs == 0 && !trait_in_other_version_found {
4074 err.span_label(
4075 pick.item.ident(self.tcx).span,
4076 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the method is available for `{0}` here",
rcvr_ty))
})format!("the method is available for `{rcvr_ty}` here"),
4077 );
4078 }
4079 break;
4080 }
4081 Err(MethodError::Ambiguity(_)) => {
4082 break;
4087 }
4088 Err(_) => (),
4089 }
4090
4091 let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else {
4092 return;
4093 };
4094 let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
4095 let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
4096 self.tcx,
4097 self.misc(rcvr.span),
4098 self.param_env,
4099 pred,
4100 ));
4101 for (rcvr_ty, pre) in &[
4102 (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::OwnedBox), "Box::new"),
4103 (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin), "Pin::new"),
4104 (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Arc), "Arc::new"),
4105 (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Rc), "Rc::new"),
4106 ] {
4107 if let Some(new_rcvr_t) = *rcvr_ty
4108 && let Ok(pick) = self.lookup_probe_for_diagnostic(
4109 item_name,
4110 new_rcvr_t,
4111 rcvr,
4112 ProbeScope::AllTraits,
4113 return_type,
4114 )
4115 {
4116 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/suggest.rs:4116",
"rustc_hir_typeck::method::suggest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
::tracing_core::__macro_support::Option::Some(4116u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("try_alt_rcvr: pick candidate {0:?}",
pick) as &dyn Value))])
});
} else { ; }
};debug!("try_alt_rcvr: pick candidate {:?}", pick);
4117 let did = pick.item.trait_container(self.tcx);
4118 let skip = skippable.contains(&did)
4124 || (("Pin::new" == *pre)
4125 && ((sym::as_ref == item_name.name) || !unpin))
4126 || inputs_len.is_some_and(|inputs_len| {
4127 pick.item.is_fn()
4128 && self
4129 .tcx
4130 .fn_sig(pick.item.def_id)
4131 .skip_binder()
4132 .skip_binder()
4133 .inputs()
4134 .len()
4135 != inputs_len
4136 });
4137 if pick.autoderefs == 0 && !skip {
4141 err.span_label(
4142 pick.item.ident(self.tcx).span,
4143 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the method is available for `{0}` here",
new_rcvr_t))
})format!("the method is available for `{new_rcvr_t}` here"),
4144 );
4145 err.multipart_suggestion(
4146 "consider wrapping the receiver expression with the \
4147 appropriate type",
4148 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(rcvr.span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}({1}", pre, post))
})), (rcvr.span.shrink_to_hi(), ")".to_string())]))vec![
4149 (rcvr.span.shrink_to_lo(), format!("{pre}({post}")),
4150 (rcvr.span.shrink_to_hi(), ")".to_string()),
4151 ],
4152 Applicability::MaybeIncorrect,
4153 );
4154 alt_rcvr_sugg = true;
4156 }
4157 }
4158 }
4159 if let Some(new_rcvr_t) = Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin)
4162 && !alt_rcvr_sugg
4164 && !unpin
4166 && let Some(pin_call) = pin_call
4168 && let Ok(pick) = self.lookup_probe_for_diagnostic(
4170 item_name,
4171 new_rcvr_t,
4172 rcvr,
4173 ProbeScope::AllTraits,
4174 return_type,
4175 )
4176 && !skippable.contains(&Some(pick.item.container_id(self.tcx)))
4179 && pick.item.impl_container(self.tcx).is_none_or(|did| {
4181 match self.tcx.type_of(did).skip_binder().kind() {
4182 ty::Adt(def, _) => Some(def.did()) != self.tcx.lang_items().pin_type(),
4183 _ => true,
4184 }
4185 })
4186 && pick.autoderefs == 0
4188 && inputs_len.is_some_and(|inputs_len| pick.item.is_fn() && self.tcx.fn_sig(pick.item.def_id).skip_binder().skip_binder().inputs().len() == inputs_len)
4191 {
4192 let indent = self
4193 .tcx
4194 .sess
4195 .source_map()
4196 .indentation_before(rcvr.span)
4197 .unwrap_or_else(|| " ".to_string());
4198 let mut expr = rcvr;
4199 while let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4200 && let hir::ExprKind::MethodCall(hir::PathSegment { .. }, ..) =
4201 call_expr.kind
4202 {
4203 expr = call_expr;
4204 }
4205 match self.tcx.parent_hir_node(expr.hir_id) {
4206 Node::LetStmt(stmt)
4207 if let Some(init) = stmt.init
4208 && let Ok(code) =
4209 self.tcx.sess.source_map().span_to_snippet(rcvr.span) =>
4210 {
4211 err.multipart_suggestion(
4214 "consider pinning the expression",
4215 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(stmt.span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("let mut pinned = std::pin::pin!({0});\n{1}",
code, indent))
})),
(init.span.until(rcvr.span.shrink_to_hi()),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pinned.{0}()", pin_call))
}))]))vec![
4216 (
4217 stmt.span.shrink_to_lo(),
4218 format!(
4219 "let mut pinned = std::pin::pin!({code});\n{indent}"
4220 ),
4221 ),
4222 (
4223 init.span.until(rcvr.span.shrink_to_hi()),
4224 format!("pinned.{pin_call}()"),
4225 ),
4226 ],
4227 Applicability::MaybeIncorrect,
4228 );
4229 }
4230 Node::Block(_) | Node::Stmt(_) => {
4231 err.multipart_suggestion(
4234 "consider pinning the expression",
4235 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(rcvr.span.shrink_to_lo(),
"let mut pinned = std::pin::pin!(".to_string()),
(rcvr.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(");\n{0}pinned.{1}()",
indent, pin_call))
}))]))vec![
4236 (
4237 rcvr.span.shrink_to_lo(),
4238 "let mut pinned = std::pin::pin!(".to_string(),
4239 ),
4240 (
4241 rcvr.span.shrink_to_hi(),
4242 format!(");\n{indent}pinned.{pin_call}()"),
4243 ),
4244 ],
4245 Applicability::MaybeIncorrect,
4246 );
4247 }
4248 _ => {
4249 err.span_help(
4252 rcvr.span,
4253 "consider pinning the expression with `std::pin::pin!()` and \
4254 assigning that to a new binding",
4255 );
4256 }
4257 }
4258 alt_rcvr_sugg = true;
4260 }
4261 }
4262 }
4263
4264 if let SelfSource::QPath(ty) = source
4265 && !valid_out_of_scope_traits.is_empty()
4266 && let hir::TyKind::Path(path) = ty.kind
4267 && let hir::QPath::Resolved(..) = path
4268 && let Some(assoc) = self
4269 .tcx
4270 .associated_items(valid_out_of_scope_traits[0])
4271 .filter_by_name_unhygienic(item_name.name)
4272 .next()
4273 {
4274 let rcvr_ty = self.node_ty_opt(ty.hir_id);
4279 trait_in_other_version_found = self
4280 .detect_and_explain_multiple_crate_versions_of_trait_item(
4281 err,
4282 assoc.def_id,
4283 ty.hir_id,
4284 rcvr_ty,
4285 );
4286 }
4287 if !trait_in_other_version_found
4288 && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
4289 {
4290 return;
4291 }
4292
4293 let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
4294
4295 let mut arbitrary_rcvr = ::alloc::vec::Vec::new()vec![];
4296 let mut candidates = all_traits(self.tcx)
4300 .into_iter()
4301 .filter(|info| match self.tcx.lookup_stability(info.def_id) {
4304 Some(attr) => attr.level.is_stable(),
4305 None => true,
4306 })
4307 .filter(|info| {
4308 static_candidates.iter().all(|sc| match *sc {
4311 CandidateSource::Trait(def_id) => def_id != info.def_id,
4312 CandidateSource::Impl(def_id) => {
4313 self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
4314 }
4315 })
4316 })
4317 .filter(|info| {
4318 (type_is_local || info.def_id.is_local())
4325 && !self.tcx.trait_is_auto(info.def_id)
4326 && self
4327 .associated_value(info.def_id, item_name)
4328 .filter(|item| {
4329 if item.is_fn() {
4330 let id = item
4331 .def_id
4332 .as_local()
4333 .map(|def_id| self.tcx.hir_node_by_def_id(def_id));
4334 if let Some(hir::Node::TraitItem(hir::TraitItem {
4335 kind: hir::TraitItemKind::Fn(fn_sig, method),
4336 ..
4337 })) = id
4338 {
4339 let self_first_arg = match method {
4340 hir::TraitFn::Required([ident, ..]) => {
4341 #[allow(non_exhaustive_omitted_patterns)] match ident {
Some(Ident { name: kw::SelfLower, .. }) => true,
_ => false,
}matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
4342 }
4343 hir::TraitFn::Provided(body_id) => {
4344 self.tcx.hir_body(*body_id).params.first().is_some_and(
4345 |param| {
4346 #[allow(non_exhaustive_omitted_patterns)] match param.pat.kind {
hir::PatKind::Binding(_, _, ident, _) if ident.name == kw::SelfLower =>
true,
_ => false,
}matches!(
4347 param.pat.kind,
4348 hir::PatKind::Binding(_, _, ident, _)
4349 if ident.name == kw::SelfLower
4350 )
4351 },
4352 )
4353 }
4354 _ => false,
4355 };
4356
4357 if !fn_sig.decl.implicit_self.has_implicit_self()
4358 && self_first_arg
4359 {
4360 if let Some(ty) = fn_sig.decl.inputs.get(0) {
4361 arbitrary_rcvr.push(ty.span);
4362 }
4363 return false;
4364 }
4365 }
4366 }
4367 item.visibility(self.tcx).is_public() || info.def_id.is_local()
4369 })
4370 .is_some()
4371 })
4372 .collect::<Vec<_>>();
4373 for span in &arbitrary_rcvr {
4374 err.span_label(
4375 *span,
4376 "the method might not be found because of this arbitrary self type",
4377 );
4378 }
4379 if alt_rcvr_sugg {
4380 return;
4381 }
4382
4383 if !candidates.is_empty() {
4384 candidates
4386 .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
4387 candidates.dedup();
4388
4389 let param_type = match *rcvr_ty.kind() {
4390 ty::Param(param) => Some(param),
4391 ty::Ref(_, ty, _) => match *ty.kind() {
4392 ty::Param(param) => Some(param),
4393 _ => None,
4394 },
4395 _ => None,
4396 };
4397 if !trait_missing_method {
4398 err.help(if param_type.is_some() {
4399 "items from traits can only be used if the type parameter is bounded by the trait"
4400 } else {
4401 "items from traits can only be used if the trait is implemented and in scope"
4402 });
4403 }
4404
4405 let candidates_len = candidates.len();
4406 let message = |action| {
4407 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following {0} an item `{3}`, perhaps you need to {1} {2}:",
if candidates_len == 1 {
"trait defines"
} else { "traits define" }, action,
if candidates_len == 1 { "it" } else { "one of them" },
item_name))
})format!(
4408 "the following {traits_define} an item `{name}`, perhaps you need to {action} \
4409 {one_of_them}:",
4410 traits_define =
4411 if candidates_len == 1 { "trait defines" } else { "traits define" },
4412 action = action,
4413 one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
4414 name = item_name,
4415 )
4416 };
4417 if let Some(param) = param_type {
4419 let generics = self.tcx.generics_of(self.body_id.to_def_id());
4420 let type_param = generics.type_param(param, self.tcx);
4421 let tcx = self.tcx;
4422 if let Some(def_id) = type_param.def_id.as_local() {
4423 let id = tcx.local_def_id_to_hir_id(def_id);
4424 match tcx.hir_node(id) {
4428 Node::GenericParam(param) => {
4429 enum Introducer {
4430 Plus,
4431 Colon,
4432 Nothing,
4433 }
4434 let hir_generics = tcx.hir_get_generics(id.owner.def_id).unwrap();
4435 let trait_def_ids: DefIdSet = hir_generics
4436 .bounds_for_param(def_id)
4437 .flat_map(|bp| bp.bounds.iter())
4438 .filter_map(|bound| bound.trait_ref()?.trait_def_id())
4439 .collect();
4440 if candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
4441 return;
4442 }
4443 let msg = message(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("restrict type parameter `{0}` with",
param.name.ident()))
})format!(
4444 "restrict type parameter `{}` with",
4445 param.name.ident(),
4446 ));
4447 let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
4448 let mut applicability = Applicability::MaybeIncorrect;
4449 let candidate_strs: Vec<_> = candidates
4452 .iter()
4453 .map(|cand| {
4454 let cand_path = tcx.def_path_str(cand.def_id);
4455 let cand_params = &tcx.generics_of(cand.def_id).own_params;
4456 let cand_args: String = cand_params
4457 .iter()
4458 .skip(1)
4459 .filter_map(|param| match param.kind {
4460 ty::GenericParamDefKind::Type {
4461 has_default: true,
4462 ..
4463 }
4464 | ty::GenericParamDefKind::Const {
4465 has_default: true,
4466 ..
4467 } => None,
4468 _ => Some(param.name.as_str()),
4469 })
4470 .intersperse(", ")
4471 .collect();
4472 if cand_args.is_empty() {
4473 cand_path
4474 } else {
4475 applicability = Applicability::HasPlaceholders;
4476 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}</* {1} */>", cand_path,
cand_args))
})format!("{cand_path}</* {cand_args} */>")
4477 }
4478 })
4479 .collect();
4480
4481 if rcvr_ty.is_ref()
4482 && param.is_impl_trait()
4483 && let Some((bounds_span, _)) = bounds_span
4484 {
4485 err.multipart_suggestions(
4486 msg,
4487 candidate_strs.iter().map(|cand| {
4488 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(param.span.shrink_to_lo(), "(".to_string()),
(bounds_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" + {0})", cand))
}))]))vec![
4489 (param.span.shrink_to_lo(), "(".to_string()),
4490 (bounds_span, format!(" + {cand})")),
4491 ]
4492 }),
4493 applicability,
4494 );
4495 return;
4496 }
4497
4498 let (sp, introducer, open_paren_sp) =
4499 if let Some((span, open_paren_sp)) = bounds_span {
4500 (span, Introducer::Plus, open_paren_sp)
4501 } else if let Some(colon_span) = param.colon_span {
4502 (colon_span.shrink_to_hi(), Introducer::Nothing, None)
4503 } else if param.is_impl_trait() {
4504 (param.span.shrink_to_hi(), Introducer::Plus, None)
4505 } else {
4506 (param.span.shrink_to_hi(), Introducer::Colon, None)
4507 };
4508
4509 let all_suggs = candidate_strs.iter().map(|cand| {
4510 let suggestion = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}",
match introducer {
Introducer::Plus => " +",
Introducer::Colon => ":",
Introducer::Nothing => "",
}, cand))
})format!(
4511 "{} {cand}",
4512 match introducer {
4513 Introducer::Plus => " +",
4514 Introducer::Colon => ":",
4515 Introducer::Nothing => "",
4516 },
4517 );
4518
4519 let mut suggs = ::alloc::vec::Vec::new()vec![];
4520
4521 if let Some(open_paren_sp) = open_paren_sp {
4522 suggs.push((open_paren_sp, "(".to_string()));
4523 suggs.push((sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("){0}", suggestion))
})format!("){suggestion}")));
4524 } else {
4525 suggs.push((sp, suggestion));
4526 }
4527
4528 suggs
4529 });
4530
4531 err.multipart_suggestions(msg, all_suggs, applicability);
4532
4533 return;
4534 }
4535 Node::Item(hir::Item {
4536 kind: hir::ItemKind::Trait(_, _, _, ident, _, bounds, _),
4537 ..
4538 }) => {
4539 let (sp, sep, article) = if bounds.is_empty() {
4540 (ident.span.shrink_to_hi(), ":", "a")
4541 } else {
4542 (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
4543 };
4544 err.span_suggestions(
4545 sp,
4546 message(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("add {0} supertrait for", article))
})format!("add {article} supertrait for")),
4547 candidates
4548 .iter()
4549 .map(|t| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", sep,
tcx.def_path_str(t.def_id)))
})format!("{} {}", sep, tcx.def_path_str(t.def_id),)),
4550 Applicability::MaybeIncorrect,
4551 );
4552 return;
4553 }
4554 _ => {}
4555 }
4556 }
4557 }
4558
4559 let (potential_candidates, explicitly_negative) = if param_type.is_some() {
4560 (candidates, Vec::new())
4563 } else if let Some(simp_rcvr_ty) =
4564 simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
4565 {
4566 let mut potential_candidates = Vec::new();
4567 let mut explicitly_negative = Vec::new();
4568 for candidate in candidates {
4569 if self
4571 .tcx
4572 .all_impls(candidate.def_id)
4573 .map(|imp_did| self.tcx.impl_trait_header(imp_did))
4574 .filter(|header| header.polarity != ty::ImplPolarity::Positive)
4575 .any(|header| {
4576 let imp = header.trait_ref.instantiate_identity();
4577 let imp_simp =
4578 simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
4579 imp_simp.is_some_and(|s| s == simp_rcvr_ty)
4580 })
4581 {
4582 explicitly_negative.push(candidate);
4583 } else {
4584 potential_candidates.push(candidate);
4585 }
4586 }
4587 (potential_candidates, explicitly_negative)
4588 } else {
4589 (candidates, Vec::new())
4591 };
4592
4593 let impls_trait = |def_id: DefId| {
4594 let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
4595 if param.index == 0 {
4596 rcvr_ty.into()
4597 } else {
4598 self.infcx.var_for_def(span, param)
4599 }
4600 });
4601 self.infcx
4602 .type_implements_trait(def_id, args, self.param_env)
4603 .must_apply_modulo_regions()
4604 && param_type.is_none()
4605 };
4606 match &potential_candidates[..] {
4607 [] => {}
4608 [trait_info] if trait_info.def_id.is_local() => {
4609 if impls_trait(trait_info.def_id) {
4610 self.suggest_valid_traits(err, item_name, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[trait_info.def_id]))vec![trait_info.def_id], false);
4611 } else {
4612 err.subdiagnostic(CandidateTraitNote {
4613 span: self.tcx.def_span(trait_info.def_id),
4614 trait_name: self.tcx.def_path_str(trait_info.def_id),
4615 item_name,
4616 action_or_ty: if trait_missing_method {
4617 "NONE".to_string()
4618 } else {
4619 param_type.map_or_else(
4620 || "implement".to_string(), |p| p.to_string(),
4622 )
4623 },
4624 });
4625 }
4626 }
4627 trait_infos => {
4628 let mut msg = message(param_type.map_or_else(
4629 || "implement".to_string(), |param| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("restrict type parameter `{0}` with",
param))
})format!("restrict type parameter `{param}` with"),
4631 ));
4632 for (i, trait_info) in trait_infos.iter().enumerate() {
4633 if impls_trait(trait_info.def_id) {
4634 self.suggest_valid_traits(
4635 err,
4636 item_name,
4637 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[trait_info.def_id]))vec![trait_info.def_id],
4638 false,
4639 );
4640 }
4641 msg.push_str(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\ncandidate #{0}: `{1}`", i + 1,
self.tcx.def_path_str(trait_info.def_id)))
})format!(
4642 "\ncandidate #{}: `{}`",
4643 i + 1,
4644 self.tcx.def_path_str(trait_info.def_id),
4645 ));
4646 }
4647 err.note(msg);
4648 }
4649 }
4650 match &explicitly_negative[..] {
4651 [] => {}
4652 [trait_info] => {
4653 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait `{0}` defines an item `{1}`, but is explicitly unimplemented",
self.tcx.def_path_str(trait_info.def_id), item_name))
})format!(
4654 "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
4655 self.tcx.def_path_str(trait_info.def_id),
4656 item_name
4657 );
4658 err.note(msg);
4659 }
4660 trait_infos => {
4661 let mut msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following traits define an item `{0}`, but are explicitly unimplemented:",
item_name))
})format!(
4662 "the following traits define an item `{item_name}`, but are explicitly unimplemented:"
4663 );
4664 for trait_info in trait_infos {
4665 msg.push_str(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\n{0}",
self.tcx.def_path_str(trait_info.def_id)))
})format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
4666 }
4667 err.note(msg);
4668 }
4669 }
4670 }
4671 }
4672
4673 fn detect_and_explain_multiple_crate_versions_of_trait_item(
4674 &self,
4675 err: &mut Diag<'_>,
4676 item_def_id: DefId,
4677 hir_id: hir::HirId,
4678 rcvr_ty: Option<Ty<'tcx>>,
4679 ) -> bool {
4680 let hir_id = self.tcx.parent_hir_id(hir_id);
4681 let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
4682 if traits.is_empty() {
4683 return false;
4684 }
4685 let trait_def_id = self.tcx.parent(item_def_id);
4686 if !self.tcx.is_trait(trait_def_id) {
4687 return false;
4688 }
4689 let hir::Node::Expr(rcvr) = self.tcx.hir_node(hir_id) else {
4690 return false;
4691 };
4692 let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, rcvr_ty.into_iter());
4693 let trait_pred = ty::Binder::dummy(ty::TraitPredicate {
4694 trait_ref,
4695 polarity: ty::PredicatePolarity::Positive,
4696 });
4697 let obligation = Obligation::new(self.tcx, self.misc(rcvr.span), self.param_env, trait_ref);
4698 self.err_ctxt().note_different_trait_with_same_name(err, &obligation, trait_pred)
4699 }
4700
4701 pub(crate) fn suggest_else_fn_with_closure(
4704 &self,
4705 err: &mut Diag<'_>,
4706 expr: &hir::Expr<'_>,
4707 found: Ty<'tcx>,
4708 expected: Ty<'tcx>,
4709 ) -> bool {
4710 let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(found) else {
4711 return false;
4712 };
4713
4714 if !self.may_coerce(output, expected) {
4715 return false;
4716 }
4717
4718 if let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4719 && let hir::ExprKind::MethodCall(
4720 hir::PathSegment { ident: method_name, .. },
4721 self_expr,
4722 args,
4723 ..,
4724 ) = call_expr.kind
4725 && let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr)
4726 {
4727 let new_name = Ident {
4728 name: Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_else", method_name.as_str()))
})format!("{}_else", method_name.as_str())),
4729 span: method_name.span,
4730 };
4731 let probe = self.lookup_probe_for_diagnostic(
4732 new_name,
4733 self_ty,
4734 self_expr,
4735 ProbeScope::TraitsInScope,
4736 Some(expected),
4737 );
4738
4739 if let Ok(pick) = probe
4741 && let fn_sig = self.tcx.fn_sig(pick.item.def_id)
4742 && let fn_args = fn_sig.skip_binder().skip_binder().inputs()
4743 && fn_args.len() == args.len() + 1
4744 {
4745 err.span_suggestion_verbose(
4746 method_name.span.shrink_to_hi(),
4747 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try calling `{0}` instead",
new_name.name.as_str()))
})format!("try calling `{}` instead", new_name.name.as_str()),
4748 "_else",
4749 Applicability::MaybeIncorrect,
4750 );
4751 return true;
4752 }
4753 }
4754 false
4755 }
4756
4757 fn type_derefs_to_local(
4760 &self,
4761 span: Span,
4762 rcvr_ty: Ty<'tcx>,
4763 source: SelfSource<'tcx>,
4764 ) -> bool {
4765 fn is_local(ty: Ty<'_>) -> bool {
4766 match ty.kind() {
4767 ty::Adt(def, _) => def.did().is_local(),
4768 ty::Foreign(did) => did.is_local(),
4769 ty::Dynamic(tr, ..) => tr.principal().is_some_and(|d| d.def_id().is_local()),
4770 ty::Param(_) => true,
4771
4772 _ => false,
4777 }
4778 }
4779
4780 if let SelfSource::QPath(_) = source {
4783 return is_local(rcvr_ty);
4784 }
4785
4786 self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
4787 }
4788
4789 fn suggest_hashmap_on_unsatisfied_hashset_buildhasher(
4790 &self,
4791 err: &mut Diag<'_>,
4792 pred: &ty::TraitPredicate<'_>,
4793 adt: ty::AdtDef<'_>,
4794 ) -> bool {
4795 if self.tcx.is_diagnostic_item(sym::HashSet, adt.did())
4796 && self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id())
4797 {
4798 err.help("you might have intended to use a HashMap instead");
4799 true
4800 } else {
4801 false
4802 }
4803 }
4804}
4805
4806#[derive(#[automatically_derived]
impl<'a> ::core::marker::Copy for SelfSource<'a> { }Copy, #[automatically_derived]
impl<'a> ::core::clone::Clone for SelfSource<'a> {
#[inline]
fn clone(&self) -> SelfSource<'a> {
let _: ::core::clone::AssertParamIsClone<&'a hir::Ty<'a>>;
let _: ::core::clone::AssertParamIsClone<&'a hir::Expr<'a>>;
*self
}
}Clone, #[automatically_derived]
impl<'a> ::core::fmt::Debug for SelfSource<'a> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
SelfSource::QPath(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "QPath",
&__self_0),
SelfSource::MethodCall(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MethodCall", &__self_0),
}
}
}Debug)]
4807enum SelfSource<'a> {
4808 QPath(&'a hir::Ty<'a>),
4809 MethodCall(&'a hir::Expr<'a> ),
4810}
4811
4812#[derive(#[automatically_derived]
impl ::core::marker::Copy for TraitInfo { }Copy, #[automatically_derived]
impl ::core::clone::Clone for TraitInfo {
#[inline]
fn clone(&self) -> TraitInfo {
let _: ::core::clone::AssertParamIsClone<DefId>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for TraitInfo {
#[inline]
fn eq(&self, other: &TraitInfo) -> bool { self.def_id == other.def_id }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TraitInfo {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
}
}Eq)]
4813pub(crate) struct TraitInfo {
4814 pub def_id: DefId,
4815}
4816
4817pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4820 tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
4821}
4822
4823fn print_disambiguation_help<'tcx>(
4824 tcx: TyCtxt<'tcx>,
4825 err: &mut Diag<'_>,
4826 source: SelfSource<'tcx>,
4827 args: Option<&'tcx [hir::Expr<'tcx>]>,
4828 trait_ref: ty::TraitRef<'tcx>,
4829 candidate_idx: Option<usize>,
4830 span: Span,
4831 item: ty::AssocItem,
4832) -> Option<String> {
4833 let trait_impl_type = trait_ref.self_ty().peel_refs();
4834 let trait_ref = if item.is_method() {
4835 trait_ref.print_only_trait_name().to_string()
4836 } else {
4837 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0} as {1}>", trait_ref.args[0],
trait_ref.print_only_trait_name()))
})format!("<{} as {}>", trait_ref.args[0], trait_ref.print_only_trait_name())
4838 };
4839 Some(
4840 if item.is_fn()
4841 && let SelfSource::MethodCall(receiver) = source
4842 && let Some(args) = args
4843 {
4844 let def_kind_descr = tcx.def_kind_descr(item.as_def_kind(), item.def_id);
4845 let item_name = item.ident(tcx);
4846 let first_input =
4847 tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
4848 let (first_arg_type, rcvr_ref) = (
4849 first_input.map(|first| first.peel_refs()),
4850 first_input
4851 .and_then(|ty| ty.ref_mutability())
4852 .map_or("", |mutbl| mutbl.ref_prefix_str()),
4853 );
4854
4855 let args = if let Some(first_arg_type) = first_arg_type
4857 && (first_arg_type == tcx.types.self_param
4858 || first_arg_type == trait_impl_type
4859 || item.is_method())
4860 {
4861 Some(receiver)
4862 } else {
4863 None
4864 }
4865 .into_iter()
4866 .chain(args)
4867 .map(|arg| {
4868 tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
4869 })
4870 .collect::<Vec<_>>()
4871 .join(", ");
4872
4873 let args = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}{1})", rcvr_ref, args))
})format!("({}{})", rcvr_ref, args);
4874 err.span_suggestion_verbose(
4875 span,
4876 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("disambiguate the {1} for {0}",
if let Some(candidate) = candidate_idx {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("candidate #{0}",
candidate))
})
} else { "the candidate".to_string() }, def_kind_descr))
})format!(
4877 "disambiguate the {def_kind_descr} for {}",
4878 if let Some(candidate) = candidate_idx {
4879 format!("candidate #{candidate}")
4880 } else {
4881 "the candidate".to_string()
4882 },
4883 ),
4884 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}{2}", trait_ref, item_name,
args))
})format!("{trait_ref}::{item_name}{args}"),
4885 Applicability::HasPlaceholders,
4886 );
4887 return None;
4888 } else {
4889 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::", trait_ref))
})format!("{trait_ref}::")
4890 },
4891 )
4892}