Skip to content

Commit d00e770

Browse files
committed
Auto merge of #95758 - compiler-errors:issue-54771, r=estebank
Only suggest removing semicolon when expression is compatible with `impl Trait` #54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc #54771
2 parents 48a9e10 + dfe13db commit d00e770

15 files changed

+97
-32
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,20 +1083,31 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10831083
let parent_node = hir.get_parent_node(obligation.cause.body_id);
10841084
let node = hir.find(parent_node);
10851085
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node
1086-
&& let body = hir.body(*body_id)
1087-
&& let hir::ExprKind::Block(blk, _) = &body.value.kind
1086+
&& let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
10881087
&& sig.decl.output.span().overlaps(span)
10891088
&& blk.expr.is_none()
1090-
&& *trait_pred.self_ty().skip_binder().kind() == ty::Tuple(ty::List::empty())
1091-
// FIXME(estebank): When encountering a method with a trait
1092-
// bound not satisfied in the return type with a body that has
1093-
// no return, suggest removal of semicolon on last statement.
1094-
// Once that is added, close #54771.
1089+
&& trait_pred.self_ty().skip_binder().is_unit()
10951090
&& let Some(stmt) = blk.stmts.last()
1096-
&& let hir::StmtKind::Semi(_) = stmt.kind
1091+
&& let hir::StmtKind::Semi(expr) = stmt.kind
1092+
// Only suggest this if the expression behind the semicolon implements the predicate
1093+
&& let Some(typeck_results) = self.in_progress_typeck_results
1094+
&& let Some(ty) = typeck_results.borrow().expr_ty_opt(expr)
1095+
&& self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred, ty))
10971096
{
1098-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
1099-
err.span_label(sp, "consider removing this semicolon");
1097+
err.span_label(
1098+
expr.span,
1099+
&format!(
1100+
"this expression has type `{}`, which implements `{}`",
1101+
ty,
1102+
trait_pred.print_modifiers_and_trait_path()
1103+
)
1104+
);
1105+
err.span_suggestion(
1106+
self.tcx.sess.source_map().end_point(stmt.span),
1107+
"remove this semicolon",
1108+
String::new(),
1109+
Applicability::MachineApplicable
1110+
);
11001111
return true;
11011112
}
11021113
false

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
936936
} else {
937937
err.span_suggestion_short(
938938
span_semi,
939-
"consider removing this semicolon",
939+
"remove this semicolon",
940940
String::new(),
941941
Applicability::MachineApplicable,
942942
);

src/test/ui/block-result/consider-removing-last-semi.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub fn f() -> String {
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | 0u8;
99
LL | "bla".to_string();
10-
| - help: consider removing this semicolon
10+
| - help: remove this semicolon
1111

1212
error[E0308]: mismatched types
1313
--> $DIR/consider-removing-last-semi.rs:8:15
@@ -18,7 +18,7 @@ LL | pub fn g() -> String {
1818
| implicitly returns `()` as its body has no tail or `return` expression
1919
LL | "this won't work".to_string();
2020
LL | "removeme".to_string();
21-
| - help: consider removing this semicolon
21+
| - help: remove this semicolon
2222

2323
error[E0308]: mismatched types
2424
--> $DIR/consider-removing-last-semi.rs:13:25
@@ -29,7 +29,7 @@ LL | pub fn macro_tests() -> u32 {
2929
| implicitly returns `()` as its body has no tail or `return` expression
3030
...
3131
LL | mac!();
32-
| - help: consider removing this semicolon
32+
| - help: remove this semicolon
3333

3434
error: aborting due to 3 previous errors
3535

src/test/ui/block-result/issue-11714.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn blah() -> i32 {
77
| implicitly returns `()` as its body has no tail or `return` expression
88
...
99
LL | ;
10-
| - help: consider removing this semicolon
10+
| - help: remove this semicolon
1111

1212
error: aborting due to previous error
1313

src/test/ui/block-result/issue-13428.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | fn bar() -> String {
1515
| implicitly returns `()` as its body has no tail or `return` expression
1616
LL | "foobar".to_string()
1717
LL | ;
18-
| - help: consider removing this semicolon
18+
| - help: remove this semicolon
1919

2020
error: aborting due to 2 previous errors
2121

src/test/ui/closures/old-closure-expression-remove-semicolon.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn foo() -> i32 {
77
fn main() {
88
let _x: i32 = {
99
//~^ ERROR mismatched types
10-
foo() //~ HELP consider removing this semicolon
10+
foo() //~ HELP remove this semicolon
1111
};
1212
}

src/test/ui/closures/old-closure-expression-remove-semicolon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn foo() -> i32 {
77
fn main() {
88
let _x: i32 = {
99
//~^ ERROR mismatched types
10-
foo(); //~ HELP consider removing this semicolon
10+
foo(); //~ HELP remove this semicolon
1111
};
1212
}

src/test/ui/closures/old-closure-expression-remove-semicolon.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _x: i32 = {
55
| ___________________^
66
LL | |
77
LL | | foo();
8-
| | - help: consider removing this semicolon
8+
| | - help: remove this semicolon
99
LL | | };
1010
| |_____^ expected `i32`, found `()`
1111

src/test/ui/coercion/coercion-missing-tail-expected-type.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn plus_one(x: i32) -> i32 {
66
| |
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | x + 1;
9-
| - help: consider removing this semicolon
9+
| - help: remove this semicolon
1010

1111
error[E0308]: mismatched types
1212
--> $DIR/coercion-missing-tail-expected-type.rs:8:13
@@ -16,7 +16,7 @@ LL | fn foo() -> Result<u8, u64> {
1616
| |
1717
| implicitly returns `()` as its body has no tail or `return` expression
1818
LL | Ok(1);
19-
| - help: consider removing this semicolon
19+
| - help: remove this semicolon
2020
|
2121
= note: expected enum `Result<u8, u64>`
2222
found unit type `()`

src/test/ui/issues/issue-6458-4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn foo(b: bool) -> Result<bool,String> {
66
| |
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | Err("bar".to_string());
9-
| - help: consider removing this semicolon
9+
| - help: remove this semicolon
1010
|
1111
= note: expected enum `Result<bool, String>`
1212
found unit type `()`

0 commit comments

Comments
 (0)