|
5 | 5 | use crate::cast;
|
6 | 6 | use crate::coercion::CoerceMany;
|
7 | 7 | use crate::coercion::DynamicCoerceMany;
|
8 |
| -use crate::errors::ReturnLikeStatementKind; |
9 |
| -use crate::errors::TypeMismatchFruTypo; |
10 |
| -use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive}; |
11 | 8 | use crate::errors::{
|
12 |
| - FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, |
13 |
| - YieldExprOutsideOfCoroutine, |
| 9 | + AddressOfTemporaryTaken, FieldMultiplySpecifiedInInitializer, |
| 10 | + FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NoteCallerChoosesTyForTyParam, |
| 11 | + ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive, |
| 12 | + TypeMismatchFruTypo, YieldExprOutsideOfCoroutine, |
14 | 13 | };
|
15 | 14 | use crate::fatally_break_rust;
|
16 | 15 | use crate::method::SelfSource;
|
@@ -116,11 +115,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
116 | 115 | expected: Ty<'tcx>,
|
117 | 116 | expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
|
118 | 117 | ) -> Ty<'tcx> {
|
| 118 | + debug!("check_expr_coercible_to_type"); |
119 | 119 | let ty = self.check_expr_with_hint(expr, expected);
|
120 | 120 | // checks don't need two phase
|
121 |
| - self.demand_coerce(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No) |
| 121 | + let (res_ty, diag) = |
| 122 | + self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No); |
| 123 | + |
| 124 | + if let Some(diag) = diag { |
| 125 | + self.note_caller_chooses_ty_for_ty_param(diag, expected, ty); |
| 126 | + } |
| 127 | + |
| 128 | + res_ty |
| 129 | + } |
| 130 | + |
| 131 | + fn note_caller_chooses_ty_for_ty_param( |
| 132 | + &self, |
| 133 | + mut diag: Diag<'tcx>, |
| 134 | + expected: Ty<'tcx>, |
| 135 | + found: Ty<'tcx>, |
| 136 | + ) { |
| 137 | + if let ty::Param(expected_ty_as_param) = expected.kind() { |
| 138 | + diag.subdiagnostic( |
| 139 | + self.dcx(), |
| 140 | + NoteCallerChoosesTyForTyParam { |
| 141 | + ty_param_name: expected_ty_as_param.name, |
| 142 | + found_ty: found, |
| 143 | + }, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + diag.emit(); |
122 | 148 | }
|
123 | 149 |
|
| 150 | + #[instrument(skip(self, expr))] |
124 | 151 | pub(super) fn check_expr_with_hint(
|
125 | 152 | &self,
|
126 | 153 | expr: &'tcx hir::Expr<'tcx>,
|
|
0 commit comments