Skip to content

Commit 39af025

Browse files
Rollup merge of #81991 - osa1:issue81839, r=estebank
Fix panic in 'remove semicolon' when types are not local It's not possible to check if removing a semicolon fixes the type error when checking match arms and one or both of the last arm's and the current arm's return types are imported "opaque" types. In these cases we don't generate a "consider removing semicolon" suggestions. Fixes #81839 --- I'm not sure how to add a test for this. I think the test would need at least two crates. Do we have any existing tests that do this so that I can take a look?
2 parents d38f6e8 + 9ef67e0 commit 39af025

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -1074,13 +1074,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10741074
};
10751075
let last_expr_ty = self.node_ty(last_expr.hir_id);
10761076
let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) {
1077+
(ty::Opaque(last_def_id, _), ty::Opaque(exp_def_id, _))
1078+
if last_def_id == exp_def_id =>
1079+
{
1080+
StatementAsExpression::CorrectType
1081+
}
10771082
(ty::Opaque(last_def_id, last_bounds), ty::Opaque(exp_def_id, exp_bounds)) => {
10781083
debug!(
10791084
"both opaque, likely future {:?} {:?} {:?} {:?}",
10801085
last_def_id, last_bounds, exp_def_id, exp_bounds
10811086
);
1082-
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_def_id.expect_local());
1083-
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_def_id.expect_local());
1087+
1088+
let (last_local_id, exp_local_id) =
1089+
match (last_def_id.as_local(), exp_def_id.as_local()) {
1090+
(Some(last_hir_id), Some(exp_hir_id)) => (last_hir_id, exp_hir_id),
1091+
(_, _) => return None,
1092+
};
1093+
1094+
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_local_id);
1095+
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_local_id);
1096+
10841097
match (
10851098
&self.tcx.hir().expect_item(last_hir_id).kind,
10861099
&self.tcx.hir().expect_item(exp_hir_id).kind,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
3+
pub struct Test {}
4+
5+
impl Test {
6+
pub async fn answer_str(&self, _s: &str) -> Test {
7+
Test {}
8+
}
9+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// aux-build:issue-81839.rs
2+
// edition:2018
3+
4+
extern crate issue_81839;
5+
6+
async fn test(ans: &str, num: i32, cx: &issue_81839::Test) -> u32 {
7+
match num {
8+
1 => {
9+
cx.answer_str("hi");
10+
}
11+
_ => cx.answer_str("hi"), //~ `match` arms have incompatible types
12+
}
13+
14+
1
15+
}
16+
17+
fn main() {}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: `match` arms have incompatible types
2+
--> $DIR/issue-81839.rs:11:14
3+
|
4+
LL | / match num {
5+
LL | | 1 => {
6+
LL | | cx.answer_str("hi");
7+
| | --------------------
8+
| | | |
9+
| | | help: consider removing this semicolon
10+
| | this is found to be of type `()`
11+
LL | | }
12+
LL | | _ => cx.answer_str("hi"),
13+
| | ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type
14+
LL | | }
15+
| |_____- `match` arms have incompatible types
16+
|
17+
::: $DIR/auxiliary/issue-81839.rs:6:49
18+
|
19+
LL | pub async fn answer_str(&self, _s: &str) -> Test {
20+
| ---- the `Output` of this `async fn`'s found opaque type
21+
|
22+
= note: expected type `()`
23+
found opaque type `impl Future`
24+
25+
error: aborting due to previous error
26+
27+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/suggestions/match-prev-arm-needing-semi.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ help: consider `await`ing on the `Future`
2424
|
2525
LL | false => async_dummy().await,
2626
| ^^^^^^
27-
help: consider removing this semicolon and boxing the expressions
28-
|
29-
LL | Box::new(async_dummy())
30-
LL |
31-
LL | }
32-
LL | false => Box::new(async_dummy()),
27+
help: consider removing this semicolon
3328
|
29+
LL | async_dummy()
30+
| --
3431

3532
error[E0308]: `match` arms have incompatible types
3633
--> $DIR/match-prev-arm-needing-semi.rs:39:18

0 commit comments

Comments
 (0)