From f6ab5f555231c66b2c10e6d5a8a34b7864814107 Mon Sep 17 00:00:00 2001 From: "J.Teeuwissen" Date: Tue, 23 May 2023 14:45:44 +0200 Subject: [PATCH] updated drop_specialization for boxes --- .../compiler/mono/src/drop_specialization.rs | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index 45f0d0a91b..61fcc63418 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -807,24 +807,55 @@ fn specialize_boxed<'a, 'i>( incremented_children: &mut MutSet, symbol: &Symbol, continuation: &'a Stmt<'a>, -) -> &'a mut Stmt<'a> { +) -> &'a Stmt<'a> { let removed = match incremented_children.iter().next() { - Some(s) => incremented_children.remove(&s.clone()), - None => false, + Some(s) => { + let s = *s; + incremented_children.remove(&s); + Some(s) + } + None => None, }; let new_continuation = specialize_drops_stmt(arena, layout_interner, ident_ids, environment, continuation); - if removed { - // No need to decrement the containing value since we already decremented the child. - arena.alloc(Stmt::Refcounting( - ModifyRc::DecRef(*symbol), - new_continuation, - )) - } else { - // No known children, keep decrementing the symbol. - arena.alloc(Stmt::Refcounting(ModifyRc::Dec(*symbol), new_continuation)) + match removed { + Some(s) => { + branch_uniqueness( + arena, + ident_ids, + layout_interner, + environment, + *symbol, + // If the symbol is unique: + // - free the box + |_, _, _| { + arena.alloc(Stmt::Refcounting( + // TODO can be replaced by free if ever added to the IR. + ModifyRc::DecRef(*symbol), + new_continuation, + )) + }, + // If the symbol is not unique: + // - increment the child + // - decref the box + |_, _, _| { + arena.alloc(Stmt::Refcounting( + ModifyRc::Inc(s, 1), + arena.alloc(Stmt::Refcounting( + ModifyRc::DecRef(*symbol), + new_continuation, + )), + )) + }, + new_continuation, + ) + } + None => { + // No known children, keep decrementing the symbol. + arena.alloc(Stmt::Refcounting(ModifyRc::Dec(*symbol), new_continuation)) + } } }