address review comment
This commit is contained in:
parent
6d23ee8430
commit
26954f60ff
1 changed files with 125 additions and 90 deletions
|
@ -313,6 +313,18 @@ pub trait TypeErrCtxtExt<'tcx> {
|
||||||
predicate: ty::Predicate<'tcx>,
|
predicate: ty::Predicate<'tcx>,
|
||||||
call_hir_id: HirId,
|
call_hir_id: HirId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
fn look_for_iterator_item_mistakes(
|
||||||
|
&self,
|
||||||
|
assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
|
||||||
|
typeck_results: &TypeckResults<'tcx>,
|
||||||
|
type_diffs: &[TypeError<'tcx>],
|
||||||
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
|
path_segment: &hir::PathSegment<'_>,
|
||||||
|
args: &[hir::Expr<'_>],
|
||||||
|
err: &mut Diagnostic,
|
||||||
|
);
|
||||||
|
|
||||||
fn point_at_chain(
|
fn point_at_chain(
|
||||||
&self,
|
&self,
|
||||||
expr: &hir::Expr<'_>,
|
expr: &hir::Expr<'_>,
|
||||||
|
@ -321,6 +333,7 @@ pub trait TypeErrCtxtExt<'tcx> {
|
||||||
param_env: ty::ParamEnv<'tcx>,
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
);
|
);
|
||||||
|
|
||||||
fn probe_assoc_types_at_expr(
|
fn probe_assoc_types_at_expr(
|
||||||
&self,
|
&self,
|
||||||
type_diffs: &[TypeError<'tcx>],
|
type_diffs: &[TypeError<'tcx>],
|
||||||
|
@ -3592,39 +3605,24 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn point_at_chain(
|
fn look_for_iterator_item_mistakes(
|
||||||
&self,
|
&self,
|
||||||
expr: &hir::Expr<'_>,
|
assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
|
||||||
typeck_results: &TypeckResults<'tcx>,
|
typeck_results: &TypeckResults<'tcx>,
|
||||||
type_diffs: Vec<TypeError<'tcx>>,
|
type_diffs: &[TypeError<'tcx>],
|
||||||
param_env: ty::ParamEnv<'tcx>,
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
|
path_segment: &hir::PathSegment<'_>,
|
||||||
|
args: &[hir::Expr<'_>],
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
) {
|
) {
|
||||||
let mut primary_spans = vec![];
|
|
||||||
let mut span_labels = vec![];
|
|
||||||
|
|
||||||
let tcx = self.tcx;
|
let tcx = self.tcx;
|
||||||
|
|
||||||
let mut print_root_expr = true;
|
|
||||||
let mut assocs = vec![];
|
|
||||||
let mut expr = expr;
|
|
||||||
let mut prev_ty = self.resolve_vars_if_possible(
|
|
||||||
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
|
|
||||||
);
|
|
||||||
while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
|
|
||||||
// Point at every method call in the chain with the resulting type.
|
|
||||||
// vec![1, 2, 3].iter().map(mapper).sum<i32>()
|
|
||||||
// ^^^^^^ ^^^^^^^^^^^
|
|
||||||
expr = rcvr_expr;
|
|
||||||
let assocs_in_this_method =
|
|
||||||
self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
|
|
||||||
// Special case for iterator chains, we look at potential failures of `Iterator::Item`
|
// Special case for iterator chains, we look at potential failures of `Iterator::Item`
|
||||||
// not being `: Clone` and `Iterator::map` calls with spurious trailing `;`.
|
// not being `: Clone` and `Iterator::map` calls with spurious trailing `;`.
|
||||||
for entry in &assocs_in_this_method {
|
for entry in assocs_in_this_method {
|
||||||
let Some((_span, (def_id, ty))) = entry else {
|
let Some((_span, (def_id, ty))) = entry else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
for diff in &type_diffs {
|
for diff in type_diffs {
|
||||||
let Sorts(expected_found) = diff else {
|
let Sorts(expected_found) = diff else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
@ -3708,6 +3706,43 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn point_at_chain(
|
||||||
|
&self,
|
||||||
|
expr: &hir::Expr<'_>,
|
||||||
|
typeck_results: &TypeckResults<'tcx>,
|
||||||
|
type_diffs: Vec<TypeError<'tcx>>,
|
||||||
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
|
err: &mut Diagnostic,
|
||||||
|
) {
|
||||||
|
let mut primary_spans = vec![];
|
||||||
|
let mut span_labels = vec![];
|
||||||
|
|
||||||
|
let tcx = self.tcx;
|
||||||
|
|
||||||
|
let mut print_root_expr = true;
|
||||||
|
let mut assocs = vec![];
|
||||||
|
let mut expr = expr;
|
||||||
|
let mut prev_ty = self.resolve_vars_if_possible(
|
||||||
|
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
|
||||||
|
);
|
||||||
|
while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
|
||||||
|
// Point at every method call in the chain with the resulting type.
|
||||||
|
// vec![1, 2, 3].iter().map(mapper).sum<i32>()
|
||||||
|
// ^^^^^^ ^^^^^^^^^^^
|
||||||
|
expr = rcvr_expr;
|
||||||
|
let assocs_in_this_method =
|
||||||
|
self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
|
||||||
|
self.look_for_iterator_item_mistakes(
|
||||||
|
&assocs_in_this_method,
|
||||||
|
typeck_results,
|
||||||
|
&type_diffs,
|
||||||
|
param_env,
|
||||||
|
path_segment,
|
||||||
|
args,
|
||||||
|
err,
|
||||||
|
);
|
||||||
assocs.push(assocs_in_this_method);
|
assocs.push(assocs_in_this_method);
|
||||||
prev_ty = self.resolve_vars_if_possible(
|
prev_ty = self.resolve_vars_if_possible(
|
||||||
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
|
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue