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, 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() {
1181 "variant or associated item"
1182 } else {
1183 match (item_ident.as_str().chars().next(), rcvr_ty.is_fresh_ty()) {
1184 (Some(name), false) if name.is_lowercase() => "function or associated item",
1185 (Some(_), false) => "associated item",
1186 (Some(_), true) | (None, false) => "variant or associated item",
1187 (None, true) => "variant",
1188 }
1189 };
1190
1191 if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
1192 tcx,
1193 rcvr_ty,
1194 source,
1195 span,
1196 item_kind,
1197 item_ident,
1198 &mut ty_file,
1199 ) {
1200 return guar;
1201 }
1202
1203 let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
1204 let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
1205 tcx.is_diagnostic_item(sym::write_macro, def_id)
1206 || tcx.is_diagnostic_item(sym::writeln_macro, def_id)
1207 }) && item_ident.name == sym::write_fmt;
1208 let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source {
1209 self.create_missing_writer_err(rcvr_ty, rcvr_expr, ty_file)
1210 } else {
1211 self.create_no_assoc_err(
1212 rcvr_ty,
1213 item_ident,
1214 item_kind,
1215 trait_missing_method,
1216 source,
1217 is_method,
1218 sugg_span,
1219 unsatisfied_predicates,
1220 )
1221 };
1222
1223 self.set_label_for_method_error(
1224 &mut err,
1225 source,
1226 rcvr_ty,
1227 item_ident,
1228 expr_id,
1229 item_ident.span,
1230 sugg_span,
1231 within_macro_span,
1232 args,
1233 );
1234
1235 self.suggest_method_call_annotation(
1236 &mut err,
1237 item_ident.span,
1238 rcvr_ty,
1239 item_ident,
1240 mode,
1241 source,
1242 expected,
1243 );
1244
1245 let static_candidates = self.suggest_static_method_candidates(
1246 &mut err,
1247 item_ident.span,
1248 rcvr_ty,
1249 item_ident,
1250 source,
1251 args,
1252 sugg_span,
1253 &no_match_data,
1254 );
1255
1256 let Ok((
1257 restrict_type_params,
1258 suggested_derive,
1259 unsatisfied_bounds,
1260 custom_span_label,
1261 bound_spans,
1262 )) = self.suggest_unsatisfied_ty_or_trait(
1263 &mut err,
1264 item_ident.span,
1265 rcvr_ty,
1266 item_ident,
1267 item_kind,
1268 source,
1269 unsatisfied_predicates,
1270 &static_candidates,
1271 )
1272 else {
1273 return err.emit();
1274 };
1275
1276 let similar_candidate = no_match_data.similar_candidate;
1277 let should_label_not_found = self.suggest_surround_method_call(
1278 &mut err,
1279 item_ident.span,
1280 rcvr_ty,
1281 item_ident,
1282 source,
1283 &similar_candidate,
1284 );
1285
1286 self.find_possible_candidates_for_method(
1287 &mut err,
1288 item_ident.span,
1289 rcvr_ty,
1290 item_ident,
1291 item_kind,
1292 mode,
1293 source,
1294 no_match_data,
1295 expected,
1296 should_label_not_found,
1297 custom_span_label,
1298 );
1299
1300 self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident);
1301
1302 if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params || suggested_derive {
1303 } else {
1305 self.suggest_traits_to_import(
1306 &mut err,
1307 item_ident.span,
1308 rcvr_ty,
1309 item_ident,
1310 args.map(|args| args.len() + 1),
1311 source,
1312 no_match_data.out_of_scope_traits.clone(),
1313 &static_candidates,
1314 unsatisfied_bounds,
1315 expected.only_has_type(self),
1316 trait_missing_method,
1317 );
1318 }
1319
1320 self.suggest_enum_variant_for_method_call(
1321 &mut err,
1322 rcvr_ty,
1323 item_ident,
1324 item_ident.span,
1325 source,
1326 unsatisfied_predicates,
1327 );
1328
1329 self.suggest_confusable_or_similarly_named_method(
1330 &mut err,
1331 item_ident.span,
1332 rcvr_ty,
1333 item_ident,
1334 mode,
1335 args,
1336 unsatisfied_predicates,
1337 similar_candidate,
1338 );
1339
1340 self.suggest_method_not_found_because_of_unsatisfied_bounds(
1341 &mut err,
1342 rcvr_ty,
1343 item_ident,
1344 item_kind,
1345 bound_spans,
1346 unsatisfied_predicates,
1347 );
1348
1349 self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_ident, expected);
1350 self.suggest_bounds_for_range_to_method(&mut err, source, item_ident);
1351 err.emit()
1352 }
1353
1354 fn set_not_found_span_label(
1355 &self,
1356 err: &mut Diag<'_>,
1357 rcvr_ty: Ty<'tcx>,
1358 item_ident: Ident,
1359 item_kind: &str,
1360 mode: Mode,
1361 source: SelfSource<'tcx>,
1362 span: Span,
1363 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1364 find_candidate_for_method: &mut bool,
1365 ) {
1366 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1367 if unsatisfied_predicates.is_empty() {
1368 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}`"));
1369 let is_string_or_ref_str = match rcvr_ty.kind() {
1370 ty::Ref(_, ty, _) => {
1371 ty.is_str()
1372 || #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String) =>
true,
_ => false,
}matches!(
1373 ty.kind(),
1374 ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String)
1375 )
1376 }
1377 ty::Adt(adt, _) => self.tcx.is_lang_item(adt.did(), LangItem::String),
1378 _ => false,
1379 };
1380 if is_string_or_ref_str && item_ident.name == sym::iter {
1381 err.span_suggestion_verbose(
1382 item_ident.span,
1383 "because of the in-memory representation of `&str`, to obtain \
1384 an `Iterator` over each of its codepoint use method `chars`",
1385 "chars",
1386 Applicability::MachineApplicable,
1387 );
1388 }
1389 if let ty::Adt(adt, _) = rcvr_ty.kind() {
1390 let mut inherent_impls_candidate = self
1391 .tcx
1392 .inherent_impls(adt.did())
1393 .into_iter()
1394 .copied()
1395 .filter(|def_id| {
1396 if let Some(assoc) = self.associated_value(*def_id, item_ident) {
1397 match (mode, assoc.is_method(), source) {
1400 (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
1401 self.tcx.at(span).type_of(*def_id).instantiate_identity()
1406 != rcvr_ty
1407 }
1408 (Mode::Path, false, _) => true,
1409 _ => false,
1410 }
1411 } else {
1412 false
1413 }
1414 })
1415 .collect::<Vec<_>>();
1416 if !inherent_impls_candidate.is_empty() {
1417 inherent_impls_candidate.sort_by_key(|&id| self.tcx.def_path_str(id));
1418 inherent_impls_candidate.dedup();
1419
1420 let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
1422 let type_candidates = inherent_impls_candidate
1423 .iter()
1424 .take(limit)
1425 .map(|impl_item| {
1426 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("- `{0}`",
self.tcx.at(span).type_of(*impl_item).instantiate_identity()))
})format!(
1427 "- `{}`",
1428 self.tcx.at(span).type_of(*impl_item).instantiate_identity()
1429 )
1430 })
1431 .collect::<Vec<_>>()
1432 .join("\n");
1433 let additional_types = if inherent_impls_candidate.len() > limit {
1434 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\nand {0} more types",
inherent_impls_candidate.len() - limit))
})format!("\nand {} more types", inherent_impls_candidate.len() - limit)
1435 } else {
1436 "".to_string()
1437 };
1438 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the {0} was found for\n{1}{2}",
item_kind, type_candidates, additional_types))
})format!(
1439 "the {item_kind} was found for\n{type_candidates}{additional_types}"
1440 ));
1441 *find_candidate_for_method = mode == Mode::MethodCall;
1442 }
1443 }
1444 } else {
1445 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}` ") };
1446 err.span_label(
1447 span,
1448 ::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"),
1449 );
1450 }
1451 }
1452
1453 fn suggest_enum_variant_for_method_call(
1455 &self,
1456 err: &mut Diag<'_>,
1457 rcvr_ty: Ty<'tcx>,
1458 item_ident: Ident,
1459 span: Span,
1460 source: SelfSource<'tcx>,
1461 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1462 ) {
1463 if !unsatisfied_predicates.is_empty() || !rcvr_ty.is_enum() {
1465 return;
1466 }
1467
1468 let tcx = self.tcx;
1469 let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
1470 if let Some(var_name) = edit_distance::find_best_match_for_name(
1471 &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
1472 item_ident.name,
1473 None,
1474 ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
1475 {
1476 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())];
1477 if let SelfSource::QPath(ty) = source
1478 && let hir::Node::Expr(ref path_expr) = tcx.parent_hir_node(ty.hir_id)
1479 && let hir::ExprKind::Path(_) = path_expr.kind
1480 && let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(parent), .. })
1481 | hir::Node::Expr(parent) = tcx.parent_hir_node(path_expr.hir_id)
1482 {
1483 let replacement_span = match parent.kind {
1485 hir::ExprKind::Call(callee, _) if callee.hir_id == path_expr.hir_id => {
1486 span.with_hi(parent.span.hi())
1487 }
1488 hir::ExprKind::Struct(..) => span.with_hi(parent.span.hi()),
1489 _ => span,
1490 };
1491 match (variant.ctor, parent.kind) {
1492 (None, hir::ExprKind::Struct(..)) => {
1493 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())];
1496 }
1497 (None, _) => {
1498 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![(
1500 replacement_span,
1501 if variant.fields.is_empty() {
1502 format!("{var_name} {{}}")
1503 } else {
1504 format!(
1505 "{var_name} {{ {} }}",
1506 variant
1507 .fields
1508 .iter()
1509 .map(|f| format!("{}: /* value */", f.name))
1510 .collect::<Vec<_>>()
1511 .join(", ")
1512 )
1513 },
1514 )];
1515 }
1516 (Some((hir::def::CtorKind::Const, _)), _) => {
1517 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())];
1519 }
1520 (Some((hir::def::CtorKind::Fn, def_id)), hir::ExprKind::Call(rcvr, args)) => {
1521 let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1522 let inputs = fn_sig.inputs().skip_binder();
1523 match (inputs, args) {
1526 (inputs, []) => {
1527 suggestion.push((
1529 rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
1530 ::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!(
1531 "({})",
1532 inputs
1533 .iter()
1534 .map(|i| format!("/* {i} */"))
1535 .collect::<Vec<String>>()
1536 .join(", ")
1537 ),
1538 ));
1539 }
1540 (_, [arg]) if inputs.len() != args.len() => {
1541 suggestion.push((
1543 arg.span,
1544 inputs
1545 .iter()
1546 .map(|i| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})format!("/* {i} */"))
1547 .collect::<Vec<String>>()
1548 .join(", "),
1549 ));
1550 }
1551 (_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
1552 suggestion.push((
1554 arg_start.span.to(arg_end.span),
1555 inputs
1556 .iter()
1557 .map(|i| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})format!("/* {i} */"))
1558 .collect::<Vec<String>>()
1559 .join(", "),
1560 ));
1561 }
1562 _ => {}
1564 }
1565 }
1566 (Some((hir::def::CtorKind::Fn, def_id)), _) => {
1567 let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1568 let inputs = fn_sig.inputs().skip_binder();
1569 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![(
1570 replacement_span,
1571 format!(
1572 "{var_name}({})",
1573 inputs
1574 .iter()
1575 .map(|i| format!("/* {i} */"))
1576 .collect::<Vec<String>>()
1577 .join(", ")
1578 ),
1579 )];
1580 }
1581 }
1582 }
1583 err.multipart_suggestion(
1584 "there is a variant with a similar name",
1585 suggestion,
1586 Applicability::HasPlaceholders,
1587 );
1588 }
1589 }
1590
1591 fn handle_unsatisfied_predicates(
1592 &self,
1593 err: &mut Diag<'_>,
1594 rcvr_ty: Ty<'tcx>,
1595 item_ident: Ident,
1596 item_kind: &str,
1597 span: Span,
1598 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1599 restrict_type_params: &mut bool,
1600 suggested_derive: &mut bool,
1601 unsatisfied_bounds: &mut bool,
1602 custom_span_label: &mut bool,
1603 bound_spans: &mut SortedMap<Span, Vec<String>>,
1604 ) {
1605 let tcx = self.tcx;
1606 let rcvr_ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1607 let mut type_params = FxIndexMap::default();
1608
1609 let mut unimplemented_traits = FxIndexMap::default();
1612
1613 let mut unimplemented_traits_only = true;
1614 for (predicate, _parent_pred, cause) in unsatisfied_predicates {
1615 if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
1616 (predicate.kind().skip_binder(), cause.as_ref())
1617 {
1618 if p.trait_ref.self_ty() != rcvr_ty {
1619 continue;
1623 }
1624 unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
1625 predicate.kind().rebind(p),
1626 Obligation {
1627 cause: cause.clone(),
1628 param_env: self.param_env,
1629 predicate: *predicate,
1630 recursion_depth: 0,
1631 },
1632 ));
1633 }
1634 }
1635
1636 for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
1641 match predicate.kind().skip_binder() {
1642 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))
1643 if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
1644 _ => {
1645 unimplemented_traits_only = false;
1646 break;
1647 }
1648 }
1649 }
1650
1651 let mut collect_type_param_suggestions =
1652 |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
1653 if let (ty::Param(_), ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))) =
1655 (self_ty.kind(), parent_pred.kind().skip_binder())
1656 {
1657 let node = match p.trait_ref.self_ty().kind() {
1658 ty::Param(_) => {
1659 Some(self.tcx.hir_node_by_def_id(self.body_id))
1662 }
1663 ty::Adt(def, _) => {
1664 def.did().as_local().map(|def_id| self.tcx.hir_node_by_def_id(def_id))
1665 }
1666 _ => None,
1667 };
1668 if let Some(hir::Node::Item(hir::Item { kind, .. })) = node
1669 && let Some(g) = kind.generics()
1670 {
1671 let key = (
1672 g.tail_span_for_predicate_suggestion(),
1673 g.add_where_or_trailing_comma(),
1674 );
1675 type_params
1676 .entry(key)
1677 .or_insert_with(UnordSet::default)
1678 .insert(obligation.to_owned());
1679 return true;
1680 }
1681 }
1682 false
1683 };
1684 let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
1685 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 });
1686 match self_ty.kind() {
1687 ty::Adt(def, _) => {
1689 bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
1690 }
1691 ty::Dynamic(preds, _) => {
1693 for pred in preds.iter() {
1694 match pred.skip_binder() {
1695 ty::ExistentialPredicate::Trait(tr) => {
1696 bound_spans
1697 .get_mut_or_insert_default(tcx.def_span(tr.def_id))
1698 .push(msg.clone());
1699 }
1700 ty::ExistentialPredicate::Projection(_)
1701 | ty::ExistentialPredicate::AutoTrait(_) => {}
1702 }
1703 }
1704 }
1705 ty::Closure(def_id, _) => {
1707 bound_spans
1708 .get_mut_or_insert_default(tcx.def_span(*def_id))
1709 .push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", quiet))
})format!("`{quiet}`"));
1710 }
1711 _ => {}
1712 }
1713 };
1714
1715 let mut format_pred = |pred: ty::Predicate<'tcx>| {
1716 let bound_predicate = pred.kind();
1717 match bound_predicate.skip_binder() {
1718 ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
1719 let pred = bound_predicate.rebind(pred);
1720 let projection_term = pred.skip_binder().projection_term;
1722 let quiet_projection_term = projection_term
1723 .with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
1724
1725 let term = pred.skip_binder().term;
1726
1727 let obligation = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} = {1}", projection_term, term))
})format!("{projection_term} = {term}");
1728 let quiet =
1729 {
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));
1730
1731 bound_span_label(projection_term.self_ty(), &obligation, &quiet);
1732 Some((obligation, projection_term.self_ty()))
1733 }
1734 ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
1735 let p = poly_trait_ref.trait_ref;
1736 let self_ty = p.self_ty();
1737 let path = p.print_only_trait_path();
1738 let obligation = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", self_ty, path))
})format!("{self_ty}: {path}");
1739 let quiet = {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("_: {0}", path))
})
}with_forced_trimmed_paths!(format!("_: {}", path));
1740 bound_span_label(self_ty, &obligation, &quiet);
1741 Some((obligation, self_ty))
1742 }
1743 _ => None,
1744 }
1745 };
1746
1747 let mut skip_list: UnordSet<_> = Default::default();
1749 let mut spanned_predicates = FxIndexMap::default();
1750 let mut manually_impl = false;
1751 for (p, parent_p, cause) in unsatisfied_predicates {
1752 let (item_def_id, cause_span, cause_msg) =
1755 match cause.as_ref().map(|cause| cause.code()) {
1756 Some(ObligationCauseCode::ImplDerived(data)) => {
1757 let msg = if let DefKind::Impl { of_trait: true } =
1758 self.tcx.def_kind(data.impl_or_alias_def_id)
1759 {
1760 ::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!(
1761 "type parameter would need to implement `{}`",
1762 self.tcx
1763 .item_name(self.tcx.impl_trait_id(data.impl_or_alias_def_id))
1764 )
1765 } else {
1766 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unsatisfied bound `{0}` introduced here",
p))
})format!("unsatisfied bound `{p}` introduced here")
1767 };
1768 (data.impl_or_alias_def_id, data.span, msg)
1769 }
1770 Some(
1771 ObligationCauseCode::WhereClauseInExpr(def_id, span, _, _)
1772 | ObligationCauseCode::WhereClause(def_id, span),
1773 ) if !span.is_dummy() => {
1774 (*def_id, *span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unsatisfied bound `{0}` introduced here",
p))
})format!("unsatisfied bound `{p}` introduced here"))
1775 }
1776 _ => continue,
1777 };
1778
1779 if !#[allow(non_exhaustive_omitted_patterns)] match p.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Projection(..) |
ty::ClauseKind::Trait(..)) => true,
_ => false,
}matches!(
1781 p.kind().skip_binder(),
1782 ty::PredicateKind::Clause(
1783 ty::ClauseKind::Projection(..) | ty::ClauseKind::Trait(..)
1784 )
1785 ) {
1786 continue;
1787 }
1788
1789 match self.tcx.hir_get_if_local(item_def_id) {
1790 Some(Node::Item(hir::Item {
1793 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
1794 ..
1795 })) if #[allow(non_exhaustive_omitted_patterns)] match self_ty.span.ctxt().outer_expn_data().kind
{
ExpnKind::Macro(MacroKind::Derive, _) => true,
_ => false,
}matches!(
1796 self_ty.span.ctxt().outer_expn_data().kind,
1797 ExpnKind::Macro(MacroKind::Derive, _)
1798 ) || #[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!(
1799 of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
1800 Some(ExpnKind::Macro(MacroKind::Derive, _))
1801 ) =>
1802 {
1803 let span = self_ty.span.ctxt().outer_expn_data().call_site;
1804 let entry = spanned_predicates.entry(span);
1805 let entry = entry.or_insert_with(|| {
1806 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1807 });
1808 entry.0.insert(cause_span);
1809 entry.1.insert((
1810 cause_span,
1811 cause_msg,
1812 ));
1813 entry.2.push(p);
1814 skip_list.insert(p);
1815 manually_impl = true;
1816 }
1817
1818 Some(Node::Item(hir::Item {
1820 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
1821 span: item_span,
1822 ..
1823 })) => {
1824 let sized_pred =
1825 unsatisfied_predicates.iter().any(|(pred, _, _)| {
1826 match pred.kind().skip_binder() {
1827 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1828 self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
1829 && pred.polarity == ty::PredicatePolarity::Positive
1830 }
1831 _ => false,
1832 }
1833 });
1834 for param in generics.params {
1835 if param.span == cause_span && sized_pred {
1836 let (sp, sugg) = match param.colon_span {
1837 Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
1838 None => (param.span.shrink_to_hi(), ": ?Sized"),
1839 };
1840 err.span_suggestion_verbose(
1841 sp,
1842 "consider relaxing the type parameter's implicit `Sized` bound",
1843 sugg,
1844 Applicability::MachineApplicable,
1845 );
1846 }
1847 }
1848 if let Some(pred) = parent_p {
1849 let _ = format_pred(*pred);
1851 }
1852 skip_list.insert(p);
1853 let entry = spanned_predicates.entry(self_ty.span);
1854 let entry = entry.or_insert_with(|| {
1855 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1856 });
1857 entry.2.push(p);
1858 if cause_span != *item_span {
1859 entry.0.insert(cause_span);
1860 entry.1.insert((cause_span, "unsatisfied trait bound introduced here".to_string()));
1861 } else {
1862 if let Some(of_trait) = of_trait {
1863 entry.0.insert(of_trait.trait_ref.path.span);
1864 }
1865 entry.0.insert(self_ty.span);
1866 };
1867 if let Some(of_trait) = of_trait {
1868 entry.1.insert((of_trait.trait_ref.path.span, String::new()));
1869 }
1870 entry.1.insert((self_ty.span, String::new()));
1871 }
1872 Some(Node::Item(hir::Item {
1873 kind: hir::ItemKind::Trait(_, rustc_ast::ast::IsAuto::Yes, ..),
1874 span: item_span,
1875 ..
1876 })) => {
1877 self.dcx().span_delayed_bug(
1878 *item_span,
1879 "auto trait is invoked with no method error, but no error reported?",
1880 );
1881 }
1882 Some(
1883 Node::Item(hir::Item {
1884 kind:
1885 hir::ItemKind::Trait(_, _, _, ident, ..)
1886 | hir::ItemKind::TraitAlias(_, ident, ..),
1887 ..
1888 })
1889 | Node::TraitItem(hir::TraitItem { ident, .. })
1891 | Node::ImplItem(hir::ImplItem { ident, .. })
1892 ) => {
1893 skip_list.insert(p);
1894 let entry = spanned_predicates.entry(ident.span);
1895 let entry = entry.or_insert_with(|| {
1896 (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1897 });
1898 entry.0.insert(cause_span);
1899 entry.1.insert((ident.span, String::new()));
1900 entry.1.insert((cause_span, "unsatisfied trait bound introduced here".to_string()));
1901 entry.2.push(p);
1902 }
1903 _ => {
1904 }
1909 }
1910 }
1911 let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
1912 spanned_predicates.sort_by_key(|(span, _)| *span);
1913 for (_, (primary_spans, span_labels, predicates)) in spanned_predicates {
1914 let mut tracker = TraitBoundDuplicateTracker::new();
1915 let mut all_trait_bounds_for_rcvr = true;
1916 for pred in &predicates {
1917 match pred.kind().skip_binder() {
1918 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1919 let self_ty = pred.trait_ref.self_ty();
1920 if self_ty.peel_refs() != rcvr_ty {
1921 all_trait_bounds_for_rcvr = false;
1922 break;
1923 }
1924 let is_ref = #[allow(non_exhaustive_omitted_patterns)] match self_ty.kind() {
ty::Ref(..) => true,
_ => false,
}matches!(self_ty.kind(), ty::Ref(..));
1925 tracker.track(pred.trait_ref.def_id, is_ref);
1926 }
1927 _ => {
1928 all_trait_bounds_for_rcvr = false;
1929 break;
1930 }
1931 }
1932 }
1933 let has_ref_dupes = tracker.has_ref_dupes();
1934 let trait_def_ids = tracker.into_trait_def_ids();
1935 let mut preds: Vec<_> = predicates
1936 .iter()
1937 .filter_map(|pred| format_pred(**pred))
1938 .map(|(p, _)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"))
1939 .collect();
1940 preds.sort();
1941 preds.dedup();
1942 let availability_note = if all_trait_bounds_for_rcvr
1943 && has_ref_dupes
1944 && trait_def_ids.len() > 1
1945 && #[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.kind() {
ty::Adt(..) => true,
_ => false,
}matches!(rcvr_ty.kind(), ty::Adt(..))
1946 {
1947 let mut trait_names = trait_def_ids
1948 .into_iter()
1949 .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)))
1950 .collect::<Vec<_>>();
1951 trait_names.sort();
1952 listify(&trait_names, |name| name.to_string()).map(|traits| {
1953 ::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!(
1954 "for `{item_ident}` to be available, `{rcvr_ty_str}` must implement {traits}"
1955 )
1956 })
1957 } else {
1958 None
1959 };
1960 let msg = if let Some(availability_note) = availability_note {
1961 availability_note
1962 } else if let [pred] = &preds[..] {
1963 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("trait bound {0} was not satisfied",
pred))
})format!("trait bound {pred} was not satisfied")
1964 } else {
1965 ::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"),)
1966 };
1967 let mut span: MultiSpan = primary_spans.into_iter().collect::<Vec<_>>().into();
1968 for (sp, label) in span_labels {
1969 span.push_span_label(sp, label);
1970 }
1971 err.span_note(span, msg);
1972 *unsatisfied_bounds = true;
1973 }
1974
1975 let mut suggested_bounds = UnordSet::default();
1976 let mut bound_list = unsatisfied_predicates
1978 .iter()
1979 .filter_map(|(pred, parent_pred, _cause)| {
1980 let mut suggested = false;
1981 format_pred(*pred).map(|(p, self_ty)| {
1982 if let Some(parent) = parent_pred
1983 && suggested_bounds.contains(parent)
1984 {
1985 } else if !suggested_bounds.contains(pred)
1987 && collect_type_param_suggestions(self_ty, *pred, &p)
1988 {
1989 suggested = true;
1990 suggested_bounds.insert(pred);
1991 }
1992 (
1993 match parent_pred {
1994 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"),
1995 Some(parent_pred) => match format_pred(*parent_pred) {
1996 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", p))
})format!("`{p}`"),
1997 Some((parent_p, _)) => {
1998 if !suggested
1999 && !suggested_bounds.contains(pred)
2000 && !suggested_bounds.contains(parent_pred)
2001 && collect_type_param_suggestions(self_ty, *parent_pred, &p)
2002 {
2003 suggested_bounds.insert(pred);
2004 }
2005 ::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}`")
2006 }
2007 },
2008 },
2009 *pred,
2010 )
2011 })
2012 })
2013 .filter(|(_, pred)| !skip_list.contains(&pred))
2014 .map(|(t, _)| t)
2015 .enumerate()
2016 .collect::<Vec<(usize, String)>>();
2017
2018 if !#[allow(non_exhaustive_omitted_patterns)] match rcvr_ty.peel_refs().kind() {
ty::Param(_) => true,
_ => false,
}matches!(rcvr_ty.peel_refs().kind(), ty::Param(_)) {
2019 for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
2020 *restrict_type_params = true;
2021 let obligations = obligations.into_sorted_stable_ord();
2023 err.span_suggestion_verbose(
2024 span,
2025 ::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!(
2026 "consider restricting the type parameter{s} to satisfy the trait \
2027 bound{s}",
2028 s = pluralize!(obligations.len())
2029 ),
2030 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", add_where_or_comma,
obligations.join(", ")))
})format!("{} {}", add_where_or_comma, obligations.join(", ")),
2031 Applicability::MaybeIncorrect,
2032 );
2033 }
2034 }
2035
2036 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() {
2041 let bound_list =
2042 bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
2043 let actual_prefix = rcvr_ty.prefix_string(self.tcx);
2044 {
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:2044",
"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(2044u32),
::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());
2045 let (primary_message, label, notes) = if unimplemented_traits.len() == 1
2046 && unimplemented_traits_only
2047 {
2048 unimplemented_traits
2049 .into_iter()
2050 .next()
2051 .map(|(_, (trait_ref, obligation))| {
2052 if trait_ref.self_ty().references_error() || rcvr_ty.references_error() {
2053 return (None, None, Vec::new());
2055 }
2056 let OnUnimplementedNote { message, label, notes, .. } = self
2057 .err_ctxt()
2058 .on_unimplemented_note(trait_ref, &obligation, err.long_ty_path());
2059 (message, label, notes)
2060 })
2061 .unwrap()
2062 } else {
2063 (None, None, Vec::new())
2064 };
2065 let primary_message = primary_message.unwrap_or_else(|| {
2066 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
2067 ::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!(
2068 "the {item_kind} `{item_ident}` exists for {actual_prefix} `{ty_str}`, \
2069 but its trait bounds were not satisfied"
2070 )
2071 });
2072 err.primary_message(primary_message);
2073 if let Some(label) = label {
2074 *custom_span_label = true;
2075 err.span_label(span, label);
2076 }
2077 if !bound_list.is_empty() {
2078 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}"));
2079 }
2080 for note in notes {
2081 err.note(note);
2082 }
2083
2084 if let ty::Adt(adt_def, _) = rcvr_ty.kind() {
2085 unsatisfied_predicates.iter().find(|(pred, _parent, _cause)| {
2086 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
2087 pred.kind().skip_binder()
2088 {
2089 self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(
2090 err, &pred, *adt_def,
2091 )
2092 } else {
2093 false
2094 }
2095 });
2096 }
2097
2098 *suggested_derive = self.suggest_derive(err, unsatisfied_predicates);
2099 *unsatisfied_bounds = true;
2100 }
2101 if manually_impl {
2102 err.help("consider manually implementing the trait to avoid undesired bounds");
2103 }
2104 }
2105
2106 fn lookup_segments_chain_for_no_match_method(
2108 &self,
2109 err: &mut Diag<'_>,
2110 item_name: Ident,
2111 item_kind: &str,
2112 source: SelfSource<'tcx>,
2113 no_match_data: &NoMatchData<'tcx>,
2114 ) {
2115 if no_match_data.unsatisfied_predicates.is_empty()
2116 && let Mode::MethodCall = no_match_data.mode
2117 && let SelfSource::MethodCall(mut source_expr) = source
2118 {
2119 let mut stack_methods = ::alloc::vec::Vec::new()vec![];
2120 while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, method_span) =
2121 source_expr.kind
2122 {
2123 if let Some(prev_match) = stack_methods.pop() {
2125 err.span_label(
2126 method_span,
2127 ::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}`"),
2128 );
2129 }
2130 let rcvr_ty = self.resolve_vars_if_possible(
2131 self.typeck_results
2132 .borrow()
2133 .expr_ty_adjusted_opt(rcvr_expr)
2134 .unwrap_or(Ty::new_misc_error(self.tcx)),
2135 );
2136
2137 let Ok(candidates) = self.probe_for_name_many(
2138 Mode::MethodCall,
2139 item_name,
2140 None,
2141 IsSuggestion(true),
2142 rcvr_ty,
2143 source_expr.hir_id,
2144 ProbeScope::TraitsInScope,
2145 ) else {
2146 return;
2147 };
2148
2149 for _matched_method in candidates {
2153 stack_methods.push(rcvr_ty);
2155 }
2156 source_expr = rcvr_expr;
2157 }
2158 if let Some(prev_match) = stack_methods.pop() {
2160 err.span_label(
2161 source_expr.span,
2162 ::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}`"),
2163 );
2164 }
2165 }
2166 }
2167
2168 fn find_likely_intended_associated_item(
2169 &self,
2170 err: &mut Diag<'_>,
2171 similar_candidate: ty::AssocItem,
2172 span: Span,
2173 args: Option<&'tcx [hir::Expr<'tcx>]>,
2174 mode: Mode,
2175 ) {
2176 let tcx = self.tcx;
2177 let def_kind = similar_candidate.as_def_kind();
2178 let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
2179 let similar_candidate_name = similar_candidate.name();
2180 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!(
2181 "there is {an} {} `{}` with a similar name",
2182 self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
2183 similar_candidate_name,
2184 );
2185 if def_kind == DefKind::AssocFn {
2190 let ty_args = self.infcx.fresh_args_for_item(span, similar_candidate.def_id);
2191 let fn_sig = tcx.fn_sig(similar_candidate.def_id).instantiate(tcx, ty_args);
2192 let fn_sig = self.instantiate_binder_with_fresh_vars(
2193 span,
2194 BoundRegionConversionTime::FnCall,
2195 fn_sig,
2196 );
2197 if similar_candidate.is_method() {
2198 if let Some(args) = args
2199 && fn_sig.inputs()[1..].len() == args.len()
2200 {
2201 err.span_suggestion_verbose(
2204 span,
2205 msg,
2206 similar_candidate_name,
2207 Applicability::MaybeIncorrect,
2208 );
2209 } else {
2210 err.span_help(
2213 tcx.def_span(similar_candidate.def_id),
2214 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}",
if let None = args {
""
} else { ", but with different arguments" }, msg))
})format!(
2215 "{msg}{}",
2216 if let None = args { "" } else { ", but with different arguments" },
2217 ),
2218 );
2219 }
2220 } else if let Some(args) = args
2221 && fn_sig.inputs().len() == args.len()
2222 {
2223 err.span_suggestion_verbose(
2226 span,
2227 msg,
2228 similar_candidate_name,
2229 Applicability::MaybeIncorrect,
2230 );
2231 } else {
2232 err.span_help(tcx.def_span(similar_candidate.def_id), msg);
2233 }
2234 } else if let Mode::Path = mode
2235 && args.unwrap_or(&[]).is_empty()
2236 {
2237 err.span_suggestion_verbose(
2239 span,
2240 msg,
2241 similar_candidate_name,
2242 Applicability::MaybeIncorrect,
2243 );
2244 } else {
2245 err.span_help(tcx.def_span(similar_candidate.def_id), msg);
2248 }
2249 }
2250
2251 pub(crate) fn confusable_method_name(
2252 &self,
2253 err: &mut Diag<'_>,
2254 rcvr_ty: Ty<'tcx>,
2255 item_name: Ident,
2256 call_args: Option<Vec<Ty<'tcx>>>,
2257 ) -> Option<Symbol> {
2258 if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
2259 for &inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter() {
2260 for inherent_method in
2261 self.tcx.associated_items(inherent_impl_did).in_definition_order()
2262 {
2263 if let Some(candidates) = {
#[allow(deprecated)]
{
{
'done:
{
for i in self.tcx.get_all_attrs(inherent_method.def_id) {
#[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)
2264 && candidates.contains(&item_name.name)
2265 && inherent_method.is_fn()
2266 {
2267 let args =
2268 ty::GenericArgs::identity_for_item(self.tcx, inherent_method.def_id)
2269 .rebase_onto(
2270 self.tcx,
2271 inherent_method.container_id(self.tcx),
2272 adt_args,
2273 );
2274 let fn_sig =
2275 self.tcx.fn_sig(inherent_method.def_id).instantiate(self.tcx, args);
2276 let fn_sig = self.instantiate_binder_with_fresh_vars(
2277 item_name.span,
2278 BoundRegionConversionTime::FnCall,
2279 fn_sig,
2280 );
2281 let name = inherent_method.name();
2282 if let Some(ref args) = call_args
2283 && fn_sig.inputs()[1..]
2284 .iter()
2285 .eq_by(args, |expected, found| self.may_coerce(*expected, *found))
2286 {
2287 err.span_suggestion_verbose(
2288 item_name.span,
2289 ::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),
2290 name,
2291 Applicability::MaybeIncorrect,
2292 );
2293 return Some(name);
2294 } else if let None = call_args {
2295 err.span_note(
2296 self.tcx.def_span(inherent_method.def_id),
2297 ::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),
2298 );
2299 return Some(name);
2300 }
2301 }
2302 }
2303 }
2304 }
2305 None
2306 }
2307 fn note_candidates_on_method_error(
2308 &self,
2309 rcvr_ty: Ty<'tcx>,
2310 item_name: Ident,
2311 self_source: SelfSource<'tcx>,
2312 args: Option<&'tcx [hir::Expr<'tcx>]>,
2313 span: Span,
2314 err: &mut Diag<'_>,
2315 sources: &mut Vec<CandidateSource>,
2316 sugg_span: Option<Span>,
2317 ) {
2318 sources.sort_by_key(|source| match *source {
2319 CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
2320 CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
2321 });
2322 sources.dedup();
2323 let limit = if sources.len() == 5 { 5 } else { 4 };
2325
2326 let mut suggs = ::alloc::vec::Vec::new()vec![];
2327 for (idx, source) in sources.iter().take(limit).enumerate() {
2328 match *source {
2329 CandidateSource::Impl(impl_did) => {
2330 let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
2333 let impl_trait_id = self.tcx.impl_opt_trait_id(impl_did)?;
2334 self.associated_value(impl_trait_id, item_name)
2335 }) else {
2336 continue;
2337 };
2338
2339 let note_span = if item.def_id.is_local() {
2340 Some(self.tcx.def_span(item.def_id))
2341 } else if impl_did.is_local() {
2342 Some(self.tcx.def_span(impl_did))
2343 } else {
2344 None
2345 };
2346
2347 let impl_ty = self.tcx.at(span).type_of(impl_did).instantiate_identity();
2348
2349 let insertion = match self.tcx.impl_opt_trait_ref(impl_did) {
2350 None => String::new(),
2351 Some(trait_ref) => {
2352 ::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!(
2353 " of the trait `{}`",
2354 self.tcx.def_path_str(trait_ref.skip_binder().def_id)
2355 )
2356 }
2357 };
2358
2359 let (note_str, idx) = if sources.len() > 1 {
2360 (
2361 ::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!(
2362 "candidate #{} is defined in an impl{} for the type `{}`",
2363 idx + 1,
2364 insertion,
2365 impl_ty,
2366 ),
2367 Some(idx + 1),
2368 )
2369 } else {
2370 (
2371 ::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!(
2372 "the candidate is defined in an impl{insertion} for the type `{impl_ty}`",
2373 ),
2374 None,
2375 )
2376 };
2377 if let Some(note_span) = note_span {
2378 err.span_note(note_span, note_str);
2380 } else {
2381 err.note(note_str);
2382 }
2383 if let Some(sugg_span) = sugg_span
2384 && let Some(trait_ref) = self.tcx.impl_opt_trait_ref(impl_did)
2385 && let Some(sugg) = print_disambiguation_help(
2386 self.tcx,
2387 err,
2388 self_source,
2389 args,
2390 trait_ref
2391 .instantiate(
2392 self.tcx,
2393 self.fresh_args_for_item(sugg_span, impl_did),
2394 )
2395 .with_replaced_self_ty(self.tcx, rcvr_ty),
2396 idx,
2397 sugg_span,
2398 item,
2399 )
2400 {
2401 suggs.push(sugg);
2402 }
2403 }
2404 CandidateSource::Trait(trait_did) => {
2405 let Some(item) = self.associated_value(trait_did, item_name) else { continue };
2406 let item_span = self.tcx.def_span(item.def_id);
2407 let idx = if sources.len() > 1 {
2408 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!(
2409 "candidate #{} is defined in the trait `{}`",
2410 idx + 1,
2411 self.tcx.def_path_str(trait_did)
2412 );
2413 err.span_note(item_span, msg);
2414 Some(idx + 1)
2415 } else {
2416 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!(
2417 "the candidate is defined in the trait `{}`",
2418 self.tcx.def_path_str(trait_did)
2419 );
2420 err.span_note(item_span, msg);
2421 None
2422 };
2423 if let Some(sugg_span) = sugg_span
2424 && let Some(sugg) = print_disambiguation_help(
2425 self.tcx,
2426 err,
2427 self_source,
2428 args,
2429 ty::TraitRef::new_from_args(
2430 self.tcx,
2431 trait_did,
2432 self.fresh_args_for_item(sugg_span, trait_did),
2433 )
2434 .with_replaced_self_ty(self.tcx, rcvr_ty),
2435 idx,
2436 sugg_span,
2437 item,
2438 )
2439 {
2440 suggs.push(sugg);
2441 }
2442 }
2443 }
2444 }
2445 if !suggs.is_empty()
2446 && let Some(span) = sugg_span
2447 {
2448 suggs.sort();
2449 err.span_suggestions(
2450 span.with_hi(item_name.span.lo()),
2451 "use fully-qualified syntax to disambiguate",
2452 suggs,
2453 Applicability::MachineApplicable,
2454 );
2455 }
2456 if sources.len() > limit {
2457 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("and {0} others",
sources.len() - limit))
})format!("and {} others", sources.len() - limit));
2458 }
2459 }
2460
2461 fn find_builder_fn(&self, err: &mut Diag<'_>, rcvr_ty: Ty<'tcx>, expr_id: hir::HirId) {
2464 let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
2465 return;
2466 };
2467 let mut items = self
2468 .tcx
2469 .inherent_impls(adt_def.did())
2470 .iter()
2471 .flat_map(|&i| self.tcx.associated_items(i).in_definition_order())
2472 .filter(|item| {
2475 #[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, .. })
2476 && self
2477 .probe_for_name(
2478 Mode::Path,
2479 item.ident(self.tcx),
2480 None,
2481 IsSuggestion(true),
2482 rcvr_ty,
2483 expr_id,
2484 ProbeScope::TraitsInScope,
2485 )
2486 .is_ok()
2487 })
2488 .filter_map(|item| {
2489 let ret_ty = self
2491 .tcx
2492 .fn_sig(item.def_id)
2493 .instantiate(self.tcx, self.fresh_args_for_item(DUMMY_SP, item.def_id))
2494 .output();
2495 let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
2496 let ty::Adt(def, args) = ret_ty.kind() else {
2497 return None;
2498 };
2499 if self.can_eq(self.param_env, ret_ty, rcvr_ty) {
2501 return Some((item.def_id, ret_ty));
2502 }
2503 if ![self.tcx.lang_items().option_type(), self.tcx.get_diagnostic_item(sym::Result)]
2505 .contains(&Some(def.did()))
2506 {
2507 return None;
2508 }
2509 let arg = args.get(0)?.expect_ty();
2510 if self.can_eq(self.param_env, rcvr_ty, arg) {
2511 Some((item.def_id, ret_ty))
2512 } else {
2513 None
2514 }
2515 })
2516 .collect::<Vec<_>>();
2517 let post = if items.len() > 5 {
2518 let items_len = items.len();
2519 items.truncate(4);
2520 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\nand {0} others", items_len - 4))
})format!("\nand {} others", items_len - 4)
2521 } else {
2522 String::new()
2523 };
2524 match items[..] {
2525 [] => {}
2526 [(def_id, ret_ty)] => {
2527 err.span_note(
2528 self.tcx.def_span(def_id),
2529 ::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!(
2530 "if you're trying to build a new `{rcvr_ty}`, consider using `{}` which \
2531 returns `{ret_ty}`",
2532 self.tcx.def_path_str(def_id),
2533 ),
2534 );
2535 }
2536 _ => {
2537 let span: MultiSpan = items
2538 .iter()
2539 .map(|&(def_id, _)| self.tcx.def_span(def_id))
2540 .collect::<Vec<Span>>()
2541 .into();
2542 err.span_note(
2543 span,
2544 ::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!(
2545 "if you're trying to build a new `{rcvr_ty}` consider using one of the \
2546 following associated functions:\n{}{post}",
2547 items
2548 .iter()
2549 .map(|&(def_id, _ret_ty)| self.tcx.def_path_str(def_id))
2550 .collect::<Vec<String>>()
2551 .join("\n")
2552 ),
2553 );
2554 }
2555 }
2556 }
2557
2558 fn suggest_associated_call_syntax(
2561 &self,
2562 err: &mut Diag<'_>,
2563 static_candidates: &[CandidateSource],
2564 rcvr_ty: Ty<'tcx>,
2565 source: SelfSource<'tcx>,
2566 item_name: Ident,
2567 args: Option<&'tcx [hir::Expr<'tcx>]>,
2568 sugg_span: Span,
2569 ) {
2570 let mut has_unsuggestable_args = false;
2571 let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
2572 let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
2576 let target_ty = self
2577 .autoderef(sugg_span, rcvr_ty)
2578 .silence_errors()
2579 .find(|(rcvr_ty, _)| {
2580 DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
2581 })
2582 .map_or(impl_ty, |(ty, _)| ty)
2583 .peel_refs();
2584 if let ty::Adt(def, args) = target_ty.kind() {
2585 let infer_args = self.tcx.mk_args_from_iter(args.into_iter().map(|arg| {
2588 if !arg.is_suggestable(self.tcx, true) {
2589 has_unsuggestable_args = true;
2590 match arg.kind() {
2591 GenericArgKind::Lifetime(_) => {
2592 self.next_region_var(RegionVariableOrigin::Misc(DUMMY_SP)).into()
2593 }
2594 GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
2595 GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
2596 }
2597 } else {
2598 arg
2599 }
2600 }));
2601
2602 self.tcx.value_path_str_with_args(def.did(), infer_args)
2603 } else {
2604 self.ty_to_value_string(target_ty)
2605 }
2606 } else {
2607 self.ty_to_value_string(rcvr_ty.peel_refs())
2608 };
2609 if let SelfSource::MethodCall(_) = source {
2610 let first_arg = static_candidates.get(0).and_then(|candidate_source| {
2611 let (assoc_did, self_ty) = match candidate_source {
2612 CandidateSource::Impl(impl_did) => {
2613 (*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
2614 }
2615 CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
2616 };
2617
2618 let assoc = self.associated_value(assoc_did, item_name)?;
2619 if !assoc.is_fn() {
2620 return None;
2621 }
2622
2623 let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
2626 sig.inputs().skip_binder().get(0).and_then(|first| {
2627 let first_ty = first.peel_refs();
2629 if first_ty == self_ty || first_ty == self.tcx.types.self_param {
2630 Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
2631 } else {
2632 None
2633 }
2634 })
2635 });
2636
2637 let mut applicability = Applicability::MachineApplicable;
2638 let args = if let SelfSource::MethodCall(receiver) = source
2639 && let Some(args) = args
2640 {
2641 let explicit_args = if first_arg.is_some() {
2643 std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
2644 } else {
2645 if has_unsuggestable_args {
2647 applicability = Applicability::HasPlaceholders;
2648 }
2649 args.iter().collect()
2650 };
2651 ::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!(
2652 "({}{})",
2653 first_arg.unwrap_or(""),
2654 explicit_args
2655 .iter()
2656 .map(|arg| self
2657 .tcx
2658 .sess
2659 .source_map()
2660 .span_to_snippet(arg.span)
2661 .unwrap_or_else(|_| {
2662 applicability = Applicability::HasPlaceholders;
2663 "_".to_owned()
2664 }))
2665 .collect::<Vec<_>>()
2666 .join(", "),
2667 )
2668 } else {
2669 applicability = Applicability::HasPlaceholders;
2670 "(...)".to_owned()
2671 };
2672 err.span_suggestion_verbose(
2673 sugg_span,
2674 "use associated function syntax instead",
2675 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}{2}", ty_str, item_name,
args))
})format!("{ty_str}::{item_name}{args}"),
2676 applicability,
2677 );
2678 } else {
2679 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}`",));
2680 }
2681 }
2682
2683 fn suggest_calling_field_as_fn(
2686 &self,
2687 span: Span,
2688 rcvr_ty: Ty<'tcx>,
2689 expr: &hir::Expr<'_>,
2690 item_name: Ident,
2691 err: &mut Diag<'_>,
2692 ) -> bool {
2693 let tcx = self.tcx;
2694 let field_receiver =
2695 self.autoderef(span, rcvr_ty).silence_errors().find_map(|(ty, _)| match ty.kind() {
2696 ty::Adt(def, args) if !def.is_enum() => {
2697 let variant = &def.non_enum_variant();
2698 tcx.find_field_index(item_name, variant).map(|index| {
2699 let field = &variant.fields[index];
2700 let field_ty = field.ty(tcx, args);
2701 (field, field_ty)
2702 })
2703 }
2704 _ => None,
2705 });
2706 if let Some((field, field_ty)) = field_receiver {
2707 let scope = tcx.parent_module_from_def_id(self.body_id);
2708 let is_accessible = field.vis.is_accessible_from(scope, tcx);
2709
2710 if is_accessible {
2711 if let Some((what, _, _)) = self.extract_callable_info(field_ty) {
2712 let what = match what {
2713 DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
2714 DefIdOrName::Name(what) => what,
2715 };
2716 let expr_span = expr.span.to(item_name.span);
2717 err.multipart_suggestion(
2718 ::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!(
2719 "to call the {what} stored in `{item_name}`, \
2720 surround the field access with parentheses",
2721 ),
2722 ::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![
2723 (expr_span.shrink_to_lo(), '('.to_string()),
2724 (expr_span.shrink_to_hi(), ')'.to_string()),
2725 ],
2726 Applicability::MachineApplicable,
2727 );
2728 } else {
2729 let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2730
2731 if let Some(span) = call_expr.span.trim_start(item_name.span) {
2732 err.span_suggestion(
2733 span,
2734 "remove the arguments",
2735 "",
2736 Applicability::MaybeIncorrect,
2737 );
2738 }
2739 }
2740 }
2741
2742 let field_kind = if is_accessible { "field" } else { "private field" };
2743 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"));
2744 return true;
2745 }
2746 false
2747 }
2748
2749 fn report_failed_method_call_on_range_end(
2752 &self,
2753 tcx: TyCtxt<'tcx>,
2754 actual: Ty<'tcx>,
2755 source: SelfSource<'tcx>,
2756 span: Span,
2757 item_name: Ident,
2758 ) -> Result<(), ErrorGuaranteed> {
2759 if let SelfSource::MethodCall(expr) = source {
2760 for (_, parent) in tcx.hir_parent_iter(expr.hir_id).take(5) {
2761 if let Node::Expr(parent_expr) = parent {
2762 if !is_range_literal(parent_expr) {
2763 continue;
2764 }
2765 let lang_item = match parent_expr.kind {
2766 ExprKind::Struct(qpath, _, _) => match tcx.qpath_lang_item(*qpath) {
2767 Some(
2768 lang_item @ (LangItem::Range
2769 | LangItem::RangeCopy
2770 | LangItem::RangeInclusiveCopy
2771 | LangItem::RangeTo
2772 | LangItem::RangeToInclusive),
2773 ) => Some(lang_item),
2774 _ => None,
2775 },
2776 ExprKind::Call(func, _) => match func.kind {
2777 ExprKind::Path(qpath)
2779 if tcx.qpath_is_lang_item(qpath, LangItem::RangeInclusiveNew) =>
2780 {
2781 Some(LangItem::RangeInclusiveStruct)
2782 }
2783 _ => None,
2784 },
2785 _ => None,
2786 };
2787
2788 if lang_item.is_none() {
2789 continue;
2790 }
2791
2792 let span_included = match parent_expr.kind {
2793 hir::ExprKind::Struct(_, eps, _) => {
2794 eps.last().is_some_and(|ep| ep.span.contains(span))
2795 }
2796 hir::ExprKind::Call(func, ..) => func.span.contains(span),
2798 _ => false,
2799 };
2800
2801 if !span_included {
2802 continue;
2803 }
2804
2805 let Some(range_def_id) =
2806 lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item))
2807 else {
2808 continue;
2809 };
2810 let range_ty =
2811 self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]);
2812
2813 let pick = self.lookup_probe_for_diagnostic(
2814 item_name,
2815 range_ty,
2816 expr,
2817 ProbeScope::AllTraits,
2818 None,
2819 );
2820 if pick.is_ok() {
2821 let range_span = parent_expr.span.with_hi(expr.span.hi());
2822 return Err(self.dcx().emit_err(errors::MissingParenthesesInRange {
2823 span,
2824 ty: actual,
2825 method_name: item_name.as_str().to_string(),
2826 add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
2827 func_name: item_name.name.as_str().to_string(),
2828 left: range_span.shrink_to_lo(),
2829 right: range_span.shrink_to_hi(),
2830 }),
2831 }));
2832 }
2833 }
2834 }
2835 }
2836 Ok(())
2837 }
2838
2839 fn report_failed_method_call_on_numerical_infer_var(
2840 &self,
2841 tcx: TyCtxt<'tcx>,
2842 actual: Ty<'tcx>,
2843 source: SelfSource<'_>,
2844 span: Span,
2845 item_kind: &str,
2846 item_name: Ident,
2847 long_ty_path: &mut Option<PathBuf>,
2848 ) -> Result<(), ErrorGuaranteed> {
2849 let found_candidate = all_traits(self.tcx)
2850 .into_iter()
2851 .any(|info| self.associated_value(info.def_id, item_name).is_some());
2852 let found_assoc = |ty: Ty<'tcx>| {
2853 simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
2854 .and_then(|simp| {
2855 tcx.incoherent_impls(simp)
2856 .iter()
2857 .find_map(|&id| self.associated_value(id, item_name))
2858 })
2859 .is_some()
2860 };
2861 let found_candidate = found_candidate
2862 || found_assoc(tcx.types.i8)
2863 || found_assoc(tcx.types.i16)
2864 || found_assoc(tcx.types.i32)
2865 || found_assoc(tcx.types.i64)
2866 || found_assoc(tcx.types.i128)
2867 || found_assoc(tcx.types.u8)
2868 || found_assoc(tcx.types.u16)
2869 || found_assoc(tcx.types.u32)
2870 || found_assoc(tcx.types.u64)
2871 || found_assoc(tcx.types.u128)
2872 || found_assoc(tcx.types.f32)
2873 || found_assoc(tcx.types.f64);
2874 if found_candidate
2875 && actual.is_numeric()
2876 && !actual.has_concrete_skeleton()
2877 && let SelfSource::MethodCall(expr) = source
2878 {
2879 let ty_str = self.tcx.short_string(actual, long_ty_path);
2880 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!(
2881 self.dcx(),
2882 span,
2883 E0689,
2884 "can't call {item_kind} `{item_name}` on ambiguous numeric type `{ty_str}`"
2885 );
2886 *err.long_ty_path() = long_ty_path.take();
2887 let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
2888 match expr.kind {
2889 ExprKind::Lit(lit) => {
2890 let snippet = tcx
2892 .sess
2893 .source_map()
2894 .span_to_snippet(lit.span)
2895 .unwrap_or_else(|_| "<numeric literal>".to_owned());
2896
2897 let snippet = snippet.trim_suffix('.');
2900 err.span_suggestion(
2901 lit.span,
2902 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you must specify a concrete type for this numeric value, like `{0}`",
concrete_type))
})format!(
2903 "you must specify a concrete type for this numeric value, \
2904 like `{concrete_type}`"
2905 ),
2906 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_{1}", snippet, concrete_type))
})format!("{snippet}_{concrete_type}"),
2907 Applicability::MaybeIncorrect,
2908 );
2909 }
2910 ExprKind::Path(QPath::Resolved(_, path)) => {
2911 if let hir::def::Res::Local(hir_id) = path.res {
2913 let span = tcx.hir_span(hir_id);
2914 let filename = tcx.sess.source_map().span_to_filename(span);
2915
2916 let parent_node = self.tcx.parent_hir_node(hir_id);
2917 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you must specify a type for this binding, like `{0}`",
concrete_type))
})format!(
2918 "you must specify a type for this binding, like `{concrete_type}`",
2919 );
2920
2921 match (filename, parent_node) {
2924 (
2925 FileName::Real(_),
2926 Node::LetStmt(hir::LetStmt {
2927 source: hir::LocalSource::Normal,
2928 ty,
2929 ..
2930 }),
2931 ) => {
2932 let type_span = ty
2933 .map(|ty| ty.span.with_lo(span.hi()))
2934 .unwrap_or(span.shrink_to_hi());
2935 err.span_suggestion(
2936 type_span,
2939 msg,
2940 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}", concrete_type))
})format!(": {concrete_type}"),
2941 Applicability::MaybeIncorrect,
2942 );
2943 }
2944 (FileName::Real(_), Node::Pat(pat))
2947 if let Node::Pat(binding_pat) = self.tcx.hir_node(hir_id)
2948 && let hir::PatKind::Binding(..) = binding_pat.kind
2949 && let Node::Pat(parent_pat) = parent_node
2950 && #[allow(non_exhaustive_omitted_patterns)] match parent_pat.kind {
hir::PatKind::Ref(..) => true,
_ => false,
}matches!(parent_pat.kind, hir::PatKind::Ref(..)) =>
2951 {
2952 err.span_label(span, "you must specify a type for this binding");
2953
2954 let mut ref_muts = Vec::new();
2955 let mut current_node = parent_node;
2956
2957 while let Node::Pat(parent_pat) = current_node {
2958 if let hir::PatKind::Ref(_, _, mutability) = parent_pat.kind {
2959 ref_muts.push(mutability);
2960 current_node = self.tcx.parent_hir_node(parent_pat.hir_id);
2961 } else {
2962 break;
2963 }
2964 }
2965
2966 let mut type_annotation = String::new();
2967 for mutability in ref_muts.iter().rev() {
2968 match mutability {
2969 hir::Mutability::Mut => type_annotation.push_str("&mut "),
2970 hir::Mutability::Not => type_annotation.push('&'),
2971 }
2972 }
2973 type_annotation.push_str(&concrete_type);
2974
2975 err.span_suggestion_verbose(
2976 pat.span.shrink_to_hi(),
2977 "specify the type in the closure argument list",
2978 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}", type_annotation))
})format!(": {type_annotation}"),
2979 Applicability::MaybeIncorrect,
2980 );
2981 }
2982 _ => {
2983 err.span_label(span, msg);
2984 }
2985 }
2986 }
2987 }
2988 _ => {}
2989 }
2990 return Err(err.emit());
2991 }
2992 Ok(())
2993 }
2994
2995 pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
2999 {
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:2999",
"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(2999u32),
::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);
3000 let [seg1, seg2] = segs else {
3001 return;
3002 };
3003 self.dcx().try_steal_modify_and_emit_err(
3004 seg1.ident.span,
3005 StashKey::CallAssocMethod,
3006 |err| {
3007 let body = self.tcx.hir_body_owned_by(self.body_id);
3008 struct LetVisitor {
3009 ident_name: Symbol,
3010 }
3011
3012 impl<'v> Visitor<'v> for LetVisitor {
3014 type Result = ControlFlow<Option<&'v hir::Expr<'v>>>;
3015 fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
3016 if let hir::StmtKind::Let(&hir::LetStmt { pat, init, .. }) = ex.kind
3017 && let hir::PatKind::Binding(_, _, ident, ..) = pat.kind
3018 && ident.name == self.ident_name
3019 {
3020 ControlFlow::Break(init)
3021 } else {
3022 hir::intravisit::walk_stmt(self, ex)
3023 }
3024 }
3025 }
3026
3027 if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
3028 && let ControlFlow::Break(Some(expr)) =
3029 (LetVisitor { ident_name: seg1.ident.name }).visit_body(body)
3030 && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
3031 {
3032 let probe = self.lookup_probe_for_diagnostic(
3033 seg2.ident,
3034 self_ty,
3035 call_expr,
3036 ProbeScope::TraitsInScope,
3037 None,
3038 );
3039 if probe.is_ok() {
3040 let sm = self.infcx.tcx.sess.source_map();
3041 err.span_suggestion_verbose(
3042 sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':')
3043 .unwrap(),
3044 "you may have meant to call an instance method",
3045 ".",
3046 Applicability::MaybeIncorrect,
3047 );
3048 }
3049 }
3050 },
3051 );
3052 }
3053
3054 fn suggest_calling_method_on_field(
3056 &self,
3057 err: &mut Diag<'_>,
3058 source: SelfSource<'tcx>,
3059 span: Span,
3060 actual: Ty<'tcx>,
3061 item_name: Ident,
3062 return_type: Option<Ty<'tcx>>,
3063 ) {
3064 if let SelfSource::MethodCall(expr) = source {
3065 let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
3066 for fields in self.get_field_candidates_considering_privacy_for_diag(
3067 span,
3068 actual,
3069 mod_id,
3070 expr.hir_id,
3071 ) {
3072 let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(expr.hir_id));
3073
3074 let lang_items = self.tcx.lang_items();
3075 let never_mention_traits = [
3076 lang_items.clone_trait(),
3077 lang_items.deref_trait(),
3078 lang_items.deref_mut_trait(),
3079 self.tcx.get_diagnostic_item(sym::AsRef),
3080 self.tcx.get_diagnostic_item(sym::AsMut),
3081 self.tcx.get_diagnostic_item(sym::Borrow),
3082 self.tcx.get_diagnostic_item(sym::BorrowMut),
3083 ];
3084 let mut candidate_fields: Vec<_> = fields
3085 .into_iter()
3086 .filter_map(|candidate_field| {
3087 self.check_for_nested_field_satisfying_condition_for_diag(
3088 span,
3089 &|_, field_ty| {
3090 self.lookup_probe_for_diagnostic(
3091 item_name,
3092 field_ty,
3093 call_expr,
3094 ProbeScope::TraitsInScope,
3095 return_type,
3096 )
3097 .is_ok_and(|pick| {
3098 !never_mention_traits
3099 .iter()
3100 .flatten()
3101 .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
3102 })
3103 },
3104 candidate_field,
3105 ::alloc::vec::Vec::new()vec![],
3106 mod_id,
3107 expr.hir_id,
3108 )
3109 })
3110 .map(|field_path| {
3111 field_path
3112 .iter()
3113 .map(|id| id.to_string())
3114 .collect::<Vec<String>>()
3115 .join(".")
3116 })
3117 .collect();
3118 candidate_fields.sort();
3119
3120 let len = candidate_fields.len();
3121 if len > 0 {
3122 err.span_suggestions(
3123 item_name.span.shrink_to_lo(),
3124 ::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!(
3125 "{} of the expressions' fields {} a method of the same name",
3126 if len > 1 { "some" } else { "one" },
3127 if len > 1 { "have" } else { "has" },
3128 ),
3129 candidate_fields.iter().map(|path| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.", path))
})format!("{path}.")),
3130 Applicability::MaybeIncorrect,
3131 );
3132 }
3133 }
3134 }
3135 }
3136
3137 fn suggest_unwrapping_inner_self(
3138 &self,
3139 err: &mut Diag<'_>,
3140 source: SelfSource<'tcx>,
3141 actual: Ty<'tcx>,
3142 item_name: Ident,
3143 ) {
3144 let tcx = self.tcx;
3145 let SelfSource::MethodCall(expr) = source else {
3146 return;
3147 };
3148 let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
3149
3150 let ty::Adt(kind, args) = actual.kind() else {
3151 return;
3152 };
3153 match kind.adt_kind() {
3154 ty::AdtKind::Enum => {
3155 let matching_variants: Vec<_> = kind
3156 .variants()
3157 .iter()
3158 .flat_map(|variant| {
3159 let [field] = &variant.fields.raw[..] else {
3160 return None;
3161 };
3162 let field_ty = field.ty(tcx, args);
3163
3164 if self.resolve_vars_if_possible(field_ty).is_ty_var() {
3166 return None;
3167 }
3168
3169 self.lookup_probe_for_diagnostic(
3170 item_name,
3171 field_ty,
3172 call_expr,
3173 ProbeScope::TraitsInScope,
3174 None,
3175 )
3176 .ok()
3177 .map(|pick| (variant, field, pick))
3178 })
3179 .collect();
3180
3181 let ret_ty_matches = |diagnostic_item| {
3182 if let Some(ret_ty) = self
3183 .ret_coercion
3184 .as_ref()
3185 .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
3186 && let ty::Adt(kind, _) = ret_ty.kind()
3187 && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
3188 {
3189 true
3190 } else {
3191 false
3192 }
3193 };
3194
3195 match &matching_variants[..] {
3196 [(_, field, pick)] => {
3197 let self_ty = field.ty(tcx, args);
3198 err.span_note(
3199 tcx.def_span(pick.item.def_id),
3200 ::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}`"),
3201 );
3202 let (article, kind, variant, question) = if tcx.is_diagnostic_item(sym::Result, kind.did())
3203 && !tcx.hir_is_inside_const_context(expr.hir_id)
3205 {
3206 ("a", "Result", "Err", ret_ty_matches(sym::Result))
3207 } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
3208 ("an", "Option", "None", ret_ty_matches(sym::Option))
3209 } else {
3210 return;
3211 };
3212 if question {
3213 err.span_suggestion_verbose(
3214 expr.span.shrink_to_hi(),
3215 ::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!(
3216 "use the `?` operator to extract the `{self_ty}` value, propagating \
3217 {article} `{kind}::{variant}` value to the caller"
3218 ),
3219 "?",
3220 Applicability::MachineApplicable,
3221 );
3222 } else {
3223 err.span_suggestion_verbose(
3224 expr.span.shrink_to_hi(),
3225 ::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!(
3226 "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
3227 panicking if the value is {article} `{kind}::{variant}`"
3228 ),
3229 ".expect(\"REASON\")",
3230 Applicability::HasPlaceholders,
3231 );
3232 }
3233 }
3234 _ => {}
3236 }
3237 }
3238 ty::AdtKind::Struct | ty::AdtKind::Union => {
3241 let [first] = ***args else {
3242 return;
3243 };
3244 let ty::GenericArgKind::Type(ty) = first.kind() else {
3245 return;
3246 };
3247 let Ok(pick) = self.lookup_probe_for_diagnostic(
3248 item_name,
3249 ty,
3250 call_expr,
3251 ProbeScope::TraitsInScope,
3252 None,
3253 ) else {
3254 return;
3255 };
3256
3257 let name = self.ty_to_value_string(actual);
3258 let inner_id = kind.did();
3259 let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
3260 pick.autoref_or_ptr_adjustment
3261 {
3262 Some(mutbl)
3263 } else {
3264 None
3265 };
3266
3267 if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
3268 err.help("use `with` or `try_with` to access thread local storage");
3269 } else if tcx.is_lang_item(kind.did(), LangItem::MaybeUninit) {
3270 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!(
3271 "if this `{name}` has been initialized, \
3272 use one of the `assume_init` methods to access the inner value"
3273 ));
3274 } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
3275 let (suggestion, borrow_kind, panic_if) = match mutable {
3276 Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
3277 Some(Mutability::Mut) => {
3278 (".borrow_mut()", "mutably borrow", "any borrows exist")
3279 }
3280 None => return,
3281 };
3282 err.span_suggestion_verbose(
3283 expr.span.shrink_to_hi(),
3284 ::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!(
3285 "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3286 panicking if {panic_if}"
3287 ),
3288 suggestion,
3289 Applicability::MaybeIncorrect,
3290 );
3291 } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
3292 err.span_suggestion_verbose(
3293 expr.span.shrink_to_hi(),
3294 ::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!(
3295 "use `.lock().unwrap()` to borrow the `{ty}`, \
3296 blocking the current thread until it can be acquired"
3297 ),
3298 ".lock().unwrap()",
3299 Applicability::MaybeIncorrect,
3300 );
3301 } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
3302 let (suggestion, borrow_kind) = match mutable {
3303 Some(Mutability::Not) => (".read().unwrap()", "borrow"),
3304 Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
3305 None => return,
3306 };
3307 err.span_suggestion_verbose(
3308 expr.span.shrink_to_hi(),
3309 ::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!(
3310 "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3311 blocking the current thread until it can be acquired"
3312 ),
3313 suggestion,
3314 Applicability::MaybeIncorrect,
3315 );
3316 } else {
3317 return;
3318 };
3319
3320 err.span_note(
3321 tcx.def_span(pick.item.def_id),
3322 ::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}`"),
3323 );
3324 }
3325 }
3326 }
3327
3328 pub(crate) fn note_unmet_impls_on_type(
3329 &self,
3330 err: &mut Diag<'_>,
3331 errors: &[FulfillmentError<'tcx>],
3332 suggest_derive: bool,
3333 ) {
3334 let preds: Vec<_> = errors
3335 .iter()
3336 .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
3337 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
3338 match pred.self_ty().kind() {
3339 ty::Adt(_, _) => Some((e.root_obligation.predicate, pred)),
3340 _ => None,
3341 }
3342 }
3343 _ => None,
3344 })
3345 .collect();
3346
3347 let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
3349 preds.iter().partition(|&(_, pred)| {
3350 if let ty::Adt(def, _) = pred.self_ty().kind() {
3351 def.did().is_local()
3352 } else {
3353 false
3354 }
3355 });
3356
3357 local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
3358 let local_def_ids = local_preds
3359 .iter()
3360 .filter_map(|(_, pred)| match pred.self_ty().kind() {
3361 ty::Adt(def, _) => Some(def.did()),
3362 _ => None,
3363 })
3364 .collect::<FxIndexSet<_>>();
3365 let mut local_spans: MultiSpan = local_def_ids
3366 .iter()
3367 .filter_map(|def_id| {
3368 let span = self.tcx.def_span(*def_id);
3369 if span.is_dummy() { None } else { Some(span) }
3370 })
3371 .collect::<Vec<_>>()
3372 .into();
3373 for (_, pred) in &local_preds {
3374 if let ty::Adt(def, _) = pred.self_ty().kind() {
3375 local_spans.push_span_label(
3376 self.tcx.def_span(def.did()),
3377 ::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()),
3378 );
3379 }
3380 }
3381 if local_spans.primary_span().is_some() {
3382 let msg = if let [(_, local_pred)] = local_preds.as_slice() {
3383 ::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!(
3384 "an implementation of `{}` might be missing for `{}`",
3385 local_pred.trait_ref.print_trait_sugared(),
3386 local_pred.self_ty()
3387 )
3388 } else {
3389 ::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!(
3390 "the following type{} would have to `impl` {} required trait{} for this \
3391 operation to be valid",
3392 pluralize!(local_def_ids.len()),
3393 if local_def_ids.len() == 1 { "its" } else { "their" },
3394 pluralize!(local_preds.len()),
3395 )
3396 };
3397 err.span_note(local_spans, msg);
3398 }
3399
3400 foreign_preds
3401 .sort_by_key(|(_, pred): &(_, ty::TraitPredicate<'_>)| pred.trait_ref.to_string());
3402
3403 for (_, pred) in &foreign_preds {
3404 let ty = pred.self_ty();
3405 let ty::Adt(def, _) = ty.kind() else { continue };
3406 let span = self.tcx.def_span(def.did());
3407 if span.is_dummy() {
3408 continue;
3409 }
3410 let mut mspan: MultiSpan = span.into();
3411 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"));
3412 err.span_note(
3413 mspan,
3414 ::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()),
3415 );
3416
3417 foreign_preds.iter().find(|&(root_pred, pred)| {
3418 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred)) =
3419 root_pred.kind().skip_binder()
3420 && let Some(root_adt) = root_pred.self_ty().ty_adt_def()
3421 {
3422 self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(err, pred, root_adt)
3423 } else {
3424 false
3425 }
3426 });
3427 }
3428
3429 let preds: Vec<_> = errors
3430 .iter()
3431 .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
3432 .collect();
3433 if suggest_derive {
3434 self.suggest_derive(err, &preds);
3435 } else {
3436 let _ = self.note_predicate_source_and_get_derives(err, &preds);
3438 }
3439 }
3440
3441 fn consider_suggesting_derives_for_ty(
3444 &self,
3445 trait_pred: ty::TraitPredicate<'tcx>,
3446 adt: ty::AdtDef<'tcx>,
3447 ) -> Option<Vec<(String, Span, Symbol)>> {
3448 let diagnostic_name = self.tcx.get_diagnostic_name(trait_pred.def_id())?;
3449
3450 let can_derive = match diagnostic_name {
3451 sym::Default
3452 | sym::Eq
3453 | sym::PartialEq
3454 | sym::Ord
3455 | sym::PartialOrd
3456 | sym::Clone
3457 | sym::Copy
3458 | sym::Hash
3459 | sym::Debug => true,
3460 _ => false,
3461 };
3462
3463 if !can_derive {
3464 return None;
3465 }
3466
3467 let trait_def_id = trait_pred.def_id();
3468 let self_ty = trait_pred.self_ty();
3469
3470 if self.tcx.non_blanket_impls_for_ty(trait_def_id, self_ty).any(|impl_def_id| {
3473 self.tcx
3474 .type_of(impl_def_id)
3475 .instantiate_identity()
3476 .ty_adt_def()
3477 .is_some_and(|def| def.did() == adt.did())
3478 }) {
3479 return None;
3480 }
3481
3482 let mut derives = Vec::new();
3483 let self_name = self_ty.to_string();
3484 let self_span = self.tcx.def_span(adt.did());
3485
3486 for super_trait in supertraits(self.tcx, ty::Binder::dummy(trait_pred.trait_ref)) {
3487 if let Some(parent_diagnostic_name) = self.tcx.get_diagnostic_name(super_trait.def_id())
3488 {
3489 derives.push((self_name.clone(), self_span, parent_diagnostic_name));
3490 }
3491 }
3492
3493 derives.push((self_name, self_span, diagnostic_name));
3494
3495 Some(derives)
3496 }
3497
3498 fn note_predicate_source_and_get_derives(
3499 &self,
3500 err: &mut Diag<'_>,
3501 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3502 ) -> Vec<(String, Span, Symbol)> {
3503 let mut derives = Vec::new();
3504 let mut traits = Vec::new();
3505 for (pred, _, _) in unsatisfied_predicates {
3506 let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) =
3507 pred.kind().no_bound_vars()
3508 else {
3509 continue;
3510 };
3511 let adt = match trait_pred.self_ty().ty_adt_def() {
3512 Some(adt) if adt.did().is_local() => adt,
3513 _ => continue,
3514 };
3515 if let Some(new_derives) = self.consider_suggesting_derives_for_ty(trait_pred, adt) {
3516 derives.extend(new_derives);
3517 } else {
3518 traits.push(trait_pred.def_id());
3519 }
3520 }
3521 traits.sort_by_key(|&id| self.tcx.def_path_str(id));
3522 traits.dedup();
3523
3524 let len = traits.len();
3525 if len > 0 {
3526 let span =
3527 MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
3528 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]));
3529 for (i, &did) in traits.iter().enumerate().skip(1) {
3530 if len > 2 {
3531 names.push_str(", ");
3532 }
3533 if i == len - 1 {
3534 names.push_str(" and ");
3535 }
3536 names.push('`');
3537 names.push_str(&self.tcx.def_path_str(did));
3538 names.push('`');
3539 }
3540 err.span_note(
3541 span,
3542 ::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),
3543 );
3544 }
3545
3546 derives
3547 }
3548
3549 pub(crate) fn suggest_derive(
3550 &self,
3551 err: &mut Diag<'_>,
3552 unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3553 ) -> bool {
3554 let mut derives = self.note_predicate_source_and_get_derives(err, unsatisfied_predicates);
3555 derives.sort();
3556 derives.dedup();
3557
3558 let mut derives_grouped = Vec::<(String, Span, String)>::new();
3559 for (self_name, self_span, trait_name) in derives.into_iter() {
3560 if let Some((last_self_name, _, last_trait_names)) = derives_grouped.last_mut() {
3561 if last_self_name == &self_name {
3562 last_trait_names.push_str(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}", trait_name))
})format!(", {trait_name}").as_str());
3563 continue;
3564 }
3565 }
3566 derives_grouped.push((self_name, self_span, trait_name.to_string()));
3567 }
3568
3569 for (self_name, self_span, traits) in &derives_grouped {
3570 err.span_suggestion_verbose(
3571 self_span.shrink_to_lo(),
3572 ::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})]`"),
3573 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#[derive({0})]\n", traits))
})format!("#[derive({traits})]\n"),
3574 Applicability::MaybeIncorrect,
3575 );
3576 }
3577 !derives_grouped.is_empty()
3578 }
3579
3580 fn note_derefed_ty_has_method(
3581 &self,
3582 err: &mut Diag<'_>,
3583 self_source: SelfSource<'tcx>,
3584 rcvr_ty: Ty<'tcx>,
3585 item_name: Ident,
3586 expected: Expectation<'tcx>,
3587 ) {
3588 let SelfSource::QPath(ty) = self_source else {
3589 return;
3590 };
3591 for (deref_ty, _) in self.autoderef(DUMMY_SP, rcvr_ty).silence_errors().skip(1) {
3592 if let Ok(pick) = self.probe_for_name(
3593 Mode::Path,
3594 item_name,
3595 expected.only_has_type(self),
3596 IsSuggestion(true),
3597 deref_ty,
3598 ty.hir_id,
3599 ProbeScope::TraitsInScope,
3600 ) {
3601 if deref_ty.is_suggestable(self.tcx, true)
3602 && pick.item.is_method()
3606 && let Some(self_ty) =
3607 self.tcx.fn_sig(pick.item.def_id).instantiate_identity().inputs().skip_binder().get(0)
3608 && self_ty.is_ref()
3609 {
3610 let suggested_path = match deref_ty.kind() {
3611 ty::Bool
3612 | ty::Char
3613 | ty::Int(_)
3614 | ty::Uint(_)
3615 | ty::Float(_)
3616 | ty::Adt(_, _)
3617 | ty::Str
3618 | ty::Alias(ty::Projection | ty::Inherent, _)
3619 | ty::Param(_) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", deref_ty))
})format!("{deref_ty}"),
3620 _ if self
3626 .tcx
3627 .sess
3628 .source_map()
3629 .span_wrapped_by_angle_or_parentheses(ty.span) =>
3630 {
3631 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", deref_ty))
})format!("{deref_ty}")
3632 }
3633 _ => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", deref_ty))
})format!("<{deref_ty}>"),
3634 };
3635 err.span_suggestion_verbose(
3636 ty.span,
3637 ::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}`"),
3638 suggested_path,
3639 Applicability::MaybeIncorrect,
3640 );
3641 } else {
3642 err.span_note(
3643 ty.span,
3644 ::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}`"),
3645 );
3646 }
3647 return;
3648 }
3649 }
3650 }
3651
3652 fn suggest_bounds_for_range_to_method(
3653 &self,
3654 err: &mut Diag<'_>,
3655 source: SelfSource<'tcx>,
3656 item_ident: Ident,
3657 ) {
3658 let SelfSource::MethodCall(rcvr_expr) = source else { return };
3659 let hir::ExprKind::Struct(qpath, fields, _) = rcvr_expr.kind else { return };
3660 let Some(lang_item) = self.tcx.qpath_lang_item(*qpath) else {
3661 return;
3662 };
3663 let is_inclusive = match lang_item {
3664 hir::LangItem::RangeTo => false,
3665 hir::LangItem::RangeToInclusive | hir::LangItem::RangeInclusiveCopy => true,
3666 _ => return,
3667 };
3668
3669 let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) else { return };
3670 let Some(_) = self
3671 .tcx
3672 .associated_items(iterator_trait)
3673 .filter_by_name_unhygienic(item_ident.name)
3674 .next()
3675 else {
3676 return;
3677 };
3678
3679 let source_map = self.tcx.sess.source_map();
3680 let range_type = if is_inclusive { "RangeInclusive" } else { "Range" };
3681 let Some(end_field) = fields.iter().find(|f| f.ident.name == rustc_span::sym::end) else {
3682 return;
3683 };
3684
3685 let element_ty = self.typeck_results.borrow().expr_ty_opt(end_field.expr);
3686 let is_integral = element_ty.is_some_and(|ty| ty.is_integral());
3687 let end_is_negative = is_integral
3688 && #[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, _));
3689
3690 let Ok(snippet) = source_map.span_to_snippet(rcvr_expr.span) else { return };
3691
3692 let offset = snippet
3693 .chars()
3694 .take_while(|&c| c == '(' || c.is_whitespace())
3695 .map(|c| c.len_utf8())
3696 .sum::<usize>();
3697
3698 let insert_span = rcvr_expr
3699 .span
3700 .with_lo(rcvr_expr.span.lo() + rustc_span::BytePos(offset as u32))
3701 .shrink_to_lo();
3702
3703 let (value, appl) = if is_integral && !end_is_negative {
3704 ("0", Applicability::MachineApplicable)
3705 } else {
3706 ("/* start */", Applicability::HasPlaceholders)
3707 };
3708
3709 err.span_suggestion_verbose(
3710 insert_span,
3711 ::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"),
3712 value,
3713 appl,
3714 );
3715 }
3716
3717 fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
3719 match ty.kind() {
3720 ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
3721 _ => self.ty_to_string(ty),
3722 }
3723 }
3724
3725 fn suggest_await_before_method(
3726 &self,
3727 err: &mut Diag<'_>,
3728 item_name: Ident,
3729 ty: Ty<'tcx>,
3730 call: &hir::Expr<'_>,
3731 span: Span,
3732 return_type: Option<Ty<'tcx>>,
3733 ) {
3734 let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else { return };
3735 let output_ty = self.resolve_vars_if_possible(output_ty);
3736 let method_exists =
3737 self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
3738 {
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:3738",
"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(3738u32),
::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);
3739 if method_exists {
3740 err.span_suggestion_verbose(
3741 span.shrink_to_lo(),
3742 "consider `await`ing on the `Future` and calling the method on its `Output`",
3743 "await.",
3744 Applicability::MaybeIncorrect,
3745 );
3746 }
3747 }
3748
3749 fn set_label_for_method_error(
3750 &self,
3751 err: &mut Diag<'_>,
3752 source: SelfSource<'tcx>,
3753 rcvr_ty: Ty<'tcx>,
3754 item_ident: Ident,
3755 expr_id: hir::HirId,
3756 span: Span,
3757 sugg_span: Span,
3758 within_macro_span: Option<Span>,
3759 args: Option<&'tcx [hir::Expr<'tcx>]>,
3760 ) {
3761 let tcx = self.tcx;
3762 if tcx.sess.source_map().is_multiline(sugg_span) {
3763 err.span_label(sugg_span.with_hi(span.lo()), "");
3764 }
3765 if let Some(within_macro_span) = within_macro_span {
3766 err.span_label(within_macro_span, "due to this macro variable");
3767 }
3768
3769 if #[allow(non_exhaustive_omitted_patterns)] match source {
SelfSource::QPath(_) => true,
_ => false,
}matches!(source, SelfSource::QPath(_)) && args.is_some() {
3770 self.find_builder_fn(err, rcvr_ty, expr_id);
3771 }
3772
3773 if tcx.ty_is_opaque_future(rcvr_ty) && item_ident.name == sym::poll {
3774 let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
3775 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!(
3776 "method `poll` found on `Pin<&mut {ty_str}>`, \
3777 see documentation for `std::pin::Pin`"
3778 ));
3779 err.help(
3780 "self type must be pinned to call `Future::poll`, \
3781 see https://rust-lang.github.io/async-book/part-reference/pinning.html",
3782 );
3783 }
3784
3785 if let Some(span) =
3786 tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
3787 {
3788 err.span_suggestion(
3789 span.shrink_to_lo(),
3790 "you are looking for the module in `std`, not the primitive type",
3791 "std::",
3792 Applicability::MachineApplicable,
3793 );
3794 }
3795 }
3796
3797 fn suggest_on_pointer_type(
3798 &self,
3799 err: &mut Diag<'_>,
3800 source: SelfSource<'tcx>,
3801 rcvr_ty: Ty<'tcx>,
3802 item_ident: Ident,
3803 ) {
3804 let tcx = self.tcx;
3805 if let SelfSource::MethodCall(rcvr_expr) = source
3807 && let ty::RawPtr(ty, ptr_mutbl) = *rcvr_ty.kind()
3808 && let Ok(pick) = self.lookup_probe_for_diagnostic(
3809 item_ident,
3810 Ty::new_ref(tcx, ty::Region::new_error_misc(tcx), ty, ptr_mutbl),
3811 self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)),
3812 ProbeScope::TraitsInScope,
3813 None,
3814 )
3815 && let ty::Ref(_, _, sugg_mutbl) = *pick.self_ty.kind()
3816 && (sugg_mutbl.is_not() || ptr_mutbl.is_mut())
3817 {
3818 let (method, method_anchor) = match sugg_mutbl {
3819 Mutability::Not => {
3820 let method_anchor = match ptr_mutbl {
3821 Mutability::Not => "as_ref",
3822 Mutability::Mut => "as_ref-1",
3823 };
3824 ("as_ref", method_anchor)
3825 }
3826 Mutability::Mut => ("as_mut", "as_mut"),
3827 };
3828 err.span_note(
3829 tcx.def_span(pick.item.def_id),
3830 ::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),
3831 );
3832 let mut_str = ptr_mutbl.ptr_str();
3833 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!(
3834 "you might want to use the unsafe method `<*{mut_str} T>::{method}` to get \
3835 an optional reference to the value behind the pointer"
3836 ));
3837 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!(
3838 "read the documentation for `<*{mut_str} T>::{method}` and ensure you satisfy its \
3839 safety preconditions before calling it to avoid undefined behavior: \
3840 https://doc.rust-lang.org/std/primitive.pointer.html#method.{method_anchor}"
3841 ));
3842 }
3843 }
3844
3845 fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
3846 where
3847 F: FnOnce(Vec<String>, Vec<String>, Span),
3848 {
3849 let parent_map = self.tcx.visible_parent_map(());
3850
3851 let scope = self.tcx.parent_module_from_def_id(self.body_id);
3852 let (accessible_candidates, inaccessible_candidates): (Vec<_>, Vec<_>) =
3853 candidates.into_iter().partition(|id| {
3854 let vis = self.tcx.visibility(*id);
3855 vis.is_accessible_from(scope, self.tcx)
3856 });
3857
3858 let sugg = |candidates: Vec<_>, visible| {
3859 let (candidates, globs): (Vec<_>, Vec<_>) =
3862 candidates.into_iter().partition(|trait_did| {
3863 if let Some(parent_did) = parent_map.get(trait_did) {
3864 if *parent_did != self.tcx.parent(*trait_did)
3866 && self
3867 .tcx
3868 .module_children(*parent_did)
3869 .iter()
3870 .filter(|child| child.res.opt_def_id() == Some(*trait_did))
3871 .all(|child| child.ident.name == kw::Underscore)
3872 {
3873 return false;
3874 }
3875 }
3876
3877 true
3878 });
3879
3880 let prefix = if visible { "use " } else { "" };
3881 let postfix = if visible { ";" } else { "" };
3882 let path_strings = candidates.iter().map(|trait_did| {
3883 ::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!(
3884 "{prefix}{}{postfix}\n",
3885 with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3886 self.tcx.def_path_str(*trait_did)
3887 )),
3888 )
3889 });
3890
3891 let glob_path_strings = globs.iter().map(|trait_did| {
3892 let parent_did = parent_map.get(trait_did).unwrap();
3893 ::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!(
3894 "{prefix}{}::*{postfix} // trait {}\n",
3895 with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3896 self.tcx.def_path_str(*parent_did)
3897 )),
3898 self.tcx.item_name(*trait_did),
3899 )
3900 });
3901 let mut sugg: Vec<_> = path_strings.chain(glob_path_strings).collect();
3902 sugg.sort();
3903 sugg
3904 };
3905
3906 let accessible_sugg = sugg(accessible_candidates, true);
3907 let inaccessible_sugg = sugg(inaccessible_candidates, false);
3908
3909 let (module, _, _) = self.tcx.hir_get_module(scope);
3910 let span = module.spans.inject_use_span;
3911 handle_candidates(accessible_sugg, inaccessible_sugg, span);
3912 }
3913
3914 fn suggest_valid_traits(
3915 &self,
3916 err: &mut Diag<'_>,
3917 item_name: Ident,
3918 mut valid_out_of_scope_traits: Vec<DefId>,
3919 explain: bool,
3920 ) -> bool {
3921 valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
3922 if !valid_out_of_scope_traits.is_empty() {
3923 let mut candidates = valid_out_of_scope_traits;
3924 candidates.sort_by_key(|&id| self.tcx.def_path_str(id));
3925 candidates.dedup();
3926
3927 let edition_fix = candidates
3929 .iter()
3930 .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
3931 .copied();
3932
3933 if explain {
3934 err.help("items from traits can only be used if the trait is in scope");
3935 }
3936
3937 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!(
3938 "{this_trait_is} implemented but not in scope",
3939 this_trait_is = if candidates.len() == 1 {
3940 format!(
3941 "trait `{}` which provides `{item_name}` is",
3942 self.tcx.item_name(candidates[0]),
3943 )
3944 } else {
3945 format!("the following traits which provide `{item_name}` are")
3946 }
3947 );
3948
3949 self.suggest_use_candidates(candidates, |accessible_sugg, inaccessible_sugg, span| {
3950 let suggest_for_access = |err: &mut Diag<'_>, mut msg: String, suggs: Vec<_>| {
3951 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!(
3952 "; perhaps you want to import {one_of}",
3953 one_of = if suggs.len() == 1 { "it" } else { "one of them" },
3954 );
3955 err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3956 };
3957 let suggest_for_privacy = |err: &mut Diag<'_>, suggs: Vec<String>| {
3958 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!(
3959 "{this_trait_is} implemented but not reachable",
3960 this_trait_is = if let [sugg] = suggs.as_slice() {
3961 format!("trait `{}` which provides `{item_name}` is", sugg.trim())
3962 } else {
3963 format!("the following traits which provide `{item_name}` are")
3964 }
3965 );
3966 if suggs.len() == 1 {
3967 err.help(msg);
3968 } else {
3969 err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3970 }
3971 };
3972 if accessible_sugg.is_empty() {
3973 suggest_for_privacy(err, inaccessible_sugg);
3975 } else if inaccessible_sugg.is_empty() {
3976 suggest_for_access(err, msg, accessible_sugg);
3977 } else {
3978 suggest_for_access(err, msg, accessible_sugg);
3979 suggest_for_privacy(err, inaccessible_sugg);
3980 }
3981 });
3982
3983 if let Some(did) = edition_fix {
3984 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!(
3985 "'{}' is included in the prelude starting in Edition 2021",
3986 with_crate_prefix!(self.tcx.def_path_str(did))
3987 ));
3988 }
3989
3990 true
3991 } else {
3992 false
3993 }
3994 }
3995
3996 fn suggest_traits_to_import(
3997 &self,
3998 err: &mut Diag<'_>,
3999 span: Span,
4000 rcvr_ty: Ty<'tcx>,
4001 item_name: Ident,
4002 inputs_len: Option<usize>,
4003 source: SelfSource<'tcx>,
4004 valid_out_of_scope_traits: Vec<DefId>,
4005 static_candidates: &[CandidateSource],
4006 unsatisfied_bounds: bool,
4007 return_type: Option<Ty<'tcx>>,
4008 trait_missing_method: bool,
4009 ) {
4010 let mut alt_rcvr_sugg = false;
4011 let mut trait_in_other_version_found = false;
4012 if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
4013 {
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:4013",
"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(4013u32),
::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!(
4014 "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
4015 span, item_name, rcvr_ty, rcvr
4016 );
4017 let skippable = [
4018 self.tcx.lang_items().clone_trait(),
4019 self.tcx.lang_items().deref_trait(),
4020 self.tcx.lang_items().deref_mut_trait(),
4021 self.tcx.lang_items().drop_trait(),
4022 self.tcx.get_diagnostic_item(sym::AsRef),
4023 ];
4024 for (rcvr_ty, post, pin_call) in &[
4028 (rcvr_ty, "", None),
4029 (
4030 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
4031 "&mut ",
4032 Some("as_mut"),
4033 ),
4034 (
4035 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
4036 "&",
4037 Some("as_ref"),
4038 ),
4039 ] {
4040 match self.lookup_probe_for_diagnostic(
4041 item_name,
4042 *rcvr_ty,
4043 rcvr,
4044 ProbeScope::AllTraits,
4045 return_type,
4046 ) {
4047 Ok(pick) => {
4048 let did = Some(pick.item.container_id(self.tcx));
4053 if skippable.contains(&did) {
4054 continue;
4055 }
4056 trait_in_other_version_found = self
4057 .detect_and_explain_multiple_crate_versions_of_trait_item(
4058 err,
4059 pick.item.def_id,
4060 rcvr.hir_id,
4061 Some(*rcvr_ty),
4062 );
4063 if pick.autoderefs == 0 && !trait_in_other_version_found {
4064 err.span_label(
4065 pick.item.ident(self.tcx).span,
4066 ::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"),
4067 );
4068 }
4069 break;
4070 }
4071 Err(MethodError::Ambiguity(_)) => {
4072 break;
4077 }
4078 Err(_) => (),
4079 }
4080
4081 let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else {
4082 return;
4083 };
4084 let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
4085 let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
4086 self.tcx,
4087 self.misc(rcvr.span),
4088 self.param_env,
4089 pred,
4090 ));
4091 for (rcvr_ty, pre) in &[
4092 (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::OwnedBox), "Box::new"),
4093 (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin), "Pin::new"),
4094 (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Arc), "Arc::new"),
4095 (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Rc), "Rc::new"),
4096 ] {
4097 if let Some(new_rcvr_t) = *rcvr_ty
4098 && let Ok(pick) = self.lookup_probe_for_diagnostic(
4099 item_name,
4100 new_rcvr_t,
4101 rcvr,
4102 ProbeScope::AllTraits,
4103 return_type,
4104 )
4105 {
4106 {
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:4106",
"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(4106u32),
::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);
4107 let did = pick.item.trait_container(self.tcx);
4108 let skip = skippable.contains(&did)
4114 || (("Pin::new" == *pre)
4115 && ((sym::as_ref == item_name.name) || !unpin))
4116 || inputs_len.is_some_and(|inputs_len| {
4117 pick.item.is_fn()
4118 && self
4119 .tcx
4120 .fn_sig(pick.item.def_id)
4121 .skip_binder()
4122 .skip_binder()
4123 .inputs()
4124 .len()
4125 != inputs_len
4126 });
4127 if pick.autoderefs == 0 && !skip {
4131 err.span_label(
4132 pick.item.ident(self.tcx).span,
4133 ::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"),
4134 );
4135 err.multipart_suggestion(
4136 "consider wrapping the receiver expression with the \
4137 appropriate type",
4138 ::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![
4139 (rcvr.span.shrink_to_lo(), format!("{pre}({post}")),
4140 (rcvr.span.shrink_to_hi(), ")".to_string()),
4141 ],
4142 Applicability::MaybeIncorrect,
4143 );
4144 alt_rcvr_sugg = true;
4146 }
4147 }
4148 }
4149 if let Some(new_rcvr_t) = Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin)
4152 && !alt_rcvr_sugg
4154 && !unpin
4156 && let Some(pin_call) = pin_call
4158 && let Ok(pick) = self.lookup_probe_for_diagnostic(
4160 item_name,
4161 new_rcvr_t,
4162 rcvr,
4163 ProbeScope::AllTraits,
4164 return_type,
4165 )
4166 && !skippable.contains(&Some(pick.item.container_id(self.tcx)))
4169 && pick.item.impl_container(self.tcx).is_none_or(|did| {
4171 match self.tcx.type_of(did).skip_binder().kind() {
4172 ty::Adt(def, _) => Some(def.did()) != self.tcx.lang_items().pin_type(),
4173 _ => true,
4174 }
4175 })
4176 && pick.autoderefs == 0
4178 && 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)
4181 {
4182 let indent = self
4183 .tcx
4184 .sess
4185 .source_map()
4186 .indentation_before(rcvr.span)
4187 .unwrap_or_else(|| " ".to_string());
4188 let mut expr = rcvr;
4189 while let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4190 && let hir::ExprKind::MethodCall(hir::PathSegment { .. }, ..) =
4191 call_expr.kind
4192 {
4193 expr = call_expr;
4194 }
4195 match self.tcx.parent_hir_node(expr.hir_id) {
4196 Node::LetStmt(stmt)
4197 if let Some(init) = stmt.init
4198 && let Ok(code) =
4199 self.tcx.sess.source_map().span_to_snippet(rcvr.span) =>
4200 {
4201 err.multipart_suggestion(
4204 "consider pinning the expression",
4205 ::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![
4206 (
4207 stmt.span.shrink_to_lo(),
4208 format!(
4209 "let mut pinned = std::pin::pin!({code});\n{indent}"
4210 ),
4211 ),
4212 (
4213 init.span.until(rcvr.span.shrink_to_hi()),
4214 format!("pinned.{pin_call}()"),
4215 ),
4216 ],
4217 Applicability::MaybeIncorrect,
4218 );
4219 }
4220 Node::Block(_) | Node::Stmt(_) => {
4221 err.multipart_suggestion(
4224 "consider pinning the expression",
4225 ::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![
4226 (
4227 rcvr.span.shrink_to_lo(),
4228 "let mut pinned = std::pin::pin!(".to_string(),
4229 ),
4230 (
4231 rcvr.span.shrink_to_hi(),
4232 format!(");\n{indent}pinned.{pin_call}()"),
4233 ),
4234 ],
4235 Applicability::MaybeIncorrect,
4236 );
4237 }
4238 _ => {
4239 err.span_help(
4242 rcvr.span,
4243 "consider pinning the expression with `std::pin::pin!()` and \
4244 assigning that to a new binding",
4245 );
4246 }
4247 }
4248 alt_rcvr_sugg = true;
4250 }
4251 }
4252 }
4253
4254 if let SelfSource::QPath(ty) = source
4255 && !valid_out_of_scope_traits.is_empty()
4256 && let hir::TyKind::Path(path) = ty.kind
4257 && let hir::QPath::Resolved(..) = path
4258 && let Some(assoc) = self
4259 .tcx
4260 .associated_items(valid_out_of_scope_traits[0])
4261 .filter_by_name_unhygienic(item_name.name)
4262 .next()
4263 {
4264 let rcvr_ty = self.node_ty_opt(ty.hir_id);
4269 trait_in_other_version_found = self
4270 .detect_and_explain_multiple_crate_versions_of_trait_item(
4271 err,
4272 assoc.def_id,
4273 ty.hir_id,
4274 rcvr_ty,
4275 );
4276 }
4277 if !trait_in_other_version_found
4278 && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
4279 {
4280 return;
4281 }
4282
4283 let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
4284
4285 let mut arbitrary_rcvr = ::alloc::vec::Vec::new()vec![];
4286 let mut candidates = all_traits(self.tcx)
4290 .into_iter()
4291 .filter(|info| match self.tcx.lookup_stability(info.def_id) {
4294 Some(attr) => attr.level.is_stable(),
4295 None => true,
4296 })
4297 .filter(|info| {
4298 static_candidates.iter().all(|sc| match *sc {
4301 CandidateSource::Trait(def_id) => def_id != info.def_id,
4302 CandidateSource::Impl(def_id) => {
4303 self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
4304 }
4305 })
4306 })
4307 .filter(|info| {
4308 (type_is_local || info.def_id.is_local())
4315 && !self.tcx.trait_is_auto(info.def_id)
4316 && self
4317 .associated_value(info.def_id, item_name)
4318 .filter(|item| {
4319 if item.is_fn() {
4320 let id = item
4321 .def_id
4322 .as_local()
4323 .map(|def_id| self.tcx.hir_node_by_def_id(def_id));
4324 if let Some(hir::Node::TraitItem(hir::TraitItem {
4325 kind: hir::TraitItemKind::Fn(fn_sig, method),
4326 ..
4327 })) = id
4328 {
4329 let self_first_arg = match method {
4330 hir::TraitFn::Required([ident, ..]) => {
4331 #[allow(non_exhaustive_omitted_patterns)] match ident {
Some(Ident { name: kw::SelfLower, .. }) => true,
_ => false,
}matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
4332 }
4333 hir::TraitFn::Provided(body_id) => {
4334 self.tcx.hir_body(*body_id).params.first().is_some_and(
4335 |param| {
4336 #[allow(non_exhaustive_omitted_patterns)] match param.pat.kind {
hir::PatKind::Binding(_, _, ident, _) if ident.name == kw::SelfLower =>
true,
_ => false,
}matches!(
4337 param.pat.kind,
4338 hir::PatKind::Binding(_, _, ident, _)
4339 if ident.name == kw::SelfLower
4340 )
4341 },
4342 )
4343 }
4344 _ => false,
4345 };
4346
4347 if !fn_sig.decl.implicit_self.has_implicit_self()
4348 && self_first_arg
4349 {
4350 if let Some(ty) = fn_sig.decl.inputs.get(0) {
4351 arbitrary_rcvr.push(ty.span);
4352 }
4353 return false;
4354 }
4355 }
4356 }
4357 item.visibility(self.tcx).is_public() || info.def_id.is_local()
4359 })
4360 .is_some()
4361 })
4362 .collect::<Vec<_>>();
4363 for span in &arbitrary_rcvr {
4364 err.span_label(
4365 *span,
4366 "the method might not be found because of this arbitrary self type",
4367 );
4368 }
4369 if alt_rcvr_sugg {
4370 return;
4371 }
4372
4373 if !candidates.is_empty() {
4374 candidates
4376 .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
4377 candidates.dedup();
4378
4379 let param_type = match *rcvr_ty.kind() {
4380 ty::Param(param) => Some(param),
4381 ty::Ref(_, ty, _) => match *ty.kind() {
4382 ty::Param(param) => Some(param),
4383 _ => None,
4384 },
4385 _ => None,
4386 };
4387 if !trait_missing_method {
4388 err.help(if param_type.is_some() {
4389 "items from traits can only be used if the type parameter is bounded by the trait"
4390 } else {
4391 "items from traits can only be used if the trait is implemented and in scope"
4392 });
4393 }
4394
4395 let candidates_len = candidates.len();
4396 let message = |action| {
4397 ::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!(
4398 "the following {traits_define} an item `{name}`, perhaps you need to {action} \
4399 {one_of_them}:",
4400 traits_define =
4401 if candidates_len == 1 { "trait defines" } else { "traits define" },
4402 action = action,
4403 one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
4404 name = item_name,
4405 )
4406 };
4407 if let Some(param) = param_type {
4409 let generics = self.tcx.generics_of(self.body_id.to_def_id());
4410 let type_param = generics.type_param(param, self.tcx);
4411 let tcx = self.tcx;
4412 if let Some(def_id) = type_param.def_id.as_local() {
4413 let id = tcx.local_def_id_to_hir_id(def_id);
4414 match tcx.hir_node(id) {
4418 Node::GenericParam(param) => {
4419 enum Introducer {
4420 Plus,
4421 Colon,
4422 Nothing,
4423 }
4424 let hir_generics = tcx.hir_get_generics(id.owner.def_id).unwrap();
4425 let trait_def_ids: DefIdSet = hir_generics
4426 .bounds_for_param(def_id)
4427 .flat_map(|bp| bp.bounds.iter())
4428 .filter_map(|bound| bound.trait_ref()?.trait_def_id())
4429 .collect();
4430 if candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
4431 return;
4432 }
4433 let msg = message(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("restrict type parameter `{0}` with",
param.name.ident()))
})format!(
4434 "restrict type parameter `{}` with",
4435 param.name.ident(),
4436 ));
4437 let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
4438 let mut applicability = Applicability::MaybeIncorrect;
4439 let candidate_strs: Vec<_> = candidates
4442 .iter()
4443 .map(|cand| {
4444 let cand_path = tcx.def_path_str(cand.def_id);
4445 let cand_params = &tcx.generics_of(cand.def_id).own_params;
4446 let cand_args: String = cand_params
4447 .iter()
4448 .skip(1)
4449 .filter_map(|param| match param.kind {
4450 ty::GenericParamDefKind::Type {
4451 has_default: true,
4452 ..
4453 }
4454 | ty::GenericParamDefKind::Const {
4455 has_default: true,
4456 ..
4457 } => None,
4458 _ => Some(param.name.as_str()),
4459 })
4460 .intersperse(", ")
4461 .collect();
4462 if cand_args.is_empty() {
4463 cand_path
4464 } else {
4465 applicability = Applicability::HasPlaceholders;
4466 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}</* {1} */>", cand_path,
cand_args))
})format!("{cand_path}</* {cand_args} */>")
4467 }
4468 })
4469 .collect();
4470
4471 if rcvr_ty.is_ref()
4472 && param.is_impl_trait()
4473 && let Some((bounds_span, _)) = bounds_span
4474 {
4475 err.multipart_suggestions(
4476 msg,
4477 candidate_strs.iter().map(|cand| {
4478 ::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![
4479 (param.span.shrink_to_lo(), "(".to_string()),
4480 (bounds_span, format!(" + {cand})")),
4481 ]
4482 }),
4483 applicability,
4484 );
4485 return;
4486 }
4487
4488 let (sp, introducer, open_paren_sp) =
4489 if let Some((span, open_paren_sp)) = bounds_span {
4490 (span, Introducer::Plus, open_paren_sp)
4491 } else if let Some(colon_span) = param.colon_span {
4492 (colon_span.shrink_to_hi(), Introducer::Nothing, None)
4493 } else if param.is_impl_trait() {
4494 (param.span.shrink_to_hi(), Introducer::Plus, None)
4495 } else {
4496 (param.span.shrink_to_hi(), Introducer::Colon, None)
4497 };
4498
4499 let all_suggs = candidate_strs.iter().map(|cand| {
4500 let suggestion = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}",
match introducer {
Introducer::Plus => " +",
Introducer::Colon => ":",
Introducer::Nothing => "",
}, cand))
})format!(
4501 "{} {cand}",
4502 match introducer {
4503 Introducer::Plus => " +",
4504 Introducer::Colon => ":",
4505 Introducer::Nothing => "",
4506 },
4507 );
4508
4509 let mut suggs = ::alloc::vec::Vec::new()vec![];
4510
4511 if let Some(open_paren_sp) = open_paren_sp {
4512 suggs.push((open_paren_sp, "(".to_string()));
4513 suggs.push((sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("){0}", suggestion))
})format!("){suggestion}")));
4514 } else {
4515 suggs.push((sp, suggestion));
4516 }
4517
4518 suggs
4519 });
4520
4521 err.multipart_suggestions(msg, all_suggs, applicability);
4522
4523 return;
4524 }
4525 Node::Item(hir::Item {
4526 kind: hir::ItemKind::Trait(_, _, _, ident, _, bounds, _),
4527 ..
4528 }) => {
4529 let (sp, sep, article) = if bounds.is_empty() {
4530 (ident.span.shrink_to_hi(), ":", "a")
4531 } else {
4532 (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
4533 };
4534 err.span_suggestions(
4535 sp,
4536 message(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("add {0} supertrait for", article))
})format!("add {article} supertrait for")),
4537 candidates
4538 .iter()
4539 .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),)),
4540 Applicability::MaybeIncorrect,
4541 );
4542 return;
4543 }
4544 _ => {}
4545 }
4546 }
4547 }
4548
4549 let (potential_candidates, explicitly_negative) = if param_type.is_some() {
4550 (candidates, Vec::new())
4553 } else if let Some(simp_rcvr_ty) =
4554 simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
4555 {
4556 let mut potential_candidates = Vec::new();
4557 let mut explicitly_negative = Vec::new();
4558 for candidate in candidates {
4559 if self
4561 .tcx
4562 .all_impls(candidate.def_id)
4563 .map(|imp_did| self.tcx.impl_trait_header(imp_did))
4564 .filter(|header| header.polarity != ty::ImplPolarity::Positive)
4565 .any(|header| {
4566 let imp = header.trait_ref.instantiate_identity();
4567 let imp_simp =
4568 simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
4569 imp_simp.is_some_and(|s| s == simp_rcvr_ty)
4570 })
4571 {
4572 explicitly_negative.push(candidate);
4573 } else {
4574 potential_candidates.push(candidate);
4575 }
4576 }
4577 (potential_candidates, explicitly_negative)
4578 } else {
4579 (candidates, Vec::new())
4581 };
4582
4583 let impls_trait = |def_id: DefId| {
4584 let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
4585 if param.index == 0 {
4586 rcvr_ty.into()
4587 } else {
4588 self.infcx.var_for_def(span, param)
4589 }
4590 });
4591 self.infcx
4592 .type_implements_trait(def_id, args, self.param_env)
4593 .must_apply_modulo_regions()
4594 && param_type.is_none()
4595 };
4596 match &potential_candidates[..] {
4597 [] => {}
4598 [trait_info] if trait_info.def_id.is_local() => {
4599 if impls_trait(trait_info.def_id) {
4600 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);
4601 } else {
4602 err.subdiagnostic(CandidateTraitNote {
4603 span: self.tcx.def_span(trait_info.def_id),
4604 trait_name: self.tcx.def_path_str(trait_info.def_id),
4605 item_name,
4606 action_or_ty: if trait_missing_method {
4607 "NONE".to_string()
4608 } else {
4609 param_type.map_or_else(
4610 || "implement".to_string(), |p| p.to_string(),
4612 )
4613 },
4614 });
4615 }
4616 }
4617 trait_infos => {
4618 let mut msg = message(param_type.map_or_else(
4619 || "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"),
4621 ));
4622 for (i, trait_info) in trait_infos.iter().enumerate() {
4623 if impls_trait(trait_info.def_id) {
4624 self.suggest_valid_traits(
4625 err,
4626 item_name,
4627 ::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],
4628 false,
4629 );
4630 }
4631 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!(
4632 "\ncandidate #{}: `{}`",
4633 i + 1,
4634 self.tcx.def_path_str(trait_info.def_id),
4635 ));
4636 }
4637 err.note(msg);
4638 }
4639 }
4640 match &explicitly_negative[..] {
4641 [] => {}
4642 [trait_info] => {
4643 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!(
4644 "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
4645 self.tcx.def_path_str(trait_info.def_id),
4646 item_name
4647 );
4648 err.note(msg);
4649 }
4650 trait_infos => {
4651 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!(
4652 "the following traits define an item `{item_name}`, but are explicitly unimplemented:"
4653 );
4654 for trait_info in trait_infos {
4655 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)));
4656 }
4657 err.note(msg);
4658 }
4659 }
4660 }
4661 }
4662
4663 fn detect_and_explain_multiple_crate_versions_of_trait_item(
4664 &self,
4665 err: &mut Diag<'_>,
4666 item_def_id: DefId,
4667 hir_id: hir::HirId,
4668 rcvr_ty: Option<Ty<'tcx>>,
4669 ) -> bool {
4670 let hir_id = self.tcx.parent_hir_id(hir_id);
4671 let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
4672 if traits.is_empty() {
4673 return false;
4674 }
4675 let trait_def_id = self.tcx.parent(item_def_id);
4676 if !self.tcx.is_trait(trait_def_id) {
4677 return false;
4678 }
4679 let hir::Node::Expr(rcvr) = self.tcx.hir_node(hir_id) else {
4680 return false;
4681 };
4682 let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, rcvr_ty.into_iter());
4683 let trait_pred = ty::Binder::dummy(ty::TraitPredicate {
4684 trait_ref,
4685 polarity: ty::PredicatePolarity::Positive,
4686 });
4687 let obligation = Obligation::new(self.tcx, self.misc(rcvr.span), self.param_env, trait_ref);
4688 self.err_ctxt().note_different_trait_with_same_name(err, &obligation, trait_pred)
4689 }
4690
4691 pub(crate) fn suggest_else_fn_with_closure(
4694 &self,
4695 err: &mut Diag<'_>,
4696 expr: &hir::Expr<'_>,
4697 found: Ty<'tcx>,
4698 expected: Ty<'tcx>,
4699 ) -> bool {
4700 let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(found) else {
4701 return false;
4702 };
4703
4704 if !self.may_coerce(output, expected) {
4705 return false;
4706 }
4707
4708 if let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4709 && let hir::ExprKind::MethodCall(
4710 hir::PathSegment { ident: method_name, .. },
4711 self_expr,
4712 args,
4713 ..,
4714 ) = call_expr.kind
4715 && let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr)
4716 {
4717 let new_name = Ident {
4718 name: Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_else", method_name.as_str()))
})format!("{}_else", method_name.as_str())),
4719 span: method_name.span,
4720 };
4721 let probe = self.lookup_probe_for_diagnostic(
4722 new_name,
4723 self_ty,
4724 self_expr,
4725 ProbeScope::TraitsInScope,
4726 Some(expected),
4727 );
4728
4729 if let Ok(pick) = probe
4731 && let fn_sig = self.tcx.fn_sig(pick.item.def_id)
4732 && let fn_args = fn_sig.skip_binder().skip_binder().inputs()
4733 && fn_args.len() == args.len() + 1
4734 {
4735 err.span_suggestion_verbose(
4736 method_name.span.shrink_to_hi(),
4737 ::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()),
4738 "_else",
4739 Applicability::MaybeIncorrect,
4740 );
4741 return true;
4742 }
4743 }
4744 false
4745 }
4746
4747 fn type_derefs_to_local(
4750 &self,
4751 span: Span,
4752 rcvr_ty: Ty<'tcx>,
4753 source: SelfSource<'tcx>,
4754 ) -> bool {
4755 fn is_local(ty: Ty<'_>) -> bool {
4756 match ty.kind() {
4757 ty::Adt(def, _) => def.did().is_local(),
4758 ty::Foreign(did) => did.is_local(),
4759 ty::Dynamic(tr, ..) => tr.principal().is_some_and(|d| d.def_id().is_local()),
4760 ty::Param(_) => true,
4761
4762 _ => false,
4767 }
4768 }
4769
4770 if let SelfSource::QPath(_) = source {
4773 return is_local(rcvr_ty);
4774 }
4775
4776 self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
4777 }
4778
4779 fn suggest_hashmap_on_unsatisfied_hashset_buildhasher(
4780 &self,
4781 err: &mut Diag<'_>,
4782 pred: &ty::TraitPredicate<'_>,
4783 adt: ty::AdtDef<'_>,
4784 ) -> bool {
4785 if self.tcx.is_diagnostic_item(sym::HashSet, adt.did())
4786 && self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id())
4787 {
4788 err.help("you might have intended to use a HashMap instead");
4789 true
4790 } else {
4791 false
4792 }
4793 }
4794}
4795
4796#[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)]
4797enum SelfSource<'a> {
4798 QPath(&'a hir::Ty<'a>),
4799 MethodCall(&'a hir::Expr<'a> ),
4800}
4801
4802#[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 {
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
}
}Eq)]
4803pub(crate) struct TraitInfo {
4804 pub def_id: DefId,
4805}
4806
4807pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4810 tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
4811}
4812
4813fn print_disambiguation_help<'tcx>(
4814 tcx: TyCtxt<'tcx>,
4815 err: &mut Diag<'_>,
4816 source: SelfSource<'tcx>,
4817 args: Option<&'tcx [hir::Expr<'tcx>]>,
4818 trait_ref: ty::TraitRef<'tcx>,
4819 candidate_idx: Option<usize>,
4820 span: Span,
4821 item: ty::AssocItem,
4822) -> Option<String> {
4823 let trait_impl_type = trait_ref.self_ty().peel_refs();
4824 let trait_ref = if item.is_method() {
4825 trait_ref.print_only_trait_name().to_string()
4826 } else {
4827 ::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())
4828 };
4829 Some(
4830 if item.is_fn()
4831 && let SelfSource::MethodCall(receiver) = source
4832 && let Some(args) = args
4833 {
4834 let def_kind_descr = tcx.def_kind_descr(item.as_def_kind(), item.def_id);
4835 let item_name = item.ident(tcx);
4836 let first_input =
4837 tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
4838 let (first_arg_type, rcvr_ref) = (
4839 first_input.map(|first| first.peel_refs()),
4840 first_input
4841 .and_then(|ty| ty.ref_mutability())
4842 .map_or("", |mutbl| mutbl.ref_prefix_str()),
4843 );
4844
4845 let args = if let Some(first_arg_type) = first_arg_type
4847 && (first_arg_type == tcx.types.self_param
4848 || first_arg_type == trait_impl_type
4849 || item.is_method())
4850 {
4851 Some(receiver)
4852 } else {
4853 None
4854 }
4855 .into_iter()
4856 .chain(args)
4857 .map(|arg| {
4858 tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
4859 })
4860 .collect::<Vec<_>>()
4861 .join(", ");
4862
4863 let args = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}{1})", rcvr_ref, args))
})format!("({}{})", rcvr_ref, args);
4864 err.span_suggestion_verbose(
4865 span,
4866 ::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!(
4867 "disambiguate the {def_kind_descr} for {}",
4868 if let Some(candidate) = candidate_idx {
4869 format!("candidate #{candidate}")
4870 } else {
4871 "the candidate".to_string()
4872 },
4873 ),
4874 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}{2}", trait_ref, item_name,
args))
})format!("{trait_ref}::{item_name}{args}"),
4875 Applicability::HasPlaceholders,
4876 );
4877 return None;
4878 } else {
4879 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::", trait_ref))
})format!("{trait_ref}::")
4880 },
4881 )
4882}