1use std::borrow::Cow;
4use std::path::PathBuf;
5use std::{debug_assert_matches, iter};
6
7use itertools::{EitherOrBoth, Itertools};
8use rustc_abi::ExternAbi;
9use rustc_data_structures::fx::FxHashSet;
10use rustc_data_structures::stack::ensure_sufficient_stack;
11use rustc_errors::codes::*;
12use rustc_errors::{
13 Applicability, Diag, EmissionGuarantee, MultiSpan, Style, SuggestionStyle, pluralize,
14 struct_span_code_err,
15};
16use rustc_hir::def::{CtorOf, DefKind, Res};
17use rustc_hir::def_id::DefId;
18use rustc_hir::intravisit::{Visitor, VisitorExt};
19use rustc_hir::lang_items::LangItem;
20use rustc_hir::{
21 self as hir, AmbigArg, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, HirId, Node,
22 expr_needs_parens,
23};
24use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk};
25use rustc_infer::traits::ImplSource;
26use rustc_middle::middle::privacy::Level;
27use rustc_middle::traits::IsConstable;
28use rustc_middle::ty::adjustment::{Adjust, DerefAdjustKind};
29use rustc_middle::ty::error::TypeError;
30use rustc_middle::ty::print::{
31 PrintPolyTraitPredicateExt as _, PrintPolyTraitRefExt, PrintTraitPredicateExt as _,
32 PrintTraitRefExt as _, with_forced_trimmed_paths, with_no_trimmed_paths,
33 with_types_for_suggestion,
34};
35use rustc_middle::ty::{
36 self, AdtKind, GenericArgs, InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder,
37 TypeSuperFoldable, TypeSuperVisitable, TypeVisitableExt, TypeVisitor, TypeckResults,
38 Unnormalized, Upcast, suggest_arbitrary_trait_bound, suggest_constraining_type_param,
39};
40use rustc_middle::{bug, span_bug};
41use rustc_span::def_id::LocalDefId;
42use rustc_span::{
43 BytePos, DUMMY_SP, DesugaringKind, ExpnKind, Ident, MacroKind, Span, Symbol, kw, sym,
44};
45use tracing::{debug, instrument};
46
47use super::{
48 DefIdOrName, FindExprBySpan, ImplCandidate, Obligation, ObligationCause, ObligationCauseCode,
49 PredicateObligation,
50};
51use crate::error_reporting::TypeErrCtxt;
52use crate::errors;
53use crate::infer::InferCtxtExt as _;
54use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
55use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt, SelectionContext};
56
57#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CoroutineInteriorOrUpvar {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
CoroutineInteriorOrUpvar::Interior(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Interior", __self_0, &__self_1),
CoroutineInteriorOrUpvar::Upvar(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Upvar",
&__self_0),
}
}
}Debug)]
58pub enum CoroutineInteriorOrUpvar {
59 Interior(Span, Option<(Span, Option<Span>)>),
61 Upvar(Span),
63}
64
65#[derive(#[automatically_derived]
impl<'a, 'tcx> ::core::fmt::Debug for CoroutineData<'a, 'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "CoroutineData",
&&self.0)
}
}Debug)]
68struct CoroutineData<'a, 'tcx>(&'a TypeckResults<'tcx>);
69
70impl<'a, 'tcx> CoroutineData<'a, 'tcx> {
71 fn try_get_upvar_span<F>(
75 &self,
76 infer_context: &InferCtxt<'tcx>,
77 coroutine_did: DefId,
78 ty_matches: F,
79 ) -> Option<CoroutineInteriorOrUpvar>
80 where
81 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
82 {
83 infer_context.tcx.upvars_mentioned(coroutine_did).and_then(|upvars| {
84 upvars.iter().find_map(|(upvar_id, upvar)| {
85 let upvar_ty = self.0.node_type(*upvar_id);
86 let upvar_ty = infer_context.resolve_vars_if_possible(upvar_ty);
87 ty_matches(ty::Binder::dummy(upvar_ty))
88 .then(|| CoroutineInteriorOrUpvar::Upvar(upvar.span))
89 })
90 })
91 }
92
93 fn get_from_await_ty<F>(
97 &self,
98 visitor: AwaitsVisitor,
99 tcx: TyCtxt<'tcx>,
100 ty_matches: F,
101 ) -> Option<Span>
102 where
103 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
104 {
105 visitor
106 .awaits
107 .into_iter()
108 .map(|id| tcx.hir_expect_expr(id))
109 .find(|await_expr| ty_matches(ty::Binder::dummy(self.0.expr_ty_adjusted(await_expr))))
110 .map(|expr| expr.span)
111 }
112}
113
114fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -> (Span, String) {
115 (
116 generics.tail_span_for_predicate_suggestion(),
117 {
let _guard =
::rustc_middle::ty::print::pretty::RtnModeHelper::with(RtnMode::ForSuggestion);
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}",
generics.add_where_or_trailing_comma(), pred))
})
}with_types_for_suggestion!(format!("{} {}", generics.add_where_or_trailing_comma(), pred)),
118 )
119}
120
121pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
125 tcx: TyCtxt<'tcx>,
126 item_id: LocalDefId,
127 hir_generics: &hir::Generics<'tcx>,
128 msg: &str,
129 err: &mut Diag<'_, G>,
130 fn_sig: Option<&hir::FnSig<'_>>,
131 projection: Option<ty::AliasTy<'_>>,
132 trait_pred: ty::PolyTraitPredicate<'tcx>,
133 super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
139) {
140 if hir_generics.where_clause_span.from_expansion()
141 || hir_generics.where_clause_span.desugaring_kind().is_some()
142 || projection.is_some_and(|projection| {
143 (tcx.is_impl_trait_in_trait(projection.kind.def_id())
144 && !tcx.features().return_type_notation())
145 || tcx
146 .lookup_stability(projection.kind.def_id())
147 .is_some_and(|stab| stab.is_unstable())
148 })
149 {
150 return;
151 }
152 let generics = tcx.generics_of(item_id);
153 if let Some((param, bound_str, fn_sig)) =
155 fn_sig.zip(projection).and_then(|(sig, p)| match *p.self_ty().kind() {
156 ty::Param(param) => {
158 let param_def = generics.type_param(param, tcx);
159 if param_def.kind.is_synthetic() {
160 let bound_str =
161 param_def.name.as_str().strip_prefix("impl ")?.trim_start().to_string();
162 return Some((param_def, bound_str, sig));
163 }
164 None
165 }
166 _ => None,
167 })
168 {
169 let type_param_name = hir_generics.params.next_type_param_name(Some(&bound_str));
170 let trait_pred = trait_pred.fold_with(&mut ReplaceImplTraitFolder {
171 tcx,
172 param,
173 replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name))
174 .to_ty(tcx),
175 });
176 if !trait_pred.is_suggestable(tcx, false) {
177 return;
178 }
179 let mut ty_spans = ::alloc::vec::Vec::new()vec![];
187 for input in fn_sig.decl.inputs {
188 ReplaceImplTraitVisitor { ty_spans: &mut ty_spans, param_did: param.def_id }
189 .visit_ty_unambig(input);
190 }
191 let type_param = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", type_param_name,
bound_str))
})format!("{type_param_name}: {bound_str}");
193
194 let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[if let Some(span) = hir_generics.span_for_param_suggestion() {
(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}", type_param))
}))
} else {
(hir_generics.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", type_param))
}))
},
predicate_constraint(hir_generics, trait_pred.upcast(tcx))]))vec![
195 if let Some(span) = hir_generics.span_for_param_suggestion() {
196 (span, format!(", {type_param}"))
197 } else {
198 (hir_generics.span, format!("<{type_param}>"))
199 },
200 predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
203 ];
204 sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
205
206 err.multipart_suggestion(
209 "introduce a type parameter with a trait bound instead of using `impl Trait`",
210 sugg,
211 Applicability::MaybeIncorrect,
212 );
213 } else {
214 if !trait_pred.is_suggestable(tcx, false) {
215 return;
216 }
217 let (sp, suggestion) = match (
219 hir_generics
220 .params
221 .iter()
222 .find(|p| !#[allow(non_exhaustive_omitted_patterns)] match p.kind {
hir::GenericParamKind::Type { synthetic: true, .. } => true,
_ => false,
}matches!(p.kind, hir::GenericParamKind::Type { synthetic: true, .. })),
223 super_traits,
224 ) {
225 (_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
226 (None, Some((ident, []))) => (
227 ident.span.shrink_to_hi(),
228 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}",
trait_pred.print_modifiers_and_trait_path()))
})format!(": {}", trait_pred.print_modifiers_and_trait_path()),
229 ),
230 (_, Some((_, [.., bounds]))) => (
231 bounds.span().shrink_to_hi(),
232 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" + {0}",
trait_pred.print_modifiers_and_trait_path()))
})format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
233 ),
234 (Some(_), Some((_, []))) => (
235 hir_generics.span.shrink_to_hi(),
236 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}",
trait_pred.print_modifiers_and_trait_path()))
})format!(": {}", trait_pred.print_modifiers_and_trait_path()),
237 ),
238 };
239
240 err.span_suggestion_verbose(
241 sp,
242 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider further restricting {0}",
msg))
})format!("consider further restricting {msg}"),
243 suggestion,
244 Applicability::MachineApplicable,
245 );
246 }
247}
248
249struct PeeledRef<'tcx> {
252 span: Span,
254 peeled_ty: Ty<'tcx>,
256}
257
258impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
259 pub fn note_field_shadowed_by_private_candidate_in_cause(
260 &self,
261 err: &mut Diag<'_>,
262 cause: &ObligationCause<'tcx>,
263 param_env: ty::ParamEnv<'tcx>,
264 ) {
265 let mut hir_ids = FxHashSet::default();
266 let mut next_code = Some(cause.code());
269 while let Some(cause_code) = next_code {
270 match cause_code {
271 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. } => {
272 hir_ids.insert(*lhs_hir_id);
273 hir_ids.insert(*rhs_hir_id);
274 }
275 ObligationCauseCode::FunctionArg { arg_hir_id, .. }
276 | ObligationCauseCode::ReturnValue(arg_hir_id)
277 | ObligationCauseCode::AwaitableExpr(arg_hir_id)
278 | ObligationCauseCode::BlockTailExpression(arg_hir_id, _)
279 | ObligationCauseCode::UnOp { hir_id: arg_hir_id } => {
280 hir_ids.insert(*arg_hir_id);
281 }
282 ObligationCauseCode::OpaqueReturnType(Some((_, hir_id))) => {
283 hir_ids.insert(*hir_id);
284 }
285 _ => {}
286 }
287 next_code = cause_code.parent();
288 }
289
290 if !cause.span.is_dummy()
291 && let Some(body) = self.tcx.hir_maybe_body_owned_by(cause.body_id)
292 {
293 let mut expr_finder = FindExprBySpan::new(cause.span, self.tcx);
294 expr_finder.visit_body(body);
295 if let Some(expr) = expr_finder.result {
296 hir_ids.insert(expr.hir_id);
297 }
298 }
299
300 #[allow(rustc::potential_query_instability)]
302 let mut hir_ids: Vec<_> = hir_ids.into_iter().collect();
303 let source_map = self.tcx.sess.source_map();
304 hir_ids.sort_by_cached_key(|hir_id| {
305 let span = self.tcx.hir_span(*hir_id);
306 let lo = source_map.lookup_byte_offset(span.lo());
307 let hi = source_map.lookup_byte_offset(span.hi());
308 (lo.sf.name.prefer_remapped_unconditionally().to_string(), lo.pos.0, hi.pos.0)
309 });
310
311 for hir_id in hir_ids {
312 self.note_field_shadowed_by_private_candidate(err, hir_id, param_env);
313 }
314 }
315
316 pub fn note_field_shadowed_by_private_candidate(
317 &self,
318 err: &mut Diag<'_>,
319 hir_id: hir::HirId,
320 param_env: ty::ParamEnv<'tcx>,
321 ) {
322 let Some(typeck_results) = &self.typeck_results else {
323 return;
324 };
325 let Node::Expr(expr) = self.tcx.hir_node(hir_id) else {
326 return;
327 };
328 let hir::ExprKind::Field(base_expr, field_ident) = expr.kind else {
329 return;
330 };
331
332 let Some(base_ty) = typeck_results.expr_ty_opt(base_expr) else {
333 return;
334 };
335 let base_ty = self.resolve_vars_if_possible(base_ty);
336 if base_ty.references_error() {
337 return;
338 }
339
340 let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(typeck_results.hir_owner.def_id);
341 let mut private_candidate: Option<(Ty<'tcx>, Ty<'tcx>, Span)> = None;
342
343 for (deref_base_ty, _) in (self.autoderef_steps)(base_ty) {
344 let ty::Adt(base_def, args) = deref_base_ty.kind() else {
345 continue;
346 };
347
348 if base_def.is_enum() {
349 continue;
350 }
351
352 let (adjusted_ident, def_scope) =
353 self.tcx.adjust_ident_and_get_scope(field_ident, base_def.did(), fn_body_hir_id);
354
355 let Some((_, field_def)) =
356 base_def.non_enum_variant().fields.iter_enumerated().find(|(_, field)| {
357 field.ident(self.tcx).normalize_to_macros_2_0() == adjusted_ident
358 })
359 else {
360 continue;
361 };
362 let field_span = self
363 .tcx
364 .def_ident_span(field_def.did)
365 .unwrap_or_else(|| self.tcx.def_span(field_def.did));
366
367 if field_def.vis.is_accessible_from(def_scope, self.tcx) {
368 let accessible_field_ty = field_def.ty(self.tcx, args);
369 if let Some((private_base_ty, private_field_ty, private_field_span)) =
370 private_candidate
371 && !self.can_eq(param_env, private_field_ty, accessible_field_ty)
372 {
373 let private_struct_span = match private_base_ty.kind() {
374 ty::Adt(private_base_def, _) => self
375 .tcx
376 .def_ident_span(private_base_def.did())
377 .unwrap_or_else(|| self.tcx.def_span(private_base_def.did())),
378 _ => DUMMY_SP,
379 };
380 let accessible_struct_span = self
381 .tcx
382 .def_ident_span(base_def.did())
383 .unwrap_or_else(|| self.tcx.def_span(base_def.did()));
384 let deref_impl_span = (typeck_results
385 .expr_adjustments(base_expr)
386 .iter()
387 .filter(|adj| {
388 #[allow(non_exhaustive_omitted_patterns)] match adj.kind {
Adjust::Deref(DerefAdjustKind::Overloaded(_)) => true,
_ => false,
}matches!(adj.kind, Adjust::Deref(DerefAdjustKind::Overloaded(_)))
389 })
390 .count()
391 == 1)
392 .then(|| {
393 self.probe(|_| {
394 let deref_trait_did =
395 self.tcx.require_lang_item(LangItem::Deref, DUMMY_SP);
396 let trait_ref =
397 ty::TraitRef::new(self.tcx, deref_trait_did, [private_base_ty]);
398 let obligation: Obligation<'tcx, ty::Predicate<'tcx>> =
399 Obligation::new(
400 self.tcx,
401 ObligationCause::dummy(),
402 param_env,
403 trait_ref,
404 );
405 let Ok(Some(ImplSource::UserDefined(impl_data))) =
406 SelectionContext::new(self)
407 .select(&obligation.with(self.tcx, trait_ref))
408 else {
409 return None;
410 };
411 Some(self.tcx.def_span(impl_data.impl_def_id))
412 })
413 })
414 .flatten();
415
416 let mut note_spans: MultiSpan = private_struct_span.into();
417 if private_struct_span != DUMMY_SP {
418 note_spans.push_span_label(private_struct_span, "in this struct");
419 }
420 if private_field_span != DUMMY_SP {
421 note_spans.push_span_label(
422 private_field_span,
423 "if this field wasn't private, it would be accessible",
424 );
425 }
426 if accessible_struct_span != DUMMY_SP {
427 note_spans.push_span_label(
428 accessible_struct_span,
429 "this struct is accessible through auto-deref",
430 );
431 }
432 if field_span != DUMMY_SP {
433 note_spans
434 .push_span_label(field_span, "this is the field that was accessed");
435 }
436 if let Some(deref_impl_span) = deref_impl_span
437 && deref_impl_span != DUMMY_SP
438 {
439 note_spans.push_span_label(
440 deref_impl_span,
441 "the field was accessed through this `Deref`",
442 );
443 }
444
445 err.span_note(
446 note_spans,
447 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there is a field `{0}` on `{1}` with type `{2}` but it is private; `{0}` from `{3}` was accessed through auto-deref instead",
field_ident, private_base_ty, private_field_ty,
deref_base_ty))
})format!(
448 "there is a field `{field_ident}` on `{private_base_ty}` with type `{private_field_ty}` but it is private; `{field_ident}` from `{deref_base_ty}` was accessed through auto-deref instead"
449 ),
450 );
451 }
452
453 return;
456 }
457
458 private_candidate.get_or_insert((
459 deref_base_ty,
460 field_def.ty(self.tcx, args),
461 field_span,
462 ));
463 }
464 }
465
466 pub fn suggest_restricting_param_bound(
467 &self,
468 err: &mut Diag<'_>,
469 trait_pred: ty::PolyTraitPredicate<'tcx>,
470 associated_ty: Option<(&'static str, Ty<'tcx>)>,
471 mut body_id: LocalDefId,
472 ) {
473 if trait_pred.skip_binder().polarity != ty::PredicatePolarity::Positive {
474 return;
475 }
476
477 let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
478
479 let self_ty = trait_pred.skip_binder().self_ty();
480 let (param_ty, projection) = match *self_ty.kind() {
481 ty::Param(_) => (true, None),
482 ty::Alias(projection @ ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
483 (false, Some(projection))
484 }
485 _ => (false, None),
486 };
487
488 let mut finder = ParamFinder { .. };
489 finder.visit_binder(&trait_pred);
490
491 loop {
494 let node = self.tcx.hir_node_by_def_id(body_id);
495 match node {
496 hir::Node::Item(hir::Item {
497 kind: hir::ItemKind::Trait { ident, generics, bounds, .. },
498 ..
499 }) if self_ty == self.tcx.types.self_param => {
500 if !param_ty { ::core::panicking::panic("assertion failed: param_ty") };assert!(param_ty);
501 suggest_restriction(
503 self.tcx,
504 body_id,
505 generics,
506 "`Self`",
507 err,
508 None,
509 projection,
510 trait_pred,
511 Some((&ident, bounds)),
512 );
513 return;
514 }
515
516 hir::Node::TraitItem(hir::TraitItem {
517 generics,
518 kind: hir::TraitItemKind::Fn(..),
519 ..
520 }) if self_ty == self.tcx.types.self_param => {
521 if !param_ty { ::core::panicking::panic("assertion failed: param_ty") };assert!(param_ty);
522 suggest_restriction(
524 self.tcx, body_id, generics, "`Self`", err, None, projection, trait_pred,
525 None,
526 );
527 return;
528 }
529
530 hir::Node::TraitItem(hir::TraitItem {
531 generics,
532 kind: hir::TraitItemKind::Fn(fn_sig, ..),
533 ..
534 })
535 | hir::Node::ImplItem(hir::ImplItem {
536 generics,
537 kind: hir::ImplItemKind::Fn(fn_sig, ..),
538 ..
539 })
540 | hir::Node::Item(hir::Item {
541 kind: hir::ItemKind::Fn { sig: fn_sig, generics, .. },
542 ..
543 }) if projection.is_some() => {
544 suggest_restriction(
546 self.tcx,
547 body_id,
548 generics,
549 "the associated type",
550 err,
551 Some(fn_sig),
552 projection,
553 trait_pred,
554 None,
555 );
556 return;
557 }
558 hir::Node::Item(hir::Item {
559 kind:
560 hir::ItemKind::Trait { generics, .. }
561 | hir::ItemKind::Impl(hir::Impl { generics, .. }),
562 ..
563 }) if projection.is_some() => {
564 suggest_restriction(
566 self.tcx,
567 body_id,
568 generics,
569 "the associated type",
570 err,
571 None,
572 projection,
573 trait_pred,
574 None,
575 );
576 return;
577 }
578
579 hir::Node::Item(hir::Item {
580 kind:
581 hir::ItemKind::Struct(_, generics, _)
582 | hir::ItemKind::Enum(_, generics, _)
583 | hir::ItemKind::Union(_, generics, _)
584 | hir::ItemKind::Trait { generics, .. }
585 | hir::ItemKind::Impl(hir::Impl { generics, .. })
586 | hir::ItemKind::Fn { generics, .. }
587 | hir::ItemKind::TyAlias(_, generics, _)
588 | hir::ItemKind::Const(_, generics, _, _)
589 | hir::ItemKind::TraitAlias(_, _, generics, _),
590 ..
591 })
592 | hir::Node::TraitItem(hir::TraitItem { generics, .. })
593 | hir::Node::ImplItem(hir::ImplItem { generics, .. })
594 if param_ty =>
595 {
596 if !trait_pred.skip_binder().trait_ref.args[1..]
605 .iter()
606 .all(|g| g.is_suggestable(self.tcx, false))
607 {
608 return;
609 }
610 let param_name = self_ty.to_string();
612 let mut constraint = {
let _guard = NoTrimmedGuard::new();
trait_pred.print_modifiers_and_trait_path().to_string()
}with_no_trimmed_paths!(
613 trait_pred.print_modifiers_and_trait_path().to_string()
614 );
615
616 if let Some((name, term)) = associated_ty {
617 if let Some(stripped) = constraint.strip_suffix('>') {
620 constraint = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}, {1} = {2}>", stripped, name,
term))
})format!("{stripped}, {name} = {term}>");
621 } else {
622 constraint.push_str(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0} = {1}>", name, term))
})format!("<{name} = {term}>"));
623 }
624 }
625
626 if suggest_constraining_type_param(
627 self.tcx,
628 generics,
629 err,
630 ¶m_name,
631 &constraint,
632 Some(trait_pred.def_id()),
633 None,
634 ) {
635 return;
636 }
637 }
638
639 hir::Node::TraitItem(hir::TraitItem {
640 generics,
641 kind: hir::TraitItemKind::Fn(..),
642 ..
643 })
644 | hir::Node::ImplItem(hir::ImplItem {
645 generics,
646 impl_kind: hir::ImplItemImplKind::Inherent { .. },
647 kind: hir::ImplItemKind::Fn(..),
648 ..
649 }) if finder.can_suggest_bound(generics) => {
650 suggest_arbitrary_trait_bound(
652 self.tcx,
653 generics,
654 err,
655 trait_pred,
656 associated_ty,
657 );
658 }
659 hir::Node::Item(hir::Item {
660 kind:
661 hir::ItemKind::Struct(_, generics, _)
662 | hir::ItemKind::Enum(_, generics, _)
663 | hir::ItemKind::Union(_, generics, _)
664 | hir::ItemKind::Trait { generics, .. }
665 | hir::ItemKind::Impl(hir::Impl { generics, .. })
666 | hir::ItemKind::Fn { generics, .. }
667 | hir::ItemKind::TyAlias(_, generics, _)
668 | hir::ItemKind::Const(_, generics, _, _)
669 | hir::ItemKind::TraitAlias(_, _, generics, _),
670 ..
671 }) if finder.can_suggest_bound(generics) => {
672 if suggest_arbitrary_trait_bound(
674 self.tcx,
675 generics,
676 err,
677 trait_pred,
678 associated_ty,
679 ) {
680 return;
681 }
682 }
683 hir::Node::Crate(..) => return,
684
685 _ => {}
686 }
687 body_id = self.tcx.local_parent(body_id);
688 }
689 }
690
691 pub(super) fn suggest_dereferences(
694 &self,
695 obligation: &PredicateObligation<'tcx>,
696 err: &mut Diag<'_>,
697 trait_pred: ty::PolyTraitPredicate<'tcx>,
698 ) -> bool {
699 let mut code = obligation.cause.code();
700 if let ObligationCauseCode::FunctionArg { arg_hir_id, call_hir_id, .. } = code
701 && let Some(typeck_results) = &self.typeck_results
702 && let hir::Node::Expr(expr) = self.tcx.hir_node(*arg_hir_id)
703 && let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
704 {
705 let mut real_trait_pred = trait_pred;
709 while let Some((parent_code, parent_trait_pred)) = code.parent_with_predicate() {
710 code = parent_code;
711 if let Some(parent_trait_pred) = parent_trait_pred {
712 real_trait_pred = parent_trait_pred;
713 }
714 }
715
716 let real_ty = self.tcx.instantiate_bound_regions_with_erased(real_trait_pred.self_ty());
719 if !self.can_eq(obligation.param_env, real_ty, arg_ty) {
720 return false;
721 }
722
723 let (is_under_ref, base_ty, span) = match expr.kind {
730 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, subexpr)
731 if let &ty::Ref(region, base_ty, hir::Mutability::Not) = real_ty.kind() =>
732 {
733 (Some(region), base_ty, subexpr.span)
734 }
735 hir::ExprKind::AddrOf(..) => return false,
737 _ => (None, real_ty, obligation.cause.span),
738 };
739
740 let autoderef = (self.autoderef_steps)(base_ty);
741 let mut is_boxed = base_ty.is_box();
742 if let Some(steps) = autoderef.into_iter().position(|(mut ty, obligations)| {
743 let can_deref = is_under_ref.is_some()
746 || self.type_is_copy_modulo_regions(obligation.param_env, ty)
747 || ty.is_numeric() || is_boxed && self.type_is_sized_modulo_regions(obligation.param_env, ty);
749 is_boxed &= ty.is_box();
750
751 if let Some(region) = is_under_ref {
753 ty = Ty::new_ref(self.tcx, region, ty, hir::Mutability::Not);
754 }
755
756 let real_trait_pred_and_ty =
758 real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
759 let obligation = self.mk_trait_obligation_with_new_self_ty(
760 obligation.param_env,
761 real_trait_pred_and_ty,
762 );
763
764 can_deref
765 && obligations
766 .iter()
767 .chain([&obligation])
768 .all(|obligation| self.predicate_may_hold(obligation))
769 }) && steps > 0
770 {
771 if span.in_external_macro(self.tcx.sess.source_map()) {
772 return false;
773 }
774 let derefs = "*".repeat(steps);
775 let msg = "consider dereferencing here";
776
777 let call_node = self.tcx.hir_node(*call_hir_id);
778 let is_receiver = #[allow(non_exhaustive_omitted_patterns)] match call_node {
Node::Expr(hir::Expr {
kind: hir::ExprKind::MethodCall(_, receiver_expr, ..), .. }) if
receiver_expr.hir_id == *arg_hir_id => true,
_ => false,
}matches!(
779 call_node,
780 Node::Expr(hir::Expr {
781 kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
782 ..
783 })
784 if receiver_expr.hir_id == *arg_hir_id
785 );
786 if is_receiver {
787 err.multipart_suggestion(
788 msg,
789 ::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!("({0}", derefs))
})), (span.shrink_to_hi(), ")".to_string())]))vec![
790 (span.shrink_to_lo(), format!("({derefs}")),
791 (span.shrink_to_hi(), ")".to_string()),
792 ],
793 Applicability::MachineApplicable,
794 )
795 } else {
796 err.span_suggestion_verbose(
797 span.shrink_to_lo(),
798 msg,
799 derefs,
800 Applicability::MachineApplicable,
801 )
802 };
803 return true;
804 }
805 } else if let (
806 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. },
807 predicate,
808 ) = code.peel_derives_with_predicate()
809 && let Some(typeck_results) = &self.typeck_results
810 && let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
811 && let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
812 && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
813 && let trait_pred = predicate.unwrap_or(trait_pred)
814 && hir::lang_items::BINARY_OPERATORS
816 .iter()
817 .filter_map(|&op| self.tcx.lang_items().get(op))
818 .any(|op| {
819 op == trait_pred.skip_binder().trait_ref.def_id
820 })
821 {
822 let trait_pred = predicate.unwrap_or(trait_pred);
824 let lhs_ty = self.tcx.instantiate_bound_regions_with_erased(trait_pred.self_ty());
825 let lhs_autoderef = (self.autoderef_steps)(lhs_ty);
826 let rhs_autoderef = (self.autoderef_steps)(rhs_ty);
827 let first_lhs = lhs_autoderef.first().unwrap().clone();
828 let first_rhs = rhs_autoderef.first().unwrap().clone();
829 let mut autoderefs = lhs_autoderef
830 .into_iter()
831 .enumerate()
832 .rev()
833 .zip_longest(rhs_autoderef.into_iter().enumerate().rev())
834 .map(|t| match t {
835 EitherOrBoth::Both(a, b) => (a, b),
836 EitherOrBoth::Left(a) => (a, (0, first_rhs.clone())),
837 EitherOrBoth::Right(b) => ((0, first_lhs.clone()), b),
838 })
839 .rev();
840 if let Some((lsteps, rsteps)) =
841 autoderefs.find_map(|((lsteps, (l_ty, _)), (rsteps, (r_ty, _)))| {
842 let trait_pred_and_ty = trait_pred.map_bound(|inner| {
846 (
847 ty::TraitPredicate {
848 trait_ref: ty::TraitRef::new_from_args(
849 self.tcx,
850 inner.trait_ref.def_id,
851 self.tcx.mk_args(
852 &[&[l_ty.into(), r_ty.into()], &inner.trait_ref.args[2..]]
853 .concat(),
854 ),
855 ),
856 ..inner
857 },
858 l_ty,
859 )
860 });
861 let obligation = self.mk_trait_obligation_with_new_self_ty(
862 obligation.param_env,
863 trait_pred_and_ty,
864 );
865 self.predicate_may_hold(&obligation).then_some(match (lsteps, rsteps) {
866 (_, 0) => (Some(lsteps), None),
867 (0, _) => (None, Some(rsteps)),
868 _ => (Some(lsteps), Some(rsteps)),
869 })
870 })
871 {
872 let make_sugg = |mut expr: &Expr<'_>, mut steps| {
873 if expr.span.in_external_macro(self.tcx.sess.source_map()) {
874 return None;
875 }
876 let mut prefix_span = expr.span.shrink_to_lo();
877 let mut msg = "consider dereferencing here";
878 if let hir::ExprKind::AddrOf(_, _, inner) = expr.kind {
879 msg = "consider removing the borrow and dereferencing instead";
880 if let hir::ExprKind::AddrOf(..) = inner.kind {
881 msg = "consider removing the borrows and dereferencing instead";
882 }
883 }
884 while let hir::ExprKind::AddrOf(_, _, inner) = expr.kind
885 && steps > 0
886 {
887 prefix_span = prefix_span.with_hi(inner.span.lo());
888 expr = inner;
889 steps -= 1;
890 }
891 if steps == 0 {
893 return Some((
894 msg.trim_end_matches(" and dereferencing instead"),
895 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(prefix_span, String::new())]))vec![(prefix_span, String::new())],
896 ));
897 }
898 let derefs = "*".repeat(steps);
899 let needs_parens = steps > 0 && expr_needs_parens(expr);
900 let mut suggestion = if needs_parens {
901 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}(", derefs))
})), (expr.span.shrink_to_hi(), ")".to_string())]))vec![
902 (
903 expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
904 format!("{derefs}("),
905 ),
906 (expr.span.shrink_to_hi(), ")".to_string()),
907 ]
908 } else {
909 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", derefs))
}))]))vec![(
910 expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
911 format!("{derefs}"),
912 )]
913 };
914 if !prefix_span.is_empty() {
916 suggestion.push((prefix_span, String::new()));
917 }
918 Some((msg, suggestion))
919 };
920
921 if let Some(lsteps) = lsteps
922 && let Some(rsteps) = rsteps
923 && lsteps > 0
924 && rsteps > 0
925 {
926 let Some((_, mut suggestion)) = make_sugg(lhs, lsteps) else {
927 return false;
928 };
929 let Some((_, mut rhs_suggestion)) = make_sugg(rhs, rsteps) else {
930 return false;
931 };
932 suggestion.append(&mut rhs_suggestion);
933 err.multipart_suggestion(
934 "consider dereferencing both sides of the expression",
935 suggestion,
936 Applicability::MachineApplicable,
937 );
938 return true;
939 } else if let Some(lsteps) = lsteps
940 && lsteps > 0
941 {
942 let Some((msg, suggestion)) = make_sugg(lhs, lsteps) else {
943 return false;
944 };
945 err.multipart_suggestion(msg, suggestion, Applicability::MachineApplicable);
946 return true;
947 } else if let Some(rsteps) = rsteps
948 && rsteps > 0
949 {
950 let Some((msg, suggestion)) = make_sugg(rhs, rsteps) else {
951 return false;
952 };
953 err.multipart_suggestion(msg, suggestion, Applicability::MachineApplicable);
954 return true;
955 }
956 }
957 }
958 false
959 }
960
961 fn get_closure_name(
965 &self,
966 def_id: DefId,
967 err: &mut Diag<'_>,
968 msg: Cow<'static, str>,
969 ) -> Option<Symbol> {
970 let get_name = |err: &mut Diag<'_>, kind: &hir::PatKind<'_>| -> Option<Symbol> {
971 match &kind {
974 hir::PatKind::Binding(hir::BindingMode::NONE, _, ident, None) => Some(ident.name),
975 _ => {
976 err.note(msg);
977 None
978 }
979 }
980 };
981
982 let hir_id = self.tcx.local_def_id_to_hir_id(def_id.as_local()?);
983 match self.tcx.parent_hir_node(hir_id) {
984 hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Let(local), .. }) => {
985 get_name(err, &local.pat.kind)
986 }
987 hir::Node::LetStmt(local) => get_name(err, &local.pat.kind),
990 _ => None,
991 }
992 }
993
994 pub(super) fn suggest_fn_call(
998 &self,
999 obligation: &PredicateObligation<'tcx>,
1000 err: &mut Diag<'_>,
1001 trait_pred: ty::PolyTraitPredicate<'tcx>,
1002 ) -> bool {
1003 if self.typeck_results.is_none() {
1006 return false;
1007 }
1008
1009 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
1010 obligation.predicate.kind().skip_binder()
1011 && self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
1012 {
1013 return false;
1015 }
1016
1017 let self_ty = self.instantiate_binder_with_fresh_vars(
1018 DUMMY_SP,
1019 BoundRegionConversionTime::FnCall,
1020 trait_pred.self_ty(),
1021 );
1022
1023 let Some((def_id_or_name, output, inputs)) =
1024 self.extract_callable_info(obligation.cause.body_id, obligation.param_env, self_ty)
1025 else {
1026 return false;
1027 };
1028
1029 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, output));
1031
1032 let new_obligation =
1033 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
1034 if !self.predicate_must_hold_modulo_regions(&new_obligation) {
1035 return false;
1036 }
1037
1038 if let ty::CoroutineClosure(def_id, args) = *self_ty.kind()
1042 && let sig = args.as_coroutine_closure().coroutine_closure_sig().skip_binder()
1043 && let ty::Tuple(inputs) = *sig.tupled_inputs_ty.kind()
1044 && inputs.is_empty()
1045 && self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Future)
1046 && let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
1047 && let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) =
1048 self.tcx.hir_node(*arg_hir_id)
1049 && let Some(hir::Node::Expr(hir::Expr {
1050 kind: hir::ExprKind::Closure(closure), ..
1051 })) = self.tcx.hir_get_if_local(def_id)
1052 && let hir::ClosureKind::CoroutineClosure(CoroutineDesugaring::Async) = closure.kind
1053 && let Some(arg_span) = closure.fn_arg_span
1054 && obligation.cause.span.contains(arg_span)
1055 {
1056 let mut body = self.tcx.hir_body(closure.body).value;
1057 let peeled = body.peel_blocks().peel_drop_temps();
1058 if let hir::ExprKind::Closure(inner) = peeled.kind {
1059 body = self.tcx.hir_body(inner.body).value;
1060 }
1061 if !#[allow(non_exhaustive_omitted_patterns)] match body.peel_blocks().peel_drop_temps().kind
{
hir::ExprKind::Block(..) => true,
_ => false,
}matches!(body.peel_blocks().peel_drop_temps().kind, hir::ExprKind::Block(..)) {
1062 return false;
1063 }
1064
1065 let sm = self.tcx.sess.source_map();
1066 let removal_span = if let Ok(snippet) =
1067 sm.span_to_snippet(arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1)))
1068 && snippet.ends_with(' ')
1069 {
1070 arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1))
1072 } else {
1073 arg_span
1074 };
1075 err.span_suggestion_verbose(
1076 removal_span,
1077 "use `async {}` instead of `async || {}` to introduce an async block",
1078 "",
1079 Applicability::MachineApplicable,
1080 );
1081 return true;
1082 }
1083
1084 let msg = match def_id_or_name {
1086 DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
1087 DefKind::Ctor(CtorOf::Struct, _) => {
1088 Cow::from("use parentheses to construct this tuple struct")
1089 }
1090 DefKind::Ctor(CtorOf::Variant, _) => {
1091 Cow::from("use parentheses to construct this tuple variant")
1092 }
1093 kind => Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use parentheses to call this {0}",
self.tcx.def_kind_descr(kind, def_id)))
})format!(
1094 "use parentheses to call this {}",
1095 self.tcx.def_kind_descr(kind, def_id)
1096 )),
1097 },
1098 DefIdOrName::Name(name) => Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use parentheses to call this {0}",
name))
})format!("use parentheses to call this {name}")),
1099 };
1100
1101 let args = inputs
1102 .into_iter()
1103 .map(|ty| {
1104 if ty.is_suggestable(self.tcx, false) {
1105 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", ty))
})format!("/* {ty} */")
1106 } else {
1107 "/* value */".to_string()
1108 }
1109 })
1110 .collect::<Vec<_>>()
1111 .join(", ");
1112
1113 if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
1114 && obligation.cause.span.can_be_used_for_suggestions()
1115 {
1116 let span = obligation.cause.span;
1117
1118 let arg_expr = match self.tcx.hir_node(*arg_hir_id) {
1119 hir::Node::Expr(expr) => Some(expr),
1120 _ => None,
1121 };
1122
1123 let is_closure_expr =
1124 arg_expr.is_some_and(|expr| #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
hir::ExprKind::Closure(..) => true,
_ => false,
}matches!(expr.kind, hir::ExprKind::Closure(..)));
1125
1126 if args.is_empty()
1129 && let Some(expr) = arg_expr
1130 && let hir::ExprKind::Closure(closure) = expr.kind
1131 {
1132 let mut body = self.tcx.hir_body(closure.body).value;
1133
1134 if let hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) =
1136 closure.kind
1137 {
1138 let peeled = body.peel_blocks().peel_drop_temps();
1139 if let hir::ExprKind::Closure(inner) = peeled.kind {
1140 body = self.tcx.hir_body(inner.body).value;
1141 }
1142 }
1143
1144 let peeled_body = body.peel_blocks().peel_drop_temps();
1145 if let hir::ExprKind::Call(callee, call_args) = peeled_body.kind
1146 && call_args.is_empty()
1147 && let hir::ExprKind::Block(..) = callee.peel_blocks().peel_drop_temps().kind
1148 {
1149 return false;
1150 }
1151 }
1152
1153 if is_closure_expr {
1154 err.multipart_suggestions(
1155 msg,
1156 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "(".to_string()),
(span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(")({0})", args))
}))]))]))vec![vec![
1157 (span.shrink_to_lo(), "(".to_string()),
1158 (span.shrink_to_hi(), format!(")({args})")),
1159 ]],
1160 Applicability::HasPlaceholders,
1161 );
1162 } else {
1163 err.span_suggestion_verbose(
1164 span.shrink_to_hi(),
1165 msg,
1166 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})", args))
})format!("({args})"),
1167 Applicability::HasPlaceholders,
1168 );
1169 }
1170 } else if let DefIdOrName::DefId(def_id) = def_id_or_name {
1171 let name = match self.tcx.hir_get_if_local(def_id) {
1172 Some(hir::Node::Expr(hir::Expr {
1173 kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
1174 ..
1175 })) => {
1176 err.span_label(*fn_decl_span, "consider calling this closure");
1177 let Some(name) = self.get_closure_name(def_id, err, msg.clone()) else {
1178 return false;
1179 };
1180 name.to_string()
1181 }
1182 Some(hir::Node::Item(hir::Item {
1183 kind: hir::ItemKind::Fn { ident, .. }, ..
1184 })) => {
1185 err.span_label(ident.span, "consider calling this function");
1186 ident.to_string()
1187 }
1188 Some(hir::Node::Ctor(..)) => {
1189 let name = self.tcx.def_path_str(def_id);
1190 err.span_label(
1191 self.tcx.def_span(def_id),
1192 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider calling the constructor for `{0}`",
name))
})format!("consider calling the constructor for `{name}`"),
1193 );
1194 name
1195 }
1196 _ => return false,
1197 };
1198 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: `{1}({2})`", msg, name, args))
})format!("{msg}: `{name}({args})`"));
1199 }
1200 true
1201 }
1202
1203 pub(super) fn suggest_cast_to_fn_pointer(
1204 &self,
1205 obligation: &PredicateObligation<'tcx>,
1206 err: &mut Diag<'_>,
1207 leaf_trait_predicate: ty::PolyTraitPredicate<'tcx>,
1208 main_trait_predicate: ty::PolyTraitPredicate<'tcx>,
1209 span: Span,
1210 ) -> bool {
1211 let &[candidate] = &self.find_similar_impl_candidates(leaf_trait_predicate)[..] else {
1212 return false;
1213 };
1214 let candidate = candidate.trait_ref;
1215
1216 if !#[allow(non_exhaustive_omitted_patterns)] match (candidate.self_ty().kind(),
main_trait_predicate.self_ty().skip_binder().kind()) {
(ty::FnPtr(..), ty::FnDef(..)) => true,
_ => false,
}matches!(
1217 (candidate.self_ty().kind(), main_trait_predicate.self_ty().skip_binder().kind(),),
1218 (ty::FnPtr(..), ty::FnDef(..))
1219 ) {
1220 return false;
1221 }
1222
1223 let parenthesized_cast = |span: Span| {
1224 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "(".to_string()),
(span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" as {0})",
candidate.self_ty()))
}))]))vec![
1225 (span.shrink_to_lo(), "(".to_string()),
1226 (span.shrink_to_hi(), format!(" as {})", candidate.self_ty())),
1227 ]
1228 };
1229 let suggestion = if self.tcx.sess.source_map().span_followed_by(span, ".").is_some() {
1231 parenthesized_cast(span)
1232 } else if let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) {
1233 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1234 expr_finder.visit_expr(body.value);
1235 if let Some(expr) = expr_finder.result
1236 && let hir::ExprKind::AddrOf(_, _, expr) = expr.kind
1237 {
1238 parenthesized_cast(expr.span)
1239 } else {
1240 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" as {0}",
candidate.self_ty()))
}))]))vec![(span.shrink_to_hi(), format!(" as {}", candidate.self_ty()))]
1241 }
1242 } else {
1243 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" as {0}",
candidate.self_ty()))
}))]))vec![(span.shrink_to_hi(), format!(" as {}", candidate.self_ty()))]
1244 };
1245
1246 let trait_ = self.tcx.short_string(candidate.print_trait_sugared(), err.long_ty_path());
1247 let self_ty = self.tcx.short_string(candidate.self_ty(), err.long_ty_path());
1248 err.multipart_suggestion(
1249 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait `{0}` is implemented for fn pointer `{1}`, try casting using `as`",
trait_, self_ty))
})format!(
1250 "the trait `{trait_}` is implemented for fn pointer \
1251 `{self_ty}`, try casting using `as`",
1252 ),
1253 suggestion,
1254 Applicability::MaybeIncorrect,
1255 );
1256 true
1257 }
1258
1259 pub(super) fn check_for_binding_assigned_block_without_tail_expression(
1260 &self,
1261 obligation: &PredicateObligation<'tcx>,
1262 err: &mut Diag<'_>,
1263 trait_pred: ty::PolyTraitPredicate<'tcx>,
1264 ) {
1265 let mut span = obligation.cause.span;
1266 while span.from_expansion() {
1267 span.remove_mark();
1269 }
1270 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1271 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1272 return;
1273 };
1274 expr_finder.visit_expr(body.value);
1275 let Some(expr) = expr_finder.result else {
1276 return;
1277 };
1278 let Some(typeck) = &self.typeck_results else {
1279 return;
1280 };
1281 let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else {
1282 return;
1283 };
1284 if !ty.is_unit() {
1285 return;
1286 };
1287 let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
1288 return;
1289 };
1290 let Res::Local(hir_id) = path.res else {
1291 return;
1292 };
1293 let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id) else {
1294 return;
1295 };
1296 let hir::Node::LetStmt(hir::LetStmt { ty: None, init: Some(init), .. }) =
1297 self.tcx.parent_hir_node(pat.hir_id)
1298 else {
1299 return;
1300 };
1301 let hir::ExprKind::Block(block, None) = init.kind else {
1302 return;
1303 };
1304 if block.expr.is_some() {
1305 return;
1306 }
1307 let [.., stmt] = block.stmts else {
1308 err.span_label(block.span, "this empty block is missing a tail expression");
1309 return;
1310 };
1311 if stmt.span.from_expansion() {
1314 return;
1315 }
1316 let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
1317 return;
1318 };
1319 let Some(ty) = typeck.expr_ty_opt(tail_expr) else {
1320 err.span_label(block.span, "this block is missing a tail expression");
1321 return;
1322 };
1323 let ty = self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(ty));
1324 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, ty));
1325
1326 let new_obligation =
1327 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
1328 if !#[allow(non_exhaustive_omitted_patterns)] match tail_expr.kind {
hir::ExprKind::Err(_) => true,
_ => false,
}matches!(tail_expr.kind, hir::ExprKind::Err(_))
1329 && self.predicate_must_hold_modulo_regions(&new_obligation)
1330 {
1331 err.span_suggestion_short(
1332 stmt.span.with_lo(tail_expr.span.hi()),
1333 "remove this semicolon",
1334 "",
1335 Applicability::MachineApplicable,
1336 );
1337 } else {
1338 err.span_label(block.span, "this block is missing a tail expression");
1339 }
1340 }
1341
1342 pub(super) fn suggest_add_clone_to_arg(
1343 &self,
1344 obligation: &PredicateObligation<'tcx>,
1345 err: &mut Diag<'_>,
1346 trait_pred: ty::PolyTraitPredicate<'tcx>,
1347 ) -> bool {
1348 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
1349 self.enter_forall(self_ty, |ty: Ty<'_>| {
1350 let Some(generics) = self.tcx.hir_get_generics(obligation.cause.body_id) else {
1351 return false;
1352 };
1353 let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
1354 let ty::Param(param) = inner_ty.kind() else { return false };
1355 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
1356 else {
1357 return false;
1358 };
1359
1360 let clone_trait = self.tcx.require_lang_item(LangItem::Clone, obligation.cause.span);
1361 let has_clone = |ty| {
1362 self.type_implements_trait(clone_trait, [ty], obligation.param_env)
1363 .must_apply_modulo_regions()
1364 };
1365
1366 let existing_clone_call = match self.tcx.hir_node(*arg_hir_id) {
1367 Node::Expr(Expr { kind: hir::ExprKind::Path(_), .. }) => None,
1369 Node::Expr(Expr {
1372 kind:
1373 hir::ExprKind::MethodCall(
1374 hir::PathSegment { ident, .. },
1375 _receiver,
1376 [],
1377 call_span,
1378 ),
1379 hir_id,
1380 ..
1381 }) if ident.name == sym::clone
1382 && !call_span.from_expansion()
1383 && !has_clone(*inner_ty) =>
1384 {
1385 let Some(typeck_results) = self.typeck_results.as_ref() else { return false };
1387 let Some((DefKind::AssocFn, did)) = typeck_results.type_dependent_def(*hir_id)
1388 else {
1389 return false;
1390 };
1391 if self.tcx.trait_of_assoc(did) != Some(clone_trait) {
1392 return false;
1393 }
1394 Some(ident.span)
1395 }
1396 _ => return false,
1397 };
1398
1399 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1400 obligation.param_env,
1401 trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
1402 );
1403
1404 if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1405 if !has_clone(param.to_ty(self.tcx)) {
1406 suggest_constraining_type_param(
1407 self.tcx,
1408 generics,
1409 err,
1410 param.name.as_str(),
1411 "Clone",
1412 Some(clone_trait),
1413 None,
1414 );
1415 }
1416 if let Some(existing_clone_call) = existing_clone_call {
1417 err.span_note(
1418 existing_clone_call,
1419 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this `clone()` copies the reference, which does not do anything, because `{0}` does not implement `Clone`",
inner_ty))
})format!(
1420 "this `clone()` copies the reference, \
1421 which does not do anything, \
1422 because `{inner_ty}` does not implement `Clone`"
1423 ),
1424 );
1425 } else {
1426 err.span_suggestion_verbose(
1427 obligation.cause.span.shrink_to_hi(),
1428 "consider using clone here",
1429 ".clone()".to_string(),
1430 Applicability::MaybeIncorrect,
1431 );
1432 }
1433 return true;
1434 }
1435 false
1436 })
1437 }
1438
1439 pub fn extract_callable_info(
1443 &self,
1444 body_id: LocalDefId,
1445 param_env: ty::ParamEnv<'tcx>,
1446 found: Ty<'tcx>,
1447 ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
1448 let Some((def_id_or_name, output, inputs)) =
1450 (self.autoderef_steps)(found).into_iter().find_map(|(found, _)| match *found.kind() {
1451 ty::FnPtr(sig_tys, _) => Some((
1452 DefIdOrName::Name("function pointer"),
1453 sig_tys.output(),
1454 sig_tys.inputs(),
1455 )),
1456 ty::FnDef(def_id, _) => {
1457 let fn_sig = found.fn_sig(self.tcx);
1458 Some((DefIdOrName::DefId(def_id), fn_sig.output(), fn_sig.inputs()))
1459 }
1460 ty::Closure(def_id, args) => {
1461 let fn_sig = args.as_closure().sig();
1462 Some((
1463 DefIdOrName::DefId(def_id),
1464 fn_sig.output(),
1465 fn_sig.inputs().map_bound(|inputs| inputs[0].tuple_fields().as_slice()),
1466 ))
1467 }
1468 ty::CoroutineClosure(def_id, args) => {
1469 let sig_parts = args.as_coroutine_closure().coroutine_closure_sig();
1470 Some((
1471 DefIdOrName::DefId(def_id),
1472 sig_parts.map_bound(|sig| {
1473 sig.to_coroutine(
1474 self.tcx,
1475 args.as_coroutine_closure().parent_args(),
1476 self.next_ty_var(DUMMY_SP),
1479 self.tcx.coroutine_for_closure(def_id),
1480 self.next_ty_var(DUMMY_SP),
1481 )
1482 }),
1483 sig_parts.map_bound(|sig| sig.tupled_inputs_ty.tuple_fields().as_slice()),
1484 ))
1485 }
1486 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
1487 self.tcx
1488 .item_self_bounds(def_id)
1489 .instantiate(self.tcx, args)
1490 .skip_norm_wip()
1491 .iter()
1492 .find_map(|pred| {
1493 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1494 && self
1495 .tcx
1496 .is_lang_item(proj.projection_term.def_id(), LangItem::FnOnceOutput)
1497 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1499 {
1500 Some((
1501 DefIdOrName::DefId(def_id),
1502 pred.kind().rebind(proj.term.expect_type()),
1503 pred.kind().rebind(args.as_slice()),
1504 ))
1505 } else {
1506 None
1507 }
1508 })
1509 }
1510 ty::Dynamic(data, _) => data.iter().find_map(|pred| {
1511 if let ty::ExistentialPredicate::Projection(proj) = pred.skip_binder()
1512 && self.tcx.is_lang_item(proj.def_id, LangItem::FnOnceOutput)
1513 && let ty::Tuple(args) = proj.args.type_at(0).kind()
1515 {
1516 Some((
1517 DefIdOrName::Name("trait object"),
1518 pred.rebind(proj.term.expect_type()),
1519 pred.rebind(args.as_slice()),
1520 ))
1521 } else {
1522 None
1523 }
1524 }),
1525 ty::Param(param) => {
1526 let generics = self.tcx.generics_of(body_id);
1527 let name = if generics.count() > param.index as usize
1528 && let def = generics.param_at(param.index as usize, self.tcx)
1529 && #[allow(non_exhaustive_omitted_patterns)] match def.kind {
ty::GenericParamDefKind::Type { .. } => true,
_ => false,
}matches!(def.kind, ty::GenericParamDefKind::Type { .. })
1530 && def.name == param.name
1531 {
1532 DefIdOrName::DefId(def.def_id)
1533 } else {
1534 DefIdOrName::Name("type parameter")
1535 };
1536 param_env.caller_bounds().iter().find_map(|pred| {
1537 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1538 && self
1539 .tcx
1540 .is_lang_item(proj.projection_term.def_id(), LangItem::FnOnceOutput)
1541 && proj.projection_term.self_ty() == found
1542 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1544 {
1545 Some((
1546 name,
1547 pred.kind().rebind(proj.term.expect_type()),
1548 pred.kind().rebind(args.as_slice()),
1549 ))
1550 } else {
1551 None
1552 }
1553 })
1554 }
1555 _ => None,
1556 })
1557 else {
1558 return None;
1559 };
1560
1561 let output = self.instantiate_binder_with_fresh_vars(
1562 DUMMY_SP,
1563 BoundRegionConversionTime::FnCall,
1564 output,
1565 );
1566 let inputs = inputs
1567 .skip_binder()
1568 .iter()
1569 .map(|ty| {
1570 self.instantiate_binder_with_fresh_vars(
1571 DUMMY_SP,
1572 BoundRegionConversionTime::FnCall,
1573 inputs.rebind(*ty),
1574 )
1575 })
1576 .collect();
1577
1578 let InferOk { value: output, obligations: _ } =
1582 self.at(&ObligationCause::dummy(), param_env).normalize(Unnormalized::new_wip(output));
1583
1584 if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
1585 }
1586
1587 pub(super) fn where_clause_expr_matches_failed_self_ty(
1588 &self,
1589 obligation: &PredicateObligation<'tcx>,
1590 old_self_ty: Ty<'tcx>,
1591 ) -> bool {
1592 let ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code() else {
1593 return true;
1594 };
1595 let (Some(typeck_results), Some(body)) = (
1596 self.typeck_results.as_ref(),
1597 self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id),
1598 ) else {
1599 return true;
1600 };
1601
1602 let mut expr_finder = FindExprBySpan::new(obligation.cause.span, self.tcx);
1603 expr_finder.visit_expr(body.value);
1604 let Some(expr) = expr_finder.result else {
1605 return true;
1606 };
1607
1608 let inner_old_self_ty = match old_self_ty.kind() {
1609 ty::Ref(_, inner_ty, _) => Some(*inner_ty),
1610 _ => None,
1611 };
1612
1613 [typeck_results.expr_ty_adjusted_opt(expr)].into_iter().flatten().any(|expr_ty| {
1614 self.can_eq(obligation.param_env, expr_ty, old_self_ty)
1615 || inner_old_self_ty
1616 .is_some_and(|inner_ty| self.can_eq(obligation.param_env, expr_ty, inner_ty))
1617 })
1618 }
1619
1620 pub(super) fn suggest_add_reference_to_arg(
1621 &self,
1622 obligation: &PredicateObligation<'tcx>,
1623 err: &mut Diag<'_>,
1624 poly_trait_pred: ty::PolyTraitPredicate<'tcx>,
1625 has_custom_message: bool,
1626 ) -> bool {
1627 let span = obligation.cause.span;
1628 let param_env = obligation.param_env;
1629
1630 let mk_result = |trait_pred_and_new_ty| {
1631 let obligation =
1632 self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
1633 self.predicate_must_hold_modulo_regions(&obligation)
1634 };
1635
1636 let code = match obligation.cause.code() {
1637 ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code,
1638 c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
1641 if self.tcx.hir_span(*hir_id).lo() == span.lo() =>
1642 {
1643 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
1647 && let hir::ExprKind::Call(base, _) = expr.kind
1648 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, segment)) = base.kind
1649 && let hir::Node::Expr(outer) = self.tcx.parent_hir_node(expr.hir_id)
1650 && let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mtbl, _) = outer.kind
1651 && ty.span == span
1652 {
1653 let trait_pred_and_imm_ref = poly_trait_pred.map_bound(|p| {
1659 (p, Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1660 });
1661 let trait_pred_and_mut_ref = poly_trait_pred.map_bound(|p| {
1662 (p, Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1663 });
1664
1665 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1666 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1667 let sugg_msg = |pre: &str| {
1668 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you likely meant to call the associated function `{0}` for type `&{2}{1}`, but the code as written calls associated function `{0}` on type `{1}`",
segment.ident, poly_trait_pred.self_ty(), pre))
})format!(
1669 "you likely meant to call the associated function `{FN}` for type \
1670 `&{pre}{TY}`, but the code as written calls associated function `{FN}` on \
1671 type `{TY}`",
1672 FN = segment.ident,
1673 TY = poly_trait_pred.self_ty(),
1674 )
1675 };
1676 match (imm_ref_self_ty_satisfies_pred, mut_ref_self_ty_satisfies_pred, mtbl) {
1677 (true, _, hir::Mutability::Not) | (_, true, hir::Mutability::Mut) => {
1678 err.multipart_suggestion(
1679 sugg_msg(mtbl.prefix_str()),
1680 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(outer.span.shrink_to_lo(), "<".to_string()),
(span.shrink_to_hi(), ">".to_string())]))vec![
1681 (outer.span.shrink_to_lo(), "<".to_string()),
1682 (span.shrink_to_hi(), ">".to_string()),
1683 ],
1684 Applicability::MachineApplicable,
1685 );
1686 }
1687 (true, _, hir::Mutability::Mut) => {
1688 err.multipart_suggestion(
1690 sugg_msg("mut "),
1691 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(outer.span.shrink_to_lo().until(span), "<&".to_string()),
(span.shrink_to_hi(), ">".to_string())]))vec![
1692 (outer.span.shrink_to_lo().until(span), "<&".to_string()),
1693 (span.shrink_to_hi(), ">".to_string()),
1694 ],
1695 Applicability::MachineApplicable,
1696 );
1697 }
1698 (_, true, hir::Mutability::Not) => {
1699 err.multipart_suggestion(
1700 sugg_msg(""),
1701 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
(span.shrink_to_hi(), ">".to_string())]))vec![
1702 (outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
1703 (span.shrink_to_hi(), ">".to_string()),
1704 ],
1705 Applicability::MachineApplicable,
1706 );
1707 }
1708 _ => {}
1709 }
1710 return false;
1712 }
1713 c
1714 }
1715 c if #[allow(non_exhaustive_omitted_patterns)] match span.ctxt().outer_expn_data().kind
{
ExpnKind::Desugaring(DesugaringKind::ForLoop) => true,
_ => false,
}matches!(
1716 span.ctxt().outer_expn_data().kind,
1717 ExpnKind::Desugaring(DesugaringKind::ForLoop)
1718 ) =>
1719 {
1720 c
1721 }
1722 _ => return false,
1723 };
1724
1725 let mut never_suggest_borrow: Vec<_> =
1729 [LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
1730 .iter()
1731 .filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
1732 .collect();
1733
1734 if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {
1735 never_suggest_borrow.push(def_id);
1736 }
1737
1738 let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
1740 blacklist: &[DefId]|
1741 -> bool {
1742 if blacklist.contains(&old_pred.def_id()) {
1743 return false;
1744 }
1745 let trait_pred_and_imm_ref = old_pred.map_bound(|trait_pred| {
1747 (
1748 trait_pred,
1749 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1750 )
1751 });
1752 let trait_pred_and_mut_ref = old_pred.map_bound(|trait_pred| {
1753 (
1754 trait_pred,
1755 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1756 )
1757 });
1758
1759 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1760 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1761
1762 let (ref_inner_ty_satisfies_pred, ref_inner_ty_is_mut) =
1763 if let ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code()
1764 && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
1765 {
1766 (
1767 mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
1768 mutability.is_mut(),
1769 )
1770 } else {
1771 (false, false)
1772 };
1773
1774 let is_immut = imm_ref_self_ty_satisfies_pred
1775 || (ref_inner_ty_satisfies_pred && !ref_inner_ty_is_mut);
1776 let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_is_mut;
1777 if !is_immut && !is_mut {
1778 return false;
1779 }
1780 let Ok(_snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
1781 return false;
1782 };
1783 if !#[allow(non_exhaustive_omitted_patterns)] match span.ctxt().outer_expn_data().kind
{
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => true,
_ => false,
}matches!(
1791 span.ctxt().outer_expn_data().kind,
1792 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1793 ) {
1794 return false;
1795 }
1796 let mut label = || {
1803 let is_sized = match obligation.predicate.kind().skip_binder() {
1806 ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
1807 self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
1808 }
1809 _ => false,
1810 };
1811
1812 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait bound `{0}` is not satisfied",
self.tcx.short_string(old_pred, err.long_ty_path())))
})format!(
1813 "the trait bound `{}` is not satisfied",
1814 self.tcx.short_string(old_pred, err.long_ty_path()),
1815 );
1816 let self_ty_str = self.tcx.short_string(old_pred.self_ty(), err.long_ty_path());
1817 let trait_path = self
1818 .tcx
1819 .short_string(old_pred.print_modifiers_and_trait_path(), err.long_ty_path());
1820
1821 if has_custom_message {
1822 let msg = if is_sized {
1823 "the trait bound `Sized` is not satisfied".into()
1824 } else {
1825 msg
1826 };
1827 err.note(msg);
1828 } else {
1829 err.messages = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(rustc_errors::DiagMessage::from(msg), Style::NoStyle)]))vec![(rustc_errors::DiagMessage::from(msg), Style::NoStyle)];
1830 }
1831 if is_sized {
1832 err.span_label(
1833 span,
1834 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait `Sized` is not implemented for `{0}`",
self_ty_str))
})format!("the trait `Sized` is not implemented for `{self_ty_str}`"),
1835 );
1836 } else {
1837 err.span_label(
1838 span,
1839 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the trait `{0}` is not implemented for `{1}`",
trait_path, self_ty_str))
})format!("the trait `{trait_path}` is not implemented for `{self_ty_str}`"),
1840 );
1841 }
1842 };
1843
1844 let mut sugg_prefixes = ::alloc::vec::Vec::new()vec![];
1845 if is_immut {
1846 sugg_prefixes.push("&");
1847 }
1848 if is_mut {
1849 sugg_prefixes.push("&mut ");
1850 }
1851 let sugg_msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider{0} borrowing here",
if is_mut && !is_immut { " mutably" } else { "" }))
})format!(
1852 "consider{} borrowing here",
1853 if is_mut && !is_immut { " mutably" } else { "" },
1854 );
1855
1856 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1860 return false;
1861 };
1862 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1863 expr_finder.visit_expr(body.value);
1864
1865 if let Some(ty) = expr_finder.ty_result {
1866 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(ty.hir_id)
1867 && let hir::ExprKind::Path(hir::QPath::TypeRelative(_, _)) = expr.kind
1868 && ty.span == span
1869 {
1870 label();
1873 err.multipart_suggestions(
1874 sugg_msg,
1875 sugg_prefixes.into_iter().map(|sugg_prefix| {
1876 ::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!("<{0}", sugg_prefix))
})), (span.shrink_to_hi(), ">".to_string())]))vec![
1877 (span.shrink_to_lo(), format!("<{sugg_prefix}")),
1878 (span.shrink_to_hi(), ">".to_string()),
1879 ]
1880 }),
1881 Applicability::MaybeIncorrect,
1882 );
1883 return true;
1884 }
1885 return false;
1886 }
1887 let Some(expr) = expr_finder.result else {
1888 return false;
1889 };
1890 if let hir::ExprKind::AddrOf(_, _, _) = expr.kind {
1891 return false;
1892 }
1893 let old_self_ty = old_pred.skip_binder().self_ty();
1894 if !old_self_ty.has_escaping_bound_vars()
1895 && !self.where_clause_expr_matches_failed_self_ty(
1896 obligation,
1897 self.tcx.instantiate_bound_regions_with_erased(old_pred.self_ty()),
1898 )
1899 {
1900 return false;
1901 }
1902 let needs_parens_post = expr_needs_parens(expr);
1903 let needs_parens_pre = match self.tcx.parent_hir_node(expr.hir_id) {
1904 Node::Expr(e)
1905 if let hir::ExprKind::MethodCall(_, base, _, _) = e.kind
1906 && base.hir_id == expr.hir_id =>
1907 {
1908 true
1909 }
1910 _ => false,
1911 };
1912
1913 label();
1914 let suggestions = sugg_prefixes.into_iter().map(|sugg_prefix| {
1915 match (needs_parens_pre, needs_parens_post) {
1916 (false, false) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), sugg_prefix.to_string())]))vec![(span.shrink_to_lo(), sugg_prefix.to_string())],
1917 (false, true) => ::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!("{0}(", sugg_prefix))
})), (span.shrink_to_hi(), ")".to_string())]))vec![
1920 (span.shrink_to_lo(), format!("{sugg_prefix}(")),
1921 (span.shrink_to_hi(), ")".to_string()),
1922 ],
1923 (true, false) => ::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!("({0}", sugg_prefix))
})), (span.shrink_to_hi(), ")".to_string())]))vec![
1926 (span.shrink_to_lo(), format!("({sugg_prefix}")),
1927 (span.shrink_to_hi(), ")".to_string()),
1928 ],
1929 (true, true) => ::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!("({0}(", sugg_prefix))
})), (span.shrink_to_hi(), "))".to_string())]))vec![
1930 (span.shrink_to_lo(), format!("({sugg_prefix}(")),
1931 (span.shrink_to_hi(), "))".to_string()),
1932 ],
1933 }
1934 });
1935 err.multipart_suggestions(sugg_msg, suggestions, Applicability::MaybeIncorrect);
1936 return true;
1937 };
1938
1939 if let ObligationCauseCode::ImplDerived(cause) = &*code {
1940 try_borrowing(cause.derived.parent_trait_pred, &[])
1941 } else if let ObligationCauseCode::WhereClause(..)
1942 | ObligationCauseCode::WhereClauseInExpr(..) = code
1943 {
1944 try_borrowing(poly_trait_pred, &never_suggest_borrow)
1945 } else {
1946 false
1947 }
1948 }
1949
1950 pub(super) fn suggest_borrowing_for_object_cast(
1952 &self,
1953 err: &mut Diag<'_>,
1954 obligation: &PredicateObligation<'tcx>,
1955 self_ty: Ty<'tcx>,
1956 target_ty: Ty<'tcx>,
1957 ) {
1958 let ty::Ref(_, object_ty, hir::Mutability::Not) = target_ty.kind() else {
1959 return;
1960 };
1961 let ty::Dynamic(predicates, _) = object_ty.kind() else {
1962 return;
1963 };
1964 let self_ref_ty = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, self_ty);
1965
1966 for predicate in predicates.iter() {
1967 if !self.predicate_must_hold_modulo_regions(
1968 &obligation.with(self.tcx, predicate.with_self_ty(self.tcx, self_ref_ty)),
1969 ) {
1970 return;
1971 }
1972 }
1973
1974 err.span_suggestion_verbose(
1975 obligation.cause.span.shrink_to_lo(),
1976 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider borrowing the value, since `&{0}` can be coerced into `{1}`",
self_ty, target_ty))
})format!(
1977 "consider borrowing the value, since `&{self_ty}` can be coerced into `{target_ty}`"
1978 ),
1979 "&",
1980 Applicability::MaybeIncorrect,
1981 );
1982 }
1983
1984 fn peel_expr_refs(
1989 &self,
1990 mut expr: &'tcx hir::Expr<'tcx>,
1991 mut ty: Ty<'tcx>,
1992 ) -> (Vec<PeeledRef<'tcx>>, Option<&'tcx hir::Param<'tcx>>) {
1993 let mut refs = Vec::new();
1994 'outer: loop {
1995 while let hir::ExprKind::AddrOf(_, _, borrowed) = expr.kind {
1996 let span =
1997 if let Some(borrowed_span) = borrowed.span.find_ancestor_inside(expr.span) {
1998 expr.span.until(borrowed_span)
1999 } else {
2000 break 'outer;
2001 };
2002
2003 let span = match self.tcx.sess.source_map().span_to_snippet(span) {
2009 Ok(ref snippet) if snippet.starts_with("&") => span,
2010 Ok(ref snippet) if let Some(amp) = snippet.find('&') => {
2011 span.with_lo(span.lo() + BytePos(amp as u32))
2012 }
2013 _ => break 'outer,
2014 };
2015
2016 let ty::Ref(_, inner_ty, _) = ty.kind() else {
2017 break 'outer;
2018 };
2019 ty = *inner_ty;
2020 refs.push(PeeledRef { span, peeled_ty: ty });
2021 expr = borrowed;
2022 }
2023 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
2024 && let Res::Local(hir_id) = path.res
2025 && let hir::Node::Pat(binding) = self.tcx.hir_node(hir_id)
2026 {
2027 match self.tcx.parent_hir_node(binding.hir_id) {
2028 hir::Node::LetStmt(local)
2030 if local.ty.is_none()
2031 && let Some(init) = local.init =>
2032 {
2033 expr = init;
2034 continue;
2035 }
2036 hir::Node::LetStmt(local)
2039 if #[allow(non_exhaustive_omitted_patterns)] match local.source {
hir::LocalSource::AsyncFn => true,
_ => false,
}matches!(local.source, hir::LocalSource::AsyncFn)
2040 && let Some(init) = local.init
2041 && let hir::ExprKind::Path(hir::QPath::Resolved(None, arg_path)) =
2042 init.kind
2043 && let Res::Local(arg_hir_id) = arg_path.res
2044 && let hir::Node::Pat(arg_binding) = self.tcx.hir_node(arg_hir_id)
2045 && let hir::Node::Param(param) =
2046 self.tcx.parent_hir_node(arg_binding.hir_id) =>
2047 {
2048 return (refs, Some(param));
2049 }
2050 hir::Node::Param(param) => {
2052 return (refs, Some(param));
2053 }
2054 _ => break 'outer,
2055 }
2056 } else {
2057 break 'outer;
2058 }
2059 }
2060 (refs, None)
2061 }
2062
2063 pub(super) fn suggest_remove_reference(
2066 &self,
2067 obligation: &PredicateObligation<'tcx>,
2068 err: &mut Diag<'_>,
2069 trait_pred: ty::PolyTraitPredicate<'tcx>,
2070 ) -> bool {
2071 let mut span = obligation.cause.span;
2072 let mut trait_pred = trait_pred;
2073 let mut code = obligation.cause.code();
2074 while let Some((c, Some(parent_trait_pred))) = code.parent_with_predicate() {
2075 code = c;
2078 trait_pred = parent_trait_pred;
2079 }
2080 while span.desugaring_kind().is_some() {
2081 span.remove_mark();
2083 }
2084 let mut expr_finder = super::FindExprBySpan::new(span, self.tcx);
2085 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
2086 return false;
2087 };
2088 expr_finder.visit_expr(body.value);
2089 let mut maybe_suggest = |suggested_ty, count, suggestions| {
2090 let trait_pred_and_suggested_ty =
2092 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
2093
2094 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
2095 obligation.param_env,
2096 trait_pred_and_suggested_ty,
2097 );
2098
2099 if self.predicate_may_hold(&new_obligation) {
2100 let msg = if count == 1 {
2101 "consider removing the leading `&`-reference".to_string()
2102 } else {
2103 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider removing {0} leading `&`-references",
count))
})format!("consider removing {count} leading `&`-references")
2104 };
2105
2106 err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable);
2107 true
2108 } else {
2109 false
2110 }
2111 };
2112
2113 let mut count = 0;
2116 let mut suggestions = ::alloc::vec::Vec::new()vec![];
2117 let mut suggested_ty = trait_pred.self_ty().skip_binder();
2119 if let Some(mut hir_ty) = expr_finder.ty_result {
2120 while let hir::TyKind::Ref(_, mut_ty) = &hir_ty.kind {
2121 count += 1;
2122 let span = hir_ty.span.until(mut_ty.ty.span);
2123 suggestions.push((span, String::new()));
2124
2125 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
2126 break;
2127 };
2128 suggested_ty = *inner_ty;
2129
2130 hir_ty = mut_ty.ty;
2131
2132 if maybe_suggest(suggested_ty, count, suggestions.clone()) {
2133 return true;
2134 }
2135 }
2136 }
2137
2138 let Some(expr) = expr_finder.result else {
2140 return false;
2141 };
2142 let suggested_ty = trait_pred.self_ty().skip_binder();
2144 let (peeled_refs, _) = self.peel_expr_refs(expr, suggested_ty);
2145 for (i, peeled) in peeled_refs.iter().enumerate() {
2146 let suggestions: Vec<_> =
2147 peeled_refs[..=i].iter().map(|r| (r.span, String::new())).collect();
2148 if maybe_suggest(peeled.peeled_ty, i + 1, suggestions) {
2149 return true;
2150 }
2151 }
2152 false
2153 }
2154
2155 fn suggest_remove_ref_from_param(&self, param: &hir::Param<'_>, err: &mut Diag<'_>) -> bool {
2157 if let Some(decl) = self.tcx.parent_hir_node(param.hir_id).fn_decl()
2158 && let Some(input_ty) = decl.inputs.iter().find(|t| param.ty_span.contains(t.span))
2159 && let hir::TyKind::Ref(_, mut_ty) = input_ty.kind
2160 {
2161 let ref_span = input_ty.span.until(mut_ty.ty.span);
2162 match self.tcx.sess.source_map().span_to_snippet(ref_span) {
2163 Ok(snippet) if snippet.starts_with("&") => {
2164 err.span_suggestion_verbose(
2165 ref_span,
2166 "consider removing the `&` from the parameter type",
2167 "",
2168 Applicability::MaybeIncorrect,
2169 );
2170 return true;
2171 }
2172 _ => {}
2173 }
2174 }
2175 false
2176 }
2177
2178 pub(super) fn suggest_remove_await(
2179 &self,
2180 obligation: &PredicateObligation<'tcx>,
2181 err: &mut Diag<'_>,
2182 ) {
2183 if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives()
2184 && let hir::Node::Expr(expr) = self.tcx.hir_node(*hir_id)
2185 {
2186 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
2194 obligation.predicate.kind().skip_binder()
2195 {
2196 let self_ty = pred.self_ty();
2197 let future_trait =
2198 self.tcx.require_lang_item(LangItem::Future, obligation.cause.span);
2199
2200 let has_future = {
2202 let mut ty = self_ty;
2203 loop {
2204 match *ty.kind() {
2205 ty::Ref(_, inner_ty, _)
2206 if !#[allow(non_exhaustive_omitted_patterns)] match inner_ty.kind() {
ty::Dynamic(..) => true,
_ => false,
}matches!(inner_ty.kind(), ty::Dynamic(..)) =>
2207 {
2208 if self
2209 .type_implements_trait(
2210 future_trait,
2211 [inner_ty],
2212 obligation.param_env,
2213 )
2214 .must_apply_modulo_regions()
2215 {
2216 break true;
2217 }
2218 ty = inner_ty;
2219 }
2220 _ => break false,
2221 }
2222 }
2223 };
2224
2225 if has_future {
2226 let (peeled_refs, terminal_param) = self.peel_expr_refs(expr, self_ty);
2227
2228 for (i, peeled) in peeled_refs.iter().enumerate() {
2230 if self
2231 .type_implements_trait(
2232 future_trait,
2233 [peeled.peeled_ty],
2234 obligation.param_env,
2235 )
2236 .must_apply_modulo_regions()
2237 {
2238 let count = i + 1;
2239 let msg = if count == 1 {
2240 "consider removing the leading `&`-reference".to_string()
2241 } else {
2242 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider removing {0} leading `&`-references",
count))
})format!("consider removing {count} leading `&`-references")
2243 };
2244 let suggestions: Vec<_> =
2245 peeled_refs[..=i].iter().map(|r| (r.span, String::new())).collect();
2246 err.multipart_suggestion(
2247 msg,
2248 suggestions,
2249 Applicability::MachineApplicable,
2250 );
2251 return;
2252 }
2253 }
2254
2255 if peeled_refs.is_empty()
2259 && let Some(param) = terminal_param
2260 && self.suggest_remove_ref_from_param(param, err)
2261 {
2262 return;
2263 }
2264
2265 err.help(
2267 "a reference to a future is not a future; \
2268 consider removing the leading `&`-reference",
2269 );
2270 return;
2271 }
2272 }
2273
2274 if let Some((_, hir::Node::Expr(await_expr))) = self.tcx.hir_parent_iter(*hir_id).nth(1)
2276 && let Some(expr_span) = expr.span.find_ancestor_inside_same_ctxt(await_expr.span)
2277 {
2278 let removal_span = self
2279 .tcx
2280 .sess
2281 .source_map()
2282 .span_extend_while_whitespace(expr_span)
2283 .shrink_to_hi()
2284 .to(await_expr.span.shrink_to_hi());
2285 err.span_suggestion_verbose(
2286 removal_span,
2287 "remove the `.await`",
2288 "",
2289 Applicability::MachineApplicable,
2290 );
2291 } else {
2292 err.span_label(obligation.cause.span, "remove the `.await`");
2293 }
2294 if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
2296 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
2297 obligation.predicate.kind().skip_binder()
2298 {
2299 err.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this call returns `{0}`",
pred.self_ty()))
})format!("this call returns `{}`", pred.self_ty()));
2300 }
2301 if let Some(typeck_results) = &self.typeck_results
2302 && let ty = typeck_results.expr_ty_adjusted(base)
2303 && let ty::FnDef(def_id, _args) = ty.kind()
2304 && let Some(hir::Node::Item(item)) = self.tcx.hir_get_if_local(*def_id)
2305 {
2306 let (ident, _, _, _) = item.expect_fn();
2307 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("alternatively, consider making `fn {0}` asynchronous",
ident))
})format!("alternatively, consider making `fn {ident}` asynchronous");
2308 if item.vis_span.is_empty() {
2309 err.span_suggestion_verbose(
2310 item.span.shrink_to_lo(),
2311 msg,
2312 "async ",
2313 Applicability::MaybeIncorrect,
2314 );
2315 } else {
2316 err.span_suggestion_verbose(
2317 item.vis_span.shrink_to_hi(),
2318 msg,
2319 " async",
2320 Applicability::MaybeIncorrect,
2321 );
2322 }
2323 }
2324 }
2325 }
2326 }
2327
2328 pub(super) fn suggest_change_mut(
2331 &self,
2332 obligation: &PredicateObligation<'tcx>,
2333 err: &mut Diag<'_>,
2334 trait_pred: ty::PolyTraitPredicate<'tcx>,
2335 ) {
2336 let points_at_arg =
2337 #[allow(non_exhaustive_omitted_patterns)] match obligation.cause.code() {
ObligationCauseCode::FunctionArg { .. } => true,
_ => false,
}matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. },);
2338
2339 let span = obligation.cause.span;
2340 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
2341 let refs_number =
2342 snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
2343 if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
2344 return;
2346 }
2347 let trait_pred = self.resolve_vars_if_possible(trait_pred);
2348 if trait_pred.has_non_region_infer() {
2349 return;
2352 }
2353
2354 if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
2356 {
2357 let suggested_ty = match mutability {
2358 hir::Mutability::Mut => Ty::new_imm_ref(self.tcx, region, t_type),
2359 hir::Mutability::Not => Ty::new_mut_ref(self.tcx, region, t_type),
2360 };
2361
2362 let trait_pred_and_suggested_ty =
2364 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
2365
2366 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
2367 obligation.param_env,
2368 trait_pred_and_suggested_ty,
2369 );
2370 let suggested_ty_would_satisfy_obligation = self
2371 .evaluate_obligation_no_overflow(&new_obligation)
2372 .must_apply_modulo_regions();
2373 if suggested_ty_would_satisfy_obligation {
2374 let sp = self
2375 .tcx
2376 .sess
2377 .source_map()
2378 .span_take_while(span, |c| c.is_whitespace() || *c == '&');
2379 if points_at_arg && mutability.is_not() && refs_number > 0 {
2380 if snippet
2382 .trim_start_matches(|c: char| c.is_whitespace() || c == '&')
2383 .starts_with("mut")
2384 {
2385 return;
2386 }
2387 err.span_suggestion_verbose(
2388 sp,
2389 "consider changing this borrow's mutability",
2390 "&mut ",
2391 Applicability::MachineApplicable,
2392 );
2393 } else {
2394 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is implemented for `{1}`, but not for `{2}`",
trait_pred.print_modifiers_and_trait_path(), suggested_ty,
trait_pred.skip_binder().self_ty()))
})format!(
2395 "`{}` is implemented for `{}`, but not for `{}`",
2396 trait_pred.print_modifiers_and_trait_path(),
2397 suggested_ty,
2398 trait_pred.skip_binder().self_ty(),
2399 ));
2400 }
2401 }
2402 }
2403 }
2404 }
2405
2406 pub(super) fn suggest_semicolon_removal(
2407 &self,
2408 obligation: &PredicateObligation<'tcx>,
2409 err: &mut Diag<'_>,
2410 span: Span,
2411 trait_pred: ty::PolyTraitPredicate<'tcx>,
2412 ) -> bool {
2413 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
2414 if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn {sig, body: body_id, .. }, .. }) = node
2415 && let hir::ExprKind::Block(blk, _) = &self.tcx.hir_body(*body_id).value.kind
2416 && sig.decl.output.span().overlaps(span)
2417 && blk.expr.is_none()
2418 && trait_pred.self_ty().skip_binder().is_unit()
2419 && let Some(stmt) = blk.stmts.last()
2420 && let hir::StmtKind::Semi(expr) = stmt.kind
2421 && let Some(typeck_results) = &self.typeck_results
2423 && let Some(ty) = typeck_results.expr_ty_opt(expr)
2424 && self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(
2425 obligation.param_env, trait_pred.map_bound(|trait_pred| (trait_pred, ty))
2426 ))
2427 {
2428 err.span_label(
2429 expr.span,
2430 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this expression has type `{0}`, which implements `{1}`",
ty, trait_pred.print_modifiers_and_trait_path()))
})format!(
2431 "this expression has type `{}`, which implements `{}`",
2432 ty,
2433 trait_pred.print_modifiers_and_trait_path()
2434 ),
2435 );
2436 err.span_suggestion(
2437 self.tcx.sess.source_map().end_point(stmt.span),
2438 "remove this semicolon",
2439 "",
2440 Applicability::MachineApplicable,
2441 );
2442 return true;
2443 }
2444 false
2445 }
2446
2447 pub(super) fn suggest_borrow_for_unsized_closure_return<G: EmissionGuarantee>(
2448 &self,
2449 body_id: LocalDefId,
2450 err: &mut Diag<'_, G>,
2451 predicate: ty::Predicate<'tcx>,
2452 ) {
2453 let Some(pred) = predicate.as_trait_clause() else {
2454 return;
2455 };
2456 if !self.tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
2457 return;
2458 }
2459
2460 let Some(span) = err.span.primary_span() else {
2461 return;
2462 };
2463 let Some(node_body_id) = self.tcx.hir_node_by_def_id(body_id).body_id() else {
2464 return;
2465 };
2466 let body = self.tcx.hir_body(node_body_id);
2467 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
2468 expr_finder.visit_expr(body.value);
2469 let Some(expr) = expr_finder.result else {
2470 return;
2471 };
2472
2473 let closure = match expr.kind {
2474 hir::ExprKind::Call(_, args) => args.iter().find_map(|arg| match arg.kind {
2475 hir::ExprKind::Closure(closure) => Some(closure),
2476 _ => None,
2477 }),
2478 hir::ExprKind::MethodCall(_, _, args, _) => {
2479 args.iter().find_map(|arg| match arg.kind {
2480 hir::ExprKind::Closure(closure) => Some(closure),
2481 _ => None,
2482 })
2483 }
2484 _ => None,
2485 };
2486 let Some(closure) = closure else {
2487 return;
2488 };
2489 if !#[allow(non_exhaustive_omitted_patterns)] match closure.fn_decl.output {
hir::FnRetTy::DefaultReturn(_) => true,
_ => false,
}matches!(closure.fn_decl.output, hir::FnRetTy::DefaultReturn(_)) {
2490 return;
2491 }
2492
2493 err.span_suggestion_verbose(
2494 self.tcx.hir_body(closure.body).value.span.shrink_to_lo(),
2495 "consider borrowing the value",
2496 "&",
2497 Applicability::MaybeIncorrect,
2498 );
2499 }
2500
2501 pub(super) fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
2502 let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig, .. }, .. }) =
2503 self.tcx.hir_node_by_def_id(obligation.cause.body_id)
2504 else {
2505 return None;
2506 };
2507
2508 if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
2509 }
2510
2511 pub(super) fn suggest_impl_trait(
2515 &self,
2516 err: &mut Diag<'_>,
2517 obligation: &PredicateObligation<'tcx>,
2518 trait_pred: ty::PolyTraitPredicate<'tcx>,
2519 ) -> bool {
2520 let ObligationCauseCode::SizedReturnType = obligation.cause.code() else {
2521 return false;
2522 };
2523 let ty::Dynamic(_, _) = trait_pred.self_ty().skip_binder().kind() else {
2524 return false;
2525 };
2526 if let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
2527 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
2528 | Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, _), .. }) =
2529 self.tcx.hir_node_by_def_id(obligation.cause.body_id)
2530 && let hir::FnRetTy::Return(ty) = fn_sig.decl.output
2531 && let hir::TyKind::Path(qpath) = ty.kind
2532 && let hir::QPath::Resolved(None, path) = qpath
2533 && let Res::Def(DefKind::TyAlias, def_id) = path.res
2534 {
2535 err.span_note(self.tcx.def_span(def_id), "this type alias is unsized");
2539 err.multipart_suggestion(
2540 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider boxing the return type, and wrapping all of the returned values in `Box::new`"))
})format!(
2541 "consider boxing the return type, and wrapping all of the returned values in \
2542 `Box::new`",
2543 ),
2544 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ty.span.shrink_to_lo(), "Box<".to_string()),
(ty.span.shrink_to_hi(), ">".to_string())]))vec![
2545 (ty.span.shrink_to_lo(), "Box<".to_string()),
2546 (ty.span.shrink_to_hi(), ">".to_string()),
2547 ],
2548 Applicability::MaybeIncorrect,
2549 );
2550 return false;
2551 }
2552
2553 err.code(E0746);
2554 err.primary_message("return type cannot be a trait object without pointer indirection");
2555 err.children.clear();
2556
2557 let mut span = obligation.cause.span;
2558 if let DefKind::Closure = self.tcx.def_kind(obligation.cause.body_id)
2559 && let parent = self.tcx.local_parent(obligation.cause.body_id)
2560 && let DefKind::Fn | DefKind::AssocFn = self.tcx.def_kind(parent)
2561 && self.tcx.asyncness(parent).is_async()
2562 && let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
2563 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
2564 | Node::TraitItem(hir::TraitItem {
2565 kind: hir::TraitItemKind::Fn(fn_sig, _), ..
2566 }) = self.tcx.hir_node_by_def_id(parent)
2567 {
2568 span = fn_sig.decl.output.span();
2573 err.span(span);
2574 }
2575 let body = self.tcx.hir_body_owned_by(obligation.cause.body_id);
2576
2577 let mut visitor = ReturnsVisitor::default();
2578 visitor.visit_body(&body);
2579
2580 let (pre, impl_span) = if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
2581 && snip.starts_with("dyn ")
2582 {
2583 ("", span.with_hi(span.lo() + BytePos(4)))
2584 } else {
2585 ("dyn ", span.shrink_to_lo())
2586 };
2587
2588 err.span_suggestion_verbose(
2589 impl_span,
2590 "consider returning an `impl Trait` instead of a `dyn Trait`",
2591 "impl ",
2592 Applicability::MaybeIncorrect,
2593 );
2594
2595 let mut sugg = ::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!("Box<{0}", pre))
})), (span.shrink_to_hi(), ">".to_string())]))vec![
2596 (span.shrink_to_lo(), format!("Box<{pre}")),
2597 (span.shrink_to_hi(), ">".to_string()),
2598 ];
2599 sugg.extend(visitor.returns.into_iter().flat_map(|expr| {
2600 let span =
2601 expr.span.find_ancestor_in_same_ctxt(obligation.cause.span).unwrap_or(expr.span);
2602 if !span.can_be_used_for_suggestions() {
2603 ::alloc::vec::Vec::new()vec![]
2604 } else if let hir::ExprKind::Call(path, ..) = expr.kind
2605 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, method)) = path.kind
2606 && method.ident.name == sym::new
2607 && let hir::TyKind::Path(hir::QPath::Resolved(.., box_path)) = ty.kind
2608 && box_path
2609 .res
2610 .opt_def_id()
2611 .is_some_and(|def_id| self.tcx.is_lang_item(def_id, LangItem::OwnedBox))
2612 {
2613 ::alloc::vec::Vec::new()vec![]
2615 } else {
2616 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "Box::new(".to_string()),
(span.shrink_to_hi(), ")".to_string())]))vec![
2617 (span.shrink_to_lo(), "Box::new(".to_string()),
2618 (span.shrink_to_hi(), ")".to_string()),
2619 ]
2620 }
2621 }));
2622
2623 err.multipart_suggestion(
2624 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("alternatively, box the return type, and wrap all of the returned values in `Box::new`"))
})format!(
2625 "alternatively, box the return type, and wrap all of the returned values in \
2626 `Box::new`",
2627 ),
2628 sugg,
2629 Applicability::MaybeIncorrect,
2630 );
2631
2632 true
2633 }
2634
2635 pub(super) fn report_closure_arg_mismatch(
2636 &self,
2637 span: Span,
2638 found_span: Option<Span>,
2639 found: ty::TraitRef<'tcx>,
2640 expected: ty::TraitRef<'tcx>,
2641 cause: &ObligationCauseCode<'tcx>,
2642 found_node: Option<Node<'_>>,
2643 param_env: ty::ParamEnv<'tcx>,
2644 ) -> Diag<'a> {
2645 pub(crate) fn build_fn_sig_ty<'tcx>(
2646 infcx: &InferCtxt<'tcx>,
2647 trait_ref: ty::TraitRef<'tcx>,
2648 ) -> Ty<'tcx> {
2649 let inputs = trait_ref.args.type_at(1);
2650 let sig = match inputs.kind() {
2651 ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id) => {
2652 infcx.tcx.mk_fn_sig_safe_rust_abi(*inputs, infcx.next_ty_var(DUMMY_SP))
2653 }
2654 _ => infcx.tcx.mk_fn_sig_safe_rust_abi([inputs], infcx.next_ty_var(DUMMY_SP)),
2655 };
2656
2657 Ty::new_fn_ptr(infcx.tcx, ty::Binder::dummy(sig))
2658 }
2659
2660 let argument_kind = match expected.self_ty().kind() {
2661 ty::Closure(..) => "closure",
2662 ty::Coroutine(..) => "coroutine",
2663 _ => "function",
2664 };
2665 let mut err = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type mismatch in {0} arguments",
argument_kind))
})).with_code(E0631)
}struct_span_code_err!(
2666 self.dcx(),
2667 span,
2668 E0631,
2669 "type mismatch in {argument_kind} arguments",
2670 );
2671
2672 err.span_label(span, "expected due to this");
2673
2674 let found_span = found_span.unwrap_or(span);
2675 err.span_label(found_span, "found signature defined here");
2676
2677 let expected = build_fn_sig_ty(self, expected);
2678 let found = build_fn_sig_ty(self, found);
2679
2680 let (expected_str, found_str) = self.cmp(expected, found);
2681
2682 let signature_kind = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} signature", argument_kind))
})format!("{argument_kind} signature");
2683 err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
2684
2685 self.note_conflicting_fn_args(&mut err, cause, expected, found, param_env);
2686 self.note_conflicting_closure_bounds(cause, &mut err);
2687
2688 if let Some(found_node) = found_node {
2689 hint_missing_borrow(self, param_env, span, found, expected, found_node, &mut err);
2690 }
2691
2692 err
2693 }
2694
2695 fn note_conflicting_fn_args(
2696 &self,
2697 err: &mut Diag<'_>,
2698 cause: &ObligationCauseCode<'tcx>,
2699 expected: Ty<'tcx>,
2700 found: Ty<'tcx>,
2701 param_env: ty::ParamEnv<'tcx>,
2702 ) {
2703 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = cause else {
2704 return;
2705 };
2706 let ty::FnPtr(sig_tys, hdr) = expected.kind() else {
2707 return;
2708 };
2709 let expected = sig_tys.with(*hdr);
2710 let ty::FnPtr(sig_tys, hdr) = found.kind() else {
2711 return;
2712 };
2713 let found = sig_tys.with(*hdr);
2714 let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id) else {
2715 return;
2716 };
2717 let hir::ExprKind::Path(path) = arg.kind else {
2718 return;
2719 };
2720 let expected_inputs = self.tcx.instantiate_bound_regions_with_erased(expected).inputs();
2721 let found_inputs = self.tcx.instantiate_bound_regions_with_erased(found).inputs();
2722 let both_tys = expected_inputs.iter().copied().zip(found_inputs.iter().copied());
2723
2724 let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| {
2725 let (expected_ty, expected_refs) = get_deref_type_and_refs(expected);
2726 let (found_ty, found_refs) = get_deref_type_and_refs(found);
2727
2728 if infcx.can_eq(param_env, found_ty, expected_ty) {
2729 if found_refs.len() == expected_refs.len()
2730 && found_refs.iter().eq(expected_refs.iter())
2731 {
2732 name
2733 } else if found_refs.len() > expected_refs.len() {
2734 let refs = &found_refs[..found_refs.len() - expected_refs.len()];
2735 if found_refs[..expected_refs.len()].iter().eq(expected_refs.iter()) {
2736 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}",
refs.iter().map(|mutbl|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}",
mutbl.prefix_str()))
})).collect::<Vec<_>>().join(""), name))
})format!(
2737 "{}{name}",
2738 refs.iter()
2739 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2740 .collect::<Vec<_>>()
2741 .join(""),
2742 )
2743 } else {
2744 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}*{1}",
refs.iter().map(|mutbl|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}",
mutbl.prefix_str()))
})).collect::<Vec<_>>().join(""), name))
})format!(
2746 "{}*{name}",
2747 refs.iter()
2748 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2749 .collect::<Vec<_>>()
2750 .join(""),
2751 )
2752 }
2753 } else if expected_refs.len() > found_refs.len() {
2754 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}",
(0..(expected_refs.len() -
found_refs.len())).map(|_|
"*").collect::<Vec<_>>().join(""), name))
})format!(
2755 "{}{name}",
2756 (0..(expected_refs.len() - found_refs.len()))
2757 .map(|_| "*")
2758 .collect::<Vec<_>>()
2759 .join(""),
2760 )
2761 } else {
2762 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}",
found_refs.iter().map(|mutbl|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}",
mutbl.prefix_str()))
})).chain(found_refs.iter().map(|_|
"*".to_string())).collect::<Vec<_>>().join(""), name))
})format!(
2763 "{}{name}",
2764 found_refs
2765 .iter()
2766 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2767 .chain(found_refs.iter().map(|_| "*".to_string()))
2768 .collect::<Vec<_>>()
2769 .join(""),
2770 )
2771 }
2772 } else {
2773 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", found))
})format!("/* {found} */")
2774 }
2775 };
2776 let args_have_same_underlying_type = both_tys.clone().all(|(expected, found)| {
2777 let (expected_ty, _) = get_deref_type_and_refs(expected);
2778 let (found_ty, _) = get_deref_type_and_refs(found);
2779 self.can_eq(param_env, found_ty, expected_ty)
2780 });
2781 let (closure_names, call_names): (Vec<_>, Vec<_>) = if args_have_same_underlying_type
2782 && !expected_inputs.is_empty()
2783 && expected_inputs.len() == found_inputs.len()
2784 && let Some(typeck) = &self.typeck_results
2785 && let Res::Def(res_kind, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2786 && res_kind.is_fn_like()
2787 {
2788 let closure: Vec<_> = self
2789 .tcx
2790 .fn_arg_idents(fn_def_id)
2791 .iter()
2792 .enumerate()
2793 .map(|(i, ident)| {
2794 if let Some(ident) = ident
2795 && !#[allow(non_exhaustive_omitted_patterns)] match ident {
Ident { name: kw::Underscore | kw::SelfLower, .. } => true,
_ => false,
}matches!(ident, Ident { name: kw::Underscore | kw::SelfLower, .. })
2796 {
2797 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", ident))
})format!("{ident}")
2798 } else {
2799 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("arg{0}", i))
})format!("arg{i}")
2800 }
2801 })
2802 .collect();
2803 let args = closure
2804 .iter()
2805 .zip(both_tys)
2806 .map(|(name, (expected, found))| {
2807 arg_expr(self.infcx, name.to_owned(), expected, found)
2808 })
2809 .collect();
2810 (closure, args)
2811 } else {
2812 let closure_args = expected_inputs
2813 .iter()
2814 .enumerate()
2815 .map(|(i, _)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("arg{0}", i))
})format!("arg{i}"))
2816 .collect::<Vec<_>>();
2817 let call_args = both_tys
2818 .enumerate()
2819 .map(|(i, (expected, found))| {
2820 arg_expr(self.infcx, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("arg{0}", i))
})format!("arg{i}"), expected, found)
2821 })
2822 .collect::<Vec<_>>();
2823 (closure_args, call_args)
2824 };
2825 let closure_names: Vec<_> = closure_names
2826 .into_iter()
2827 .zip(expected_inputs.iter())
2828 .map(|(name, ty)| {
2829 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}",
if ty.has_infer_types() {
String::new()
} else if ty.references_error() {
": /* type */".to_string()
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": {0}", ty))
})
}, name))
})format!(
2830 "{name}{}",
2831 if ty.has_infer_types() {
2832 String::new()
2833 } else if ty.references_error() {
2834 ": /* type */".to_string()
2835 } else {
2836 format!(": {ty}")
2837 }
2838 )
2839 })
2840 .collect();
2841 err.multipart_suggestion(
2842 "consider wrapping the function in a closure",
2843 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(arg.span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("|{0}| ",
closure_names.join(", ")))
})),
(arg.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})",
call_names.join(", ")))
}))]))vec![
2844 (arg.span.shrink_to_lo(), format!("|{}| ", closure_names.join(", "))),
2845 (arg.span.shrink_to_hi(), format!("({})", call_names.join(", "))),
2846 ],
2847 Applicability::MaybeIncorrect,
2848 );
2849 }
2850
2851 fn note_conflicting_closure_bounds(
2854 &self,
2855 cause: &ObligationCauseCode<'tcx>,
2856 err: &mut Diag<'_>,
2857 ) {
2858 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = *cause
2862 && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
2863 && let Some(pred) = predicates.predicates.get(idx).map(|p| p.as_ref().skip_norm_wip())
2864 && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()
2865 && self.tcx.is_fn_trait(trait_pred.def_id())
2866 {
2867 let expected_self =
2868 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.self_ty()));
2869 let expected_args =
2870 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.trait_ref.args));
2871
2872 let other_pred = predicates.into_iter().enumerate().find(|&(other_idx, (pred, _))| {
2875 let pred = pred.skip_norm_wip();
2876 match pred.kind().skip_binder() {
2877 ty::ClauseKind::Trait(trait_pred)
2878 if self.tcx.is_fn_trait(trait_pred.def_id())
2879 && other_idx != idx
2880 && expected_self
2883 == self.tcx.anonymize_bound_vars(
2884 pred.kind().rebind(trait_pred.self_ty()),
2885 )
2886 && expected_args
2888 != self.tcx.anonymize_bound_vars(
2889 pred.kind().rebind(trait_pred.trait_ref.args),
2890 ) =>
2891 {
2892 true
2893 }
2894 _ => false,
2895 }
2896 });
2897 if let Some((_, (_, other_pred_span))) = other_pred {
2899 err.span_note(
2900 other_pred_span,
2901 "closure inferred to have a different signature due to this bound",
2902 );
2903 }
2904 }
2905 }
2906
2907 pub(super) fn suggest_fully_qualified_path(
2908 &self,
2909 err: &mut Diag<'_>,
2910 item_def_id: DefId,
2911 span: Span,
2912 trait_ref: DefId,
2913 ) {
2914 if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id)
2915 && let ty::AssocKind::Const { .. } | ty::AssocKind::Type { .. } = assoc_item.kind
2916 {
2917 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}s cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`",
self.tcx.def_kind_descr(assoc_item.as_def_kind(),
item_def_id)))
})format!(
2918 "{}s cannot be accessed directly on a `trait`, they can only be \
2919 accessed through a specific `impl`",
2920 self.tcx.def_kind_descr(assoc_item.as_def_kind(), item_def_id)
2921 ));
2922
2923 if !assoc_item.is_impl_trait_in_trait() {
2924 err.span_suggestion_verbose(
2925 span,
2926 "use the fully qualified path to an implementation",
2927 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<Type as {0}>::{1}",
self.tcx.def_path_str(trait_ref), assoc_item.name()))
})format!(
2928 "<Type as {}>::{}",
2929 self.tcx.def_path_str(trait_ref),
2930 assoc_item.name()
2931 ),
2932 Applicability::HasPlaceholders,
2933 );
2934 }
2935 }
2936 }
2937
2938 #[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("maybe_note_obligation_cause_for_async_await",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(2980u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["obligation.predicate",
"obligation.cause.span"],
::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(&debug(&obligation.predicate)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&obligation.cause.span)
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: bool = loop {};
return __tracing_attr_fake_return;
}
{
let (mut trait_ref, mut target_ty) =
match obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) =>
(Some(p), Some(p.self_ty())),
_ => (None, None),
};
let mut coroutine = None;
let mut outer_coroutine = None;
let mut next_code = Some(obligation.cause.code());
let mut seen_upvar_tys_infer_tuple = false;
while let Some(code) = next_code {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3019",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3019u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["code"],
::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(&debug(&code) as
&dyn Value))])
});
} else { ; }
};
match code {
ObligationCauseCode::FunctionArg { parent_code, .. } => {
next_code = Some(parent_code);
}
ObligationCauseCode::ImplDerived(cause) => {
let ty =
cause.derived.parent_trait_pred.skip_binder().self_ty();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3026",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3026u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["message",
"parent_trait_ref", "self_ty.kind"],
::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!("ImplDerived")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&cause.derived.parent_trait_pred)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty.kind())
as &dyn Value))])
});
} else { ; }
};
match *ty.kind() {
ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
coroutine = coroutine.or(Some(did));
outer_coroutine = Some(did);
}
ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
seen_upvar_tys_infer_tuple = true;
}
_ if coroutine.is_none() => {
trait_ref =
Some(cause.derived.parent_trait_pred.skip_binder());
target_ty = Some(ty);
}
_ => {}
}
next_code = Some(&cause.derived.parent_code);
}
ObligationCauseCode::WellFormedDerived(derived_obligation) |
ObligationCauseCode::BuiltinDerived(derived_obligation) => {
let ty =
derived_obligation.parent_trait_pred.skip_binder().self_ty();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3056",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3056u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["parent_trait_ref",
"self_ty.kind"],
::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(&debug(&derived_obligation.parent_trait_pred)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty.kind())
as &dyn Value))])
});
} else { ; }
};
match *ty.kind() {
ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
coroutine = coroutine.or(Some(did));
outer_coroutine = Some(did);
}
ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
seen_upvar_tys_infer_tuple = true;
}
_ if coroutine.is_none() => {
trait_ref =
Some(derived_obligation.parent_trait_pred.skip_binder());
target_ty = Some(ty);
}
_ => {}
}
next_code = Some(&derived_obligation.parent_code);
}
_ => break,
}
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3087",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3087u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["coroutine",
"trait_ref", "target_ty"],
::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(&debug(&coroutine)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&trait_ref)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&target_ty)
as &dyn Value))])
});
} else { ; }
};
let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
(coroutine, trait_ref, target_ty) else { return false; };
let span = self.tcx.def_span(coroutine_did);
let coroutine_did_root =
self.tcx.typeck_root_def_id(coroutine_did);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3097",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3097u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["coroutine_did",
"coroutine_did_root", "typeck_results.hir_owner", "span"],
::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(&debug(&coroutine_did)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&coroutine_did_root)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&self.typeck_results.as_ref().map(|t|
t.hir_owner)) as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&span) as
&dyn Value))])
});
} else { ; }
};
let coroutine_body =
coroutine_did.as_local().and_then(|def_id|
self.tcx.hir_maybe_body_owned_by(def_id));
let mut visitor = AwaitsVisitor::default();
if let Some(body) = coroutine_body { visitor.visit_body(&body); }
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3110",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3110u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["awaits"],
::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(&debug(&visitor.awaits)
as &dyn Value))])
});
} else { ; }
};
let target_ty_erased =
self.tcx.erase_and_anonymize_regions(target_ty);
let ty_matches =
|ty| -> bool
{
let ty_erased =
self.tcx.instantiate_bound_regions_with_erased(ty);
let ty_erased =
self.tcx.erase_and_anonymize_regions(ty_erased);
let eq = ty_erased == target_ty_erased;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3131",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3131u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["ty_erased",
"target_ty_erased", "eq"],
::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(&debug(&ty_erased)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&target_ty_erased)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&eq) as
&dyn Value))])
});
} else { ; }
};
eq
};
let coroutine_data =
match &self.typeck_results {
Some(t) if t.hir_owner.to_def_id() == coroutine_did_root =>
CoroutineData(t),
_ if coroutine_did.is_local() => {
CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
}
_ => return false,
};
let coroutine_within_in_progress_typeck =
match &self.typeck_results {
Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
_ => false,
};
let mut interior_or_upvar_span = None;
let from_awaited_ty =
coroutine_data.get_from_await_ty(visitor, self.tcx,
ty_matches);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3155",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3155u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["from_awaited_ty"],
::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(&debug(&from_awaited_ty)
as &dyn Value))])
});
} else { ; }
};
if coroutine_did.is_local() &&
!coroutine_within_in_progress_typeck &&
let Some(coroutine_info) =
self.tcx.mir_coroutine_witnesses(coroutine_did) {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3163",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3163u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["coroutine_info"],
::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(&debug(&coroutine_info)
as &dyn Value))])
});
} else { ; }
};
'find_source:
for (variant, source_info) in
coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3167",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3167u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["variant"],
::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(&debug(&variant) as
&dyn Value))])
});
} else { ; }
};
for &local in variant {
let decl = &coroutine_info.field_tys[local];
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3170",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3170u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["decl"],
::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(&debug(&decl) as
&dyn Value))])
});
} else { ; }
};
if ty_matches(ty::Binder::dummy(decl.ty)) &&
!decl.ignore_for_traits {
interior_or_upvar_span =
Some(CoroutineInteriorOrUpvar::Interior(decl.source_info.span,
Some((source_info.span, from_awaited_ty))));
break 'find_source;
}
}
}
}
if interior_or_upvar_span.is_none() {
interior_or_upvar_span =
coroutine_data.try_get_upvar_span(self, coroutine_did,
ty_matches);
}
if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
interior_or_upvar_span =
Some(CoroutineInteriorOrUpvar::Interior(span, None));
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3191",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3191u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["interior_or_upvar_span"],
::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(&debug(&interior_or_upvar_span)
as &dyn Value))])
});
} else { ; }
};
if let Some(interior_or_upvar_span) = interior_or_upvar_span {
let is_async = self.tcx.coroutine_is_async(coroutine_did);
self.note_obligation_cause_for_async_await(err,
interior_or_upvar_span, is_async, outer_coroutine,
trait_ref, target_ty, obligation, next_code);
true
} else { false }
}
}
}#[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
2981 pub fn maybe_note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2982 &self,
2983 err: &mut Diag<'_, G>,
2984 obligation: &PredicateObligation<'tcx>,
2985 ) -> bool {
2986 let (mut trait_ref, mut target_ty) = match obligation.predicate.kind().skip_binder() {
3009 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => (Some(p), Some(p.self_ty())),
3010 _ => (None, None),
3011 };
3012 let mut coroutine = None;
3013 let mut outer_coroutine = None;
3014 let mut next_code = Some(obligation.cause.code());
3015
3016 let mut seen_upvar_tys_infer_tuple = false;
3017
3018 while let Some(code) = next_code {
3019 debug!(?code);
3020 match code {
3021 ObligationCauseCode::FunctionArg { parent_code, .. } => {
3022 next_code = Some(parent_code);
3023 }
3024 ObligationCauseCode::ImplDerived(cause) => {
3025 let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
3026 debug!(
3027 parent_trait_ref = ?cause.derived.parent_trait_pred,
3028 self_ty.kind = ?ty.kind(),
3029 "ImplDerived",
3030 );
3031
3032 match *ty.kind() {
3033 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
3034 coroutine = coroutine.or(Some(did));
3035 outer_coroutine = Some(did);
3036 }
3037 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
3038 seen_upvar_tys_infer_tuple = true;
3043 }
3044 _ if coroutine.is_none() => {
3045 trait_ref = Some(cause.derived.parent_trait_pred.skip_binder());
3046 target_ty = Some(ty);
3047 }
3048 _ => {}
3049 }
3050
3051 next_code = Some(&cause.derived.parent_code);
3052 }
3053 ObligationCauseCode::WellFormedDerived(derived_obligation)
3054 | ObligationCauseCode::BuiltinDerived(derived_obligation) => {
3055 let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
3056 debug!(
3057 parent_trait_ref = ?derived_obligation.parent_trait_pred,
3058 self_ty.kind = ?ty.kind(),
3059 );
3060
3061 match *ty.kind() {
3062 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
3063 coroutine = coroutine.or(Some(did));
3064 outer_coroutine = Some(did);
3065 }
3066 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
3067 seen_upvar_tys_infer_tuple = true;
3072 }
3073 _ if coroutine.is_none() => {
3074 trait_ref = Some(derived_obligation.parent_trait_pred.skip_binder());
3075 target_ty = Some(ty);
3076 }
3077 _ => {}
3078 }
3079
3080 next_code = Some(&derived_obligation.parent_code);
3081 }
3082 _ => break,
3083 }
3084 }
3085
3086 debug!(?coroutine, ?trait_ref, ?target_ty);
3088 let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
3089 (coroutine, trait_ref, target_ty)
3090 else {
3091 return false;
3092 };
3093
3094 let span = self.tcx.def_span(coroutine_did);
3095
3096 let coroutine_did_root = self.tcx.typeck_root_def_id(coroutine_did);
3097 debug!(
3098 ?coroutine_did,
3099 ?coroutine_did_root,
3100 typeck_results.hir_owner = ?self.typeck_results.as_ref().map(|t| t.hir_owner),
3101 ?span,
3102 );
3103
3104 let coroutine_body =
3105 coroutine_did.as_local().and_then(|def_id| self.tcx.hir_maybe_body_owned_by(def_id));
3106 let mut visitor = AwaitsVisitor::default();
3107 if let Some(body) = coroutine_body {
3108 visitor.visit_body(&body);
3109 }
3110 debug!(awaits = ?visitor.awaits);
3111
3112 let target_ty_erased = self.tcx.erase_and_anonymize_regions(target_ty);
3115 let ty_matches = |ty| -> bool {
3116 let ty_erased = self.tcx.instantiate_bound_regions_with_erased(ty);
3129 let ty_erased = self.tcx.erase_and_anonymize_regions(ty_erased);
3130 let eq = ty_erased == target_ty_erased;
3131 debug!(?ty_erased, ?target_ty_erased, ?eq);
3132 eq
3133 };
3134
3135 let coroutine_data = match &self.typeck_results {
3140 Some(t) if t.hir_owner.to_def_id() == coroutine_did_root => CoroutineData(t),
3141 _ if coroutine_did.is_local() => {
3142 CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
3143 }
3144 _ => return false,
3145 };
3146
3147 let coroutine_within_in_progress_typeck = match &self.typeck_results {
3148 Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
3149 _ => false,
3150 };
3151
3152 let mut interior_or_upvar_span = None;
3153
3154 let from_awaited_ty = coroutine_data.get_from_await_ty(visitor, self.tcx, ty_matches);
3155 debug!(?from_awaited_ty);
3156
3157 if coroutine_did.is_local()
3159 && !coroutine_within_in_progress_typeck
3161 && let Some(coroutine_info) = self.tcx.mir_coroutine_witnesses(coroutine_did)
3162 {
3163 debug!(?coroutine_info);
3164 'find_source: for (variant, source_info) in
3165 coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
3166 {
3167 debug!(?variant);
3168 for &local in variant {
3169 let decl = &coroutine_info.field_tys[local];
3170 debug!(?decl);
3171 if ty_matches(ty::Binder::dummy(decl.ty)) && !decl.ignore_for_traits {
3172 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(
3173 decl.source_info.span,
3174 Some((source_info.span, from_awaited_ty)),
3175 ));
3176 break 'find_source;
3177 }
3178 }
3179 }
3180 }
3181
3182 if interior_or_upvar_span.is_none() {
3183 interior_or_upvar_span =
3184 coroutine_data.try_get_upvar_span(self, coroutine_did, ty_matches);
3185 }
3186
3187 if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
3188 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(span, None));
3189 }
3190
3191 debug!(?interior_or_upvar_span);
3192 if let Some(interior_or_upvar_span) = interior_or_upvar_span {
3193 let is_async = self.tcx.coroutine_is_async(coroutine_did);
3194 self.note_obligation_cause_for_async_await(
3195 err,
3196 interior_or_upvar_span,
3197 is_async,
3198 outer_coroutine,
3199 trait_ref,
3200 target_ty,
3201 obligation,
3202 next_code,
3203 );
3204 true
3205 } else {
3206 false
3207 }
3208 }
3209
3210 #[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("note_obligation_cause_for_async_await",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3212u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&[],
::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,
&{ meta.fields().value_set(&[]) })
} 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: () = loop {};
return __tracing_attr_fake_return;
}
{
let source_map = self.tcx.sess.source_map();
let (await_or_yield, an_await_or_yield) =
if is_async {
("await", "an await")
} else { ("yield", "a yield") };
let future_or_coroutine =
if is_async { "future" } else { "coroutine" };
let trait_explanation =
if let Some(name @ (sym::Send | sym::Sync)) =
self.tcx.get_diagnostic_name(trait_pred.def_id()) {
let (trait_name, trait_verb) =
if name == sym::Send {
("`Send`", "sent")
} else { ("`Sync`", "shared") };
err.code = None;
err.primary_message(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} cannot be {1} between threads safely",
future_or_coroutine, trait_verb))
}));
let original_span = err.span.primary_span().unwrap();
let mut span = MultiSpan::from_span(original_span);
let message =
outer_coroutine.and_then(|coroutine_did|
{
Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
CoroutineKind::Coroutine(_) =>
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("coroutine is not {0}",
trait_name))
}),
CoroutineKind::Desugared(CoroutineDesugaring::Async,
CoroutineSource::Fn) =>
self.tcx.parent(coroutine_did).as_local().map(|parent_did|
self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
self.tcx.hir_opt_name(parent_hir_id)).map(|name|
{
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("future returned by `{0}` is not {1}",
name, trait_name))
})
})?,
CoroutineKind::Desugared(CoroutineDesugaring::Async,
CoroutineSource::Block) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("future created by async block is not {0}",
trait_name))
})
}
CoroutineKind::Desugared(CoroutineDesugaring::Async,
CoroutineSource::Closure) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("future created by async closure is not {0}",
trait_name))
})
}
CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
CoroutineSource::Fn) =>
self.tcx.parent(coroutine_did).as_local().map(|parent_did|
self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
self.tcx.hir_opt_name(parent_hir_id)).map(|name|
{
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("async iterator returned by `{0}` is not {1}",
name, trait_name))
})
})?,
CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
CoroutineSource::Block) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("async iterator created by async gen block is not {0}",
trait_name))
})
}
CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
CoroutineSource::Closure) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("async iterator created by async gen closure is not {0}",
trait_name))
})
}
CoroutineKind::Desugared(CoroutineDesugaring::Gen,
CoroutineSource::Fn) => {
self.tcx.parent(coroutine_did).as_local().map(|parent_did|
self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
self.tcx.hir_opt_name(parent_hir_id)).map(|name|
{
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("iterator returned by `{0}` is not {1}",
name, trait_name))
})
})?
}
CoroutineKind::Desugared(CoroutineDesugaring::Gen,
CoroutineSource::Block) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("iterator created by gen block is not {0}",
trait_name))
})
}
CoroutineKind::Desugared(CoroutineDesugaring::Gen,
CoroutineSource::Closure) => {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("iterator created by gen closure is not {0}",
trait_name))
})
}
})
}).unwrap_or_else(||
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is not {1}",
future_or_coroutine, trait_name))
}));
span.push_span_label(original_span, message);
err.span(span);
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("is not {0}", trait_name))
})
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("does not implement `{0}`",
trait_pred.print_modifiers_and_trait_path()))
})
};
let mut explain_yield =
|interior_span: Span, yield_span: Span|
{
let mut span = MultiSpan::from_span(yield_span);
let snippet =
match source_map.span_to_snippet(interior_span) {
Ok(snippet) if !snippet.contains('\n') =>
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", snippet))
}),
_ => "the value".to_string(),
};
span.push_span_label(yield_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} occurs here, with {1} maybe used later",
await_or_yield, snippet))
}));
span.push_span_label(interior_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("has type `{0}` which {1}",
target_ty, trait_explanation))
}));
err.span_note(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1} as this value is used across {2}",
future_or_coroutine, trait_explanation, an_await_or_yield))
}));
};
match interior_or_upvar_span {
CoroutineInteriorOrUpvar::Interior(interior_span,
interior_extra_info) => {
if let Some((yield_span, from_awaited_ty)) =
interior_extra_info {
if let Some(await_span) = from_awaited_ty {
let mut span = MultiSpan::from_span(await_span);
span.push_span_label(await_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("await occurs here on type `{0}`, which {1}",
target_ty, trait_explanation))
}));
err.span_note(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("future {0} as it awaits another future which {0}",
trait_explanation))
}));
} else { explain_yield(interior_span, yield_span); }
}
}
CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
let non_send =
match target_ty.kind() {
ty::Ref(_, ref_ty, mutability) =>
match self.evaluate_obligation(obligation) {
Ok(eval) if !eval.may_apply() =>
Some((ref_ty, mutability.is_mut())),
_ => None,
},
_ => None,
};
let (span_label, span_note) =
match non_send {
Some((ref_ty, is_mut)) => {
let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
let ref_kind = if is_mut { "&mut" } else { "&" };
(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("has type `{0}` which {1}, because `{2}` is not `{3}`",
target_ty, trait_explanation, ref_ty, ref_ty_trait))
}),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("captured value {0} because `{1}` references cannot be sent unless their referent is `{2}`",
trait_explanation, ref_kind, ref_ty_trait))
}))
}
None =>
(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("has type `{0}` which {1}",
target_ty, trait_explanation))
}),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("captured value {0}",
trait_explanation))
})),
};
let mut span = MultiSpan::from_span(upvar_span);
span.push_span_label(upvar_span, span_label);
err.span_note(span, span_note);
}
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3435",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(3435u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["next_code"],
::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(&debug(&next_code)
as &dyn Value))])
});
} else { ; }
};
self.note_obligation_cause_code(obligation.cause.body_id, err,
obligation.predicate, obligation.param_env,
next_code.unwrap(), &mut Vec::new(), &mut Default::default());
}
}
}#[instrument(level = "debug", skip_all)]
3213 fn note_obligation_cause_for_async_await<G: EmissionGuarantee>(
3214 &self,
3215 err: &mut Diag<'_, G>,
3216 interior_or_upvar_span: CoroutineInteriorOrUpvar,
3217 is_async: bool,
3218 outer_coroutine: Option<DefId>,
3219 trait_pred: ty::TraitPredicate<'tcx>,
3220 target_ty: Ty<'tcx>,
3221 obligation: &PredicateObligation<'tcx>,
3222 next_code: Option<&ObligationCauseCode<'tcx>>,
3223 ) {
3224 let source_map = self.tcx.sess.source_map();
3225
3226 let (await_or_yield, an_await_or_yield) =
3227 if is_async { ("await", "an await") } else { ("yield", "a yield") };
3228 let future_or_coroutine = if is_async { "future" } else { "coroutine" };
3229
3230 let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
3233 self.tcx.get_diagnostic_name(trait_pred.def_id())
3234 {
3235 let (trait_name, trait_verb) =
3236 if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
3237
3238 err.code = None;
3239 err.primary_message(format!(
3240 "{future_or_coroutine} cannot be {trait_verb} between threads safely"
3241 ));
3242
3243 let original_span = err.span.primary_span().unwrap();
3244 let mut span = MultiSpan::from_span(original_span);
3245
3246 let message = outer_coroutine
3247 .and_then(|coroutine_did| {
3248 Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
3249 CoroutineKind::Coroutine(_) => format!("coroutine is not {trait_name}"),
3250 CoroutineKind::Desugared(
3251 CoroutineDesugaring::Async,
3252 CoroutineSource::Fn,
3253 ) => self
3254 .tcx
3255 .parent(coroutine_did)
3256 .as_local()
3257 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
3258 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
3259 .map(|name| {
3260 format!("future returned by `{name}` is not {trait_name}")
3261 })?,
3262 CoroutineKind::Desugared(
3263 CoroutineDesugaring::Async,
3264 CoroutineSource::Block,
3265 ) => {
3266 format!("future created by async block is not {trait_name}")
3267 }
3268 CoroutineKind::Desugared(
3269 CoroutineDesugaring::Async,
3270 CoroutineSource::Closure,
3271 ) => {
3272 format!("future created by async closure is not {trait_name}")
3273 }
3274 CoroutineKind::Desugared(
3275 CoroutineDesugaring::AsyncGen,
3276 CoroutineSource::Fn,
3277 ) => self
3278 .tcx
3279 .parent(coroutine_did)
3280 .as_local()
3281 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
3282 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
3283 .map(|name| {
3284 format!("async iterator returned by `{name}` is not {trait_name}")
3285 })?,
3286 CoroutineKind::Desugared(
3287 CoroutineDesugaring::AsyncGen,
3288 CoroutineSource::Block,
3289 ) => {
3290 format!("async iterator created by async gen block is not {trait_name}")
3291 }
3292 CoroutineKind::Desugared(
3293 CoroutineDesugaring::AsyncGen,
3294 CoroutineSource::Closure,
3295 ) => {
3296 format!(
3297 "async iterator created by async gen closure is not {trait_name}"
3298 )
3299 }
3300 CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Fn) => {
3301 self.tcx
3302 .parent(coroutine_did)
3303 .as_local()
3304 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
3305 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
3306 .map(|name| {
3307 format!("iterator returned by `{name}` is not {trait_name}")
3308 })?
3309 }
3310 CoroutineKind::Desugared(
3311 CoroutineDesugaring::Gen,
3312 CoroutineSource::Block,
3313 ) => {
3314 format!("iterator created by gen block is not {trait_name}")
3315 }
3316 CoroutineKind::Desugared(
3317 CoroutineDesugaring::Gen,
3318 CoroutineSource::Closure,
3319 ) => {
3320 format!("iterator created by gen closure is not {trait_name}")
3321 }
3322 })
3323 })
3324 .unwrap_or_else(|| format!("{future_or_coroutine} is not {trait_name}"));
3325
3326 span.push_span_label(original_span, message);
3327 err.span(span);
3328
3329 format!("is not {trait_name}")
3330 } else {
3331 format!("does not implement `{}`", trait_pred.print_modifiers_and_trait_path())
3332 };
3333
3334 let mut explain_yield = |interior_span: Span, yield_span: Span| {
3335 let mut span = MultiSpan::from_span(yield_span);
3336 let snippet = match source_map.span_to_snippet(interior_span) {
3337 Ok(snippet) if !snippet.contains('\n') => format!("`{snippet}`"),
3340 _ => "the value".to_string(),
3341 };
3342 span.push_span_label(
3359 yield_span,
3360 format!("{await_or_yield} occurs here, with {snippet} maybe used later"),
3361 );
3362 span.push_span_label(
3363 interior_span,
3364 format!("has type `{target_ty}` which {trait_explanation}"),
3365 );
3366 err.span_note(
3367 span,
3368 format!("{future_or_coroutine} {trait_explanation} as this value is used across {an_await_or_yield}"),
3369 );
3370 };
3371 match interior_or_upvar_span {
3372 CoroutineInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
3373 if let Some((yield_span, from_awaited_ty)) = interior_extra_info {
3374 if let Some(await_span) = from_awaited_ty {
3375 let mut span = MultiSpan::from_span(await_span);
3377 span.push_span_label(
3378 await_span,
3379 format!(
3380 "await occurs here on type `{target_ty}`, which {trait_explanation}"
3381 ),
3382 );
3383 err.span_note(
3384 span,
3385 format!(
3386 "future {trait_explanation} as it awaits another future which {trait_explanation}"
3387 ),
3388 );
3389 } else {
3390 explain_yield(interior_span, yield_span);
3392 }
3393 }
3394 }
3395 CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
3396 let non_send = match target_ty.kind() {
3398 ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(obligation) {
3399 Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())),
3400 _ => None,
3401 },
3402 _ => None,
3403 };
3404
3405 let (span_label, span_note) = match non_send {
3406 Some((ref_ty, is_mut)) => {
3410 let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
3411 let ref_kind = if is_mut { "&mut" } else { "&" };
3412 (
3413 format!(
3414 "has type `{target_ty}` which {trait_explanation}, because `{ref_ty}` is not `{ref_ty_trait}`"
3415 ),
3416 format!(
3417 "captured value {trait_explanation} because `{ref_kind}` references cannot be sent unless their referent is `{ref_ty_trait}`"
3418 ),
3419 )
3420 }
3421 None => (
3422 format!("has type `{target_ty}` which {trait_explanation}"),
3423 format!("captured value {trait_explanation}"),
3424 ),
3425 };
3426
3427 let mut span = MultiSpan::from_span(upvar_span);
3428 span.push_span_label(upvar_span, span_label);
3429 err.span_note(span, span_note);
3430 }
3431 }
3432
3433 debug!(?next_code);
3436 self.note_obligation_cause_code(
3437 obligation.cause.body_id,
3438 err,
3439 obligation.predicate,
3440 obligation.param_env,
3441 next_code.unwrap(),
3442 &mut Vec::new(),
3443 &mut Default::default(),
3444 );
3445 }
3446
3447 pub(super) fn note_obligation_cause_code<G: EmissionGuarantee, T>(
3448 &self,
3449 body_id: LocalDefId,
3450 err: &mut Diag<'_, G>,
3451 predicate: T,
3452 param_env: ty::ParamEnv<'tcx>,
3453 cause_code: &ObligationCauseCode<'tcx>,
3454 obligated_types: &mut Vec<Ty<'tcx>>,
3455 seen_requirements: &mut FxHashSet<DefId>,
3456 ) where
3457 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
3458 {
3459 let tcx = self.tcx;
3460 let predicate = predicate.upcast(tcx);
3461 let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
3462 if let Some(pred) = predicate.as_trait_clause()
3463 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
3464 && let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
3465 {
3466 err.span_suggestion_verbose(
3467 expr.span.until(inner.span),
3468 "references are always `Sized`, even if they point to unsized data; consider \
3469 not dereferencing the expression",
3470 String::new(),
3471 Applicability::MaybeIncorrect,
3472 );
3473 }
3474 };
3475 match *cause_code {
3476 ObligationCauseCode::ExprAssignable
3477 | ObligationCauseCode::MatchExpressionArm { .. }
3478 | ObligationCauseCode::Pattern { .. }
3479 | ObligationCauseCode::IfExpression { .. }
3480 | ObligationCauseCode::IfExpressionWithNoElse
3481 | ObligationCauseCode::MainFunctionType
3482 | ObligationCauseCode::LangFunctionType(_)
3483 | ObligationCauseCode::IntrinsicType
3484 | ObligationCauseCode::MethodReceiver
3485 | ObligationCauseCode::ReturnNoExpression
3486 | ObligationCauseCode::Misc
3487 | ObligationCauseCode::WellFormed(..)
3488 | ObligationCauseCode::MatchImpl(..)
3489 | ObligationCauseCode::ReturnValue(_)
3490 | ObligationCauseCode::BlockTailExpression(..)
3491 | ObligationCauseCode::AwaitableExpr(_)
3492 | ObligationCauseCode::ForLoopIterator
3493 | ObligationCauseCode::QuestionMark
3494 | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
3495 | ObligationCauseCode::LetElse
3496 | ObligationCauseCode::UnOp { .. }
3497 | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
3498 | ObligationCauseCode::AlwaysApplicableImpl
3499 | ObligationCauseCode::ConstParam(_)
3500 | ObligationCauseCode::ReferenceOutlivesReferent(..)
3501 | ObligationCauseCode::ObjectTypeBound(..) => {}
3502 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. } => {
3503 if let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
3504 && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
3505 && tcx.sess.source_map().lookup_char_pos(lhs.span.lo()).line
3506 != tcx.sess.source_map().lookup_char_pos(rhs.span.hi()).line
3507 {
3508 err.span_label(lhs.span, "");
3509 err.span_label(rhs.span, "");
3510 }
3511 }
3512 ObligationCauseCode::RustCall => {
3513 if let Some(pred) = predicate.as_trait_clause()
3514 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
3515 {
3516 err.note("argument required to be sized due to `extern \"rust-call\"` ABI");
3517 }
3518 }
3519 ObligationCauseCode::SliceOrArrayElem => {
3520 err.note("slice and array elements must have `Sized` type");
3521 }
3522 ObligationCauseCode::ArrayLen(array_ty) => {
3523 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the length of array `{0}` must be type `usize`",
array_ty))
})format!("the length of array `{array_ty}` must be type `usize`"));
3524 }
3525 ObligationCauseCode::TupleElem => {
3526 err.note("only the last element of a tuple may have a dynamically sized type");
3527 }
3528 ObligationCauseCode::DynCompatible(span) => {
3529 err.multipart_suggestion(
3530 "you might have meant to use `Self` to refer to the implementing type",
3531 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span, "Self".into())]))vec![(span, "Self".into())],
3532 Applicability::MachineApplicable,
3533 );
3534 }
3535 ObligationCauseCode::WhereClause(item_def_id, span)
3536 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
3537 | ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)
3538 if !span.is_dummy() =>
3539 {
3540 if let ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, pos) = &cause_code {
3541 if let Node::Expr(expr) = tcx.parent_hir_node(*hir_id)
3542 && let hir::ExprKind::Call(_, args) = expr.kind
3543 && let Some(expr) = args.get(*pos)
3544 {
3545 suggest_remove_deref(err, &expr);
3546 } else if let Node::Expr(expr) = self.tcx.hir_node(*hir_id)
3547 && let hir::ExprKind::MethodCall(_, _, args, _) = expr.kind
3548 && let Some(expr) = args.get(*pos)
3549 {
3550 suggest_remove_deref(err, &expr);
3551 }
3552 }
3553 let item_name = tcx.def_path_str(item_def_id);
3554 let short_item_name = { let _guard = ForceTrimmedGuard::new(); tcx.def_path_str(item_def_id) }with_forced_trimmed_paths!(tcx.def_path_str(item_def_id));
3555 let mut multispan = MultiSpan::from(span);
3556 let sm = tcx.sess.source_map();
3557 if let Some(ident) = tcx.opt_item_ident(item_def_id) {
3558 let same_line =
3559 match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
3560 (Ok(l), Ok(r)) => l.line == r.line,
3561 _ => true,
3562 };
3563 if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line {
3564 multispan.push_span_label(
3565 ident.span,
3566 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by a bound in this {0}",
tcx.def_kind(item_def_id).descr(item_def_id)))
})format!(
3567 "required by a bound in this {}",
3568 tcx.def_kind(item_def_id).descr(item_def_id)
3569 ),
3570 );
3571 }
3572 }
3573 let mut a = "a";
3574 let mut this = "this bound";
3575 let mut note = None;
3576 let mut help = None;
3577 if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() {
3578 match clause {
3579 ty::ClauseKind::Trait(trait_pred) => {
3580 let def_id = trait_pred.def_id();
3581 let visible_item = if let Some(local) = def_id.as_local() {
3582 let ty = trait_pred.self_ty();
3583 if let ty::Adt(adt, _) = ty.kind() {
3587 let visibilities = &tcx.resolutions(()).effective_visibilities;
3588 visibilities.effective_vis(local).is_none_or(|v| {
3589 v.at_level(Level::Reexported)
3590 .is_accessible_from(adt.did(), tcx)
3591 })
3592 } else {
3593 true
3595 }
3596 } else {
3597 tcx.visible_parent_map(()).get(&def_id).is_some()
3599 };
3600 if tcx.is_lang_item(def_id, LangItem::Sized) {
3601 if tcx
3603 .generics_of(item_def_id)
3604 .own_params
3605 .iter()
3606 .any(|param| tcx.def_span(param.def_id) == span)
3607 {
3608 a = "an implicit `Sized`";
3609 this =
3610 "the implicit `Sized` requirement on this type parameter";
3611 }
3612 if let Some(hir::Node::TraitItem(hir::TraitItem {
3613 generics,
3614 kind: hir::TraitItemKind::Type(bounds, None),
3615 ..
3616 })) = tcx.hir_get_if_local(item_def_id)
3617 && !bounds.iter()
3619 .filter_map(|bound| bound.trait_ref())
3620 .any(|tr| tr.trait_def_id().is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Sized)))
3621 {
3622 let (span, separator) = if let [.., last] = bounds {
3623 (last.span().shrink_to_hi(), " +")
3624 } else {
3625 (generics.span.shrink_to_hi(), ":")
3626 };
3627 err.span_suggestion_verbose(
3628 span,
3629 "consider relaxing the implicit `Sized` restriction",
3630 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ?Sized", separator))
})format!("{separator} ?Sized"),
3631 Applicability::MachineApplicable,
3632 );
3633 }
3634 }
3635 if let DefKind::Trait = tcx.def_kind(item_def_id)
3636 && !visible_item
3637 {
3638 note = Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{1}` is a \"sealed trait\", because to implement it you also need to implement `{0}`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it",
{
let _guard = NoTrimmedGuard::new();
tcx.def_path_str(def_id)
}, short_item_name))
})format!(
3639 "`{short_item_name}` is a \"sealed trait\", because to implement it \
3640 you also need to implement `{}`, which is not accessible; this is \
3641 usually done to force you to use one of the provided types that \
3642 already implement it",
3643 with_no_trimmed_paths!(tcx.def_path_str(def_id)),
3644 ));
3645 let impls_of = tcx.trait_impls_of(def_id);
3646 let impls = impls_of
3647 .non_blanket_impls()
3648 .values()
3649 .flatten()
3650 .chain(impls_of.blanket_impls().iter())
3651 .collect::<Vec<_>>();
3652 if !impls.is_empty() {
3653 let len = impls.len();
3654 let mut types = impls
3655 .iter()
3656 .map(|&&t| {
3657 {
let _guard = NoTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}",
tcx.type_of(t).instantiate_identity().skip_norm_wip()))
})
}with_no_trimmed_paths!(format!(
3658 " {}",
3659 tcx.type_of(t)
3660 .instantiate_identity()
3661 .skip_norm_wip(),
3662 ))
3663 })
3664 .collect::<Vec<_>>();
3665 let post = if types.len() > 9 {
3666 types.truncate(8);
3667 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\nand {0} others", len - 8))
})format!("\nand {} others", len - 8)
3668 } else {
3669 String::new()
3670 };
3671 help = Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the following type{0} implement{1} the trait:\n{2}{3}",
if len == 1 { "" } else { "s" },
if len == 1 { "s" } else { "" }, types.join("\n"), post))
})format!(
3672 "the following type{} implement{} the trait:\n{}{post}",
3673 pluralize!(len),
3674 if len == 1 { "s" } else { "" },
3675 types.join("\n"),
3676 ));
3677 }
3678 }
3679 }
3680 ty::ClauseKind::ConstArgHasType(..) => {
3681 let descr =
3682 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by a const generic parameter in `{0}`",
item_name))
})format!("required by a const generic parameter in `{item_name}`");
3683 if span.is_visible(sm) {
3684 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by this const generic parameter in `{0}`",
short_item_name))
})format!(
3685 "required by this const generic parameter in `{short_item_name}`"
3686 );
3687 multispan.push_span_label(span, msg);
3688 err.span_note(multispan, descr);
3689 } else {
3690 err.span_note(tcx.def_span(item_def_id), descr);
3691 }
3692 return;
3693 }
3694 _ => (),
3695 }
3696 }
3697
3698 let is_in_fmt_lit = if let Some(s) = err.span.primary_span() {
3701 #[allow(non_exhaustive_omitted_patterns)] match s.desugaring_kind() {
Some(DesugaringKind::FormatLiteral { .. }) => true,
_ => false,
}matches!(s.desugaring_kind(), Some(DesugaringKind::FormatLiteral { .. }))
3702 } else {
3703 false
3704 };
3705 if !is_in_fmt_lit {
3706 let descr = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by {0} bound in `{1}`", a,
item_name))
})format!("required by {a} bound in `{item_name}`");
3707 if span.is_visible(sm) {
3708 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by {0} in `{1}`", this,
short_item_name))
})format!("required by {this} in `{short_item_name}`");
3709 multispan.push_span_label(span, msg);
3710 err.span_note(multispan, descr);
3711 } else {
3712 err.span_note(tcx.def_span(item_def_id), descr);
3713 }
3714 }
3715 if let Some(note) = note {
3716 err.note(note);
3717 }
3718 if let Some(help) = help {
3719 err.help(help);
3720 }
3721 }
3722 ObligationCauseCode::WhereClause(..)
3723 | ObligationCauseCode::WhereClauseInExpr(..)
3724 | ObligationCauseCode::HostEffectInExpr(..) => {
3725 }
3728 ObligationCauseCode::OpaqueTypeBound(span, definition_def_id) => {
3729 err.span_note(span, "required by a bound in an opaque type");
3730 if let Some(definition_def_id) = definition_def_id
3731 && self.tcx.typeck(definition_def_id).coroutine_stalled_predicates.is_empty()
3735 {
3736 err.span_note(
3739 tcx.def_span(definition_def_id),
3740 "this definition site has more where clauses than the opaque type",
3741 );
3742 }
3743 }
3744 ObligationCauseCode::Coercion { source, target } => {
3745 let source =
3746 tcx.short_string(self.resolve_vars_if_possible(source), err.long_ty_path());
3747 let target =
3748 tcx.short_string(self.resolve_vars_if_possible(target), err.long_ty_path());
3749 err.note({
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required for the cast from `{0}` to `{1}`",
source, target))
})
}with_forced_trimmed_paths!(format!(
3750 "required for the cast from `{source}` to `{target}`",
3751 )));
3752 }
3753 ObligationCauseCode::RepeatElementCopy { is_constable, elt_span } => {
3754 err.note(
3755 "the `Copy` trait is required because this value will be copied for each element of the array",
3756 );
3757 let sm = tcx.sess.source_map();
3758 if #[allow(non_exhaustive_omitted_patterns)] match is_constable {
IsConstable::Fn | IsConstable::Ctor => true,
_ => false,
}matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
3759 && let Ok(_) = sm.span_to_snippet(elt_span)
3760 {
3761 err.multipart_suggestion(
3762 "create an inline `const` block",
3763 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(elt_span.shrink_to_lo(), "const { ".to_string()),
(elt_span.shrink_to_hi(), " }".to_string())]))vec![
3764 (elt_span.shrink_to_lo(), "const { ".to_string()),
3765 (elt_span.shrink_to_hi(), " }".to_string()),
3766 ],
3767 Applicability::MachineApplicable,
3768 );
3769 } else {
3770 err.help("consider using `core::array::from_fn` to initialize the array");
3772 err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information");
3773 }
3774 }
3775 ObligationCauseCode::VariableType(hir_id) => {
3776 if let Some(typeck_results) = &self.typeck_results
3777 && let Some(ty) = typeck_results.node_type_opt(hir_id)
3778 && let ty::Error(_) = ty.kind()
3779 {
3780 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` isn\'t satisfied, but the type of this pattern is `{{type error}}`",
predicate))
})format!(
3781 "`{predicate}` isn't satisfied, but the type of this pattern is \
3782 `{{type error}}`",
3783 ));
3784 err.downgrade_to_delayed_bug();
3785 }
3786 let mut local = true;
3787 match tcx.parent_hir_node(hir_id) {
3788 Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
3789 err.span_suggestion_verbose(
3790 ty.span.shrink_to_lo(),
3791 "consider borrowing here",
3792 "&",
3793 Applicability::MachineApplicable,
3794 );
3795 }
3796 Node::LetStmt(hir::LetStmt {
3797 init: Some(hir::Expr { kind: hir::ExprKind::Index(..), span, .. }),
3798 ..
3799 }) => {
3800 err.span_suggestion_verbose(
3804 span.shrink_to_lo(),
3805 "consider borrowing here",
3806 "&",
3807 Applicability::MachineApplicable,
3808 );
3809 }
3810 Node::LetStmt(hir::LetStmt { init: Some(expr), .. }) => {
3811 suggest_remove_deref(err, &expr);
3814 }
3815 Node::Param(param) => {
3816 err.span_suggestion_verbose(
3817 param.ty_span.shrink_to_lo(),
3818 "function arguments must have a statically known size, borrowed types \
3819 always have a known size",
3820 "&",
3821 Applicability::MachineApplicable,
3822 );
3823 local = false;
3824 }
3825 _ => {}
3826 }
3827 if local {
3828 err.note("all local variables must have a statically known size");
3829 }
3830 }
3831 ObligationCauseCode::SizedArgumentType(hir_id) => {
3832 let mut ty = None;
3833 let borrowed_msg = "function arguments must have a statically known size, borrowed \
3834 types always have a known size";
3835 if let Some(hir_id) = hir_id
3836 && let hir::Node::Param(param) = self.tcx.hir_node(hir_id)
3837 && let Some(decl) = self.tcx.parent_hir_node(hir_id).fn_decl()
3838 && let Some(t) = decl.inputs.iter().find(|t| param.ty_span.contains(t.span))
3839 {
3840 ty = Some(t);
3848 } else if let Some(hir_id) = hir_id
3849 && let hir::Node::Ty(t) = self.tcx.hir_node(hir_id)
3850 {
3851 ty = Some(t);
3852 }
3853 if let Some(ty) = ty {
3854 match ty.kind {
3855 hir::TyKind::TraitObject(traits, _) => {
3856 let (span, kw) = match traits {
3857 [first, ..] if first.span.lo() == ty.span.lo() => {
3858 (ty.span.shrink_to_lo(), "dyn ")
3860 }
3861 [first, ..] => (ty.span.until(first.span), ""),
3862 [] => ::rustc_middle::util::bug::span_bug_fmt(ty.span,
format_args!("trait object with no traits: {0:?}", ty))span_bug!(ty.span, "trait object with no traits: {ty:?}"),
3863 };
3864 let needs_parens = traits.len() != 1;
3865 if let Some(hir_id) = hir_id
3867 && #[allow(non_exhaustive_omitted_patterns)] match self.tcx.parent_hir_node(hir_id)
{
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. }) => true,
_ => false,
}matches!(
3868 self.tcx.parent_hir_node(hir_id),
3869 hir::Node::Item(hir::Item {
3870 kind: hir::ItemKind::Fn { .. },
3871 ..
3872 })
3873 )
3874 {
3875 err.span_suggestion_verbose(
3876 span,
3877 "you can use `impl Trait` as the argument type",
3878 "impl ",
3879 Applicability::MaybeIncorrect,
3880 );
3881 }
3882 let sugg = if !needs_parens {
3883 ::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!("&{0}", kw))
}))]))vec![(span.shrink_to_lo(), format!("&{kw}"))]
3884 } else {
3885 ::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!("&({0}", kw))
})), (ty.span.shrink_to_hi(), ")".to_string())]))vec![
3886 (span.shrink_to_lo(), format!("&({kw}")),
3887 (ty.span.shrink_to_hi(), ")".to_string()),
3888 ]
3889 };
3890 err.multipart_suggestion(
3891 borrowed_msg,
3892 sugg,
3893 Applicability::MachineApplicable,
3894 );
3895 }
3896 hir::TyKind::Slice(_ty) => {
3897 err.span_suggestion_verbose(
3898 ty.span.shrink_to_lo(),
3899 "function arguments must have a statically known size, borrowed \
3900 slices always have a known size",
3901 "&",
3902 Applicability::MachineApplicable,
3903 );
3904 }
3905 hir::TyKind::Path(_) => {
3906 err.span_suggestion_verbose(
3907 ty.span.shrink_to_lo(),
3908 borrowed_msg,
3909 "&",
3910 Applicability::MachineApplicable,
3911 );
3912 }
3913 _ => {}
3914 }
3915 } else {
3916 err.note("all function arguments must have a statically known size");
3917 }
3918 if tcx.sess.opts.unstable_features.is_nightly_build()
3919 && !tcx.features().unsized_fn_params()
3920 {
3921 err.help("unsized fn params are gated as an unstable feature");
3922 }
3923 }
3924 ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
3925 err.note("the return type of a function must have a statically known size");
3926 }
3927 ObligationCauseCode::SizedYieldType => {
3928 err.note("the yield type of a coroutine must have a statically known size");
3929 }
3930 ObligationCauseCode::AssignmentLhsSized => {
3931 err.note("the left-hand-side of an assignment must have a statically known size");
3932 }
3933 ObligationCauseCode::TupleInitializerSized => {
3934 err.note("tuples must have a statically known size to be initialized");
3935 }
3936 ObligationCauseCode::StructInitializerSized => {
3937 err.note("structs must have a statically known size to be initialized");
3938 }
3939 ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
3940 match *item {
3941 AdtKind::Struct => {
3942 if last {
3943 err.note(
3944 "the last field of a packed struct may only have a \
3945 dynamically sized type if it does not need drop to be run",
3946 );
3947 } else {
3948 err.note(
3949 "only the last field of a struct may have a dynamically sized type",
3950 );
3951 }
3952 }
3953 AdtKind::Union => {
3954 err.note("no field of a union may have a dynamically sized type");
3955 }
3956 AdtKind::Enum => {
3957 err.note("no field of an enum variant may have a dynamically sized type");
3958 }
3959 }
3960 err.help("change the field's type to have a statically known size");
3961 err.span_suggestion_verbose(
3962 span.shrink_to_lo(),
3963 "borrowed types always have a statically known size",
3964 "&",
3965 Applicability::MachineApplicable,
3966 );
3967 err.multipart_suggestion(
3968 "the `Box` type always has a statically known size and allocates its contents \
3969 in the heap",
3970 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "Box<".to_string()),
(span.shrink_to_hi(), ">".to_string())]))vec![
3971 (span.shrink_to_lo(), "Box<".to_string()),
3972 (span.shrink_to_hi(), ">".to_string()),
3973 ],
3974 Applicability::MachineApplicable,
3975 );
3976 }
3977 ObligationCauseCode::SizedConstOrStatic => {
3978 err.note("statics and constants must have a statically known size");
3979 }
3980 ObligationCauseCode::InlineAsmSized => {
3981 err.note("all inline asm arguments must have a statically known size");
3982 }
3983 ObligationCauseCode::SizedClosureCapture(closure_def_id) => {
3984 err.note(
3985 "all values captured by value by a closure must have a statically known size",
3986 );
3987 let hir::ExprKind::Closure(closure) =
3988 tcx.hir_node_by_def_id(closure_def_id).expect_expr().kind
3989 else {
3990 ::rustc_middle::util::bug::bug_fmt(format_args!("expected closure in SizedClosureCapture obligation"));bug!("expected closure in SizedClosureCapture obligation");
3991 };
3992 if let hir::CaptureBy::Value { .. } = closure.capture_clause
3993 && let Some(span) = closure.fn_arg_span
3994 {
3995 err.span_label(span, "this closure captures all values by move");
3996 }
3997 }
3998 ObligationCauseCode::SizedCoroutineInterior(coroutine_def_id) => {
3999 let what = match tcx.coroutine_kind(coroutine_def_id) {
4000 None
4001 | Some(hir::CoroutineKind::Coroutine(_))
4002 | Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => {
4003 "yield"
4004 }
4005 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
4006 "await"
4007 }
4008 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => {
4009 "yield`/`await"
4010 }
4011 };
4012 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("all values live across `{0}` must have a statically known size",
what))
})format!(
4013 "all values live across `{what}` must have a statically known size"
4014 ));
4015 }
4016 ObligationCauseCode::SharedStatic => {
4017 err.note("shared static variables must have a type that implements `Sync`");
4018 }
4019 ObligationCauseCode::BuiltinDerived(ref data) => {
4020 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
4021 let ty = parent_trait_ref.skip_binder().self_ty();
4022 if parent_trait_ref.references_error() {
4023 err.downgrade_to_delayed_bug();
4026 return;
4027 }
4028
4029 let is_upvar_tys_infer_tuple = if !#[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Tuple(..) => true,
_ => false,
}matches!(ty.kind(), ty::Tuple(..)) {
4032 false
4033 } else if let ObligationCauseCode::BuiltinDerived(data) = &*data.parent_code {
4034 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
4035 let nested_ty = parent_trait_ref.skip_binder().self_ty();
4036 #[allow(non_exhaustive_omitted_patterns)] match nested_ty.kind() {
ty::Coroutine(..) => true,
_ => false,
}matches!(nested_ty.kind(), ty::Coroutine(..))
4037 || #[allow(non_exhaustive_omitted_patterns)] match nested_ty.kind() {
ty::Closure(..) => true,
_ => false,
}matches!(nested_ty.kind(), ty::Closure(..))
4038 } else {
4039 false
4040 };
4041
4042 let is_builtin_async_fn_trait =
4043 tcx.async_fn_trait_kind_from_def_id(data.parent_trait_pred.def_id()).is_some();
4044
4045 if !is_upvar_tys_infer_tuple && !is_builtin_async_fn_trait {
4046 let mut msg = || {
4047 let ty_str = tcx.short_string(ty, err.long_ty_path());
4048 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required because it appears within the type `{0}`",
ty_str))
})format!("required because it appears within the type `{ty_str}`")
4049 };
4050 match *ty.kind() {
4051 ty::Adt(def, _) => {
4052 let msg = msg();
4053 match tcx.opt_item_ident(def.did()) {
4054 Some(ident) => {
4055 err.span_note(ident.span, msg);
4056 }
4057 None => {
4058 err.note(msg);
4059 }
4060 }
4061 }
4062 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) => {
4063 let is_future = tcx.ty_is_opaque_future(ty);
4066 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:4066",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(4066u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["message",
"obligated_types", "is_future"],
::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!("note_obligation_cause_code: check for async fn")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&obligated_types)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&is_future)
as &dyn Value))])
});
} else { ; }
};debug!(
4067 ?obligated_types,
4068 ?is_future,
4069 "note_obligation_cause_code: check for async fn"
4070 );
4071 if is_future
4072 && obligated_types.last().is_some_and(|ty| match ty.kind() {
4073 ty::Coroutine(last_def_id, ..) => {
4074 tcx.coroutine_is_async(*last_def_id)
4075 }
4076 _ => false,
4077 })
4078 {
4079 } else {
4081 let msg = msg();
4082 err.span_note(tcx.def_span(def_id), msg);
4083 }
4084 }
4085 ty::Coroutine(def_id, _) => {
4086 let sp = tcx.def_span(def_id);
4087
4088 let kind = tcx.coroutine_kind(def_id).unwrap();
4090 err.span_note(
4091 sp,
4092 {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required because it\'s used within this {0:#}",
kind))
})
}with_forced_trimmed_paths!(format!(
4093 "required because it's used within this {kind:#}",
4094 )),
4095 );
4096 }
4097 ty::CoroutineWitness(..) => {
4098 }
4101 ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
4102 err.span_note(
4103 tcx.def_span(def_id),
4104 "required because it's used within this closure",
4105 );
4106 }
4107 ty::Str => {
4108 err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes");
4109 }
4110 _ => {
4111 let msg = msg();
4112 err.note(msg);
4113 }
4114 };
4115 }
4116
4117 obligated_types.push(ty);
4118
4119 let parent_predicate = parent_trait_ref;
4120 if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
4121 ensure_sufficient_stack(|| {
4123 self.note_obligation_cause_code(
4124 body_id,
4125 err,
4126 parent_predicate,
4127 param_env,
4128 &data.parent_code,
4129 obligated_types,
4130 seen_requirements,
4131 )
4132 });
4133 } else {
4134 ensure_sufficient_stack(|| {
4135 self.note_obligation_cause_code(
4136 body_id,
4137 err,
4138 parent_predicate,
4139 param_env,
4140 cause_code.peel_derives(),
4141 obligated_types,
4142 seen_requirements,
4143 )
4144 });
4145 }
4146 }
4147 ObligationCauseCode::ImplDerived(ref data) => {
4148 let mut parent_trait_pred =
4149 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
4150 let parent_def_id = parent_trait_pred.def_id();
4151 if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
4152 && !tcx.features().enabled(sym::try_trait_v2)
4153 {
4154 return;
4158 }
4159 if tcx.is_diagnostic_item(sym::PinDerefMutHelper, parent_def_id) {
4160 let parent_predicate =
4161 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
4162
4163 ensure_sufficient_stack(|| {
4165 self.note_obligation_cause_code(
4166 body_id,
4167 err,
4168 parent_predicate,
4169 param_env,
4170 &data.derived.parent_code,
4171 obligated_types,
4172 seen_requirements,
4173 )
4174 });
4175 return;
4176 }
4177 let self_ty_str =
4178 tcx.short_string(parent_trait_pred.skip_binder().self_ty(), err.long_ty_path());
4179 let trait_name = tcx.short_string(
4180 parent_trait_pred.print_modifiers_and_trait_path(),
4181 err.long_ty_path(),
4182 );
4183 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required for `{0}` to implement `{1}`",
self_ty_str, trait_name))
})format!("required for `{self_ty_str}` to implement `{trait_name}`");
4184 let mut is_auto_trait = false;
4185 match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
4186 Some(Node::Item(hir::Item {
4187 kind: hir::ItemKind::Trait { is_auto, ident, .. },
4188 ..
4189 })) => {
4190 is_auto_trait = #[allow(non_exhaustive_omitted_patterns)] match is_auto {
hir::IsAuto::Yes => true,
_ => false,
}matches!(is_auto, hir::IsAuto::Yes);
4193 err.span_note(ident.span, msg);
4194 }
4195 Some(Node::Item(hir::Item {
4196 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
4197 ..
4198 })) => {
4199 let mut spans = Vec::with_capacity(2);
4200 if let Some(of_trait) = of_trait
4201 && !of_trait.trait_ref.path.span.in_derive_expansion()
4202 {
4203 spans.push(of_trait.trait_ref.path.span);
4204 }
4205 spans.push(self_ty.span);
4206 let mut spans: MultiSpan = spans.into();
4207 let mut derived = false;
4208 if #[allow(non_exhaustive_omitted_patterns)] match self_ty.span.ctxt().outer_expn_data().kind
{
ExpnKind::Macro(MacroKind::Derive, _) => true,
_ => false,
}matches!(
4209 self_ty.span.ctxt().outer_expn_data().kind,
4210 ExpnKind::Macro(MacroKind::Derive, _)
4211 ) || #[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!(
4212 of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
4213 Some(ExpnKind::Macro(MacroKind::Derive, _))
4214 ) {
4215 derived = true;
4216 spans.push_span_label(
4217 data.span,
4218 if data.span.in_derive_expansion() {
4219 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type parameter would need to implement `{0}`",
trait_name))
})format!("type parameter would need to implement `{trait_name}`")
4220 } else {
4221 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unsatisfied trait bound"))
})format!("unsatisfied trait bound")
4222 },
4223 );
4224 } else if !data.span.is_dummy() && !data.span.overlaps(self_ty.span) {
4225 if let Some(pred) = predicate.as_trait_clause()
4228 && self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
4229 && self
4230 .tcx
4231 .generics_of(data.impl_or_alias_def_id)
4232 .own_params
4233 .iter()
4234 .any(|param| self.tcx.def_span(param.def_id) == data.span)
4235 {
4236 spans.push_span_label(
4237 data.span,
4238 "unsatisfied trait bound implicitly introduced here",
4239 );
4240 } else {
4241 spans.push_span_label(
4242 data.span,
4243 "unsatisfied trait bound introduced here",
4244 );
4245 }
4246 }
4247 err.span_note(spans, msg);
4248 if derived && trait_name != "Copy" {
4249 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider manually implementing `{0}` to avoid undesired bounds",
trait_name))
})format!(
4250 "consider manually implementing `{trait_name}` to avoid undesired \
4251 bounds",
4252 ));
4253 }
4254 point_at_assoc_type_restriction(
4255 tcx,
4256 err,
4257 &self_ty_str,
4258 &trait_name,
4259 predicate,
4260 &generics,
4261 &data,
4262 );
4263 }
4264 _ => {
4265 err.note(msg);
4266 }
4267 };
4268
4269 let mut parent_predicate = parent_trait_pred;
4270 let mut data = &data.derived;
4271 let mut count = 0;
4272 seen_requirements.insert(parent_def_id);
4273 if is_auto_trait {
4274 while let ObligationCauseCode::BuiltinDerived(derived) = &*data.parent_code {
4277 let child_trait_ref =
4278 self.resolve_vars_if_possible(derived.parent_trait_pred);
4279 let child_def_id = child_trait_ref.def_id();
4280 if seen_requirements.insert(child_def_id) {
4281 break;
4282 }
4283 data = derived;
4284 parent_predicate = child_trait_ref.upcast(tcx);
4285 parent_trait_pred = child_trait_ref;
4286 }
4287 }
4288 while let ObligationCauseCode::ImplDerived(child) = &*data.parent_code {
4289 let child_trait_pred =
4291 self.resolve_vars_if_possible(child.derived.parent_trait_pred);
4292 let child_def_id = child_trait_pred.def_id();
4293 if seen_requirements.insert(child_def_id) {
4294 break;
4295 }
4296 count += 1;
4297 data = &child.derived;
4298 parent_predicate = child_trait_pred.upcast(tcx);
4299 parent_trait_pred = child_trait_pred;
4300 }
4301 if count > 0 {
4302 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} redundant requirement{1} hidden",
count, if count == 1 { "" } else { "s" }))
})format!(
4303 "{} redundant requirement{} hidden",
4304 count,
4305 pluralize!(count)
4306 ));
4307 let self_ty = tcx.short_string(
4308 parent_trait_pred.skip_binder().self_ty(),
4309 err.long_ty_path(),
4310 );
4311 let trait_path = tcx.short_string(
4312 parent_trait_pred.print_modifiers_and_trait_path(),
4313 err.long_ty_path(),
4314 );
4315 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required for `{0}` to implement `{1}`",
self_ty, trait_path))
})format!("required for `{self_ty}` to implement `{trait_path}`"));
4316 }
4317 ensure_sufficient_stack(|| {
4319 self.note_obligation_cause_code(
4320 body_id,
4321 err,
4322 parent_predicate,
4323 param_env,
4324 &data.parent_code,
4325 obligated_types,
4326 seen_requirements,
4327 )
4328 });
4329 }
4330 ObligationCauseCode::ImplDerivedHost(ref data) => {
4331 let self_ty = tcx.short_string(
4332 self.resolve_vars_if_possible(data.derived.parent_host_pred.self_ty()),
4333 err.long_ty_path(),
4334 );
4335 let trait_path = tcx.short_string(
4336 data.derived
4337 .parent_host_pred
4338 .map_bound(|pred| pred.trait_ref)
4339 .print_only_trait_path(),
4340 err.long_ty_path(),
4341 );
4342 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required for `{1}` to implement `{0} {2}`",
data.derived.parent_host_pred.skip_binder().constness,
self_ty, trait_path))
})format!(
4343 "required for `{self_ty}` to implement `{} {trait_path}`",
4344 data.derived.parent_host_pred.skip_binder().constness,
4345 );
4346 match tcx.hir_get_if_local(data.impl_def_id) {
4347 Some(Node::Item(hir::Item {
4348 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
4349 ..
4350 })) => {
4351 let mut spans = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self_ty.span]))vec![self_ty.span];
4352 spans.extend(of_trait.map(|t| t.trait_ref.path.span));
4353 let mut spans: MultiSpan = spans.into();
4354 spans.push_span_label(data.span, "unsatisfied trait bound introduced here");
4355 err.span_note(spans, msg);
4356 }
4357 _ => {
4358 err.note(msg);
4359 }
4360 }
4361 ensure_sufficient_stack(|| {
4362 self.note_obligation_cause_code(
4363 body_id,
4364 err,
4365 data.derived.parent_host_pred,
4366 param_env,
4367 &data.derived.parent_code,
4368 obligated_types,
4369 seen_requirements,
4370 )
4371 });
4372 }
4373 ObligationCauseCode::BuiltinDerivedHost(ref data) => {
4374 ensure_sufficient_stack(|| {
4375 self.note_obligation_cause_code(
4376 body_id,
4377 err,
4378 data.parent_host_pred,
4379 param_env,
4380 &data.parent_code,
4381 obligated_types,
4382 seen_requirements,
4383 )
4384 });
4385 }
4386 ObligationCauseCode::WellFormedDerived(ref data) => {
4387 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
4388 let parent_predicate = parent_trait_ref;
4389 ensure_sufficient_stack(|| {
4391 self.note_obligation_cause_code(
4392 body_id,
4393 err,
4394 parent_predicate,
4395 param_env,
4396 &data.parent_code,
4397 obligated_types,
4398 seen_requirements,
4399 )
4400 });
4401 }
4402 ObligationCauseCode::TypeAlias(ref nested, span, def_id) => {
4403 ensure_sufficient_stack(|| {
4405 self.note_obligation_cause_code(
4406 body_id,
4407 err,
4408 predicate,
4409 param_env,
4410 nested,
4411 obligated_types,
4412 seen_requirements,
4413 )
4414 });
4415 let mut multispan = MultiSpan::from(span);
4416 multispan.push_span_label(span, "required by this bound");
4417 err.span_note(
4418 multispan,
4419 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("required by a bound on the type alias `{0}`",
tcx.item_name(def_id)))
})format!("required by a bound on the type alias `{}`", tcx.item_name(def_id)),
4420 );
4421 }
4422 ObligationCauseCode::FunctionArg {
4423 arg_hir_id, call_hir_id, ref parent_code, ..
4424 } => {
4425 self.note_function_argument_obligation(
4426 body_id,
4427 err,
4428 arg_hir_id,
4429 parent_code,
4430 param_env,
4431 predicate,
4432 call_hir_id,
4433 );
4434 ensure_sufficient_stack(|| {
4435 self.note_obligation_cause_code(
4436 body_id,
4437 err,
4438 predicate,
4439 param_env,
4440 parent_code,
4441 obligated_types,
4442 seen_requirements,
4443 )
4444 });
4445 }
4446 ObligationCauseCode::CompareImplItem { trait_item_def_id, .. }
4449 if tcx.is_impl_trait_in_trait(trait_item_def_id) => {}
4450 ObligationCauseCode::CompareImplItem { trait_item_def_id, kind, .. } => {
4451 let item_name = tcx.item_name(trait_item_def_id);
4452 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the requirement `{0}` appears on the `impl`\'s {1} `{2}` but not on the corresponding trait\'s {1}",
predicate, kind, item_name))
})format!(
4453 "the requirement `{predicate}` appears on the `impl`'s {kind} \
4454 `{item_name}` but not on the corresponding trait's {kind}",
4455 );
4456 let sp = tcx
4457 .opt_item_ident(trait_item_def_id)
4458 .map(|i| i.span)
4459 .unwrap_or_else(|| tcx.def_span(trait_item_def_id));
4460 let mut assoc_span: MultiSpan = sp.into();
4461 assoc_span.push_span_label(
4462 sp,
4463 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this trait\'s {0} doesn\'t have the requirement `{1}`",
kind, predicate))
})format!("this trait's {kind} doesn't have the requirement `{predicate}`"),
4464 );
4465 if let Some(ident) = tcx
4466 .opt_associated_item(trait_item_def_id)
4467 .and_then(|i| tcx.opt_item_ident(i.container_id(tcx)))
4468 {
4469 assoc_span.push_span_label(ident.span, "in this trait");
4470 }
4471 err.span_note(assoc_span, msg);
4472 }
4473 ObligationCauseCode::TrivialBound => {
4474 tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
4475 }
4476 ObligationCauseCode::OpaqueReturnType(expr_info) => {
4477 let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
4478 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
4479 let expr = tcx.hir_expect_expr(hir_id);
4480 (expr_ty, expr)
4481 } else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
4482 && let body = tcx.hir_body(body_id)
4483 && let hir::ExprKind::Block(block, _) = body.value.kind
4484 && let Some(expr) = block.expr
4485 && let Some(expr_ty) = self
4486 .typeck_results
4487 .as_ref()
4488 .and_then(|typeck| typeck.node_type_opt(expr.hir_id))
4489 && let Some(pred) = predicate.as_clause()
4490 && let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
4491 && self.can_eq(param_env, pred.self_ty(), expr_ty)
4492 {
4493 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
4494 (expr_ty, expr)
4495 } else {
4496 return;
4497 };
4498 err.span_label(
4499 expr.span,
4500 {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("return type was inferred to be `{0}` here",
expr_ty))
})
}with_forced_trimmed_paths!(format!(
4501 "return type was inferred to be `{expr_ty}` here",
4502 )),
4503 );
4504 suggest_remove_deref(err, &expr);
4505 }
4506 ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
4507 err.span_note(
4508 span,
4509 "unsized values must be place expressions and cannot be put in temporaries",
4510 );
4511 }
4512 ObligationCauseCode::CompareEii { .. } => {
4513 {
::core::panicking::panic_fmt(format_args!("trait bounds on EII not yet supported "));
}panic!("trait bounds on EII not yet supported ")
4514 }
4515 }
4516 }
4517
4518 #[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("suggest_await_before_try",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(4518u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["obligation",
"trait_pred", "span", "trait_pred.self_ty"],
::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(&obligation)
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(&trait_pred)
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(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&trait_pred.self_ty())
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: () = loop {};
return __tracing_attr_fake_return;
}
{
let future_trait =
self.tcx.require_lang_item(LangItem::Future, span);
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
let impls_future =
self.type_implements_trait(future_trait,
[self.tcx.instantiate_bound_regions_with_erased(self_ty)],
obligation.param_env);
if !impls_future.must_apply_modulo_regions() { return; }
let item_def_id =
self.tcx.associated_item_def_ids(future_trait)[0];
let projection_ty =
trait_pred.map_bound(|trait_pred|
{
Ty::new_projection(self.tcx, item_def_id,
[trait_pred.self_ty()])
});
let InferOk { value: projection_ty, .. } =
self.at(&obligation.cause,
obligation.param_env).normalize(Unnormalized::new_wip(projection_ty));
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:4554",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(4554u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["normalized_projection_type"],
::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(&debug(&self.resolve_vars_if_possible(projection_ty))
as &dyn Value))])
});
} else { ; }
};
let try_obligation =
self.mk_trait_obligation_with_new_self_ty(obligation.param_env,
trait_pred.map_bound(|trait_pred|
(trait_pred, projection_ty.skip_binder())));
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:4561",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(4561u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["try_trait_obligation"],
::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(&debug(&try_obligation)
as &dyn Value))])
});
} else { ; }
};
if self.predicate_may_hold(&try_obligation) &&
let Ok(snippet) =
self.tcx.sess.source_map().span_to_snippet(span) &&
snippet.ends_with('?') {
match self.tcx.coroutine_kind(obligation.cause.body_id) {
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
_)) => {
err.span_suggestion_verbose(span.with_hi(span.hi() -
BytePos(1)).shrink_to_hi(),
"consider `await`ing on the `Future`", ".await",
Applicability::MaybeIncorrect);
}
_ => {
let mut span: MultiSpan =
span.with_lo(span.hi() - BytePos(1)).into();
span.push_span_label(self.tcx.def_span(obligation.cause.body_id),
"this is not `async`");
err.span_note(span,
"this implements `Future` and its output type supports \
`?`, but the future cannot be awaited in a synchronous function");
}
}
}
}
}
}#[instrument(
4519 level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
4520 )]
4521 pub(super) fn suggest_await_before_try(
4522 &self,
4523 err: &mut Diag<'_>,
4524 obligation: &PredicateObligation<'tcx>,
4525 trait_pred: ty::PolyTraitPredicate<'tcx>,
4526 span: Span,
4527 ) {
4528 let future_trait = self.tcx.require_lang_item(LangItem::Future, span);
4529
4530 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
4531 let impls_future = self.type_implements_trait(
4532 future_trait,
4533 [self.tcx.instantiate_bound_regions_with_erased(self_ty)],
4534 obligation.param_env,
4535 );
4536 if !impls_future.must_apply_modulo_regions() {
4537 return;
4538 }
4539
4540 let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
4541 let projection_ty = trait_pred.map_bound(|trait_pred| {
4543 Ty::new_projection(
4544 self.tcx,
4545 item_def_id,
4546 [trait_pred.self_ty()],
4548 )
4549 });
4550 let InferOk { value: projection_ty, .. } = self
4551 .at(&obligation.cause, obligation.param_env)
4552 .normalize(Unnormalized::new_wip(projection_ty));
4553
4554 debug!(
4555 normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
4556 );
4557 let try_obligation = self.mk_trait_obligation_with_new_self_ty(
4558 obligation.param_env,
4559 trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
4560 );
4561 debug!(try_trait_obligation = ?try_obligation);
4562 if self.predicate_may_hold(&try_obligation)
4563 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
4564 && snippet.ends_with('?')
4565 {
4566 match self.tcx.coroutine_kind(obligation.cause.body_id) {
4567 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
4568 err.span_suggestion_verbose(
4569 span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
4570 "consider `await`ing on the `Future`",
4571 ".await",
4572 Applicability::MaybeIncorrect,
4573 );
4574 }
4575 _ => {
4576 let mut span: MultiSpan = span.with_lo(span.hi() - BytePos(1)).into();
4577 span.push_span_label(
4578 self.tcx.def_span(obligation.cause.body_id),
4579 "this is not `async`",
4580 );
4581 err.span_note(
4582 span,
4583 "this implements `Future` and its output type supports \
4584 `?`, but the future cannot be awaited in a synchronous function",
4585 );
4586 }
4587 }
4588 }
4589 }
4590
4591 pub(super) fn suggest_floating_point_literal(
4592 &self,
4593 obligation: &PredicateObligation<'tcx>,
4594 err: &mut Diag<'_>,
4595 trait_pred: ty::PolyTraitPredicate<'tcx>,
4596 ) {
4597 let rhs_span = match obligation.cause.code() {
4598 ObligationCauseCode::BinOp { rhs_span, rhs_is_lit, .. } if *rhs_is_lit => rhs_span,
4599 _ => return,
4600 };
4601 if let ty::Float(_) = trait_pred.skip_binder().self_ty().kind()
4602 && let ty::Infer(InferTy::IntVar(_)) =
4603 trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4604 {
4605 err.span_suggestion_verbose(
4606 rhs_span.shrink_to_hi(),
4607 "consider using a floating-point literal by writing it with `.0`",
4608 ".0",
4609 Applicability::MaybeIncorrect,
4610 );
4611 }
4612 }
4613
4614 pub fn can_suggest_derive(
4615 &self,
4616 obligation: &PredicateObligation<'tcx>,
4617 trait_pred: ty::PolyTraitPredicate<'tcx>,
4618 ) -> bool {
4619 if trait_pred.polarity() == ty::PredicatePolarity::Negative {
4620 return false;
4621 }
4622 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
4623 return false;
4624 };
4625 let (adt, args) = match trait_pred.skip_binder().self_ty().kind() {
4626 ty::Adt(adt, args) if adt.did().is_local() => (adt, args),
4627 _ => return false,
4628 };
4629 let is_derivable_trait = match diagnostic_name {
4630 sym::Copy | sym::Clone => true,
4631 _ if adt.is_union() => false,
4632 sym::PartialEq | sym::PartialOrd => {
4633 let rhs_ty = trait_pred.skip_binder().trait_ref.args.type_at(1);
4634 trait_pred.skip_binder().self_ty() == rhs_ty
4635 }
4636 sym::Eq | sym::Ord | sym::Hash | sym::Debug | sym::Default => true,
4637 _ => false,
4638 };
4639 is_derivable_trait &&
4640 adt.all_fields().all(|field| {
4642 let field_ty = ty::GenericArg::from(field.ty(self.tcx, args));
4643 let trait_args = match diagnostic_name {
4644 sym::PartialEq | sym::PartialOrd => {
4645 Some(field_ty)
4646 }
4647 _ => None,
4648 };
4649 let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
4650 trait_ref: ty::TraitRef::new(self.tcx,
4651 trait_pred.def_id(),
4652 [field_ty].into_iter().chain(trait_args),
4653 ),
4654 ..*tr
4655 });
4656 let field_obl = Obligation::new(
4657 self.tcx,
4658 obligation.cause.clone(),
4659 obligation.param_env,
4660 trait_pred,
4661 );
4662 self.predicate_must_hold_modulo_regions(&field_obl)
4663 })
4664 }
4665
4666 pub fn suggest_derive(
4667 &self,
4668 obligation: &PredicateObligation<'tcx>,
4669 err: &mut Diag<'_>,
4670 trait_pred: ty::PolyTraitPredicate<'tcx>,
4671 ) {
4672 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
4673 return;
4674 };
4675 let adt = match trait_pred.skip_binder().self_ty().kind() {
4676 ty::Adt(adt, _) if adt.did().is_local() => adt,
4677 _ => return,
4678 };
4679 if self.can_suggest_derive(obligation, trait_pred) {
4680 err.span_suggestion_verbose(
4681 self.tcx.def_span(adt.did()).shrink_to_lo(),
4682 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider annotating `{0}` with `#[derive({1})]`",
trait_pred.skip_binder().self_ty(), diagnostic_name))
})format!(
4683 "consider annotating `{}` with `#[derive({})]`",
4684 trait_pred.skip_binder().self_ty(),
4685 diagnostic_name,
4686 ),
4687 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#[derive({0})]\n",
diagnostic_name))
})format!("#[derive({diagnostic_name})]\n"),
4689 Applicability::MaybeIncorrect,
4690 );
4691 }
4692 }
4693
4694 pub(super) fn suggest_dereferencing_index(
4695 &self,
4696 obligation: &PredicateObligation<'tcx>,
4697 err: &mut Diag<'_>,
4698 trait_pred: ty::PolyTraitPredicate<'tcx>,
4699 ) {
4700 if let ObligationCauseCode::ImplDerived(_) = obligation.cause.code()
4701 && self
4702 .tcx
4703 .is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
4704 && let ty::Slice(_) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4705 && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
4706 && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
4707 {
4708 err.span_suggestion_verbose(
4709 obligation.cause.span.shrink_to_lo(),
4710 "dereference this index",
4711 '*',
4712 Applicability::MachineApplicable,
4713 );
4714 }
4715 }
4716
4717 fn note_function_argument_obligation<G: EmissionGuarantee>(
4718 &self,
4719 body_id: LocalDefId,
4720 err: &mut Diag<'_, G>,
4721 arg_hir_id: HirId,
4722 parent_code: &ObligationCauseCode<'tcx>,
4723 param_env: ty::ParamEnv<'tcx>,
4724 failed_pred: ty::Predicate<'tcx>,
4725 call_hir_id: HirId,
4726 ) {
4727 let tcx = self.tcx;
4728 if let Node::Expr(expr) = tcx.hir_node(arg_hir_id)
4729 && let Some(typeck_results) = &self.typeck_results
4730 {
4731 if let hir::Expr { kind: hir::ExprKind::MethodCall(_, rcvr, _, _), .. } = expr
4732 && let Some(ty) = typeck_results.node_type_opt(rcvr.hir_id)
4733 && let Some(failed_pred) = failed_pred.as_trait_clause()
4734 && let pred = failed_pred.map_bound(|pred| pred.with_replaced_self_ty(tcx, ty))
4735 && self.predicate_must_hold_modulo_regions(&Obligation::misc(
4736 tcx, expr.span, body_id, param_env, pred,
4737 ))
4738 && expr.span.hi() != rcvr.span.hi()
4739 {
4740 let should_sugg = match tcx.hir_node(call_hir_id) {
4741 Node::Expr(hir::Expr {
4742 kind: hir::ExprKind::MethodCall(_, call_receiver, _, _),
4743 ..
4744 }) if let Some((DefKind::AssocFn, did)) =
4745 typeck_results.type_dependent_def(call_hir_id)
4746 && call_receiver.hir_id == arg_hir_id =>
4747 {
4748 if tcx.inherent_impl_of_assoc(did).is_some() {
4752 Some(ty) == typeck_results.node_type_opt(arg_hir_id)
4754 } else {
4755 let trait_id = tcx
4757 .trait_of_assoc(did)
4758 .unwrap_or_else(|| tcx.impl_trait_id(tcx.parent(did)));
4759 let args = typeck_results.node_args(call_hir_id);
4760 let tr = ty::TraitRef::from_assoc(tcx, trait_id, args)
4761 .with_replaced_self_ty(tcx, ty);
4762 self.type_implements_trait(tr.def_id, tr.args, param_env)
4763 .must_apply_modulo_regions()
4764 }
4765 }
4766 _ => true,
4767 };
4768
4769 if should_sugg {
4770 err.span_suggestion_verbose(
4771 expr.span.with_lo(rcvr.span.hi()),
4772 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider removing this method call, as the receiver has type `{0}` and `{1}` trivially holds",
ty, pred))
})format!(
4773 "consider removing this method call, as the receiver has type `{ty}` and \
4774 `{pred}` trivially holds",
4775 ),
4776 "",
4777 Applicability::MaybeIncorrect,
4778 );
4779 }
4780 }
4781 if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
4782 let inner_expr = expr.peel_blocks();
4783 let ty = typeck_results
4784 .expr_ty_adjusted_opt(inner_expr)
4785 .unwrap_or(Ty::new_misc_error(tcx));
4786 let span = inner_expr.span;
4787 if Some(span) != err.span.primary_span()
4788 && !span.in_external_macro(tcx.sess.source_map())
4789 {
4790 err.span_label(
4791 span,
4792 if ty.references_error() {
4793 String::new()
4794 } else {
4795 let ty = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
4796 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this tail expression is of type `{0}`",
ty))
})format!("this tail expression is of type `{ty}`")
4797 },
4798 );
4799 if let ty::PredicateKind::Clause(clause) = failed_pred.kind().skip_binder()
4800 && let ty::ClauseKind::Trait(pred) = clause
4801 && tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
4802 {
4803 if let [stmt, ..] = block.stmts
4804 && let hir::StmtKind::Semi(value) = stmt.kind
4805 && let hir::ExprKind::Closure(hir::Closure {
4806 body, fn_decl_span, ..
4807 }) = value.kind
4808 && let body = tcx.hir_body(*body)
4809 && !#[allow(non_exhaustive_omitted_patterns)] match body.value.kind {
hir::ExprKind::Block(..) => true,
_ => false,
}matches!(body.value.kind, hir::ExprKind::Block(..))
4810 {
4811 err.multipart_suggestion(
4814 "you might have meant to open the closure body instead of placing \
4815 a closure within a block",
4816 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(expr.span.with_hi(value.span.lo()), String::new()),
(fn_decl_span.shrink_to_hi(), " {".to_string())]))vec![
4817 (expr.span.with_hi(value.span.lo()), String::new()),
4818 (fn_decl_span.shrink_to_hi(), " {".to_string()),
4819 ],
4820 Applicability::MaybeIncorrect,
4821 );
4822 } else {
4823 err.span_suggestion_verbose(
4825 expr.span.shrink_to_lo(),
4826 "you might have meant to create the closure instead of a block",
4827 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("|{0}| ",
(0..pred.trait_ref.args.len() -
1).map(|_| "_").collect::<Vec<_>>().join(", ")))
})format!(
4828 "|{}| ",
4829 (0..pred.trait_ref.args.len() - 1)
4830 .map(|_| "_")
4831 .collect::<Vec<_>>()
4832 .join(", ")
4833 ),
4834 Applicability::MaybeIncorrect,
4835 );
4836 }
4837 }
4838 }
4839 }
4840
4841 let mut type_diffs = ::alloc::vec::Vec::new()vec![];
4846 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = *parent_code
4847 && let Some(node_args) = typeck_results.node_args_opt(call_hir_id)
4848 && let where_clauses =
4849 self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args)
4850 && let Some(where_pred) = where_clauses.predicates.get(idx)
4851 {
4852 let where_pred = where_pred.as_ref().skip_norm_wip();
4853 if let Some(where_pred) = where_pred.as_trait_clause()
4854 && let Some(failed_pred) = failed_pred.as_trait_clause()
4855 && where_pred.def_id() == failed_pred.def_id()
4856 {
4857 self.enter_forall(where_pred, |where_pred| {
4858 let failed_pred = self.instantiate_binder_with_fresh_vars(
4859 expr.span,
4860 BoundRegionConversionTime::FnCall,
4861 failed_pred,
4862 );
4863
4864 let zipped =
4865 iter::zip(where_pred.trait_ref.args, failed_pred.trait_ref.args);
4866 for (expected, actual) in zipped {
4867 self.probe(|_| {
4868 match self
4869 .at(&ObligationCause::misc(expr.span, body_id), param_env)
4870 .eq(DefineOpaqueTypes::Yes, expected, actual)
4873 {
4874 Ok(_) => (), Err(err) => type_diffs.push(err),
4876 }
4877 })
4878 }
4879 })
4880 } else if let Some(where_pred) = where_pred.as_projection_clause()
4881 && let Some(failed_pred) = failed_pred.as_projection_clause()
4882 && let Some(found) = failed_pred.skip_binder().term.as_type()
4883 {
4884 type_diffs = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[TypeError::Sorts(ty::error::ExpectedFound {
expected: where_pred.skip_binder().projection_term.expect_ty(self.tcx).to_ty(self.tcx),
found,
})]))vec![TypeError::Sorts(ty::error::ExpectedFound {
4885 expected: where_pred
4886 .skip_binder()
4887 .projection_term
4888 .expect_ty(self.tcx)
4889 .to_ty(self.tcx),
4890 found,
4891 })];
4892 }
4893 }
4894 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4895 && let hir::Path { res: Res::Local(hir_id), .. } = path
4896 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4897 && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
4898 && let Some(binding_expr) = local.init
4899 {
4900 self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
4904 } else {
4905 self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
4906 }
4907 }
4908 let call_node = tcx.hir_node(call_hir_id);
4909 if let Node::Expr(hir::Expr { kind: hir::ExprKind::MethodCall(path, rcvr, ..), .. }) =
4910 call_node
4911 {
4912 if Some(rcvr.span) == err.span.primary_span() {
4913 err.replace_span_with(path.ident.span, true);
4914 }
4915 }
4916
4917 if let Node::Expr(expr) = call_node {
4918 if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
4919 | hir::ExprKind::MethodCall(
4920 hir::PathSegment { ident: Ident { span, .. }, .. },
4921 ..,
4922 ) = expr.kind
4923 {
4924 if Some(*span) != err.span.primary_span() {
4925 let msg = if span.is_desugaring(DesugaringKind::FormatLiteral { source: true })
4926 {
4927 "required by this formatting parameter"
4928 } else if span.is_desugaring(DesugaringKind::FormatLiteral { source: false }) {
4929 "required by a formatting parameter in this expression"
4930 } else {
4931 "required by a bound introduced by this call"
4932 };
4933 err.span_label(*span, msg);
4934 }
4935 }
4936
4937 if let hir::ExprKind::MethodCall(_, expr, ..) = expr.kind {
4938 self.suggest_option_method_if_applicable(failed_pred, param_env, err, expr);
4939 }
4940 }
4941 }
4942
4943 fn suggest_option_method_if_applicable<G: EmissionGuarantee>(
4944 &self,
4945 failed_pred: ty::Predicate<'tcx>,
4946 param_env: ty::ParamEnv<'tcx>,
4947 err: &mut Diag<'_, G>,
4948 expr: &hir::Expr<'_>,
4949 ) {
4950 let tcx = self.tcx;
4951 let infcx = self.infcx;
4952 let Some(typeck_results) = self.typeck_results.as_ref() else { return };
4953
4954 let Some(option_ty_adt) = typeck_results.expr_ty_adjusted(expr).ty_adt_def() else {
4956 return;
4957 };
4958 if !tcx.is_diagnostic_item(sym::Option, option_ty_adt.did()) {
4959 return;
4960 }
4961
4962 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, .. }))
4965 = failed_pred.kind().skip_binder()
4966 && tcx.is_fn_trait(trait_ref.def_id)
4967 && let [self_ty, found_ty] = trait_ref.args.as_slice()
4968 && let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn())
4969 && let fn_sig @ ty::FnSig {
4970 ..
4971 } = fn_ty.fn_sig(tcx).skip_binder()
4972 && fn_sig.abi() == ExternAbi::Rust
4973 && !fn_sig.c_variadic()
4974 && fn_sig.safety() == hir::Safety::Safe
4975
4976 && let Some(&ty::Ref(_, target_ty, needs_mut)) = fn_sig.inputs().first().map(|t| t.kind())
4978 && !target_ty.has_escaping_bound_vars()
4979
4980 && let Some(ty::Tuple(tys)) = found_ty.as_type().map(Ty::kind)
4982 && let &[found_ty] = tys.as_slice()
4983 && !found_ty.has_escaping_bound_vars()
4984
4985 && let Some(deref_target_did) = tcx.lang_items().deref_target()
4987 && let projection = Ty::new_projection_from_args(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)]))
4988 && let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(Unnormalized::new_wip(projection))
4989 && obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation))
4990 && infcx.can_eq(param_env, deref_target, target_ty)
4991 {
4992 let help = if let hir::Mutability::Mut = needs_mut
4993 && let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait()
4994 && infcx
4995 .type_implements_trait(deref_mut_did, iter::once(found_ty), param_env)
4996 .must_apply_modulo_regions()
4997 {
4998 Some(("call `Option::as_deref_mut()` first", ".as_deref_mut()"))
4999 } else if let hir::Mutability::Not = needs_mut {
5000 Some(("call `Option::as_deref()` first", ".as_deref()"))
5001 } else {
5002 None
5003 };
5004
5005 if let Some((msg, sugg)) = help {
5006 err.span_suggestion_with_style(
5007 expr.span.shrink_to_hi(),
5008 msg,
5009 sugg,
5010 Applicability::MaybeIncorrect,
5011 SuggestionStyle::ShowAlways,
5012 );
5013 }
5014 }
5015 }
5016
5017 fn look_for_iterator_item_mistakes<G: EmissionGuarantee>(
5018 &self,
5019 assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
5020 typeck_results: &TypeckResults<'tcx>,
5021 type_diffs: &[TypeError<'tcx>],
5022 param_env: ty::ParamEnv<'tcx>,
5023 path_segment: &hir::PathSegment<'_>,
5024 args: &[hir::Expr<'_>],
5025 prev_ty: Ty<'_>,
5026 err: &mut Diag<'_, G>,
5027 ) {
5028 let tcx = self.tcx;
5029 for entry in assocs_in_this_method {
5032 let Some((_span, (def_id, ty))) = entry else {
5033 continue;
5034 };
5035 for diff in type_diffs {
5036 let TypeError::Sorts(expected_found) = diff else {
5037 continue;
5038 };
5039 if tcx.is_diagnostic_item(sym::IntoIteratorItem, *def_id)
5040 && path_segment.ident.name == sym::iter
5041 && self.can_eq(
5042 param_env,
5043 Ty::new_ref(
5044 tcx,
5045 tcx.lifetimes.re_erased,
5046 expected_found.found,
5047 ty::Mutability::Not,
5048 ),
5049 *ty,
5050 )
5051 && let [] = args
5052 {
5053 err.span_suggestion_verbose(
5055 path_segment.ident.span,
5056 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider consuming the `{0}` to construct the `Iterator`",
prev_ty))
})format!("consider consuming the `{prev_ty}` to construct the `Iterator`"),
5057 "into_iter".to_string(),
5058 Applicability::MachineApplicable,
5059 );
5060 }
5061 if tcx.is_diagnostic_item(sym::IntoIteratorItem, *def_id)
5062 && path_segment.ident.name == sym::into_iter
5063 && self.can_eq(
5064 param_env,
5065 expected_found.found,
5066 Ty::new_ref(tcx, tcx.lifetimes.re_erased, *ty, ty::Mutability::Not),
5067 )
5068 && let [] = args
5069 {
5070 err.span_suggestion_verbose(
5072 path_segment.ident.span,
5073 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider not consuming the `{0}` to construct the `Iterator`",
prev_ty))
})format!(
5074 "consider not consuming the `{prev_ty}` to construct the `Iterator`"
5075 ),
5076 "iter".to_string(),
5077 Applicability::MachineApplicable,
5078 );
5079 }
5080 if tcx.is_diagnostic_item(sym::IteratorItem, *def_id)
5081 && path_segment.ident.name == sym::map
5082 && self.can_eq(param_env, expected_found.found, *ty)
5083 && let [arg] = args
5084 && let hir::ExprKind::Closure(closure) = arg.kind
5085 {
5086 let body = tcx.hir_body(closure.body);
5087 if let hir::ExprKind::Block(block, None) = body.value.kind
5088 && let None = block.expr
5089 && let [.., stmt] = block.stmts
5090 && let hir::StmtKind::Semi(expr) = stmt.kind
5091 && expected_found.found.is_unit()
5095 && expr.span.hi() != stmt.span.hi()
5100 {
5101 err.span_suggestion_verbose(
5102 expr.span.shrink_to_hi().with_hi(stmt.span.hi()),
5103 "consider removing this semicolon",
5104 String::new(),
5105 Applicability::MachineApplicable,
5106 );
5107 }
5108 let expr = if let hir::ExprKind::Block(block, None) = body.value.kind
5109 && let Some(expr) = block.expr
5110 {
5111 expr
5112 } else {
5113 body.value
5114 };
5115 if let hir::ExprKind::MethodCall(path_segment, rcvr, [], span) = expr.kind
5116 && path_segment.ident.name == sym::clone
5117 && let Some(expr_ty) = typeck_results.expr_ty_opt(expr)
5118 && let Some(rcvr_ty) = typeck_results.expr_ty_opt(rcvr)
5119 && self.can_eq(param_env, expr_ty, rcvr_ty)
5120 && let ty::Ref(_, ty, _) = expr_ty.kind()
5121 {
5122 err.span_label(
5123 span,
5124 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this method call is cloning the reference `{0}`, not `{1}` which doesn\'t implement `Clone`",
expr_ty, ty))
})format!(
5125 "this method call is cloning the reference `{expr_ty}`, not \
5126 `{ty}` which doesn't implement `Clone`",
5127 ),
5128 );
5129 let ty::Param(..) = ty.kind() else {
5130 continue;
5131 };
5132 let node =
5133 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(expr.hir_id).def_id);
5134
5135 let pred = ty::Binder::dummy(ty::TraitPredicate {
5136 trait_ref: ty::TraitRef::new(
5137 tcx,
5138 tcx.require_lang_item(LangItem::Clone, span),
5139 [*ty],
5140 ),
5141 polarity: ty::PredicatePolarity::Positive,
5142 });
5143 let Some(generics) = node.generics() else {
5144 continue;
5145 };
5146 let Some(body_id) = node.body_id() else {
5147 continue;
5148 };
5149 suggest_restriction(
5150 tcx,
5151 tcx.hir_body_owner_def_id(body_id),
5152 generics,
5153 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type parameter `{0}`", ty))
})format!("type parameter `{ty}`"),
5154 err,
5155 node.fn_sig(),
5156 None,
5157 pred,
5158 None,
5159 );
5160 }
5161 }
5162 }
5163 }
5164 }
5165
5166 fn point_at_chain<G: EmissionGuarantee>(
5167 &self,
5168 expr: &hir::Expr<'_>,
5169 typeck_results: &TypeckResults<'tcx>,
5170 type_diffs: Vec<TypeError<'tcx>>,
5171 param_env: ty::ParamEnv<'tcx>,
5172 err: &mut Diag<'_, G>,
5173 ) {
5174 let mut primary_spans = ::alloc::vec::Vec::new()vec![];
5175 let mut span_labels = ::alloc::vec::Vec::new()vec![];
5176
5177 let tcx = self.tcx;
5178
5179 let mut print_root_expr = true;
5180 let mut assocs = ::alloc::vec::Vec::new()vec![];
5181 let mut expr = expr;
5182 let mut prev_ty = self.resolve_vars_if_possible(
5183 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
5184 );
5185 while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
5186 expr = rcvr_expr;
5190 let assocs_in_this_method =
5191 self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
5192 prev_ty = self.resolve_vars_if_possible(
5193 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
5194 );
5195 self.look_for_iterator_item_mistakes(
5196 &assocs_in_this_method,
5197 typeck_results,
5198 &type_diffs,
5199 param_env,
5200 path_segment,
5201 args,
5202 prev_ty,
5203 err,
5204 );
5205 assocs.push(assocs_in_this_method);
5206
5207 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
5208 && let hir::Path { res: Res::Local(hir_id), .. } = path
5209 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
5210 {
5211 let parent = self.tcx.parent_hir_node(binding.hir_id);
5212 if let hir::Node::LetStmt(local) = parent
5214 && let Some(binding_expr) = local.init
5215 {
5216 expr = binding_expr;
5218 }
5219 if let hir::Node::Param(param) = parent {
5220 let prev_ty = self.resolve_vars_if_possible(
5222 typeck_results
5223 .node_type_opt(param.hir_id)
5224 .unwrap_or(Ty::new_misc_error(tcx)),
5225 );
5226 let assocs_in_this_method = self.probe_assoc_types_at_expr(
5227 &type_diffs,
5228 param.ty_span,
5229 prev_ty,
5230 param.hir_id,
5231 param_env,
5232 );
5233 if assocs_in_this_method.iter().any(|a| a.is_some()) {
5234 assocs.push(assocs_in_this_method);
5235 print_root_expr = false;
5236 }
5237 break;
5238 }
5239 }
5240 }
5241 if let Some(ty) = typeck_results.expr_ty_opt(expr)
5244 && print_root_expr
5245 {
5246 let ty = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
5247 span_labels.push((expr.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this expression has type `{0}`",
ty))
})format!("this expression has type `{ty}`")));
5251 };
5252 let mut assocs = assocs.into_iter().peekable();
5255 while let Some(assocs_in_method) = assocs.next() {
5256 let Some(prev_assoc_in_method) = assocs.peek() else {
5257 for entry in assocs_in_method {
5258 let Some((span, (assoc, ty))) = entry else {
5259 continue;
5260 };
5261 if primary_spans.is_empty()
5262 || type_diffs.iter().any(|diff| {
5263 let TypeError::Sorts(expected_found) = diff else {
5264 return false;
5265 };
5266 self.can_eq(param_env, expected_found.found, ty)
5267 })
5268 {
5269 primary_spans.push(span);
5275 }
5276 span_labels.push((
5277 span,
5278 {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is `{1}` here",
self.tcx.def_path_str(assoc), ty))
})
}with_forced_trimmed_paths!(format!(
5279 "`{}` is `{ty}` here",
5280 self.tcx.def_path_str(assoc),
5281 )),
5282 ));
5283 }
5284 break;
5285 };
5286 for (entry, prev_entry) in
5287 assocs_in_method.into_iter().zip(prev_assoc_in_method.into_iter())
5288 {
5289 match (entry, prev_entry) {
5290 (Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
5291 let ty_str = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
5292
5293 let assoc = { let _guard = ForceTrimmedGuard::new(); self.tcx.def_path_str(assoc) }with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
5294 if !self.can_eq(param_env, ty, *prev_ty) {
5295 if type_diffs.iter().any(|diff| {
5296 let TypeError::Sorts(expected_found) = diff else {
5297 return false;
5298 };
5299 self.can_eq(param_env, expected_found.found, ty)
5300 }) {
5301 primary_spans.push(span);
5302 }
5303 span_labels
5304 .push((span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` changed to `{1}` here",
assoc, ty_str))
})format!("`{assoc}` changed to `{ty_str}` here")));
5305 } else {
5306 span_labels.push((span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` remains `{1}` here", assoc,
ty_str))
})format!("`{assoc}` remains `{ty_str}` here")));
5307 }
5308 }
5309 (Some((span, (assoc, ty))), None) => {
5310 span_labels.push((
5311 span,
5312 {
let _guard = ForceTrimmedGuard::new();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is `{1}` here",
self.tcx.def_path_str(assoc), self.ty_to_string(ty)))
})
}with_forced_trimmed_paths!(format!(
5313 "`{}` is `{}` here",
5314 self.tcx.def_path_str(assoc),
5315 self.ty_to_string(ty),
5316 )),
5317 ));
5318 }
5319 (None, Some(_)) | (None, None) => {}
5320 }
5321 }
5322 }
5323 if !primary_spans.is_empty() {
5324 let mut multi_span: MultiSpan = primary_spans.into();
5325 for (span, label) in span_labels {
5326 multi_span.push_span_label(span, label);
5327 }
5328 err.span_note(
5329 multi_span,
5330 "the method call chain might not have had the expected associated types",
5331 );
5332 }
5333 }
5334
5335 fn probe_assoc_types_at_expr(
5336 &self,
5337 type_diffs: &[TypeError<'tcx>],
5338 span: Span,
5339 prev_ty: Ty<'tcx>,
5340 body_id: HirId,
5341 param_env: ty::ParamEnv<'tcx>,
5342 ) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>> {
5343 let ocx = ObligationCtxt::new(self.infcx);
5344 let mut assocs_in_this_method = Vec::with_capacity(type_diffs.len());
5345 for diff in type_diffs {
5346 let TypeError::Sorts(expected_found) = diff else {
5347 continue;
5348 };
5349 let &ty::Alias(ty::AliasTy { kind: kind @ ty::Projection { def_id }, .. }) =
5350 expected_found.expected.kind()
5351 else {
5352 continue;
5353 };
5354
5355 let args = GenericArgs::for_item(self.tcx, def_id, |param, _| {
5359 if param.index == 0 {
5360 if true {
{
match param.kind {
ty::GenericParamDefKind::Type { .. } => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"ty::GenericParamDefKind::Type { .. }",
::core::option::Option::None);
}
}
};
};debug_assert_matches!(param.kind, ty::GenericParamDefKind::Type { .. });
5361 return prev_ty.into();
5362 }
5363 self.var_for_def(span, param)
5364 });
5365 let ty = self.infcx.next_ty_var(span);
5369 let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
5371 ty::ClauseKind::Projection(ty::ProjectionPredicate {
5372 projection_term: ty::AliasTerm::new_from_args(self.tcx, kind.into(), args),
5373 term: ty.into(),
5374 }),
5375 ));
5376 let body_def_id = self.tcx.hir_enclosing_body_owner(body_id);
5377 ocx.register_obligation(Obligation::misc(
5379 self.tcx,
5380 span,
5381 body_def_id,
5382 param_env,
5383 projection,
5384 ));
5385 if ocx.try_evaluate_obligations().is_empty()
5386 && let ty = self.resolve_vars_if_possible(ty)
5387 && !ty.is_ty_var()
5388 {
5389 assocs_in_this_method.push(Some((span, (def_id, ty))));
5390 } else {
5391 assocs_in_this_method.push(None);
5396 }
5397 }
5398 assocs_in_this_method
5399 }
5400
5401 pub(super) fn suggest_convert_to_slice(
5405 &self,
5406 err: &mut Diag<'_>,
5407 obligation: &PredicateObligation<'tcx>,
5408 trait_pred: ty::PolyTraitPredicate<'tcx>,
5409 candidate_impls: &[ImplCandidate<'tcx>],
5410 span: Span,
5411 ) {
5412 if span.in_external_macro(self.tcx.sess.source_map()) {
5413 return;
5414 }
5415 let (ObligationCauseCode::BinOp { .. } | ObligationCauseCode::FunctionArg { .. }) =
5418 obligation.cause.code()
5419 else {
5420 return;
5421 };
5422
5423 let (element_ty, mut mutability) = match *trait_pred.skip_binder().self_ty().kind() {
5428 ty::Array(element_ty, _) => (element_ty, None),
5429
5430 ty::Ref(_, pointee_ty, mutability) => match *pointee_ty.kind() {
5431 ty::Array(element_ty, _) => (element_ty, Some(mutability)),
5432 _ => return,
5433 },
5434
5435 _ => return,
5436 };
5437
5438 let mut is_slice = |candidate: Ty<'tcx>| match *candidate.kind() {
5441 ty::RawPtr(t, m) | ty::Ref(_, t, m) => {
5442 if let ty::Slice(e) = *t.kind()
5443 && e == element_ty
5444 && m == mutability.unwrap_or(m)
5445 {
5446 mutability = Some(m);
5448 true
5449 } else {
5450 false
5451 }
5452 }
5453 _ => false,
5454 };
5455
5456 if let Some(slice_ty) = candidate_impls
5458 .iter()
5459 .map(|trait_ref| trait_ref.trait_ref.self_ty())
5460 .find(|t| is_slice(*t))
5461 {
5462 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("convert the array to a `{0}` slice instead",
slice_ty))
})format!("convert the array to a `{slice_ty}` slice instead");
5463
5464 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
5465 let mut suggestions = ::alloc::vec::Vec::new()vec![];
5466 if snippet.starts_with('&') {
5467 } else if let Some(hir::Mutability::Mut) = mutability {
5468 suggestions.push((span.shrink_to_lo(), "&mut ".into()));
5469 } else {
5470 suggestions.push((span.shrink_to_lo(), "&".into()));
5471 }
5472 suggestions.push((span.shrink_to_hi(), "[..]".into()));
5473 err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect);
5474 } else {
5475 err.span_help(span, msg);
5476 }
5477 }
5478 }
5479
5480 pub(super) fn suggest_tuple_wrapping(
5485 &self,
5486 err: &mut Diag<'_>,
5487 root_obligation: &PredicateObligation<'tcx>,
5488 obligation: &PredicateObligation<'tcx>,
5489 ) {
5490 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() else {
5491 return;
5492 };
5493
5494 let Some(root_pred) = root_obligation.predicate.as_trait_clause() else { return };
5495
5496 let trait_ref = root_pred.map_bound(|root_pred| {
5497 root_pred.trait_ref.with_replaced_self_ty(
5498 self.tcx,
5499 Ty::new_tup(self.tcx, &[root_pred.trait_ref.self_ty()]),
5500 )
5501 });
5502
5503 let obligation =
5504 Obligation::new(self.tcx, obligation.cause.clone(), obligation.param_env, trait_ref);
5505
5506 if self.predicate_must_hold_modulo_regions(&obligation) {
5507 let arg_span = self.tcx.hir_span(*arg_hir_id);
5508 err.multipart_suggestion(
5509 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use a unary tuple instead"))
})format!("use a unary tuple instead"),
5510 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(arg_span.shrink_to_lo(), "(".into()),
(arg_span.shrink_to_hi(), ",)".into())]))vec![(arg_span.shrink_to_lo(), "(".into()), (arg_span.shrink_to_hi(), ",)".into())],
5511 Applicability::MaybeIncorrect,
5512 );
5513 }
5514 }
5515
5516 pub(super) fn suggest_shadowed_inherent_method(
5517 &self,
5518 err: &mut Diag<'_>,
5519 obligation: &PredicateObligation<'tcx>,
5520 trait_predicate: ty::PolyTraitPredicate<'tcx>,
5521 ) {
5522 let ObligationCauseCode::FunctionArg { call_hir_id, .. } = obligation.cause.code() else {
5523 return;
5524 };
5525 let Node::Expr(call) = self.tcx.hir_node(*call_hir_id) else { return };
5526 let hir::ExprKind::MethodCall(segment, rcvr, args, ..) = call.kind else { return };
5527 let Some(typeck) = &self.typeck_results else { return };
5528 let Some(rcvr_ty) = typeck.expr_ty_adjusted_opt(rcvr) else { return };
5529 let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
5530 let autoderef = (self.autoderef_steps)(rcvr_ty);
5531 for (ty, def_id) in autoderef.iter().filter_map(|(ty, obligations)| {
5532 if let ty::Adt(def, _) = ty.kind()
5533 && *ty != rcvr_ty.peel_refs()
5534 && obligations.iter().all(|obligation| self.predicate_may_hold(obligation))
5535 {
5536 Some((ty, def.did()))
5537 } else {
5538 None
5539 }
5540 }) {
5541 for impl_def_id in self.tcx.inherent_impls(def_id) {
5542 if *impl_def_id == trait_predicate.def_id() {
5543 continue;
5544 }
5545 for m in self
5546 .tcx
5547 .provided_trait_methods(*impl_def_id)
5548 .filter(|m| m.name() == segment.ident.name)
5549 {
5550 let fn_sig = self.tcx.fn_sig(m.def_id);
5551 if fn_sig.skip_binder().inputs().skip_binder().len() != args.len() + 1 {
5552 continue;
5553 }
5554 let rcvr_ty = fn_sig.skip_binder().input(0).skip_binder();
5555 let (mutability, _ty) = match rcvr_ty.kind() {
5556 ty::Ref(_, ty, hir::Mutability::Mut) => ("&mut ", ty),
5557 ty::Ref(_, ty, _) => ("&", ty),
5558 _ => ("", &rcvr_ty),
5559 };
5560 let path = self.tcx.def_path_str(def_id);
5561 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there\'s an inherent method on `{0}` of the same name, which can be auto-dereferenced from `{1}`",
ty, rcvr_ty))
})format!(
5562 "there's an inherent method on `{ty}` of the same name, which can be \
5563 auto-dereferenced from `{rcvr_ty}`"
5564 ));
5565 err.multipart_suggestion(
5566 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to access the inherent method on `{0}`, use the fully-qualified path",
ty))
})format!(
5567 "to access the inherent method on `{ty}`, use the fully-qualified path",
5568 ),
5569 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(call.span.until(rcvr.span),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2}::{0}({1}", m.name(),
mutability, path))
})),
match &args {
[] =>
(rcvr.span.shrink_to_hi().with_hi(call.span.hi()),
")".to_string()),
[first, ..] =>
(rcvr.span.between(first.span), ", ".to_string()),
}]))vec![
5570 (
5571 call.span.until(rcvr.span),
5572 format!("{path}::{}({}", m.name(), mutability),
5573 ),
5574 match &args {
5575 [] => (
5576 rcvr.span.shrink_to_hi().with_hi(call.span.hi()),
5577 ")".to_string(),
5578 ),
5579 [first, ..] => (rcvr.span.between(first.span), ", ".to_string()),
5580 },
5581 ],
5582 Applicability::MaybeIncorrect,
5583 );
5584 }
5585 }
5586 }
5587 }
5588
5589 pub(super) fn explain_hrtb_projection(
5590 &self,
5591 diag: &mut Diag<'_>,
5592 pred: ty::PolyTraitPredicate<'tcx>,
5593 param_env: ty::ParamEnv<'tcx>,
5594 cause: &ObligationCause<'tcx>,
5595 ) {
5596 if pred.skip_binder().has_escaping_bound_vars() && pred.skip_binder().has_non_region_infer()
5597 {
5598 self.probe(|_| {
5599 let ocx = ObligationCtxt::new(self);
5600 self.enter_forall(pred, |pred| {
5601 let pred = ocx.normalize(
5602 &ObligationCause::dummy(),
5603 param_env,
5604 Unnormalized::new_wip(pred),
5605 );
5606 ocx.register_obligation(Obligation::new(
5607 self.tcx,
5608 ObligationCause::dummy(),
5609 param_env,
5610 pred,
5611 ));
5612 });
5613 if !ocx.try_evaluate_obligations().is_empty() {
5614 return;
5616 }
5617
5618 if let ObligationCauseCode::FunctionArg {
5619 call_hir_id,
5620 arg_hir_id,
5621 parent_code: _,
5622 } = cause.code()
5623 {
5624 let arg_span = self.tcx.hir_span(*arg_hir_id);
5625 let mut sp: MultiSpan = arg_span.into();
5626
5627 sp.push_span_label(
5628 arg_span,
5629 "the trait solver is unable to infer the \
5630 generic types that should be inferred from this argument",
5631 );
5632 sp.push_span_label(
5633 self.tcx.hir_span(*call_hir_id),
5634 "add turbofish arguments to this call to \
5635 specify the types manually, even if it's redundant",
5636 );
5637 diag.span_note(
5638 sp,
5639 "this is a known limitation of the trait solver that \
5640 will be lifted in the future",
5641 );
5642 } else {
5643 let mut sp: MultiSpan = cause.span.into();
5644 sp.push_span_label(
5645 cause.span,
5646 "try adding turbofish arguments to this expression to \
5647 specify the types manually, even if it's redundant",
5648 );
5649 diag.span_note(
5650 sp,
5651 "this is a known limitation of the trait solver that \
5652 will be lifted in the future",
5653 );
5654 }
5655 });
5656 }
5657 }
5658
5659 pub(super) fn suggest_desugaring_async_fn_in_trait(
5660 &self,
5661 err: &mut Diag<'_>,
5662 trait_pred: ty::PolyTraitPredicate<'tcx>,
5663 ) {
5664 if self.tcx.features().return_type_notation() {
5666 return;
5667 }
5668
5669 let trait_def_id = trait_pred.def_id();
5670
5671 if !self.tcx.trait_is_auto(trait_def_id) {
5673 return;
5674 }
5675
5676 let ty::Alias(alias_ty @ ty::AliasTy { kind: ty::Projection { def_id }, .. }) =
5678 trait_pred.self_ty().skip_binder().kind()
5679 else {
5680 return;
5681 };
5682 let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
5683 self.tcx.opt_rpitit_info(*def_id)
5684 else {
5685 return;
5686 };
5687
5688 let auto_trait = self.tcx.def_path_str(trait_def_id);
5689 let Some(fn_def_id) = fn_def_id.as_local() else {
5691 if self.tcx.asyncness(fn_def_id).is_async() {
5693 err.span_note(
5694 self.tcx.def_span(fn_def_id),
5695 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}::{1}` is an `async fn` in trait, which does not automatically imply that its future is `{2}`",
alias_ty.trait_ref(self.tcx), self.tcx.item_name(fn_def_id),
auto_trait))
})format!(
5696 "`{}::{}` is an `async fn` in trait, which does not \
5697 automatically imply that its future is `{auto_trait}`",
5698 alias_ty.trait_ref(self.tcx),
5699 self.tcx.item_name(fn_def_id)
5700 ),
5701 );
5702 }
5703 return;
5704 };
5705 let hir::Node::TraitItem(item) = self.tcx.hir_node_by_def_id(fn_def_id) else {
5706 return;
5707 };
5708
5709 let (sig, body) = item.expect_fn();
5711 let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(opaq_def, ..), .. }) =
5712 sig.decl.output
5713 else {
5714 return;
5716 };
5717
5718 if opaq_def.def_id.to_def_id() != opaque_def_id {
5721 return;
5722 }
5723
5724 let Some(sugg) = suggest_desugaring_async_fn_to_impl_future_in_trait(
5725 self.tcx,
5726 *sig,
5727 *body,
5728 opaque_def_id.expect_local(),
5729 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" + {0}", auto_trait))
})format!(" + {auto_trait}"),
5730 ) else {
5731 return;
5732 };
5733
5734 let function_name = self.tcx.def_path_str(fn_def_id);
5735 err.multipart_suggestion(
5736 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` can be made part of the associated future\'s guarantees for all implementations of `{1}`",
auto_trait, function_name))
})format!(
5737 "`{auto_trait}` can be made part of the associated future's \
5738 guarantees for all implementations of `{function_name}`"
5739 ),
5740 sugg,
5741 Applicability::MachineApplicable,
5742 );
5743 }
5744
5745 pub fn ty_kind_suggestion(
5746 &self,
5747 param_env: ty::ParamEnv<'tcx>,
5748 ty: Ty<'tcx>,
5749 ) -> Option<String> {
5750 let tcx = self.infcx.tcx;
5751 let implements_default = |ty| {
5752 let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
5753 return false;
5754 };
5755 self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
5756 };
5757
5758 Some(match *ty.kind() {
5759 ty::Never | ty::Error(_) => return None,
5760 ty::Bool => "false".to_string(),
5761 ty::Char => "\'x\'".to_string(),
5762 ty::Int(_) | ty::Uint(_) => "42".into(),
5763 ty::Float(_) => "3.14159".into(),
5764 ty::Slice(_) => "[]".to_string(),
5765 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => {
5766 "vec![]".to_string()
5767 }
5768 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::String) => {
5769 "String::new()".to_string()
5770 }
5771 ty::Adt(def, args) if def.is_box() => {
5772 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Box::new({0})",
self.ty_kind_suggestion(param_env, args[0].expect_ty())?))
})format!("Box::new({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
5773 }
5774 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Option) => {
5775 "None".to_string()
5776 }
5777 ty::Adt(def, args) if Some(def.did()) == tcx.get_diagnostic_item(sym::Result) => {
5778 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Ok({0})",
self.ty_kind_suggestion(param_env, args[0].expect_ty())?))
})format!("Ok({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
5779 }
5780 ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
5781 ty::Ref(_, ty, mutability) => {
5782 if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
5783 "\"\"".to_string()
5784 } else {
5785 let ty = self.ty_kind_suggestion(param_env, ty)?;
5786 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}{1}", mutability.prefix_str(),
ty))
})format!("&{}{ty}", mutability.prefix_str())
5787 }
5788 }
5789 ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
5790 if len == 0 {
5791 "[]".to_string()
5792 } else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
5793 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}; {1}]",
self.ty_kind_suggestion(param_env, ty)?, len))
})format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
5795 } else {
5796 "/* value */".to_string()
5797 }
5798 }
5799 ty::Tuple(tys) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}{1})",
tys.iter().map(|ty|
self.ty_kind_suggestion(param_env,
ty)).collect::<Option<Vec<String>>>()?.join(", "),
if tys.len() == 1 { "," } else { "" }))
})format!(
5800 "({}{})",
5801 tys.iter()
5802 .map(|ty| self.ty_kind_suggestion(param_env, ty))
5803 .collect::<Option<Vec<String>>>()?
5804 .join(", "),
5805 if tys.len() == 1 { "," } else { "" }
5806 ),
5807 _ => "/* value */".to_string(),
5808 })
5809 }
5810
5811 pub(super) fn suggest_add_result_as_return_type(
5815 &self,
5816 obligation: &PredicateObligation<'tcx>,
5817 err: &mut Diag<'_>,
5818 trait_pred: ty::PolyTraitPredicate<'tcx>,
5819 ) {
5820 if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
5821 return;
5822 }
5823
5824 fn choose_suggest_items<'tcx, 'hir>(
5831 tcx: TyCtxt<'tcx>,
5832 node: hir::Node<'hir>,
5833 ) -> Option<(&'hir hir::FnDecl<'hir>, hir::BodyId)> {
5834 match node {
5835 hir::Node::Item(item)
5836 if let hir::ItemKind::Fn { sig, body: body_id, .. } = item.kind =>
5837 {
5838 Some((sig.decl, body_id))
5839 }
5840 hir::Node::ImplItem(item)
5841 if let hir::ImplItemKind::Fn(sig, body_id) = item.kind =>
5842 {
5843 let parent = tcx.parent_hir_node(item.hir_id());
5844 if let hir::Node::Item(item) = parent
5845 && let hir::ItemKind::Impl(imp) = item.kind
5846 && imp.of_trait.is_none()
5847 {
5848 return Some((sig.decl, body_id));
5849 }
5850 None
5851 }
5852 _ => None,
5853 }
5854 }
5855
5856 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
5857 if let Some((fn_decl, body_id)) = choose_suggest_items(self.tcx, node)
5858 && let hir::FnRetTy::DefaultReturn(ret_span) = fn_decl.output
5859 && self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
5860 && trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
5861 && let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
5862 && self.tcx.is_diagnostic_item(sym::Result, def.did())
5863 {
5864 let mut sugg_spans =
5865 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ret_span,
" -> Result<(), Box<dyn std::error::Error>>".to_string())]))vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
5866 let body = self.tcx.hir_body(body_id);
5867 if let hir::ExprKind::Block(b, _) = body.value.kind
5868 && b.expr.is_none()
5869 {
5870 let span = self.tcx.sess.source_map().end_point(b.span);
5872 sugg_spans.push((
5873 span.shrink_to_lo(),
5874 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", " Ok(())\n",
self.tcx.sess.source_map().indentation_before(span).unwrap_or_default()))
})format!(
5875 "{}{}",
5876 " Ok(())\n",
5877 self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
5878 ),
5879 ));
5880 }
5881 err.multipart_suggestion(
5882 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider adding return type"))
})format!("consider adding return type"),
5883 sugg_spans,
5884 Applicability::MaybeIncorrect,
5885 );
5886 }
5887 }
5888
5889 #[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("suggest_unsized_bound_if_applicable",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(5889u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&[],
::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,
&{ meta.fields().value_set(&[]) })
} 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: () = loop {};
return __tracing_attr_fake_return;
}
{
let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
obligation.predicate.kind().skip_binder() else { return; };
let (ObligationCauseCode::WhereClause(item_def_id, span) |
ObligationCauseCode::WhereClauseInExpr(item_def_id, span,
..)) =
*obligation.cause.code().peel_derives() else { return; };
if span.is_dummy() { return; }
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5909",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(5909u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["pred",
"item_def_id", "span"],
::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(&debug(&pred) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&item_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&span) as
&dyn Value))])
});
} else { ; }
};
let (Some(node), true) =
(self.tcx.hir_get_if_local(item_def_id),
self.tcx.is_lang_item(pred.def_id(),
LangItem::Sized)) else { return; };
let Some(generics) = node.generics() else { return; };
let sized_trait = self.tcx.lang_items().sized_trait();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5922",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(5922u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["generics.params"],
::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(&debug(&generics.params)
as &dyn Value))])
});
} else { ; }
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5923",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(5923u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["generics.predicates"],
::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(&debug(&generics.predicates)
as &dyn Value))])
});
} else { ; }
};
let Some(param) =
generics.params.iter().find(|param|
param.span == span) else { return; };
let explicitly_sized =
generics.bounds_for_param(param.def_id).flat_map(|bp|
bp.bounds).any(|bound|
bound.trait_ref().and_then(|tr| tr.trait_def_id()) ==
sized_trait);
if explicitly_sized { return; }
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5936",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(5936u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["param"],
::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(&debug(¶m) as
&dyn Value))])
});
} else { ; }
};
match node {
hir::Node::Item(item @ hir::Item {
kind: hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) |
hir::ItemKind::Union(..), .. }) => {
if self.suggest_indirection_for_unsized(err, item, param) {
return;
}
}
_ => {}
};
let (span, separator, open_paren_sp) =
if let Some((s, open_paren_sp)) =
generics.bounds_span_for_suggestions(param.def_id) {
(s, " +", open_paren_sp)
} else {
(param.name.ident().span.shrink_to_hi(), ":", None)
};
let mut suggs = ::alloc::vec::Vec::new();
let suggestion =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ?Sized", separator))
});
if let Some(open_paren_sp) = open_paren_sp {
suggs.push((open_paren_sp, "(".to_string()));
suggs.push((span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("){0}", suggestion))
})));
} else { suggs.push((span, suggestion)); }
err.multipart_suggestion("consider relaxing the implicit `Sized` restriction",
suggs, Applicability::MachineApplicable);
}
}
}#[instrument(level = "debug", skip_all)]
5890 pub(super) fn suggest_unsized_bound_if_applicable(
5891 &self,
5892 err: &mut Diag<'_>,
5893 obligation: &PredicateObligation<'tcx>,
5894 ) {
5895 let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
5896 obligation.predicate.kind().skip_binder()
5897 else {
5898 return;
5899 };
5900 let (ObligationCauseCode::WhereClause(item_def_id, span)
5901 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)) =
5902 *obligation.cause.code().peel_derives()
5903 else {
5904 return;
5905 };
5906 if span.is_dummy() {
5907 return;
5908 }
5909 debug!(?pred, ?item_def_id, ?span);
5910
5911 let (Some(node), true) = (
5912 self.tcx.hir_get_if_local(item_def_id),
5913 self.tcx.is_lang_item(pred.def_id(), LangItem::Sized),
5914 ) else {
5915 return;
5916 };
5917
5918 let Some(generics) = node.generics() else {
5919 return;
5920 };
5921 let sized_trait = self.tcx.lang_items().sized_trait();
5922 debug!(?generics.params);
5923 debug!(?generics.predicates);
5924 let Some(param) = generics.params.iter().find(|param| param.span == span) else {
5925 return;
5926 };
5927 let explicitly_sized = generics
5930 .bounds_for_param(param.def_id)
5931 .flat_map(|bp| bp.bounds)
5932 .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
5933 if explicitly_sized {
5934 return;
5935 }
5936 debug!(?param);
5937 match node {
5938 hir::Node::Item(
5939 item @ hir::Item {
5940 kind:
5942 hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
5943 ..
5944 },
5945 ) => {
5946 if self.suggest_indirection_for_unsized(err, item, param) {
5947 return;
5948 }
5949 }
5950 _ => {}
5951 };
5952
5953 let (span, separator, open_paren_sp) =
5955 if let Some((s, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
5956 (s, " +", open_paren_sp)
5957 } else {
5958 (param.name.ident().span.shrink_to_hi(), ":", None)
5959 };
5960
5961 let mut suggs = vec![];
5962 let suggestion = format!("{separator} ?Sized");
5963
5964 if let Some(open_paren_sp) = open_paren_sp {
5965 suggs.push((open_paren_sp, "(".to_string()));
5966 suggs.push((span, format!("){suggestion}")));
5967 } else {
5968 suggs.push((span, suggestion));
5969 }
5970
5971 err.multipart_suggestion(
5972 "consider relaxing the implicit `Sized` restriction",
5973 suggs,
5974 Applicability::MachineApplicable,
5975 );
5976 }
5977
5978 fn suggest_indirection_for_unsized(
5979 &self,
5980 err: &mut Diag<'_>,
5981 item: &hir::Item<'tcx>,
5982 param: &hir::GenericParam<'tcx>,
5983 ) -> bool {
5984 let mut visitor = FindTypeParam { param: param.name.ident().name, .. };
5988 visitor.visit_item(item);
5989 if visitor.invalid_spans.is_empty() {
5990 return false;
5991 }
5992 let mut multispan: MultiSpan = param.span.into();
5993 multispan.push_span_label(
5994 param.span,
5995 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this could be changed to `{0}: ?Sized`...",
param.name.ident()))
})format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
5996 );
5997 for sp in visitor.invalid_spans {
5998 multispan.push_span_label(
5999 sp,
6000 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("...if indirection were used here: `Box<{0}>`",
param.name.ident()))
})format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
6001 );
6002 }
6003 err.span_help(
6004 multispan,
6005 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you could relax the implicit `Sized` bound on `{0}` if it were used through indirection like `&{0}` or `Box<{0}>`",
param.name.ident()))
})format!(
6006 "you could relax the implicit `Sized` bound on `{T}` if it were \
6007 used through indirection like `&{T}` or `Box<{T}>`",
6008 T = param.name.ident(),
6009 ),
6010 );
6011 true
6012 }
6013 pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
6014 &self,
6015 err: &mut Diag<'_>,
6016 predicate: T,
6017 param_env: ty::ParamEnv<'tcx>,
6018 cause_code: &ObligationCauseCode<'tcx>,
6019 ) where
6020 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
6021 {
6022 let tcx = self.tcx;
6023 let predicate = predicate.upcast(tcx);
6024 match *cause_code {
6025 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, rhs_span, .. }
6026 if let Some(typeck_results) = &self.typeck_results
6027 && let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
6028 && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
6029 && let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
6030 && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
6031 {
6032 if let Some(pred) = predicate.as_trait_clause()
6033 && tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
6034 && self
6035 .infcx
6036 .type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
6037 .must_apply_modulo_regions()
6038 {
6039 let lhs_span = tcx.hir_span(lhs_hir_id);
6040 let sm = tcx.sess.source_map();
6041 if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
6042 && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
6043 {
6044 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` implements `PartialEq<{1}>`",
rhs_ty, lhs_ty))
})format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
6045 err.multipart_suggestion(
6046 "consider swapping the equality",
6047 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)]))vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
6048 Applicability::MaybeIncorrect,
6049 );
6050 }
6051 }
6052 }
6053 _ => {}
6054 }
6055 }
6056}
6057
6058fn hint_missing_borrow<'tcx>(
6060 infcx: &InferCtxt<'tcx>,
6061 param_env: ty::ParamEnv<'tcx>,
6062 span: Span,
6063 found: Ty<'tcx>,
6064 expected: Ty<'tcx>,
6065 found_node: Node<'_>,
6066 err: &mut Diag<'_>,
6067) {
6068 if #[allow(non_exhaustive_omitted_patterns)] match found_node {
Node::TraitItem(..) => true,
_ => false,
}matches!(found_node, Node::TraitItem(..)) {
6069 return;
6070 }
6071
6072 let found_args = match found.kind() {
6073 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
6074 kind => {
6075 ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("found was converted to a FnPtr above but is now {0:?}",
kind))span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
6076 }
6077 };
6078 let expected_args = match expected.kind() {
6079 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
6080 kind => {
6081 ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("expected was converted to a FnPtr above but is now {0:?}",
kind))span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
6082 }
6083 };
6084
6085 let Some(fn_decl) = found_node.fn_decl() else {
6087 return;
6088 };
6089
6090 let args = fn_decl.inputs.iter();
6091
6092 let mut to_borrow = Vec::new();
6093 let mut remove_borrow = Vec::new();
6094
6095 for ((found_arg, expected_arg), arg) in found_args.zip(expected_args).zip(args) {
6096 let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
6097 let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
6098
6099 if infcx.can_eq(param_env, found_ty, expected_ty) {
6100 if found_refs.len() < expected_refs.len()
6102 && found_refs[..] == expected_refs[expected_refs.len() - found_refs.len()..]
6103 {
6104 to_borrow.push((
6105 arg.span.shrink_to_lo(),
6106 expected_refs[..expected_refs.len() - found_refs.len()]
6107 .iter()
6108 .map(|mutbl| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
})format!("&{}", mutbl.prefix_str()))
6109 .collect::<Vec<_>>()
6110 .join(""),
6111 ));
6112 } else if found_refs.len() > expected_refs.len() {
6113 let mut span = arg.span.shrink_to_lo();
6114 let mut left = found_refs.len() - expected_refs.len();
6115 let mut ty = arg;
6116 while let hir::TyKind::Ref(_, mut_ty) = &ty.kind
6117 && left > 0
6118 {
6119 span = span.with_hi(mut_ty.ty.span.lo());
6120 ty = mut_ty.ty;
6121 left -= 1;
6122 }
6123 if left == 0 {
6124 remove_borrow.push((span, String::new()));
6125 }
6126 }
6127 }
6128 }
6129
6130 if !to_borrow.is_empty() {
6131 err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
6132 }
6133
6134 if !remove_borrow.is_empty() {
6135 err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
6136 }
6137}
6138
6139#[derive(#[automatically_derived]
impl<'v> ::core::fmt::Debug for SelfVisitor<'v> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "SelfVisitor",
"paths", &self.paths, "name", &&self.name)
}
}Debug)]
6142pub struct SelfVisitor<'v> {
6143 pub paths: Vec<&'v hir::Ty<'v>> = Vec::new(),
6144 pub name: Option<Symbol>,
6145}
6146
6147impl<'v> Visitor<'v> for SelfVisitor<'v> {
6148 fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
6149 if let hir::TyKind::Path(path) = ty.kind
6150 && let hir::QPath::TypeRelative(inner_ty, segment) = path
6151 && (Some(segment.ident.name) == self.name || self.name.is_none())
6152 && let hir::TyKind::Path(inner_path) = inner_ty.kind
6153 && let hir::QPath::Resolved(None, inner_path) = inner_path
6154 && let Res::SelfTyAlias { .. } = inner_path.res
6155 {
6156 self.paths.push(ty.as_unambig_ty());
6157 }
6158 hir::intravisit::walk_ty(self, ty);
6159 }
6160}
6161
6162#[derive(#[automatically_derived]
impl<'v> ::core::default::Default for ReturnsVisitor<'v> {
#[inline]
fn default() -> ReturnsVisitor<'v> {
ReturnsVisitor {
returns: ::core::default::Default::default(),
in_block_tail: ::core::default::Default::default(),
}
}
}Default)]
6165pub struct ReturnsVisitor<'v> {
6166 pub returns: Vec<&'v hir::Expr<'v>>,
6167 in_block_tail: bool,
6168}
6169
6170impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
6171 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
6172 match ex.kind {
6177 hir::ExprKind::Ret(Some(ex)) => {
6178 self.returns.push(ex);
6179 }
6180 hir::ExprKind::Block(block, _) if self.in_block_tail => {
6181 self.in_block_tail = false;
6182 for stmt in block.stmts {
6183 hir::intravisit::walk_stmt(self, stmt);
6184 }
6185 self.in_block_tail = true;
6186 if let Some(expr) = block.expr {
6187 self.visit_expr(expr);
6188 }
6189 }
6190 hir::ExprKind::If(_, then, else_opt) if self.in_block_tail => {
6191 self.visit_expr(then);
6192 if let Some(el) = else_opt {
6193 self.visit_expr(el);
6194 }
6195 }
6196 hir::ExprKind::Match(_, arms, _) if self.in_block_tail => {
6197 for arm in arms {
6198 self.visit_expr(arm.body);
6199 }
6200 }
6201 _ if !self.in_block_tail => hir::intravisit::walk_expr(self, ex),
6203 _ => self.returns.push(ex),
6204 }
6205 }
6206
6207 fn visit_body(&mut self, body: &hir::Body<'v>) {
6208 if !!self.in_block_tail {
::core::panicking::panic("assertion failed: !self.in_block_tail")
};assert!(!self.in_block_tail);
6209 self.in_block_tail = true;
6210 hir::intravisit::walk_body(self, body);
6211 }
6212}
6213
6214#[derive(#[automatically_derived]
impl ::core::default::Default for AwaitsVisitor {
#[inline]
fn default() -> AwaitsVisitor {
AwaitsVisitor { awaits: ::core::default::Default::default() }
}
}Default)]
6216struct AwaitsVisitor {
6217 awaits: Vec<HirId>,
6218}
6219
6220impl<'v> Visitor<'v> for AwaitsVisitor {
6221 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
6222 if let hir::ExprKind::Yield(_, hir::YieldSource::Await { expr: Some(id) }) = ex.kind {
6223 self.awaits.push(id)
6224 }
6225 hir::intravisit::walk_expr(self, ex)
6226 }
6227}
6228
6229pub trait NextTypeParamName {
6233 fn next_type_param_name(&self, name: Option<&str>) -> String;
6234}
6235
6236impl NextTypeParamName for &[hir::GenericParam<'_>] {
6237 fn next_type_param_name(&self, name: Option<&str>) -> String {
6238 let name = name.and_then(|n| n.chars().next()).map(|c| c.to_uppercase().to_string());
6240 let name = name.as_deref();
6241
6242 let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
6244
6245 let used_names: Vec<Symbol> = self
6247 .iter()
6248 .filter_map(|param| match param.name {
6249 hir::ParamName::Plain(ident) => Some(ident.name),
6250 _ => None,
6251 })
6252 .collect();
6253
6254 possible_names
6256 .iter()
6257 .find(|n| !used_names.contains(&Symbol::intern(n)))
6258 .unwrap_or(&"ParamName")
6259 .to_string()
6260 }
6261}
6262
6263struct ReplaceImplTraitVisitor<'a> {
6265 ty_spans: &'a mut Vec<Span>,
6266 param_did: DefId,
6267}
6268
6269impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
6270 fn visit_ty(&mut self, t: &'hir hir::Ty<'hir, AmbigArg>) {
6271 if let hir::TyKind::Path(hir::QPath::Resolved(
6272 None,
6273 hir::Path { res: Res::Def(_, segment_did), .. },
6274 )) = t.kind
6275 {
6276 if self.param_did == *segment_did {
6277 self.ty_spans.push(t.span);
6282 return;
6283 }
6284 }
6285
6286 hir::intravisit::walk_ty(self, t);
6287 }
6288}
6289
6290pub(super) fn get_explanation_based_on_obligation<'tcx>(
6291 tcx: TyCtxt<'tcx>,
6292 obligation: &PredicateObligation<'tcx>,
6293 trait_predicate: ty::PolyTraitPredicate<'tcx>,
6294 pre_message: String,
6295 long_ty_path: &mut Option<PathBuf>,
6296) -> String {
6297 if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
6298 "consider using `()`, or a `Result`".to_owned()
6299 } else {
6300 let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
6301 ty::FnDef(_, _) => Some("fn item"),
6302 ty::Closure(_, _) => Some("closure"),
6303 _ => None,
6304 };
6305
6306 let desc = match ty_desc {
6307 Some(desc) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", desc))
})format!(" {desc}"),
6308 None => String::new(),
6309 };
6310 if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
6311 let mention_unstable = !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
6316 && try { tcx.lookup_stability(trait_predicate.def_id())?.level.is_stable() }
6317 == Some(false);
6318 let unstable = if mention_unstable { "nightly-only, unstable " } else { "" };
6319
6320 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2}the {3}trait `{0}` is not implemented for{4} `{1}`",
trait_predicate.print_modifiers_and_trait_path(),
tcx.short_string(trait_predicate.self_ty().skip_binder(),
long_ty_path), pre_message, unstable, desc))
})format!(
6321 "{pre_message}the {unstable}trait `{}` is not implemented for{desc} `{}`",
6322 trait_predicate.print_modifiers_and_trait_path(),
6323 tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
6324 )
6325 } else {
6326 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}the trait bound `{1}` is not satisfied",
pre_message, trait_predicate))
})format!("{pre_message}the trait bound `{trait_predicate}` is not satisfied")
6330 }
6331 }
6332}
6333
6334struct ReplaceImplTraitFolder<'tcx> {
6336 tcx: TyCtxt<'tcx>,
6337 param: &'tcx ty::GenericParamDef,
6338 replace_ty: Ty<'tcx>,
6339}
6340
6341impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceImplTraitFolder<'tcx> {
6342 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
6343 if let ty::Param(ty::ParamTy { index, .. }) = t.kind() {
6344 if self.param.index == *index {
6345 return self.replace_ty;
6346 }
6347 }
6348 t.super_fold_with(self)
6349 }
6350
6351 fn cx(&self) -> TyCtxt<'tcx> {
6352 self.tcx
6353 }
6354}
6355
6356pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
6357 tcx: TyCtxt<'tcx>,
6358 sig: hir::FnSig<'tcx>,
6359 body: hir::TraitFn<'tcx>,
6360 opaque_def_id: LocalDefId,
6361 add_bounds: &str,
6362) -> Option<Vec<(Span, String)>> {
6363 let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
6364 return None;
6365 };
6366 let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);
6367
6368 let future = tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
6369 let [hir::GenericBound::Trait(trait_ref)] = future.bounds else {
6370 return None;
6372 };
6373 let Some(hir::PathSegment { args: Some(args), .. }) = trait_ref.trait_ref.path.segments.last()
6374 else {
6375 return None;
6377 };
6378 let Some(future_output_ty) = args.constraints.first().and_then(|constraint| constraint.ty())
6379 else {
6380 return None;
6382 };
6383
6384 let mut sugg = if future_output_ty.span.is_empty() {
6385 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(async_span, String::new()),
(future_output_ty.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" -> impl std::future::Future<Output = ()>{0}",
add_bounds))
}))]))vec![
6386 (async_span, String::new()),
6387 (
6388 future_output_ty.span,
6389 format!(" -> impl std::future::Future<Output = ()>{add_bounds}"),
6390 ),
6391 ]
6392 } else {
6393 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(future_output_ty.span.shrink_to_lo(),
"impl std::future::Future<Output = ".to_owned()),
(future_output_ty.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(">{0}", add_bounds))
})), (async_span, String::new())]))vec![
6394 (future_output_ty.span.shrink_to_lo(), "impl std::future::Future<Output = ".to_owned()),
6395 (future_output_ty.span.shrink_to_hi(), format!(">{add_bounds}")),
6396 (async_span, String::new()),
6397 ]
6398 };
6399
6400 if let hir::TraitFn::Provided(body) = body {
6402 let body = tcx.hir_body(body);
6403 let body_span = body.value.span;
6404 let body_span_without_braces =
6405 body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
6406 if body_span_without_braces.is_empty() {
6407 sugg.push((body_span_without_braces, " async {} ".to_owned()));
6408 } else {
6409 sugg.extend([
6410 (body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
6411 (body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
6412 ]);
6413 }
6414 }
6415
6416 Some(sugg)
6417}
6418
6419fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
6422 tcx: TyCtxt<'_>,
6423 err: &mut Diag<'_, G>,
6424 self_ty_str: &str,
6425 trait_name: &str,
6426 predicate: ty::Predicate<'_>,
6427 generics: &hir::Generics<'_>,
6428 data: &ImplDerivedCause<'_>,
6429) {
6430 let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() else {
6431 return;
6432 };
6433 let ty::ClauseKind::Projection(proj) = clause else {
6434 return;
6435 };
6436 let Some(name) = tcx
6437 .opt_rpitit_info(proj.projection_term.def_id())
6438 .and_then(|data| match data {
6439 ty::ImplTraitInTraitData::Trait { fn_def_id, .. } => Some(tcx.item_name(fn_def_id)),
6440 ty::ImplTraitInTraitData::Impl { .. } => None,
6441 })
6442 .or_else(|| tcx.opt_item_name(proj.projection_term.def_id()))
6443 else {
6444 return;
6445 };
6446 let mut predicates = generics.predicates.iter().peekable();
6447 let mut prev: Option<(&hir::WhereBoundPredicate<'_>, Span)> = None;
6448 while let Some(pred) = predicates.next() {
6449 let curr_span = pred.span;
6450 let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
6451 continue;
6452 };
6453 let mut bounds = pred.bounds.iter();
6454 while let Some(bound) = bounds.next() {
6455 let Some(trait_ref) = bound.trait_ref() else {
6456 continue;
6457 };
6458 if bound.span() != data.span {
6459 continue;
6460 }
6461 if let hir::TyKind::Path(path) = pred.bounded_ty.kind
6462 && let hir::QPath::TypeRelative(ty, segment) = path
6463 && segment.ident.name == name
6464 && let hir::TyKind::Path(inner_path) = ty.kind
6465 && let hir::QPath::Resolved(None, inner_path) = inner_path
6466 && let Res::SelfTyAlias { .. } = inner_path.res
6467 {
6468 let span = if pred.origin == hir::PredicateOrigin::WhereClause
6471 && generics
6472 .predicates
6473 .iter()
6474 .filter(|p| {
6475 #[allow(non_exhaustive_omitted_patterns)] match p.kind {
hir::WherePredicateKind::BoundPredicate(p) if
hir::PredicateOrigin::WhereClause == p.origin => true,
_ => false,
}matches!(
6476 p.kind,
6477 hir::WherePredicateKind::BoundPredicate(p)
6478 if hir::PredicateOrigin::WhereClause == p.origin
6479 )
6480 })
6481 .count()
6482 == 1
6483 {
6484 generics.where_clause_span
6487 } else if let Some(next_pred) = predicates.peek()
6488 && let hir::WherePredicateKind::BoundPredicate(next) = next_pred.kind
6489 && pred.origin == next.origin
6490 {
6491 curr_span.until(next_pred.span)
6493 } else if let Some((prev, prev_span)) = prev
6494 && pred.origin == prev.origin
6495 {
6496 prev_span.shrink_to_hi().to(curr_span)
6498 } else if pred.origin == hir::PredicateOrigin::WhereClause {
6499 curr_span.with_hi(generics.where_clause_span.hi())
6500 } else {
6501 curr_span
6502 };
6503
6504 err.span_suggestion_verbose(
6505 span,
6506 "associated type for the current `impl` cannot be restricted in `where` \
6507 clauses, remove this bound",
6508 "",
6509 Applicability::MaybeIncorrect,
6510 );
6511 }
6512 if let Some(new) =
6513 tcx.associated_items(data.impl_or_alias_def_id).find_by_ident_and_kind(
6514 tcx,
6515 Ident::with_dummy_span(name),
6516 ty::AssocTag::Type,
6517 data.impl_or_alias_def_id,
6518 )
6519 {
6520 let span = tcx.def_span(new.def_id);
6523 err.span_label(
6524 span,
6525 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("associated type `<{0} as {1}>::{2}` is specified here",
self_ty_str, trait_name, name))
})format!(
6526 "associated type `<{self_ty_str} as {trait_name}>::{name}` is specified \
6527 here",
6528 ),
6529 );
6530 let mut visitor = SelfVisitor { name: Some(name), .. };
6533 visitor.visit_trait_ref(trait_ref);
6534 for path in visitor.paths {
6535 err.span_suggestion_verbose(
6536 path.span,
6537 "replace the associated type with the type specified in this `impl`",
6538 tcx.type_of(new.def_id).skip_binder(),
6539 Applicability::MachineApplicable,
6540 );
6541 }
6542 } else {
6543 let mut visitor = SelfVisitor { name: None, .. };
6544 visitor.visit_trait_ref(trait_ref);
6545 let span: MultiSpan =
6546 visitor.paths.iter().map(|p| p.span).collect::<Vec<Span>>().into();
6547 err.span_note(
6548 span,
6549 "associated types for the current `impl` cannot be restricted in `where` \
6550 clauses",
6551 );
6552 }
6553 }
6554 prev = Some((pred, curr_span));
6555 }
6556}
6557
6558fn get_deref_type_and_refs(mut ty: Ty<'_>) -> (Ty<'_>, Vec<hir::Mutability>) {
6559 let mut refs = ::alloc::vec::Vec::new()vec![];
6560
6561 while let ty::Ref(_, new_ty, mutbl) = ty.kind() {
6562 ty = *new_ty;
6563 refs.push(*mutbl);
6564 }
6565
6566 (ty, refs)
6567}
6568
6569struct FindTypeParam {
6572 param: rustc_span::Symbol,
6573 invalid_spans: Vec<Span> = Vec::new(),
6574 nested: bool = false,
6575}
6576
6577impl<'v> Visitor<'v> for FindTypeParam {
6578 fn visit_where_predicate(&mut self, _: &'v hir::WherePredicate<'v>) {
6579 }
6581
6582 fn visit_ty(&mut self, ty: &hir::Ty<'_, AmbigArg>) {
6583 match ty.kind {
6590 hir::TyKind::Ptr(_) | hir::TyKind::Ref(..) | hir::TyKind::TraitObject(..) => {}
6591 hir::TyKind::Path(hir::QPath::Resolved(None, path))
6592 if let [segment] = path.segments
6593 && segment.ident.name == self.param =>
6594 {
6595 if !self.nested {
6596 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:6596",
"rustc_trait_selection::error_reporting::traits::suggestions",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
::tracing_core::__macro_support::Option::Some(6596u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
::tracing_core::field::FieldSet::new(&["message", "ty"],
::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!("FindTypeParam::visit_ty")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty) as
&dyn Value))])
});
} else { ; }
};debug!(?ty, "FindTypeParam::visit_ty");
6597 self.invalid_spans.push(ty.span);
6598 }
6599 }
6600 hir::TyKind::Path(_) => {
6601 let prev = self.nested;
6602 self.nested = true;
6603 hir::intravisit::walk_ty(self, ty);
6604 self.nested = prev;
6605 }
6606 _ => {
6607 hir::intravisit::walk_ty(self, ty);
6608 }
6609 }
6610 }
6611}
6612
6613struct ParamFinder {
6616 params: Vec<Symbol> = Vec::new(),
6617}
6618
6619impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParamFinder {
6620 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
6621 match t.kind() {
6622 ty::Param(p) => self.params.push(p.name),
6623 _ => {}
6624 }
6625 t.super_visit_with(self)
6626 }
6627}
6628
6629impl ParamFinder {
6630 fn can_suggest_bound(&self, generics: &hir::Generics<'_>) -> bool {
6633 if self.params.is_empty() {
6634 return true;
6637 }
6638 generics.params.iter().any(|p| match p.name {
6639 hir::ParamName::Plain(p_name) => {
6640 self.params.iter().any(|p| *p == p_name.name || *p == kw::SelfUpper)
6642 }
6643 _ => true,
6644 })
6645 }
6646}