From de6f430140a49e3ecf34dcb914326bb5b9789772 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Thu, 6 May 2021 13:15:40 +0500 Subject: Fix panics on pattern_arena.borrow with ugly cloning --- .../hir_ty/src/diagnostics/pattern/deconstruct_pat.rs | 19 +++++++++---------- crates/hir_ty/src/diagnostics/pattern/usefulness.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs index 479e0dc73..ddf6b1198 100644 --- a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs +++ b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs @@ -837,20 +837,19 @@ impl Fields { pat: PatId, cx: &MatchCheckCtx<'_>, ) -> Self { - // TODO: these alocations are so unfortunate (+1 for switching to references) - match cx.pattern_arena.borrow()[pat].kind.as_ref() { + // TODO: these alocations and clones are so unfortunate (+1 for switching to references) + let mut arena = cx.pattern_arena.borrow_mut(); + match arena[pat].kind.as_ref() { PatKind::Deref { subpattern } => { assert_eq!(self.len(), 1); - let subpattern = cx.pattern_arena.borrow_mut().alloc(subpattern.clone()); - Fields::from_single_pattern(subpattern) + let subpattern = subpattern.clone(); + Fields::from_single_pattern(arena.alloc(subpattern)) } PatKind::Leaf { subpatterns } | PatKind::Variant { subpatterns, .. } => { - let subpatterns = subpatterns.iter().map(|field_pat| { - ( - field_pat.field, - cx.pattern_arena.borrow_mut().alloc(field_pat.pattern.clone()), - ) - }); + let subpatterns = subpatterns.clone(); + let subpatterns = subpatterns + .iter() + .map(|field_pat| (field_pat.field, arena.alloc(field_pat.pattern.clone()))); self.replace_with_fieldpats(subpatterns) } diff --git a/crates/hir_ty/src/diagnostics/pattern/usefulness.rs b/crates/hir_ty/src/diagnostics/pattern/usefulness.rs index 28b9e71f8..3bfb7afed 100644 --- a/crates/hir_ty/src/diagnostics/pattern/usefulness.rs +++ b/crates/hir_ty/src/diagnostics/pattern/usefulness.rs @@ -94,12 +94,12 @@ impl PatIdExt for PatId { fn expand_or_pat(self, cx: &MatchCheckCtx<'_>) -> Vec { fn expand(pat: PatId, vec: &mut Vec, mut pat_arena: &mut PatternArena) { if let PatKind::Or { pats } = pat_arena[pat].kind.as_ref() { - // for pat in pats { - // // TODO(iDawer): Ugh, I want to go back to references (PatId -> &Pat) - // let pat = pat_arena.alloc(pat.clone()); - // expand(pat, vec, pat_arena); - // } - todo!() + let pats = pats.clone(); + for pat in pats { + // TODO(iDawer): Ugh, I want to go back to references (PatId -> &Pat) + let pat = pat_arena.alloc(pat.clone()); + expand(pat, vec, pat_arena); + } } else { vec.push(pat) } -- cgit v1.2.3