Extract suggest_specify_actual_length into a separate function
This commit is contained in:
parent
6a05cd85ad
commit
21d5bedd5f
1 changed files with 61 additions and 59 deletions
|
@ -1976,16 +1976,32 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
(ty::Bool, ty::Tuple(list)) => if list.len() == 0 {
|
(ty::Bool, ty::Tuple(list)) => if list.len() == 0 {
|
||||||
suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
|
suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
|
||||||
}
|
}
|
||||||
(ty::Array(_, _), ty::Array(_, _)) => 'block: {
|
(ty::Array(_, _), ty::Array(_, _)) => suggestions.extend(self.specify_actual_length(terr, trace, span)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let code = trace.cause.code();
|
||||||
|
if let &MatchExpressionArm(box MatchExpressionArmCause { source, .. }) = code
|
||||||
|
&& let hir::MatchSource::TryDesugar = source
|
||||||
|
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
|
||||||
|
{
|
||||||
|
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert { found: found_ty.content(), expected: expected_ty.content() });
|
||||||
|
}
|
||||||
|
suggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
fn specify_actual_length(
|
||||||
|
&self,
|
||||||
|
terr: TypeError<'_>,
|
||||||
|
trace: &TypeTrace<'_>,
|
||||||
|
span: Span,
|
||||||
|
) -> Option<TypeErrorAdditionalDiags> {
|
||||||
let hir = self.tcx.hir();
|
let hir = self.tcx.hir();
|
||||||
let TypeError::FixedArraySize(sz) = terr else {
|
let TypeError::FixedArraySize(sz) = terr else {
|
||||||
break 'block;
|
return None;
|
||||||
};
|
};
|
||||||
let tykind = match hir.find_by_def_id(trace.cause.body_id) {
|
let tykind = match hir.find_by_def_id(trace.cause.body_id) {
|
||||||
Some(hir::Node::Item(hir::Item {
|
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
|
||||||
kind: hir::ItemKind::Fn(_, _, body_id),
|
|
||||||
..
|
|
||||||
})) => {
|
|
||||||
let body = hir.body(*body_id);
|
let body = hir.body(*body_id);
|
||||||
struct LetVisitor<'v> {
|
struct LetVisitor<'v> {
|
||||||
span: Span,
|
span: Span,
|
||||||
|
@ -2014,39 +2030,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut visitor = LetVisitor {span, result: None};
|
let mut visitor = LetVisitor { span, result: None };
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
visitor.result.map(|r| &r.peel_refs().kind)
|
visitor.result.map(|r| &r.peel_refs().kind)
|
||||||
}
|
}
|
||||||
Some(hir::Node::Item(hir::Item {
|
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. })) => {
|
||||||
kind: hir::ItemKind::Const(ty, _),
|
|
||||||
..
|
|
||||||
})) => {
|
|
||||||
Some(&ty.peel_refs().kind)
|
Some(&ty.peel_refs().kind)
|
||||||
}
|
}
|
||||||
_ => None
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(tykind) = tykind
|
if let Some(tykind) = tykind
|
||||||
&& let hir::TyKind::Array(_, length) = tykind
|
&& let hir::TyKind::Array(_, length) = tykind
|
||||||
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
|
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
|
||||||
&& let Some(span) = self.tcx.hir().opt_span(*hir_id)
|
&& let Some(span) = self.tcx.hir().opt_span(*hir_id)
|
||||||
{
|
{
|
||||||
suggestions.push(TypeErrorAdditionalDiags::ConsiderSpecifyingLength { span, length: sz.found });
|
Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength { span, length: sz.found })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let code = trace.cause.code();
|
|
||||||
if let &MatchExpressionArm(box MatchExpressionArmCause { source, .. }) = code
|
|
||||||
&& let hir::MatchSource::TryDesugar = source
|
|
||||||
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
|
|
||||||
{
|
|
||||||
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert { found: found_ty.content(), expected: expected_ty.content() });
|
|
||||||
}
|
|
||||||
suggestions
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn report_and_explain_type_error(
|
pub fn report_and_explain_type_error(
|
||||||
&self,
|
&self,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue