Skip to content

Commit

Permalink
Rollup merge of rust-lang#100452 - ouz-a:issue-93242, r=jackh726
Browse files Browse the repository at this point in the history
Fake capture closures if typeck results are empty

This ICE happens because `closure_min_captures` is empty, the reason it's empty is with the 2021 edition `enable_precise_capture` is set to true, which makes it so that we can't fake capture any information because that result of the `unwrap` is none hence the ICE.

Other solution is editing [maybe_read_scrutinee](https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_typeck/expr_use_visitor.rs.html#453-463) to this since empty slice contains no sub patterns.

Fixes rust-lang#93242

```rust
PatKind::Slice(_, ref slice, _) => {
    if slice.is_none(){
    need_to_be_read = true;
    }
}
// instead of
PatKind::Or(_)
| PatKind::Box(_)
| PatKind::Slice(..)
| PatKind::Ref(..)
| PatKind::Wild => {}
```
  • Loading branch information
Dylan-DPC authored Oct 24, 2022
2 parents a27e602 + 8b984e5 commit 48ade8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_hir_typeck/src/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// We now fake capture information for all variables that are mentioned within the closure
// We do this after handling migrations so that min_captures computes before
if !enable_precise_capture(self.tcx, span) {
if !enable_precise_capture(self.tcx, span)
// (ouz-a) #93242 - ICE happens because closure_min_captures is empty with
// 2021 edition, because it sets `enable_precise_capture` to true, which won't allow us
// fake capture information this check sidesteps that and avoids the ICE.
|| (infer_kind == None && self.typeck_results.borrow().closure_min_captures.is_empty())
{
let mut capture_information: InferredCaptureInformation<'tcx> = Default::default();

if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/closures/issue-93242.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
// edition:2021

pub fn something(path: &[usize]) -> impl Fn() -> usize + '_ {
move || match path {
[] => 0,
_ => 1,
}
}

fn main(){}

0 comments on commit 48ade8e

Please sign in to comment.