1use rustc_errors::{Applicability, Diag, MultiSpan, listify};
2use rustc_hir as hir;
3use rustc_hir::def::Res;
4use rustc_hir::intravisit::Visitor;
5use rustc_infer::infer::DefineOpaqueTypes;
6use rustc_middle::bug;
7use rustc_middle::ty::adjustment::AllowTwoPhase;
8use rustc_middle::ty::error::{ExpectedFound, TypeError};
9use rustc_middle::ty::print::with_no_trimmed_paths;
10use rustc_middle::ty::{self, AssocItem, BottomUpFolder, Ty, TypeFoldable, TypeVisitableExt};
11use rustc_span::{DUMMY_SP, Ident, Span, sym};
12use rustc_trait_selection::infer::InferCtxtExt;
13use rustc_trait_selection::traits::ObligationCause;
14use tracing::instrument;
15
16use super::method::probe;
17use crate::FnCtxt;
18
19impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20 pub(crate) fn emit_type_mismatch_suggestions(
21 &self,
22 err: &mut Diag<'_>,
23 expr: &hir::Expr<'tcx>,
24 expr_ty: Ty<'tcx>,
25 expected: Ty<'tcx>,
26 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
27 error: Option<TypeError<'tcx>>,
28 ) {
29 if expr_ty == expected {
30 return;
31 }
32 self.annotate_alternative_method_deref(err, expr, error);
33 self.explain_self_literal(err, expr, expected, expr_ty);
34
35 let suggested = self.suggest_missing_parentheses(err, expr)
37 || self.suggest_missing_unwrap_expect(err, expr, expected, expr_ty)
38 || self.suggest_remove_last_method_call(err, expr, expected)
39 || self.suggest_associated_const(err, expr, expected)
40 || self.suggest_semicolon_in_repeat_expr(err, expr, expr_ty)
41 || self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr)
42 || self.suggest_option_to_bool(err, expr, expr_ty, expected)
43 || self.suggest_compatible_variants(err, expr, expected, expr_ty)
44 || self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty)
45 || self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty)
46 || self.suggest_no_capture_closure(err, expected, expr_ty)
47 || self.suggest_boxing_when_appropriate(
48 err,
49 expr.peel_blocks().span,
50 expr.hir_id,
51 expected,
52 expr_ty,
53 )
54 || self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
55 || self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
56 || self.suggest_clone_for_ref(err, expr, expr_ty, expected)
57 || self.suggest_into(err, expr, expr_ty, expected)
58 || self.suggest_floating_point_literal(err, expr, expected)
59 || self.suggest_null_ptr_for_literal_zero_given_to_ptr_arg(err, expr, expected)
60 || self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty)
61 || self.suggest_returning_value_after_loop(err, expr, expected);
62
63 if !suggested {
64 self.note_source_of_type_mismatch_constraint(
65 err,
66 expr,
67 TypeMismatchSource::Ty(expected),
68 );
69 }
70 }
71
72 pub(crate) fn emit_coerce_suggestions(
73 &self,
74 err: &mut Diag<'_>,
75 expr: &hir::Expr<'tcx>,
76 expr_ty: Ty<'tcx>,
77 expected: Ty<'tcx>,
78 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
79 error: Option<TypeError<'tcx>>,
80 ) {
81 if expr_ty == expected {
82 return;
83 }
84
85 self.annotate_expected_due_to_let_ty(err, expr, error);
86 self.annotate_loop_expected_due_to_inference(err, expr, error);
87 if self.annotate_mut_binding_to_immutable_binding(err, expr, expr_ty, expected, error) {
88 return;
89 }
90
91 if matches!(error, Some(TypeError::RegionsInsufficientlyPolymorphic(..))) {
95 return;
96 }
97
98 if self.is_destruct_assignment_desugaring(expr) {
99 return;
100 }
101 self.emit_type_mismatch_suggestions(err, expr, expr_ty, expected, expected_ty_expr, error);
102 self.note_type_is_not_clone(err, expected, expr_ty, expr);
103 self.note_internal_mutation_in_method(err, expr, Some(expected), expr_ty);
104 self.suggest_method_call_on_range_literal(err, expr, expr_ty, expected);
105 self.suggest_return_binding_for_missing_tail_expr(err, expr, expr_ty, expected);
106 self.note_wrong_return_ty_due_to_generic_arg(err, expr, expr_ty);
107 }
108
109 fn adjust_expr_for_assert_eq_macro(
112 &self,
113 found_expr: &mut &'tcx hir::Expr<'tcx>,
114 expected_expr: &mut Option<&'tcx hir::Expr<'tcx>>,
115 ) {
116 let Some(expected_expr) = expected_expr else {
117 return;
118 };
119
120 if !found_expr.span.eq_ctxt(expected_expr.span) {
121 return;
122 }
123
124 if !found_expr
125 .span
126 .ctxt()
127 .outer_expn_data()
128 .macro_def_id
129 .is_some_and(|def_id| self.tcx.is_diagnostic_item(sym::assert_eq_macro, def_id))
130 {
131 return;
132 }
133
134 let hir::ExprKind::Unary(
135 hir::UnOp::Deref,
136 hir::Expr { kind: hir::ExprKind::Path(found_path), .. },
137 ) = found_expr.kind
138 else {
139 return;
140 };
141 let hir::ExprKind::Unary(
142 hir::UnOp::Deref,
143 hir::Expr { kind: hir::ExprKind::Path(expected_path), .. },
144 ) = expected_expr.kind
145 else {
146 return;
147 };
148
149 for (path, name, idx, var) in [
150 (expected_path, "left_val", 0, expected_expr),
151 (found_path, "right_val", 1, found_expr),
152 ] {
153 if let hir::QPath::Resolved(_, path) = path
154 && let [segment] = path.segments
155 && segment.ident.name.as_str() == name
156 && let Res::Local(hir_id) = path.res
157 && let Some((_, hir::Node::Expr(match_expr))) =
158 self.tcx.hir_parent_iter(hir_id).nth(2)
159 && let hir::ExprKind::Match(scrutinee, _, _) = match_expr.kind
160 && let hir::ExprKind::Tup(exprs) = scrutinee.kind
161 && let hir::ExprKind::AddrOf(_, _, macro_arg) = exprs[idx].kind
162 {
163 *var = macro_arg;
164 }
165 }
166 }
167
168 pub(crate) fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
171 if let Err(e) = self.demand_suptype_diag(sp, expected, actual) {
172 e.emit();
173 }
174 }
175
176 pub(crate) fn demand_suptype_diag(
177 &'a self,
178 sp: Span,
179 expected: Ty<'tcx>,
180 actual: Ty<'tcx>,
181 ) -> Result<(), Diag<'a>> {
182 self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
183 }
184
185 #[instrument(skip(self), level = "debug")]
186 pub(crate) fn demand_suptype_with_origin(
187 &'a self,
188 cause: &ObligationCause<'tcx>,
189 expected: Ty<'tcx>,
190 actual: Ty<'tcx>,
191 ) -> Result<(), Diag<'a>> {
192 self.at(cause, self.param_env)
193 .sup(DefineOpaqueTypes::Yes, expected, actual)
194 .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
195 .map_err(|e| {
196 self.err_ctxt().report_mismatched_types(cause, self.param_env, expected, actual, e)
197 })
198 }
199
200 pub(crate) fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
201 if let Err(err) = self.demand_eqtype_diag(sp, expected, actual) {
202 err.emit();
203 }
204 }
205
206 pub(crate) fn demand_eqtype_diag(
207 &'a self,
208 sp: Span,
209 expected: Ty<'tcx>,
210 actual: Ty<'tcx>,
211 ) -> Result<(), Diag<'a>> {
212 self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
213 }
214
215 pub(crate) fn demand_eqtype_with_origin(
216 &'a self,
217 cause: &ObligationCause<'tcx>,
218 expected: Ty<'tcx>,
219 actual: Ty<'tcx>,
220 ) -> Result<(), Diag<'a>> {
221 self.at(cause, self.param_env)
222 .eq(DefineOpaqueTypes::Yes, expected, actual)
223 .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
224 .map_err(|e| {
225 self.err_ctxt().report_mismatched_types(cause, self.param_env, expected, actual, e)
226 })
227 }
228
229 pub(crate) fn demand_coerce(
230 &self,
231 expr: &'tcx hir::Expr<'tcx>,
232 checked_ty: Ty<'tcx>,
233 expected: Ty<'tcx>,
234 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
235 allow_two_phase: AllowTwoPhase,
236 ) -> Ty<'tcx> {
237 match self.demand_coerce_diag(expr, checked_ty, expected, expected_ty_expr, allow_two_phase)
238 {
239 Ok(ty) => ty,
240 Err(err) => {
241 err.emit();
242 expected
246 }
247 }
248 }
249
250 #[instrument(level = "debug", skip(self, expr, expected_ty_expr, allow_two_phase))]
255 pub(crate) fn demand_coerce_diag(
256 &'a self,
257 mut expr: &'tcx hir::Expr<'tcx>,
258 checked_ty: Ty<'tcx>,
259 expected: Ty<'tcx>,
260 mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
261 allow_two_phase: AllowTwoPhase,
262 ) -> Result<Ty<'tcx>, Diag<'a>> {
263 let expected = self.resolve_vars_with_obligations(expected);
264
265 let e = match self.coerce(expr, checked_ty, expected, allow_two_phase, None) {
266 Ok(ty) => return Ok(ty),
267 Err(e) => e,
268 };
269
270 self.adjust_expr_for_assert_eq_macro(&mut expr, &mut expected_ty_expr);
271
272 self.set_tainted_by_errors(self.dcx().span_delayed_bug(
273 expr.span,
274 "`TypeError` when attempting coercion but no error emitted",
275 ));
276 let expr = expr.peel_drop_temps();
277 let cause = self.misc(expr.span);
278 let expr_ty = self.resolve_vars_if_possible(checked_ty);
279 let mut err =
280 self.err_ctxt().report_mismatched_types(&cause, self.param_env, expected, expr_ty, e);
281
282 self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr, Some(e));
283
284 Err(err)
285 }
286
287 pub(crate) fn note_source_of_type_mismatch_constraint(
290 &self,
291 err: &mut Diag<'_>,
292 expr: &hir::Expr<'_>,
293 source: TypeMismatchSource<'tcx>,
294 ) -> bool {
295 let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind else {
296 return false;
297 };
298 let [hir::PathSegment { ident, args: None, .. }] = p.segments else {
299 return false;
300 };
301 let hir::def::Res::Local(local_hir_id) = p.res else {
302 return false;
303 };
304 let hir::Node::Pat(pat) = self.tcx.hir_node(local_hir_id) else {
305 return false;
306 };
307 let (init_ty_hir_id, init) = match self.tcx.parent_hir_node(pat.hir_id) {
308 hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init, .. }) => (ty.hir_id, *init),
309 hir::Node::LetStmt(hir::LetStmt { init: Some(init), .. }) => (init.hir_id, Some(*init)),
310 _ => return false,
311 };
312 let Some(init_ty) = self.node_ty_opt(init_ty_hir_id) else {
313 return false;
314 };
315
316 struct FindExprs<'tcx> {
318 hir_id: hir::HirId,
319 uses: Vec<&'tcx hir::Expr<'tcx>>,
320 }
321 impl<'tcx> Visitor<'tcx> for FindExprs<'tcx> {
322 fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
323 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = ex.kind
324 && let hir::def::Res::Local(hir_id) = path.res
325 && hir_id == self.hir_id
326 {
327 self.uses.push(ex);
328 }
329 hir::intravisit::walk_expr(self, ex);
330 }
331 }
332
333 let mut expr_finder = FindExprs { hir_id: local_hir_id, uses: init.into_iter().collect() };
334 let body = self.tcx.hir_body_owned_by(self.body_id);
335 expr_finder.visit_expr(body.value);
336
337 let mut fudger = BottomUpFolder {
339 tcx: self.tcx,
340 ty_op: |ty| {
341 if let ty::Infer(infer) = ty.kind() {
342 match infer {
343 ty::TyVar(_) => self.next_ty_var(DUMMY_SP),
344 ty::IntVar(_) => self.next_int_var(),
345 ty::FloatVar(_) => self.next_float_var(),
346 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
347 bug!("unexpected fresh ty outside of the trait solver")
348 }
349 }
350 } else {
351 ty
352 }
353 },
354 lt_op: |_| self.tcx.lifetimes.re_erased,
355 ct_op: |ct| {
356 if let ty::ConstKind::Infer(_) = ct.kind() {
357 self.next_const_var(DUMMY_SP)
358 } else {
359 ct
360 }
361 },
362 };
363
364 let expected_ty = match source {
365 TypeMismatchSource::Ty(expected_ty) => expected_ty,
366 TypeMismatchSource::Arg { call_expr, incompatible_arg: idx } => {
373 let hir::ExprKind::MethodCall(segment, _, args, _) = call_expr.kind else {
374 return false;
375 };
376 let Some(arg_ty) = self.node_ty_opt(args[idx].hir_id) else {
377 return false;
378 };
379 let possible_rcvr_ty = expr_finder.uses.iter().rev().find_map(|binding| {
380 let possible_rcvr_ty = self.node_ty_opt(binding.hir_id)?;
381 if possible_rcvr_ty.is_ty_var() {
382 return None;
383 }
384 let possible_rcvr_ty = possible_rcvr_ty.fold_with(&mut fudger);
386 let method = self
387 .lookup_method_for_diagnostic(
388 possible_rcvr_ty,
389 segment,
390 DUMMY_SP,
391 call_expr,
392 binding,
393 )
394 .ok()?;
395 if Some(method.def_id)
397 != self.typeck_results.borrow().type_dependent_def_id(call_expr.hir_id)
398 {
399 return None;
400 }
401 let _ = self
405 .at(&ObligationCause::dummy(), self.param_env)
406 .eq(DefineOpaqueTypes::Yes, method.sig.inputs()[idx + 1], arg_ty)
407 .ok()?;
408 self.select_obligations_where_possible(|errs| {
409 errs.clear();
411 });
412 Some(self.resolve_vars_if_possible(possible_rcvr_ty))
413 });
414 if let Some(rcvr_ty) = possible_rcvr_ty {
415 rcvr_ty
416 } else {
417 return false;
418 }
419 }
420 };
421
422 if !self.can_eq(self.param_env, expected_ty, init_ty.fold_with(&mut fudger)) {
425 return false;
426 }
427
428 for window in expr_finder.uses.windows(2) {
429 let [binding, next_usage] = *window else {
433 continue;
434 };
435
436 if binding.hir_id == expr.hir_id {
438 break;
439 }
440
441 let Some(next_use_ty) = self.node_ty_opt(next_usage.hir_id) else {
442 continue;
443 };
444
445 if self.can_eq(self.param_env, expected_ty, next_use_ty.fold_with(&mut fudger)) {
448 continue;
449 }
450
451 if let hir::Node::Expr(parent_expr) = self.tcx.parent_hir_node(binding.hir_id)
452 && let hir::ExprKind::MethodCall(segment, rcvr, args, _) = parent_expr.kind
453 && rcvr.hir_id == binding.hir_id
454 {
455 let Some(rcvr_ty) = self.node_ty_opt(rcvr.hir_id) else {
459 continue;
460 };
461 let rcvr_ty = rcvr_ty.fold_with(&mut fudger);
462 let Ok(method) = self.lookup_method_for_diagnostic(
463 rcvr_ty,
464 segment,
465 DUMMY_SP,
466 parent_expr,
467 rcvr,
468 ) else {
469 continue;
470 };
471 if Some(method.def_id)
473 != self.typeck_results.borrow().type_dependent_def_id(parent_expr.hir_id)
474 {
475 continue;
476 }
477
478 let ideal_rcvr_ty = rcvr_ty.fold_with(&mut fudger);
479 let ideal_method = self
480 .lookup_method_for_diagnostic(
481 ideal_rcvr_ty,
482 segment,
483 DUMMY_SP,
484 parent_expr,
485 rcvr,
486 )
487 .ok()
488 .and_then(|method| {
489 let _ = self
490 .at(&ObligationCause::dummy(), self.param_env)
491 .eq(DefineOpaqueTypes::Yes, ideal_rcvr_ty, expected_ty)
492 .ok()?;
493 Some(method)
494 });
495
496 for (idx, (expected_arg_ty, arg_expr)) in
499 std::iter::zip(&method.sig.inputs()[1..], args).enumerate()
500 {
501 let Some(arg_ty) = self.node_ty_opt(arg_expr.hir_id) else {
502 continue;
503 };
504 let arg_ty = arg_ty.fold_with(&mut fudger);
505 let _ =
506 self.coerce(arg_expr, arg_ty, *expected_arg_ty, AllowTwoPhase::No, None);
507 self.select_obligations_where_possible(|errs| {
508 errs.clear();
510 });
511 if self.can_eq(self.param_env, rcvr_ty, expected_ty) {
515 continue;
516 }
517 err.span_label(arg_expr.span, format!("this argument has type `{arg_ty}`..."));
518 err.span_label(
519 binding.span,
520 format!("... which causes `{ident}` to have type `{next_use_ty}`"),
521 );
522 if matches!(source, TypeMismatchSource::Ty(_))
531 && let Some(ideal_method) = ideal_method
532 && Some(ideal_method.def_id)
533 == self
534 .typeck_results
535 .borrow()
536 .type_dependent_def_id(parent_expr.hir_id)
537 && let ideal_arg_ty =
538 self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
539 && !ideal_arg_ty.has_non_region_infer()
540 {
541 self.emit_type_mismatch_suggestions(
542 err,
543 arg_expr,
544 arg_ty,
545 ideal_arg_ty,
546 None,
547 None,
548 );
549 }
550 return true;
551 }
552 }
553 err.span_label(
554 binding.span,
555 format!("here the type of `{ident}` is inferred to be `{next_use_ty}`"),
556 );
557 return true;
558 }
559
560 false
562 }
563
564 pub(crate) fn annotate_loop_expected_due_to_inference(
567 &self,
568 err: &mut Diag<'_>,
569 expr: &hir::Expr<'_>,
570 error: Option<TypeError<'tcx>>,
571 ) {
572 let Some(TypeError::Sorts(ExpectedFound { expected, .. })) = error else {
573 return;
574 };
575 let mut parent_id = self.tcx.parent_hir_id(expr.hir_id);
576 let mut parent;
577 'outer: loop {
578 let (hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(p), .. })
580 | hir::Node::Block(&hir::Block { expr: Some(p), .. })
581 | hir::Node::Expr(p)) = self.tcx.hir_node(parent_id)
582 else {
583 break;
584 };
585 parent = p;
586 parent_id = self.tcx.parent_hir_id(parent_id);
587 let hir::ExprKind::Break(destination, _) = parent.kind else {
588 continue;
589 };
590 let mut parent_id = parent_id;
591 let mut direct = false;
592 loop {
593 let parent = match self.tcx.hir_node(parent_id) {
595 hir::Node::Expr(parent) => {
596 parent_id = self.tcx.parent_hir_id(parent.hir_id);
597 parent
598 }
599 hir::Node::Stmt(hir::Stmt {
600 hir_id,
601 kind: hir::StmtKind::Semi(parent) | hir::StmtKind::Expr(parent),
602 ..
603 }) => {
604 parent_id = self.tcx.parent_hir_id(*hir_id);
605 parent
606 }
607 hir::Node::Block(_) => {
608 parent_id = self.tcx.parent_hir_id(parent_id);
609 parent
610 }
611 _ => break,
612 };
613 if let hir::ExprKind::Loop(..) = parent.kind {
614 direct = !direct;
617 }
618 if let hir::ExprKind::Loop(block, label, _, span) = parent.kind
619 && (destination.label == label || direct)
620 {
621 if let Some((reason_span, message)) =
622 self.maybe_get_coercion_reason(parent_id, parent.span)
623 {
624 err.span_label(reason_span, message);
625 err.span_label(
626 span,
627 format!("this loop is expected to be of type `{expected}`"),
628 );
629 break 'outer;
630 } else {
631 struct FindBreaks<'tcx> {
634 label: Option<rustc_ast::Label>,
635 uses: Vec<&'tcx hir::Expr<'tcx>>,
636 nest_depth: usize,
637 }
638 impl<'tcx> Visitor<'tcx> for FindBreaks<'tcx> {
639 fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
640 let nest_depth = self.nest_depth;
641 if let hir::ExprKind::Loop(_, label, _, _) = ex.kind {
642 if label == self.label {
643 return;
645 }
646 self.nest_depth += 1;
647 }
648 if let hir::ExprKind::Break(destination, _) = ex.kind
649 && (self.label == destination.label
650 || destination.label.is_none() && self.nest_depth == 0)
652 {
653 self.uses.push(ex);
654 }
655 hir::intravisit::walk_expr(self, ex);
656 self.nest_depth = nest_depth;
657 }
658 }
659 let mut expr_finder = FindBreaks { label, uses: vec![], nest_depth: 0 };
660 expr_finder.visit_block(block);
661 let mut exit = false;
662 for ex in expr_finder.uses {
663 let hir::ExprKind::Break(_, val) = ex.kind else {
664 continue;
665 };
666 let ty = match val {
667 Some(val) => {
668 match self.typeck_results.borrow().expr_ty_adjusted_opt(val) {
669 None => continue,
670 Some(ty) => ty,
671 }
672 }
673 None => self.tcx.types.unit,
674 };
675 if self.can_eq(self.param_env, ty, expected) {
676 err.span_label(ex.span, "expected because of this `break`");
677 exit = true;
678 }
679 }
680 if exit {
681 break 'outer;
682 }
683 }
684 }
685 }
686 }
687 }
688
689 fn annotate_expected_due_to_let_ty(
690 &self,
691 err: &mut Diag<'_>,
692 expr: &hir::Expr<'_>,
693 error: Option<TypeError<'tcx>>,
694 ) {
695 match (self.tcx.parent_hir_node(expr.hir_id), error) {
696 (hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init: Some(init), .. }), _)
697 if init.hir_id == expr.hir_id =>
698 {
699 err.span_label(ty.span, "expected due to this");
701 }
702 (
703 hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }),
704 Some(TypeError::Sorts(ExpectedFound { expected, .. })),
705 ) if rhs.hir_id == expr.hir_id && !expected.is_closure() => {
706 let mut primary_span = lhs.span;
709 let mut secondary_span = lhs.span;
710 let mut post_message = "";
711 match lhs.kind {
712 hir::ExprKind::Path(hir::QPath::Resolved(
713 None,
714 hir::Path {
715 res:
716 hir::def::Res::Def(
717 hir::def::DefKind::Static { .. } | hir::def::DefKind::Const,
718 def_id,
719 ),
720 ..
721 },
722 )) => {
723 if let Some(hir::Node::Item(hir::Item {
724 kind:
725 hir::ItemKind::Static(_, ident, ty, _)
726 | hir::ItemKind::Const(ident, _, ty, _),
727 ..
728 })) = self.tcx.hir_get_if_local(*def_id)
729 {
730 primary_span = ty.span;
731 secondary_span = ident.span;
732 post_message = " type";
733 }
734 }
735 hir::ExprKind::Path(hir::QPath::Resolved(
736 None,
737 hir::Path { res: hir::def::Res::Local(hir_id), .. },
738 )) => {
739 if let hir::Node::Pat(pat) = self.tcx.hir_node(*hir_id) {
740 primary_span = pat.span;
741 secondary_span = pat.span;
742 match self.tcx.parent_hir_node(pat.hir_id) {
743 hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
744 primary_span = ty.span;
745 post_message = " type";
746 }
747 hir::Node::LetStmt(hir::LetStmt { init: Some(init), .. }) => {
748 primary_span = init.span;
749 post_message = " value";
750 }
751 hir::Node::Param(hir::Param { ty_span, .. }) => {
752 primary_span = *ty_span;
753 post_message = " parameter type";
754 }
755 _ => {}
756 }
757 }
758 }
759 _ => {}
760 }
761
762 if primary_span != secondary_span
763 && self
764 .tcx
765 .sess
766 .source_map()
767 .is_multiline(secondary_span.shrink_to_hi().until(primary_span))
768 {
769 err.span_label(secondary_span, "expected due to the type of this binding");
772 err.span_label(primary_span, format!("expected due to this{post_message}"));
773 } else if post_message.is_empty() {
774 err.span_label(primary_span, "expected due to the type of this binding");
776 } else {
777 err.span_label(primary_span, format!("expected due to this{post_message}"));
779 }
780
781 if !lhs.is_syntactic_place_expr() {
782 err.downgrade_to_delayed_bug();
785 }
786 }
787 (
788 hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(_, lhs, rhs), .. }),
789 Some(TypeError::Sorts(ExpectedFound { expected, .. })),
790 ) if rhs.hir_id == expr.hir_id
791 && self.typeck_results.borrow().expr_ty_adjusted_opt(lhs) == Some(expected) =>
792 {
793 err.span_label(lhs.span, format!("expected because this is `{expected}`"));
794 }
795 _ => {}
796 }
797 }
798
799 fn annotate_mut_binding_to_immutable_binding(
818 &self,
819 err: &mut Diag<'_>,
820 expr: &hir::Expr<'_>,
821 expr_ty: Ty<'tcx>,
822 expected: Ty<'tcx>,
823 error: Option<TypeError<'tcx>>,
824 ) -> bool {
825 if let Some(TypeError::Sorts(ExpectedFound { .. })) = error
826 && let ty::Ref(_, inner, hir::Mutability::Not) = expected.kind()
827
828 && self.can_eq(self.param_env, *inner, expr_ty)
830
831 && let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }) =
833 self.tcx.parent_hir_node(expr.hir_id)
834 && rhs.hir_id == expr.hir_id
835
836 && let hir::ExprKind::Path(hir::QPath::Resolved(
838 None,
839 hir::Path { res: hir::def::Res::Local(hir_id), .. },
840 )) = lhs.kind
841 && let hir::Node::Pat(pat) = self.tcx.hir_node(*hir_id)
842
843 && let hir::Node::Param(hir::Param { ty_span, .. }) =
845 self.tcx.parent_hir_node(pat.hir_id)
846 && let item = self.tcx.hir_get_parent_item(pat.hir_id)
847 && let item = self.tcx.hir_owner_node(item)
848 && let Some(fn_decl) = item.fn_decl()
849
850 && let hir::PatKind::Binding(hir::BindingMode::MUT, _hir_id, ident, _) = pat.kind
852
853 && let Some(ty_ref) = fn_decl
855 .inputs
856 .iter()
857 .filter_map(|ty| match ty.kind {
858 hir::TyKind::Ref(lt, mut_ty) if ty.span == *ty_span => Some((lt, mut_ty)),
859 _ => None,
860 })
861 .next()
862 {
863 let mut sugg = if ty_ref.1.mutbl.is_mut() {
864 vec![]
866 } else {
867 vec![(
869 ty_ref.1.ty.span.shrink_to_lo(),
870 format!("{}mut ", if ty_ref.0.ident.span.is_empty() { "" } else { " " },),
871 )]
872 };
873 sugg.extend([
874 (pat.span.until(ident.span), String::new()),
875 (lhs.span.shrink_to_lo(), "*".to_string()),
876 ]);
877 err.multipart_suggestion_verbose(
880 "you might have meant to mutate the pointed at value being passed in, instead of \
881 changing the reference in the local binding",
882 sugg,
883 Applicability::MaybeIncorrect,
884 );
885 return true;
886 }
887 false
888 }
889
890 fn annotate_alternative_method_deref(
891 &self,
892 err: &mut Diag<'_>,
893 expr: &hir::Expr<'_>,
894 error: Option<TypeError<'tcx>>,
895 ) {
896 let Some(TypeError::Sorts(ExpectedFound { expected, .. })) = error else {
897 return;
898 };
899 let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }) =
900 self.tcx.parent_hir_node(expr.hir_id)
901 else {
902 return;
903 };
904 if rhs.hir_id != expr.hir_id || expected.is_closure() {
905 return;
906 }
907 let hir::ExprKind::Unary(hir::UnOp::Deref, deref) = lhs.kind else {
908 return;
909 };
910 let hir::ExprKind::MethodCall(path, base, args, _) = deref.kind else {
911 return;
912 };
913 let Some(self_ty) = self.typeck_results.borrow().expr_ty_adjusted_opt(base) else {
914 return;
915 };
916
917 let Ok(pick) = self.lookup_probe_for_diagnostic(
918 path.ident,
919 self_ty,
920 deref,
921 probe::ProbeScope::TraitsInScope,
922 None,
923 ) else {
924 return;
925 };
926
927 let Ok(in_scope_methods) = self.probe_for_name_many(
928 probe::Mode::MethodCall,
929 path.ident,
930 Some(expected),
931 probe::IsSuggestion(true),
932 self_ty,
933 deref.hir_id,
934 probe::ProbeScope::TraitsInScope,
935 ) else {
936 return;
937 };
938
939 let other_methods_in_scope: Vec<_> =
940 in_scope_methods.iter().filter(|c| c.item.def_id != pick.item.def_id).collect();
941
942 let Ok(all_methods) = self.probe_for_name_many(
943 probe::Mode::MethodCall,
944 path.ident,
945 Some(expected),
946 probe::IsSuggestion(true),
947 self_ty,
948 deref.hir_id,
949 probe::ProbeScope::AllTraits,
950 ) else {
951 return;
952 };
953
954 let suggestions: Vec<_> = all_methods
955 .into_iter()
956 .filter(|c| c.item.def_id != pick.item.def_id)
957 .map(|c| {
958 let m = c.item;
959 let generic_args = ty::GenericArgs::for_item(self.tcx, m.def_id, |param, _| {
960 self.var_for_def(deref.span, param)
961 });
962 let mutability =
963 match self.tcx.fn_sig(m.def_id).skip_binder().input(0).skip_binder().kind() {
964 ty::Ref(_, _, hir::Mutability::Mut) => "&mut ",
965 ty::Ref(_, _, _) => "&",
966 _ => "",
967 };
968 vec![
969 (
970 deref.span.until(base.span),
971 format!(
972 "{}({}",
973 with_no_trimmed_paths!(
974 self.tcx.def_path_str_with_args(m.def_id, generic_args,)
975 ),
976 mutability,
977 ),
978 ),
979 match &args {
980 [] => (base.span.shrink_to_hi().with_hi(deref.span.hi()), ")".to_string()),
981 [first, ..] => (base.span.between(first.span), ", ".to_string()),
982 },
983 ]
984 })
985 .collect();
986 if suggestions.is_empty() {
987 return;
988 }
989 let mut path_span: MultiSpan = path.ident.span.into();
990 path_span.push_span_label(
991 path.ident.span,
992 with_no_trimmed_paths!(format!(
993 "refers to `{}`",
994 self.tcx.def_path_str(pick.item.def_id),
995 )),
996 );
997 let container_id = pick.item.container_id(self.tcx);
998 let container = with_no_trimmed_paths!(self.tcx.def_path_str(container_id));
999 for def_id in pick.import_ids {
1000 let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
1001 path_span
1002 .push_span_label(self.tcx.hir_span(hir_id), format!("`{container}` imported here"));
1003 }
1004 let tail = with_no_trimmed_paths!(match &other_methods_in_scope[..] {
1005 [] => return,
1006 [candidate] => format!(
1007 "the method of the same name on {} `{}`",
1008 match candidate.kind {
1009 probe::CandidateKind::InherentImplCandidate { .. } => "the inherent impl for",
1010 _ => "trait",
1011 },
1012 self.tcx.def_path_str(candidate.item.container_id(self.tcx))
1013 ),
1014 _ if other_methods_in_scope.len() < 5 => {
1015 format!(
1016 "the methods of the same name on {}",
1017 listify(
1018 &other_methods_in_scope[..other_methods_in_scope.len() - 1],
1019 |c| format!("`{}`", self.tcx.def_path_str(c.item.container_id(self.tcx)))
1020 )
1021 .unwrap_or_default(),
1022 )
1023 }
1024 _ => format!(
1025 "the methods of the same name on {} other traits",
1026 other_methods_in_scope.len()
1027 ),
1028 });
1029 err.span_note(
1030 path_span,
1031 format!(
1032 "the `{}` call is resolved to the method in `{container}`, shadowing {tail}",
1033 path.ident,
1034 ),
1035 );
1036 if suggestions.len() > other_methods_in_scope.len() {
1037 err.note(format!(
1038 "additionally, there are {} other available methods that aren't in scope",
1039 suggestions.len() - other_methods_in_scope.len()
1040 ));
1041 }
1042 err.multipart_suggestions(
1043 format!(
1044 "you might have meant to call {}; you can use the fully-qualified path to call {} \
1045 explicitly",
1046 if suggestions.len() == 1 {
1047 "the other method"
1048 } else {
1049 "one of the other methods"
1050 },
1051 if suggestions.len() == 1 { "it" } else { "one of them" },
1052 ),
1053 suggestions,
1054 Applicability::MaybeIncorrect,
1055 );
1056 }
1057
1058 pub(crate) fn get_conversion_methods_for_diagnostic(
1059 &self,
1060 span: Span,
1061 expected: Ty<'tcx>,
1062 checked_ty: Ty<'tcx>,
1063 hir_id: hir::HirId,
1064 ) -> Vec<AssocItem> {
1065 let methods = self.probe_for_return_type_for_diagnostic(
1066 span,
1067 probe::Mode::MethodCall,
1068 expected,
1069 checked_ty,
1070 hir_id,
1071 |m| {
1072 self.has_only_self_parameter(m)
1073 && self
1074 .tcx
1075 .has_attr(m.def_id, sym::rustc_conversion_suggestion)
1086 },
1087 );
1088
1089 methods
1090 }
1091
1092 fn has_only_self_parameter(&self, method: &AssocItem) -> bool {
1094 method.is_method()
1095 && self.tcx.fn_sig(method.def_id).skip_binder().inputs().skip_binder().len() == 1
1096 }
1097
1098 pub(crate) fn maybe_get_block_expr(
1100 &self,
1101 expr: &hir::Expr<'tcx>,
1102 ) -> Option<&'tcx hir::Expr<'tcx>> {
1103 match expr {
1104 hir::Expr { kind: hir::ExprKind::Block(block, ..), .. } => block.expr,
1105 _ => None,
1106 }
1107 }
1108
1109 pub(crate) fn is_destruct_assignment_desugaring(&self, expr: &hir::Expr<'_>) -> bool {
1114 if let hir::ExprKind::Path(hir::QPath::Resolved(
1115 _,
1116 hir::Path { res: hir::def::Res::Local(bind_hir_id), .. },
1117 )) = expr.kind
1118 {
1119 let bind = self.tcx.hir_node(*bind_hir_id);
1120 let parent = self.tcx.parent_hir_node(*bind_hir_id);
1121 if let hir::Node::Pat(hir::Pat {
1122 kind: hir::PatKind::Binding(_, _hir_id, _, _), ..
1123 }) = bind
1124 && let hir::Node::Pat(hir::Pat { default_binding_modes: false, .. }) = parent
1125 {
1126 return true;
1127 }
1128 }
1129 false
1130 }
1131
1132 fn explain_self_literal(
1133 &self,
1134 err: &mut Diag<'_>,
1135 expr: &hir::Expr<'tcx>,
1136 expected: Ty<'tcx>,
1137 found: Ty<'tcx>,
1138 ) {
1139 match expr.peel_drop_temps().kind {
1140 hir::ExprKind::Struct(
1141 hir::QPath::Resolved(
1142 None,
1143 hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
1144 ),
1145 ..,
1146 )
1147 | hir::ExprKind::Call(
1148 hir::Expr {
1149 kind:
1150 hir::ExprKind::Path(hir::QPath::Resolved(
1151 None,
1152 hir::Path {
1153 res: hir::def::Res::SelfTyAlias { alias_to, .. },
1154 span,
1155 ..
1156 },
1157 )),
1158 ..
1159 },
1160 ..,
1161 ) => {
1162 if let Some(hir::Node::Item(hir::Item {
1163 kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
1164 ..
1165 })) = self.tcx.hir_get_if_local(*alias_to)
1166 {
1167 err.span_label(self_ty.span, "this is the type of the `Self` literal");
1168 }
1169 if let ty::Adt(e_def, e_args) = expected.kind()
1170 && let ty::Adt(f_def, _f_args) = found.kind()
1171 && e_def == f_def
1172 {
1173 err.span_suggestion_verbose(
1174 *span,
1175 "use the type name directly",
1176 self.tcx.value_path_str_with_args(e_def.did(), e_args),
1177 Applicability::MaybeIncorrect,
1178 );
1179 }
1180 }
1181 _ => {}
1182 }
1183 }
1184
1185 fn note_wrong_return_ty_due_to_generic_arg(
1186 &self,
1187 err: &mut Diag<'_>,
1188 expr: &hir::Expr<'_>,
1189 checked_ty: Ty<'tcx>,
1190 ) {
1191 let hir::Node::Expr(parent_expr) = self.tcx.parent_hir_node(expr.hir_id) else {
1192 return;
1193 };
1194 enum CallableKind {
1195 Function,
1196 Method,
1197 Constructor,
1198 }
1199 let mut maybe_emit_help = |def_id: hir::def_id::DefId,
1200 callable: Ident,
1201 args: &[hir::Expr<'_>],
1202 kind: CallableKind| {
1203 let arg_idx = args.iter().position(|a| a.hir_id == expr.hir_id).unwrap();
1204 let fn_ty = self.tcx.type_of(def_id).skip_binder();
1205 if !fn_ty.is_fn() {
1206 return;
1207 }
1208 let fn_sig = fn_ty.fn_sig(self.tcx).skip_binder();
1209 let Some(&arg) = fn_sig
1210 .inputs()
1211 .get(arg_idx + if matches!(kind, CallableKind::Method) { 1 } else { 0 })
1212 else {
1213 return;
1214 };
1215 if matches!(arg.kind(), ty::Param(_))
1216 && fn_sig.output().contains(arg)
1217 && self.node_ty(args[arg_idx].hir_id) == checked_ty
1218 {
1219 let mut multi_span: MultiSpan = parent_expr.span.into();
1220 multi_span.push_span_label(
1221 args[arg_idx].span,
1222 format!(
1223 "this argument influences the {} of `{}`",
1224 if matches!(kind, CallableKind::Constructor) {
1225 "type"
1226 } else {
1227 "return type"
1228 },
1229 callable
1230 ),
1231 );
1232 err.span_help(
1233 multi_span,
1234 format!(
1235 "the {} `{}` due to the type of the argument passed",
1236 match kind {
1237 CallableKind::Function => "return type of this call is",
1238 CallableKind::Method => "return type of this call is",
1239 CallableKind::Constructor => "type constructed contains",
1240 },
1241 checked_ty
1242 ),
1243 );
1244 }
1245 };
1246 match parent_expr.kind {
1247 hir::ExprKind::Call(fun, args) => {
1248 let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = fun.kind else {
1249 return;
1250 };
1251 let hir::def::Res::Def(kind, def_id) = path.res else {
1252 return;
1253 };
1254 let callable_kind = if matches!(kind, hir::def::DefKind::Ctor(_, _)) {
1255 CallableKind::Constructor
1256 } else {
1257 CallableKind::Function
1258 };
1259 maybe_emit_help(def_id, path.segments.last().unwrap().ident, args, callable_kind);
1260 }
1261 hir::ExprKind::MethodCall(method, _receiver, args, _span) => {
1262 let Some(def_id) =
1263 self.typeck_results.borrow().type_dependent_def_id(parent_expr.hir_id)
1264 else {
1265 return;
1266 };
1267 maybe_emit_help(def_id, method.ident, args, CallableKind::Method)
1268 }
1269 _ => return,
1270 }
1271 }
1272}
1273
1274pub(crate) enum TypeMismatchSource<'tcx> {
1275 Ty(Ty<'tcx>),
1278 Arg { call_expr: &'tcx hir::Expr<'tcx>, incompatible_arg: usize },
1282}