mirror of
https://github.com/carp-lang/Carp.git
synced 2024-08-16 08:40:41 +03:00
feat: don't manage blittable types (#1407)
This commit is contained in:
parent
fe91fb80ba
commit
3e5bdc0698
@ -7,15 +7,25 @@ import Types
|
|||||||
-- | Should this type be handled by the memory management system.
|
-- | Should this type be handled by the memory management system.
|
||||||
-- Implementation note: This top-level pattern match should be able to just
|
-- Implementation note: This top-level pattern match should be able to just
|
||||||
-- match on all types and see whether they implement 'delete', but for some
|
-- match on all types and see whether they implement 'delete', but for some
|
||||||
-- reson that doesn't work. Might need to handle generic types separately?
|
-- reason that doesn't work. Might need to handle generic types separately?
|
||||||
|
--
|
||||||
|
-- TODO: When blit and delete are both implemented, issue a warning.
|
||||||
isManaged :: TypeEnv -> Env -> Ty -> Bool
|
isManaged :: TypeEnv -> Env -> Ty -> Bool
|
||||||
isManaged typeEnv globalEnv structTy@StructTy {} =
|
isManaged typeEnv globalEnv structTy@StructTy {} =
|
||||||
interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [structTy] UnitTy StaticLifetimeTy)
|
not (isBlittable typeEnv globalEnv structTy)
|
||||||
|
&& interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [structTy] UnitTy StaticLifetimeTy)
|
||||||
isManaged typeEnv globalEnv funcTy@FuncTy {} =
|
isManaged typeEnv globalEnv funcTy@FuncTy {} =
|
||||||
interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [funcTy] UnitTy StaticLifetimeTy)
|
not (isBlittable typeEnv globalEnv funcTy)
|
||||||
|
&& interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [funcTy] UnitTy StaticLifetimeTy)
|
||||||
isManaged _ _ StringTy =
|
isManaged _ _ StringTy =
|
||||||
True
|
True
|
||||||
isManaged _ _ PatternTy =
|
isManaged _ _ PatternTy =
|
||||||
True
|
True
|
||||||
isManaged _ _ _ =
|
isManaged _ _ _ =
|
||||||
False
|
False
|
||||||
|
|
||||||
|
-- | Returns true if this type implements the "blit" interface and is thus
|
||||||
|
-- freely copyable.
|
||||||
|
isBlittable :: TypeEnv -> Env -> Ty -> Bool
|
||||||
|
isBlittable typeEnv globalEnv t =
|
||||||
|
interfaceImplementedForTy typeEnv globalEnv "blit" (FuncTy [t] t StaticLifetimeTy)
|
||||||
|
@ -18,6 +18,32 @@
|
|||||||
(f)
|
(f)
|
||||||
(assert-equal state 0l (Debug.memory-balance) descr)))
|
(assert-equal state 0l (Debug.memory-balance) descr)))
|
||||||
|
|
||||||
|
(deftype BlitEnum Copy Move)
|
||||||
|
(defmodule BlitEnum
|
||||||
|
(defn blit [x] (the BlitEnum x))
|
||||||
|
(implements blit blit)
|
||||||
|
|
||||||
|
(defn prn [x]
|
||||||
|
(match x
|
||||||
|
(BlitEnum.Copy) @"copy semantics"
|
||||||
|
(BlitEnum.Move) @"move semantics"))
|
||||||
|
(implements prn prn)
|
||||||
|
|
||||||
|
(defn change [x]
|
||||||
|
(match x
|
||||||
|
(BlitEnum.Copy) (BlitEnum.Move)
|
||||||
|
(BlitEnum.Move) (BlitEnum.Copy)))
|
||||||
|
)
|
||||||
|
|
||||||
|
(defn blit-1 []
|
||||||
|
;; blit types are not borrow checked and have copy semantics
|
||||||
|
(let-do [enum (BlitEnum.Copy)
|
||||||
|
enum-copy enum]
|
||||||
|
(IO.println &(prn enum))
|
||||||
|
(IO.println &(prn enum-copy))
|
||||||
|
(IO.println &(prn (change enum)))
|
||||||
|
(IO.println &(prn (change enum-copy)))))
|
||||||
|
|
||||||
(defn scope-1 []
|
(defn scope-1 []
|
||||||
(let [s @""]
|
(let [s @""]
|
||||||
()))
|
()))
|
||||||
@ -568,4 +594,5 @@
|
|||||||
(assert-no-leak test sumtype-12 "sumtype-12 does not leak")
|
(assert-no-leak test sumtype-12 "sumtype-12 does not leak")
|
||||||
(assert-no-leak test box-1 "box-1 does not leak")
|
(assert-no-leak test box-1 "box-1 does not leak")
|
||||||
(assert-no-leak test box-2 "box-2 does not leak")
|
(assert-no-leak test box-2 "box-2 does not leak")
|
||||||
|
(assert-no-leak test blit-1 "blit-1 does not leak")
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user