-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #121153 - chenyukang:yukang-fix-105431-type-mismatch,…
… r=estebank Suggest removing superfluous semicolon when statements used as expression Fixes #105431 - it's not a pure recursive visitor, so I guess there may be some more complex scenarios not covered. - moved `consider_removing_semicolon` to `compiler/rustc_infer` for reusing this helper function.
- Loading branch information
Showing
6 changed files
with
308 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#![allow(unused)] | ||
|
||
fn test_if() -> i32 { | ||
let x = if true { | ||
eprintln!("hello"); | ||
3; | ||
} | ||
else { | ||
4; | ||
}; | ||
x //~ ERROR mismatched types | ||
} | ||
|
||
fn test_if_without_binding() -> i32 { | ||
if true { //~ ERROR mismatched types | ||
eprintln!("hello"); | ||
3; | ||
} | ||
else { //~ ERROR mismatched types | ||
4; | ||
} | ||
} | ||
|
||
fn test_match() -> i32 { | ||
let v = 1; | ||
let res = match v { | ||
1 => { 1; } | ||
_ => { 2; } | ||
}; | ||
res //~ ERROR mismatched types | ||
} | ||
|
||
fn test_match_match_without_binding() -> i32 { | ||
let v = 1; | ||
match v { | ||
1 => { 1; } //~ ERROR mismatched types | ||
_ => { 2; } //~ ERROR mismatched types | ||
} | ||
} | ||
|
||
fn test_match_arm_different_types() -> i32 { | ||
let v = 1; | ||
let res = match v { | ||
1 => { if 1 < 2 { 1 } else { 2 } } | ||
_ => { 2; } //~ ERROR `match` arms have incompatible types | ||
}; | ||
res | ||
} | ||
|
||
fn test_if_match_mixed() -> i32 { | ||
let x = if true { | ||
3; | ||
} else { | ||
match 1 { | ||
1 => { 1 } | ||
_ => { 2 } | ||
}; | ||
}; | ||
x //~ ERROR mismatched types | ||
} | ||
|
||
fn test_if_match_mixed_failed() -> i32 { | ||
let x = if true { | ||
3; | ||
} else { | ||
// because this is a tailed expr, so we won't check deeper | ||
match 1 { | ||
1 => { 33; } | ||
_ => { 44; } | ||
} | ||
}; | ||
x //~ ERROR mismatched types | ||
} | ||
|
||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:11:5 | ||
| | ||
LL | fn test_if() -> i32 { | ||
| --- expected `i32` because of return type | ||
... | ||
LL | x | ||
| ^ expected `i32`, found `()` | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - 3; | ||
LL + 3 | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - 4; | ||
LL + 4 | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:15:13 | ||
| | ||
LL | if true { | ||
| _____________^ | ||
LL | | eprintln!("hello"); | ||
LL | | 3; | ||
| | - help: remove this semicolon to return this value | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:19:10 | ||
| | ||
LL | else { | ||
| __________^ | ||
LL | | 4; | ||
| | - help: remove this semicolon to return this value | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:30:5 | ||
| | ||
LL | fn test_match() -> i32 { | ||
| --- expected `i32` because of return type | ||
... | ||
LL | res | ||
| ^^^ expected `i32`, found `()` | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - 1 => { 1; } | ||
LL + 1 => { 1 } | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - _ => { 2; } | ||
LL + _ => { 2 } | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:36:14 | ||
| | ||
LL | 1 => { 1; } | ||
| ^^^-^^ | ||
| | | | ||
| | help: remove this semicolon to return this value | ||
| expected `i32`, found `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:37:14 | ||
| | ||
LL | _ => { 2; } | ||
| ^^^-^^ | ||
| | | | ||
| | help: remove this semicolon to return this value | ||
| expected `i32`, found `()` | ||
|
||
error[E0308]: `match` arms have incompatible types | ||
--> $DIR/stmts-as-exp-105431.rs:45:16 | ||
| | ||
LL | let res = match v { | ||
| _______________- | ||
LL | | 1 => { if 1 < 2 { 1 } else { 2 } } | ||
| | ------------------------- this is found to be of type `{integer}` | ||
LL | | _ => { 2; } | ||
| | ^- | ||
| | || | ||
| | |help: consider removing this semicolon | ||
| | expected integer, found `()` | ||
LL | | }; | ||
| |_____- `match` arms have incompatible types | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:59:5 | ||
| | ||
LL | fn test_if_match_mixed() -> i32 { | ||
| --- expected `i32` because of return type | ||
... | ||
LL | x | ||
| ^ expected `i32`, found `()` | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - 3; | ||
LL + 3 | ||
| | ||
help: remove this semicolon to return this value | ||
| | ||
LL - }; | ||
LL + } | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/stmts-as-exp-105431.rs:72:5 | ||
| | ||
LL | fn test_if_match_mixed_failed() -> i32 { | ||
| --- expected `i32` because of return type | ||
LL | let x = if true { | ||
LL | 3; | ||
| - help: remove this semicolon to return this value | ||
... | ||
LL | x | ||
| ^ expected `i32`, found `()` | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |