1
Fork 0

Account for possible boxable impl Future in semicolon removal suggestions

This commit is contained in:
Esteban Küber 2020-10-21 19:43:15 -07:00
parent a4ee3ca1e4
commit 671d7c4afb
7 changed files with 152 additions and 32 deletions

View file

@ -688,13 +688,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}; };
let msg = "`match` arms have incompatible types"; let msg = "`match` arms have incompatible types";
err.span_label(outer_error_span, msg); err.span_label(outer_error_span, msg);
if let Some(sp) = semi_span { if let Some((sp, boxed)) = semi_span {
err.span_suggestion_short( if boxed {
sp, err.span_suggestion_verbose(
"consider removing this semicolon", sp,
String::new(), "consider removing this semicolon and boxing the expression",
Applicability::MachineApplicable, String::new(),
); Applicability::HasPlaceholders,
);
} else {
err.span_suggestion_short(
sp,
"consider removing this semicolon",
String::new(),
Applicability::MachineApplicable,
);
}
} }
if let Some(ret_sp) = opt_suggest_box_span { if let Some(ret_sp) = opt_suggest_box_span {
// Get return type span and point to it. // Get return type span and point to it.
@ -717,13 +726,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some(sp) = outer { if let Some(sp) = outer {
err.span_label(sp, "`if` and `else` have incompatible types"); err.span_label(sp, "`if` and `else` have incompatible types");
} }
if let Some(sp) = semicolon { if let Some((sp, boxed)) = semicolon {
err.span_suggestion_short( if boxed {
sp, err.span_suggestion_verbose(
"consider removing this semicolon", sp,
String::new(), "consider removing this semicolon and boxing the expression",
Applicability::MachineApplicable, String::new(),
); Applicability::HasPlaceholders,
);
} else {
err.span_suggestion_short(
sp,
"consider removing this semicolon",
String::new(),
Applicability::MachineApplicable,
);
}
} }
if let Some(ret_sp) = opt_suggest_box_span { if let Some(ret_sp) = opt_suggest_box_span {
self.suggest_boxing_for_return_impl_trait( self.suggest_boxing_for_return_impl_trait(

View file

@ -344,7 +344,7 @@ static_assert_size!(ObligationCauseCode<'_>, 32);
pub struct MatchExpressionArmCause<'tcx> { pub struct MatchExpressionArmCause<'tcx> {
pub arm_span: Span, pub arm_span: Span,
pub scrut_span: Span, pub scrut_span: Span,
pub semi_span: Option<Span>, pub semi_span: Option<(Span, bool)>,
pub source: hir::MatchSource, pub source: hir::MatchSource,
pub prior_arms: Vec<Span>, pub prior_arms: Vec<Span>,
pub last_ty: Ty<'tcx>, pub last_ty: Ty<'tcx>,
@ -357,7 +357,7 @@ pub struct IfExpressionCause {
pub then: Span, pub then: Span,
pub else_sp: Span, pub else_sp: Span,
pub outer: Option<Span>, pub outer: Option<Span>,
pub semicolon: Option<Span>, pub semicolon: Option<(Span, bool)>,
pub opt_suggest_box_span: Option<Span>, pub opt_suggest_box_span: Option<Span>,
} }

View file

@ -521,7 +521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
block: &'tcx hir::Block<'tcx>, block: &'tcx hir::Block<'tcx>,
expected_ty: Option<Ty<'tcx>>, expected_ty: Option<Ty<'tcx>>,
) -> (Span, Option<Span>) { ) -> (Span, Option<(Span, bool)>) {
if let Some(expr) = &block.expr { if let Some(expr) = &block.expr {
(expr.span, None) (expr.span, None)
} else if let Some(stmt) = block.stmts.last() { } else if let Some(stmt) = block.stmts.last() {

View file

@ -1061,7 +1061,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
blk: &'tcx hir::Block<'tcx>, blk: &'tcx hir::Block<'tcx>,
expected_ty: Ty<'tcx>, expected_ty: Ty<'tcx>,
) -> Option<Span> { ) -> Option<(Span, bool)> {
// Be helpful when the user wrote `{... expr;}` and // Be helpful when the user wrote `{... expr;}` and
// taking the `;` off is enough to fix the error. // taking the `;` off is enough to fix the error.
let last_stmt = blk.stmts.last()?; let last_stmt = blk.stmts.last()?;
@ -1070,13 +1070,62 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => return None, _ => return None,
}; };
let last_expr_ty = self.node_ty(last_expr.hir_id); let last_expr_ty = self.node_ty(last_expr.hir_id);
if matches!(last_expr_ty.kind(), ty::Error(_)) let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) {
|| self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err() (ty::Opaque(last_def_id, last_bounds), ty::Opaque(exp_def_id, exp_bounds)) => {
debug!(
"both opaque, likely future {:?} {:?} {:?} {:?}",
last_def_id, last_bounds, exp_def_id, exp_bounds
);
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_def_id.expect_local());
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_def_id.expect_local());
if let (
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: exp_bounds, .. }),
) = (
&self.tcx.hir().expect_item(last_hir_id).kind,
&self.tcx.hir().expect_item(exp_hir_id).kind,
) {
debug!("{:?} {:?}", last_bounds, exp_bounds);
last_bounds.iter().zip(exp_bounds.iter()).all(|(left, right)| {
match (left, right) {
(
hir::GenericBound::Trait(tl, ml),
hir::GenericBound::Trait(tr, mr),
) => {
tl.trait_ref.trait_def_id() == tr.trait_ref.trait_def_id()
&& ml == mr
}
(
hir::GenericBound::LangItemTrait(langl, _, _, argsl),
hir::GenericBound::LangItemTrait(langr, _, _, argsr),
) => {
// FIXME: consider the bounds!
debug!("{:?} {:?}", argsl, argsr);
langl == langr
}
_ => false,
}
})
} else {
false
}
}
_ => false,
};
debug!(
"needs_box {:?} {:?} {:?}",
needs_box,
last_expr_ty.kind(),
self.can_sub(self.param_env, last_expr_ty, expected_ty)
);
if (matches!(last_expr_ty.kind(), ty::Error(_))
|| self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err())
&& !needs_box
{ {
return None; return None;
} }
let original_span = original_sp(last_stmt.span, blk.span); let original_span = original_sp(last_stmt.span, blk.span);
Some(original_span.with_lo(original_span.hi() - BytePos(1))) Some((original_span.with_lo(original_span.hi() - BytePos(1)), needs_box))
} }
// Instantiates the given path, which must refer to an item with the given // Instantiates the given path, which must refer to an item with the given

View file

@ -758,13 +758,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected_ty: Ty<'tcx>, expected_ty: Ty<'tcx>,
err: &mut DiagnosticBuilder<'_>, err: &mut DiagnosticBuilder<'_>,
) { ) {
if let Some(span_semi) = self.could_remove_semicolon(blk, expected_ty) { if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) {
err.span_suggestion( if boxed {
span_semi, err.span_suggestion_verbose(
"consider removing this semicolon", span_semi,
String::new(), "consider removing this semicolon and boxing the expression",
Applicability::MachineApplicable, String::new(),
); Applicability::HasPlaceholders,
);
} else {
err.span_suggestion_short(
span_semi,
"consider removing this semicolon",
String::new(),
Applicability::MachineApplicable,
);
}
} }
} }

View file

@ -14,6 +14,7 @@ fn extra_semicolon() {
} }
async fn async_dummy() {} //~ NOTE the `Output` of this `async fn`'s found opaque type async fn async_dummy() {} //~ NOTE the `Output` of this `async fn`'s found opaque type
async fn async_dummy2() {} //~ NOTE the `Output` of this `async fn`'s found opaque type
async fn async_extra_semicolon_same() { async fn async_extra_semicolon_same() {
let _ = match true { //~ NOTE `match` arms have incompatible types let _ = match true { //~ NOTE `match` arms have incompatible types
@ -28,5 +29,17 @@ async fn async_extra_semicolon_same() {
}; };
} }
fn main() {} async fn async_extra_semicolon_different() {
let _ = match true { //~ NOTE `match` arms have incompatible types
true => {
async_dummy(); //~ NOTE this is found to be
//~^ HELP consider removing this semicolon
}
false => async_dummy2(), //~ ERROR `match` arms have incompatible types
//~^ NOTE expected `()`, found opaque type
//~| NOTE expected type `()`
//~| HELP consider `await`ing on the `Future`
};
}
fn main() {}

View file

@ -1,5 +1,5 @@
error[E0308]: `match` arms have incompatible types error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:24:18 --> $DIR/match-prev-arm-needing-semi.rs:25:18
| |
LL | async fn async_dummy() {} LL | async fn async_dummy() {}
| - the `Output` of this `async fn`'s found opaque type | - the `Output` of this `async fn`'s found opaque type
@ -20,7 +20,7 @@ LL | | };
| |
= note: expected type `()` = note: expected type `()`
found opaque type `impl Future` found opaque type `impl Future`
help: consider removing this semicolon help: consider removing this semicolon and boxing the expression
| |
LL | async_dummy() LL | async_dummy()
| -- | --
@ -29,6 +29,37 @@ help: consider `await`ing on the `Future`
LL | false => async_dummy().await, LL | false => async_dummy().await,
| ^^^^^^ | ^^^^^^
error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:38:18
|
LL | async fn async_dummy2() {}
| - the `Output` of this `async fn`'s found opaque type
...
LL | let _ = match true {
| _____________-
LL | | true => {
LL | | async_dummy();
| | -------------- this is found to be of type `()`
LL | |
LL | | }
LL | | false => async_dummy2(),
| | ^^^^^^^^^^^^^^ expected `()`, found opaque type
... |
LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected type `()`
found opaque type `impl Future`
help: consider removing this semicolon and boxing the expression
|
LL | async_dummy()
| --
help: consider `await`ing on the `Future`
|
LL | false => async_dummy2().await,
| ^^^^^^
error[E0308]: `match` arms have incompatible types error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:11:18 --> $DIR/match-prev-arm-needing-semi.rs:11:18
| |
@ -48,6 +79,6 @@ LL | |
LL | | }; LL | | };
| |_____- `match` arms have incompatible types | |_____- `match` arms have incompatible types
error: aborting due to 2 previous errors error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.