Skip to content

Commit 0cc1ef4

Browse files
committed
Don't ICE in has_self_borrows when coroutine captures-by-ref ty is still inferred
`coroutine_captures_by_ref_ty` is a fresh `next_ty_var` until upvar inference unifies it to an `FnPtr`. If `has_self_borrows` is reached before that — e.g. when the body contains an unresolved `as _` cast — the `_ => panic!()` arm fires. Treat the `Infer` case like `Error`: report self-borrows defensively so the closure falls back to `FnOnce`-only and the caller surfaces a normal type-inference error instead of an ICE. Closes #155999.
1 parent ba1a955 commit 0cc1ef4

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

compiler/rustc_type_ir/src/ty_kind/closure.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ impl<I: Interner> CoroutineClosureArgs<I> {
322322
.skip_binder()
323323
.visit_with(&mut HasRegionsBoundAt { binder: ty::INNERMOST })
324324
.is_break(),
325-
ty::Error(_) => true,
325+
// The captures-by-ref ty is a fresh inference variable until
326+
// upvar inference unifies it; treat that case like `Error`.
327+
ty::Error(_) | ty::Infer(_) => true,
326328
_ => panic!(),
327329
}
328330
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ edition:2021
2+
3+
// Regression test for #155999.
4+
5+
fn needs_fn_mut<T>(x: impl FnMut() -> T) {
6+
needs_fn_mut(async || x as _)
7+
//~^ ERROR type annotations needed
8+
}
9+
10+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/cast-as-infer-self-borrows.rs:6:32
3+
|
4+
LL | needs_fn_mut(async || x as _)
5+
| ^ cannot infer type
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)