Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't ICE when we have leftover child captures due to ambiguous closure params #124525

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 0 additions & 8 deletions tests/crashes/123901.rs

This file was deleted.

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,)
};
Comment on lines +6 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is not very helpful to users. Can we link the inference variable of the coroutine with the one of the unconstrained argument?

}

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`.