Skip to content

Commit 9d50d34

Browse files
Don't ICE when we have leftover child captures due to ambiguous closure params
1 parent 7a58674 commit 9d50d34

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

compiler/rustc_hir_typeck/src/upvar.rs

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
384384
let tupled_upvars_ty_for_borrow = Ty::new_tup_from_iter(
385385
self.tcx,
386386
ty::analyze_coroutine_closure_captures(
387+
self.tcx,
387388
typeck_results.closure_min_captures_flattened(closure_def_id),
388389
typeck_results
389390
.closure_min_captures_flattened(

compiler/rustc_middle/src/ty/closure.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::def_id::LocalDefId;
1313
use rustc_hir::HirId;
1414
use rustc_span::def_id::LocalDefIdMap;
1515
use rustc_span::symbol::Ident;
16-
use rustc_span::{Span, Symbol};
16+
use rustc_span::{Span, Symbol, DUMMY_SP};
1717

1818
use super::TyCtxt;
1919

@@ -418,6 +418,7 @@ impl BorrowKind {
418418
}
419419

420420
pub fn analyze_coroutine_closure_captures<'a, 'tcx: 'a, T>(
421+
tcx: TyCtxt<'tcx>,
421422
parent_captures: impl IntoIterator<Item = &'a CapturedPlace<'tcx>>,
422423
child_captures: impl IntoIterator<Item = &'a CapturedPlace<'tcx>>,
423424
mut for_each: impl FnMut((usize, &'a CapturedPlace<'tcx>), (usize, &'a CapturedPlace<'tcx>)) -> T,
@@ -466,7 +467,13 @@ pub fn analyze_coroutine_closure_captures<'a, 'tcx: 'a, T>(
466467
"we captured {parent_capture:#?} but it was not used in the child coroutine?"
467468
);
468469
}
469-
assert_eq!(child_captures.next(), None, "leftover child captures?");
470+
471+
if let Some((_, capture)) = child_captures.next() {
472+
tcx.dcx().span_delayed_bug(
473+
capture.info.path_expr_id.map_or(DUMMY_SP, |hir_id| tcx.hir().span(hir_id)),
474+
"leftover child captures: expecting an error",
475+
);
476+
}
470477
},
471478
)
472479
}

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody {
125125
.len();
126126

127127
let field_remapping: UnordMap<_, _> = ty::analyze_coroutine_closure_captures(
128+
tcx,
128129
tcx.closure_captures(parent_def_id).iter().copied(),
129130
tcx.closure_captures(coroutine_def_id).iter().skip(num_args).copied(),
130131
|(parent_field_idx, parent_capture), (child_field_idx, child_capture)| {

tests/crashes/123901.rs

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ edition: 2021
2+
3+
#![feature(async_closure)]
4+
5+
pub fn test(test: &()) {
6+
async |unconstrained| {
7+
//~^ ERROR type annotations needed
8+
(test,)
9+
};
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/leftover-captures-due-to-ambiguity.rs:6:27
3+
|
4+
LL | async |unconstrained| {
5+
| ___________________________^
6+
LL | |
7+
LL | | (test,)
8+
LL | | };
9+
| |_____^ cannot infer type
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)