Auto merge of #83956 - estebank:issue-83892, r=varkor

Use a more appropriate span for `;` suggestion

Fix #83892.
This commit is contained in:
bors 2021-04-09 04:19:25 +00:00
commit 971608b54c
6 changed files with 42 additions and 9 deletions

View File

@ -1440,9 +1440,8 @@ fn report_return_mismatched_types<'a>(
// as prior return coercions would not be relevant (#57664).
let parent_id = fcx.tcx.hir().get_parent_node(id);
let fn_decl = if let Some((expr, blk_id)) = expression {
pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
&mut err, expr, expected, found, cause.span, blk_id,
);
pointing_at_return_type =
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
let parent = fcx.tcx.hir().get(parent_id);
if let (Some(cond_expr), true, false) = (
fcx.tcx.hir().get_if_cause(expr.hir_id),

View File

@ -603,7 +603,7 @@ fn check_expr_break(
&cause,
&mut |mut err| {
self.suggest_mismatched_types_on_tail(
&mut err, expr, ty, e_ty, cause.span, target_id,
&mut err, expr, ty, e_ty, target_id,
);
if let Some(val) = ty_kind_suggestion(ty) {
let label = destination

View File

@ -41,15 +41,14 @@ pub fn suggest_mismatched_types_on_tail(
expr: &'tcx hir::Expr<'tcx>,
expected: Ty<'tcx>,
found: Ty<'tcx>,
cause_span: Span,
blk_id: hir::HirId,
) -> bool {
let expr = expr.peel_drop_temps();
// If the expression is from an external macro, then do not suggest
// adding a semicolon, because there's nowhere to put it.
// See issue #81943.
if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, cause_span) {
self.suggest_missing_semicolon(err, expr, expected, cause_span);
if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, expr.span) {
self.suggest_missing_semicolon(err, expr, expected);
}
let mut pointing_at_return_type = false;
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
@ -389,7 +388,6 @@ fn suggest_missing_semicolon(
err: &mut DiagnosticBuilder<'_>,
expression: &'tcx hir::Expr<'tcx>,
expected: Ty<'tcx>,
cause_span: Span,
) {
if expected.is_unit() {
// `BlockTailExpression` only relevant if the tail expr would be
@ -404,7 +402,7 @@ fn suggest_missing_semicolon(
if expression.can_have_side_effects() =>
{
err.span_suggestion(
cause_span.shrink_to_hi(),
expression.span.shrink_to_hi(),
"consider using a semicolon here",
";".to_string(),
Applicability::MachineApplicable,

View File

@ -0,0 +1,11 @@
// run-rustfix
fn func() -> u8 {
0
}
fn main() {
match () {
() => func() //~ ERROR mismatched types
};
}

View File

@ -0,0 +1,11 @@
// run-rustfix
fn func() -> u8 {
0
}
fn main() {
match () {
() => func() //~ ERROR mismatched types
}
}

View File

@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/issue-83892.rs:9:15
|
LL | fn main() {
| - expected `()` because of default return type
LL | match () {
LL | () => func()
| ^^^^^^ expected `()`, found `u8`
LL | }
| - help: consider using a semicolon here: `;`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.