Skip to content

Commit

Permalink
Workaround of early exiting when ExprUseVisitor meets unresolved ty…
Browse files Browse the repository at this point in the history
…pe var.

Add regression test for issue-87814

Set needs_to_read to true when typeck errors.
  • Loading branch information
ldm0 committed Aug 21, 2021
1 parent b6e334d commit 09f6027
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self;
let mut needs_to_be_read = false;
for arm in arms.iter() {
return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
match mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
match &pat.kind {
PatKind::Binding(.., opt_sub_pat) => {
// If the opt_sub_pat is None, than the binding does not count as
Expand Down Expand Up @@ -290,7 +290,13 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
// examined
}
}
}));
}) {
Ok(_) => (),
Err(_) => {
// If typeck failed, assume borrow is needed.
needs_to_be_read = true;
}
}
}

if needs_to_be_read {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/closures/issue-87814-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
fn main() {
let mut schema_all = vec![];
(0..42).for_each(|_x| match Err(()) as Result<(), _> {
Ok(()) => schema_all.push(()),
Err(_) => (),
});
}
11 changes: 11 additions & 0 deletions src/test/ui/closures/issue-87814-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
#![feature(try_reserve)]

fn main() {
let mut schema_all: (Vec<String>, Vec<String>) = (vec![], vec![]);

let _c = || match schema_all.0.try_reserve(1) as Result<(), _> {
Ok(()) => (),
Err(_) => (),
};
}

0 comments on commit 09f6027

Please sign in to comment.