Skip to content

Commit

Permalink
Don't ICE when we have leftover child captures due to ambiguous closu…
Browse files Browse the repository at this point in the history
…re params
  • Loading branch information
compiler-errors committed Apr 29, 2024
1 parent 7a58674 commit 92cdff1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let tupled_upvars_ty_for_borrow = Ty::new_tup_from_iter(
self.tcx,
ty::analyze_coroutine_closure_captures(
self.tcx,
typeck_results.closure_min_captures_flattened(closure_def_id),
typeck_results
.closure_min_captures_flattened(
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_middle/src/ty/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir::def_id::LocalDefId;
use rustc_hir::HirId;
use rustc_span::def_id::LocalDefIdMap;
use rustc_span::symbol::Ident;
use rustc_span::{Span, Symbol};
use rustc_span::{Span, Symbol, DUMMY_SP};

use super::TyCtxt;

Expand Down Expand Up @@ -418,6 +418,7 @@ impl BorrowKind {
}

pub fn analyze_coroutine_closure_captures<'a, 'tcx: 'a, T>(
tcx: TyCtxt<'tcx>,
parent_captures: impl IntoIterator<Item = &'a CapturedPlace<'tcx>>,
child_captures: impl IntoIterator<Item = &'a CapturedPlace<'tcx>>,
mut for_each: impl FnMut((usize, &'a CapturedPlace<'tcx>), (usize, &'a CapturedPlace<'tcx>)) -> T,
Expand Down Expand Up @@ -466,7 +467,13 @@ pub fn analyze_coroutine_closure_captures<'a, 'tcx: 'a, T>(
"we captured {parent_capture:#?} but it was not used in the child coroutine?"
);
}
assert_eq!(child_captures.next(), None, "leftover child captures?");

if let Some((_, capture)) = child_captures.next() {
tcx.dcx().span_delayed_bug(
capture.info.path_expr_id.map_or(DUMMY_SP, |hir_id| tcx.hir().span(hir_id)),
"leftover child captures: expecting an error",
);
}
},
)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/coroutine/by_move_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody {
.len();

let field_remapping: UnordMap<_, _> = ty::analyze_coroutine_closure_captures(
tcx,
tcx.closure_captures(parent_def_id).iter().copied(),
tcx.closure_captures(coroutine_def_id).iter().skip(num_args).copied(),
|(parent_field_idx, parent_capture), (child_field_idx, child_capture)| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ edition: 2021

#![feature(async_closure)]

pub fn test(test: &()) {
async |unconstrained| {
//~^ ERROR type annotations needed
(test,)
};
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0282]: type annotations needed
--> $DIR/leftover-captures-due-to-ambiguity.rs:6:27
|
LL | async |unconstrained| {
| ___________________________^
LL | |
LL | | (test,)
LL | | };
| |_____^ cannot infer type

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.

0 comments on commit 92cdff1

Please sign in to comment.