87c87fcbd7e3 — Leonard Ritter 15 days ago
* `Rc`: ensure `Rc.clone` properly dereferences source pointers when duping
3 files changed, 34 insertions(+), 3 deletions(-)

M lib/scopes/Rc.sc
M lib/scopes/testing.sc
M testing/test_rc.sc
M lib/scopes/Rc.sc +1 -1
@@ 140,7 140,7 @@ typedef+ Rc
                 \ 0 STRONGRC_INDEX
         let rc = (add (load refcount) 1)
         store rc refcount
-        dupe value
+        deref (dupe value)
 
     inline wrap (value)
         ((gen-type (typeof value)) . wrap) value

          
M lib/scopes/testing.sc +4 -0
@@ 213,6 213,10 @@ typedef One :: (tuple i32 (mutable point
     fn refcount ()
         deref _refcount
 
+    fn reset-refcount ()
+        _refcount = 0
+        ;
+
     fn test-refcount-balanced ()
         # this also fixes the refcount for subsequent tests
         let balanced? = (_refcount == 0)

          
M testing/test_rc.sc +29 -2
@@ 156,17 156,44 @@ do
 
 do
     # singleton test
-    T := (Rc i32)
+    T := (Rc One)
 
     fn singleton ()
         using import Option
         global data : (Option T)
         if (not data)
             data = (T 17)
-        deref ('unwrap data)
+        'unwrap data
 
     local example : T = (Rc.clone (singleton))
     test (example == (singleton))
     ;
+test ((One.refcount) == 1)
+One.reset-refcount;
+
+
+do
+    using import struct
+
+    # singleton test
+    typedef Inner :: i32
+        inline __typecall (cls v)
+            bitcast v this-type
+
+    struct T
+        a : Inner
+
+    RcT := (Rc T)
+
+    fn singleton ()
+        using import Option
+        global data : (Option RcT)
+        if (not data)
+            data = (RcT (a = (Inner 17)))
+        'unwrap data
+
+    let k = (Rc.clone (singleton))
+    local example : RcT = k
+    # error: value of type %1000:<Rc T> must be unique
 
 ;
  No newline at end of file