feat: don't manage blittable types (#1407)

This commit is contained in:
Scott Olsen 2022-04-04 13:05:54 -04:00 committed by GitHub
parent fe91fb80ba
commit 3e5bdc0698
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 3 deletions

View File

@ -7,15 +7,25 @@ import Types
-- | Should this type be handled by the memory management system.
-- 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
-- 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 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 {} =
interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [funcTy] UnitTy StaticLifetimeTy)
not (isBlittable typeEnv globalEnv funcTy)
&& interfaceImplementedForTy typeEnv globalEnv "delete" (FuncTy [funcTy] UnitTy StaticLifetimeTy)
isManaged _ _ StringTy =
True
isManaged _ _ PatternTy =
True
isManaged _ _ _ =
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)

View File

@ -18,6 +18,32 @@
(f)
(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 []
(let [s @""]
()))
@ -568,4 +594,5 @@
(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-2 "box-2 does not leak")
(assert-no-leak test blit-1 "blit-1 does not leak")
)