-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect match statement intended to be tail expression #81458
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -539,7 +539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
self.overwrite_local_ty_if_err(local, ty, pat_ty); | ||
} | ||
|
||
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>) { | ||
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>, is_last: bool) { | ||
// Don't do all the complex logic below for `DeclItem`. | ||
match stmt.kind { | ||
hir::StmtKind::Item(..) => return, | ||
|
@@ -567,7 +567,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
}); | ||
} | ||
hir::StmtKind::Semi(ref expr) => { | ||
self.check_expr(&expr); | ||
// All of this is equivalent to calling `check_expr`, but it is inlined out here | ||
// in order to capture the fact that this `match` is the last statement in its | ||
// function. This is done for better suggestions to remove the `;`. | ||
let expectation = match expr.kind { | ||
hir::ExprKind::Match(..) if is_last => IsLast(stmt.span), | ||
_ => NoExpectation, | ||
}; | ||
Comment on lines
+573
to
+576
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the other piece of code isn't at fault, let's try reverting to the logic you had before (bubbling the information down in the function arguments |
||
self.check_expr_with_expectation(expr, expectation); | ||
} | ||
} | ||
|
||
|
@@ -626,8 +633,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
let ctxt = BreakableCtxt { coerce: Some(coerce), may_break: false }; | ||
|
||
let (ctxt, ()) = self.with_breakable_ctxt(blk.hir_id, ctxt, || { | ||
for s in blk.stmts { | ||
self.check_stmt(s); | ||
for (pos, s) in blk.stmts.iter().enumerate() { | ||
self.check_stmt(s, blk.stmts.len() - 1 == pos); | ||
} | ||
|
||
// check the tail expression **without** holding the | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
pub trait Foo {} | ||
|
||
struct Bar; | ||
struct Baz; | ||
|
||
impl Foo for Bar { } | ||
impl Foo for Baz { } | ||
|
||
fn not_all_paths(a: &str) -> u32 { //~ ERROR mismatched types | ||
match a { | ||
"baz" => 0, | ||
_ => 1, | ||
}; | ||
} | ||
|
||
fn right(b: &str) -> Box<dyn Foo> { | ||
match b { | ||
"baz" => Box::new(Baz), | ||
_ => Box::new(Bar), | ||
} | ||
} | ||
|
||
fn wrong(c: &str) -> Box<dyn Foo> { //~ ERROR mismatched types | ||
match c { | ||
"baz" => Box::new(Baz), | ||
_ => Box::new(Bar), //~ ERROR `match` arms have incompatible types | ||
}; | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:9:30 | ||
| | ||
LL | fn not_all_paths(a: &str) -> u32 { | ||
| ------------- ^^^ expected `u32`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
... | ||
LL | }; | ||
| - help: consider removing this semicolon | ||
|
||
error[E0308]: `match` arms have incompatible types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:26:14 | ||
| | ||
LL | / match c { | ||
LL | | "baz" => Box::new(Baz), | ||
| | ------------- this is found to be of type `Box<Baz>` | ||
LL | | _ => Box::new(Bar), | ||
| | ^^^^^^^^^^^^^ expected struct `Baz`, found struct `Bar` | ||
LL | | }; | ||
| |_____- `match` arms have incompatible types | ||
| | ||
= note: expected type `Box<Baz>` | ||
found struct `Box<Bar>` | ||
note: you might have meant to return the `match` expression | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:27:6 | ||
| | ||
LL | fn wrong(c: &str) -> Box<dyn Foo> { | ||
| ------------ the `match` arms can conform to this return type | ||
LL | / match c { | ||
LL | | "baz" => Box::new(Baz), | ||
LL | | _ => Box::new(Bar), | ||
LL | | }; | ||
| | -^ the `match` is a statement because of this semicolon, consider removing it | ||
| |_____| | ||
| this could be implicitly returned but it is a statement, not a tail expression | ||
Comment on lines
+25
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new part. |
||
|
||
error[E0308]: mismatched types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:23:22 | ||
| | ||
LL | fn wrong(c: &str) -> Box<dyn Foo> { | ||
| ----- ^^^^^^^^^^^^ expected struct `Box`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
| | ||
= note: expected struct `Box<(dyn Foo + 'static)>` | ||
found unit type `()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this on every tail expression could potentially be the cause of the regression
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #82738 to check this.