Add test cases and address review comments

This commit is contained in:
Esteban Küber 2020-08-22 17:24:26 -07:00
parent 5d2a935e6c
commit dc53cfea7e
5 changed files with 156 additions and 19 deletions

View file

@ -617,14 +617,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
ref prior_arms,
last_ty,
scrut_hir_id,
suggest_box,
opt_suggest_box_span,
arm_span,
..
}) => match source {
hir::MatchSource::IfLetDesugar { .. } => {
let msg = "`if let` arms have incompatible types";
err.span_label(cause.span, msg);
if let Some(ret_sp) = suggest_box {
if let Some(ret_sp) = opt_suggest_box_span {
self.suggest_boxing_for_return_impl_trait(
err,
ret_sp,
@ -684,7 +684,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
}
if let Some(ret_sp) = suggest_box {
if let Some(ret_sp) = opt_suggest_box_span {
// Get return type span and point to it.
self.suggest_boxing_for_return_impl_trait(
err,
@ -699,7 +699,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
else_sp,
outer,
semicolon,
suggest_box,
opt_suggest_box_span,
}) => {
err.span_label(then, "expected because of this");
if let Some(sp) = outer {
@ -713,7 +713,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
}
if let Some(ret_sp) = suggest_box {
if let Some(ret_sp) = opt_suggest_box_span {
self.suggest_boxing_for_return_impl_trait(
err,
ret_sp,

View file

@ -350,7 +350,7 @@ pub struct MatchExpressionArmCause<'tcx> {
pub prior_arms: Vec<Span>,
pub last_ty: Ty<'tcx>,
pub scrut_hir_id: hir::HirId,
pub suggest_box: Option<Span>,
pub opt_suggest_box_span: Option<Span>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -359,7 +359,7 @@ pub struct IfExpressionCause {
pub else_sp: Span,
pub outer: Option<Span>,
pub semicolon: Option<Span>,
pub suggest_box: Option<Span>,
pub opt_suggest_box_span: Option<Span>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]

View file

@ -119,13 +119,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// When we have a `match` as a tail expression in a `fn` with a returned `impl Trait`
// we check if the different arms would work with boxed trait objects instead and
// provide a structured suggestion in that case.
let suggest_box = match (
let opt_suggest_box_span = match (
orig_expected,
self.body_id
.owner
.to_def_id()
.as_local()
.and_then(|id| self.ret_coercion_impl_trait.map(|ty| (id, ty))),
self.ret_coercion_impl_trait.map(|ty| (self.body_id.owner, ty)),
) {
(Expectation::ExpectHasType(expected), Some((id, ty)))
if self.in_tail_expr && self.can_coerce(arm_ty, expected) =>
@ -181,7 +177,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&arm.body,
then_ty,
arm_ty,
suggest_box,
opt_suggest_box_span,
);
coercion.coerce(self, &cause, &arm.body, arm_ty);
}
@ -205,7 +201,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prior_arms: other_arms.clone(),
last_ty: prior_arm_ty.unwrap(),
scrut_hir_id: scrut.hir_id,
suggest_box,
opt_suggest_box_span,
}),
),
};
@ -330,7 +326,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
else_expr: &'tcx hir::Expr<'tcx>,
then_ty: Ty<'tcx>,
else_ty: Ty<'tcx>,
suggest_box: Option<Span>,
opt_suggest_box_span: Option<Span>,
) -> ObligationCause<'tcx> {
let mut outer_sp = if self.tcx.sess.source_map().is_multiline(span) {
// The `if`/`else` isn't in one line in the output, include some context to make it
@ -421,7 +417,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
else_sp: error_sp,
outer: outer_sp,
semicolon: remove_semicolon,
suggest_box,
opt_suggest_box_span,
}),
)
}