94b3cfa730c3 — Leonard Ritter 15 days ago
* `Option`: renamed `'unwrap` to `'force-unwrap` and `'try-unwrap` to `'unwrap`
* `Rc`: `'upgrade` no longer returns an option but raises an `UpgradeError`
* `Rc`: `'force-upgrade` causes a runtime assertion when it fails
5 files changed, 44 insertions(+), 25 deletions(-)

M lib/scopes/Option.sc
M lib/scopes/Rc.sc
M lib/scopes/testing.sc
M testing/test_option.sc
M testing/test_rc.sc
M lib/scopes/Option.sc +3 -3
@@ 60,15 60,15 @@ inline Option (T)
         inline __rimply (other-cls cls)
             option-rimply other-cls cls T
 
-        inline unwrap (self)
+        fn force-unwrap (self)
             assert self "unwrapping empty Option failed"
             extract-payload self T
 
-        inline try-unwrap (self)
+        fn unwrap (self)
             if (not self)
                 raise (UnwrapError)
             extract-payload self T
 
 do
-    let Option
+    let Option UnwrapError
     locals;

          
M lib/scopes/Rc.sc +28 -13
@@ 10,8 10,6 @@ 
     module provides a strong reference type `Rc`, as well as a weak reference
     type `Weak`.
 
-using import Option
-
 let
     PAYLOAD_INDEX = 0
     METADATA_INDEX = 1

          
@@ 72,6 70,10 @@ inline gen-type (T)
             wrap (T args...)
     RcType
 
+typedef UpgradeError : (tuple)
+    inline __typecall (cls)
+        bitcast none this-type
+
 typedef+ ReferenceCounted
     fn strong-count (value)
         viewing value

          
@@ 96,7 98,7 @@ typedef+ Weak
         let md = (extractvalue self METADATA_INDEX)
         let refcount = (getelementptr md 0 WEAKRC_INDEX)
         let rc = (sub (load refcount) 1)
-        assert (rc >= 0)
+        assert (rc >= 0) "corrupt refcount encountered"
         store rc refcount
         if (rc == 0)
             let strongrefcount = (getelementptr md 0 STRONGRC_INDEX)

          
@@ 114,15 116,28 @@ typedef+ Weak
                 extractvalue self METADATA_INDEX
                 \ 0 STRONGRC_INDEX
         let rc = (load refcount)
-        assert (rc >= 0)
-        let RcType = ((typeof self) . RcType)
-        let OptionType = (Option RcType)
+        assert (rc >= 0) "corrupt refcount encountered"
         if (rc == 0)
-            OptionType none
-        else
-            let rc = (add rc 1)
-            store rc refcount
-            OptionType (bitcast (dupe self) RcType)
+            raise (UpgradeError)
+        let RcType = ((typeof self) . RcType)
+        let rc = (add rc 1)
+        store rc refcount
+        bitcast (dupe self) RcType
+
+    fn force-upgrade (self)
+        viewing self
+        let refcount =
+            getelementptr
+                extractvalue self METADATA_INDEX
+                \ 0 STRONGRC_INDEX
+        let rc = (load refcount)
+        assert (rc >= 0) "corrupt refcount encountered"
+        assert (rc > 0) "upgrading Weak failed"
+        let RcType = ((typeof self) . RcType)
+        let rc = (add rc 1)
+        store rc refcount
+        bitcast (dupe self) RcType
+
 
 typedef+ Rc
     inline... __typecall

          
@@ 200,7 215,7 @@ typedef+ Rc
         let md = (extractvalue self METADATA_INDEX)
         let refcount = (getelementptr md 0 STRONGRC_INDEX)
         let rc = (sub (load refcount) 1)
-        assert (rc >= 0)
+        assert (rc >= 0) "corrupt refcount encountered"
         if (rc == 0)
             let payload = (view self)
             __drop payload

          
@@ 219,5 234,5 @@ typedef+ Rc
     unlet _view _drop
 
 do
-    let Rc Weak
+    let Rc Weak UpgradeError
     locals;

          
M lib/scopes/testing.sc +6 -2
@@ 84,8 84,12 @@ define-sugar-macro test-error
             false
         except (err)
             io-write! "ASSERT OK: "
-            print
-                'format err
+            static-if ((typeof err) == Error)
+                print
+                    'format err
+            else
+                print
+                    typeof err
             true
 
     inline assertion-error! (msg)

          
M testing/test_option.sc +2 -2
@@ 8,13 8,13 @@ do
     let opt = (optT (One 1234))
     let opt2 = (optT)
     let result =
-        try ('try-unwrap opt)
+        try ('unwrap opt)
         else
             error "unwrap failed"
     test (result == (One 1234))
     let fallback = (One 12345)
     let result =
-        try (deref ('try-unwrap opt2))
+        try (deref ('unwrap opt2))
         else (view fallback)
     test (result == (One 12345))
     ;

          
M testing/test_rc.sc +5 -5
@@ 49,8 49,8 @@ do
     test ((Rc.strong-count w) == 1)
     test ((Rc.weak-count w) == 1)
 
-    let v = ('upgrade w)
-    let p = ('unwrap v)
+    let v = ('force-upgrade w)
+    let p = v
 
     test ((Rc.strong-count c) == 2)
     test ((Rc.weak-count c) == 1)

          
@@ 67,7 67,7 @@ do
     test ((Rc.strong-count w) == 0)
     test ((Rc.weak-count w) == 1)
 
-    test (not ('upgrade w))
+    test-error ('upgrade w)
 
     ;
 

          
@@ 163,7 163,7 @@ do
         global data : (Option T)
         if (not data)
             data = (T 17)
-        'unwrap data
+        'force-unwrap data
 
     local example : T = (Rc.clone (singleton))
     test (example == (singleton))

          
@@ 190,7 190,7 @@ do
         global data : (Option RcT)
         if (not data)
             data = (RcT (a = (Inner 17)))
-        'unwrap data
+        'force-unwrap data
 
     let k = (Rc.clone (singleton))
     local example : RcT = k