Skip to content

Commit

Permalink
Merge pull request #5534 from unisonweb/cp/fix-unit-tags
Browse files Browse the repository at this point in the history
Ensure units are all created with matching tags
  • Loading branch information
aryairani authored Jan 13, 2025
2 parents 4324c53 + 8203840 commit 6b6fadb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion unison-runtime/src/Unison/Runtime/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Unison.Runtime.Builtin.Types
import Unison.Runtime.Foreign.Function.Type (ForeignFunc (..), foreignFuncBuiltinName)
import Unison.Runtime.Stack (UnboxedTypeTag (..), Val (..), unboxedTypeTagToInt)
import Unison.Runtime.Stack qualified as Closure
import Unison.Runtime.TypeTags qualified as TT
import Unison.Symbol
import Unison.Type qualified as Ty
import Unison.Util.EnumContainers as EC
Expand Down Expand Up @@ -1709,7 +1710,7 @@ declareForeign sand op func = do
in (Map.insert func (sand, code) funcs)

unitValue :: Val
unitValue = BoxedVal $ Closure.Enum Ty.unitRef (PackedTag 0)
unitValue = BoxedVal $ Closure.Enum Ty.unitRef TT.unitTag

natValue :: Word64 -> Val
natValue w = NatVal w
Expand Down
9 changes: 5 additions & 4 deletions unison-runtime/src/Unison/Runtime/Foreign/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import Unison.Builtin.Decls qualified as Ty
import Unison.Prelude hiding (Text, some)
import Unison.Reference
import Unison.Referent (Referent, pattern Ref)
import Unison.Runtime.ANF (Code, PackedTag (..), Value, internalBug)
import Unison.Runtime.ANF (Code, Value, internalBug)
import Unison.Runtime.ANF qualified as ANF
import Unison.Runtime.ANF.Rehash (checkGroupHashes)
import Unison.Runtime.ANF.Serialize qualified as ANF
Expand All @@ -150,6 +150,7 @@ import Unison.Runtime.Foreign qualified as F
import Unison.Runtime.Foreign.Function.Type (ForeignFunc (..))
import Unison.Runtime.MCode
import Unison.Runtime.Stack
import Unison.Runtime.TypeTags qualified as TT
import Unison.Symbol
import Unison.Type
( iarrayRef,
Expand Down Expand Up @@ -1764,10 +1765,10 @@ toUnisonPair ::
toUnisonPair (x, y) =
DataC
Ty.pairRef
(PackedTag 0)
[BoxedVal $ wr x, BoxedVal $ DataC Ty.pairRef (PackedTag 0) [BoxedVal $ wr y, BoxedVal $ un]]
TT.pairTag
[BoxedVal $ wr x, BoxedVal $ DataC Ty.pairRef TT.pairTag [BoxedVal $ wr y, BoxedVal $ un]]
where
un = DataC Ty.unitRef (PackedTag 0) []
un = DataC Ty.unitRef TT.unitTag []
wr z = Foreign $ wrapBuiltin z

unwrapForeignClosure :: Closure -> a
Expand Down
34 changes: 18 additions & 16 deletions unison-runtime/src/Unison/Runtime/Interface.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import Unison.Runtime.Machine
import Unison.Runtime.Pattern
import Unison.Runtime.Serialize as SER
import Unison.Runtime.Stack
import Unison.Runtime.TypeTags qualified as TT
import Unison.Symbol (Symbol)
import Unison.Syntax.HashQualified qualified as HQ (toText)
import Unison.Syntax.NamePrinter (prettyHashQualified)
Expand Down Expand Up @@ -1063,7 +1064,7 @@ executeMainComb ::
CCache ->
IO (Either (Pretty ColorText) ())
executeMainComb init cc = do
rSection <- resolveSection cc $ Ins (Pack RF.unitRef (PackedTag 0) ZArgs) $ Call True init init (VArg1 0)
rSection <- resolveSection cc $ Ins (Pack RF.unitRef TT.unitTag ZArgs) $ Call True init init (VArg1 0)
result <-
UnliftIO.try . eval0 cc Nothing $ rSection
case result of
Expand Down Expand Up @@ -1440,18 +1441,19 @@ buildSCache crsrc cssrc cacheableCombs trsrc ftm fty int rtmsrc rtysrc sndbx =
restrictTyR m = Map.restrictKeys m typeRefs

standalone :: CCache -> Word64 -> IO StoredCache
standalone cc init = readTVarIO (combRefs cc) >>= \crs ->
case EC.lookup init crs of
Just rinit ->
buildSCache crs
<$> readTVarIO (srcCombs cc)
<*> readTVarIO (cacheableCombs cc)
<*> readTVarIO (tagRefs cc)
<*> readTVarIO (freshTm cc)
<*> readTVarIO (freshTy cc)
<*> (readTVarIO (intermed cc) >>= traceNeeded rinit)
<*> readTVarIO (refTm cc)
<*> readTVarIO (refTy cc)
<*> readTVarIO (sandbox cc)
Nothing ->
die $ "standalone: unknown combinator: " ++ show init
standalone cc init =
readTVarIO (combRefs cc) >>= \crs ->
case EC.lookup init crs of
Just rinit ->
buildSCache crs
<$> readTVarIO (srcCombs cc)
<*> readTVarIO (cacheableCombs cc)
<*> readTVarIO (tagRefs cc)
<*> readTVarIO (freshTm cc)
<*> readTVarIO (freshTy cc)
<*> (readTVarIO (intermed cc) >>= traceNeeded rinit)
<*> readTVarIO (refTm cc)
<*> readTVarIO (refTy cc)
<*> readTVarIO (sandbox cc)
Nothing ->
die $ "standalone: unknown combinator: " ++ show init
4 changes: 2 additions & 2 deletions unison-runtime/src/Unison/Runtime/Machine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ unitValue = BoxedVal $ unitClosure
{-# NOINLINE unitValue #-}

unitClosure :: Closure
unitClosure = Enum Ty.unitRef (PackedTag 0)
unitClosure = Enum Ty.unitRef TT.unitTag
{-# NOINLINE unitClosure #-}

litToVal :: MLit -> Val
Expand Down Expand Up @@ -691,7 +691,7 @@ eval env !denv !activeThreads !stk !k r (NMatch _mr i br) = do
eval env denv activeThreads stk k r $ selectBranch n br
eval env !denv !activeThreads !stk !k r (RMatch i pu br) = do
(t, stk) <- dumpDataNoTag Nothing stk =<< peekOff stk i
if t == PackedTag 0
if t == TT.pureEffectTag
then eval env denv activeThreads stk k r pu
else case ANF.unpackTags t of
(ANF.rawTag -> e, ANF.rawTag -> t)
Expand Down
13 changes: 13 additions & 0 deletions unison-runtime/src/Unison/Runtime/TypeTags.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Unison.Runtime.TypeTags
rightTag,
falseTag,
trueTag,
pairTag,
pureEffectTag,
)
where

Expand Down Expand Up @@ -143,6 +145,17 @@ leftTag, rightTag :: PackedTag
(packTags et lt, packTags et rt)
| otherwise = error "internal error: either tags"

pairTag :: PackedTag
pairTag
| Just n <- Map.lookup Ty.pairRef builtinTypeNumbering,
pt <- toEnum (fromIntegral n) =
packTags pt 0
| otherwise = internalBug "internal error: pairTag"

-- | A tag we use to represent the 'pure' effect case.
pureEffectTag :: PackedTag
pureEffectTag = PackedTag 0

-- | Construct a tag for a single-constructor builtin type
mkSimpleTag :: String -> Reference -> PackedTag
mkSimpleTag msg r = mkEnumTag msg r 0
Expand Down

0 comments on commit 6b6fadb

Please sign in to comment.