Skip to content

Commit 2c8cae9

Browse files
committed
lol
1 parent 9fa850a commit 2c8cae9

File tree

1 file changed

+38
-29
lines changed
  • Cabal/src/Distribution/Simple/SetupHooks

1 file changed

+38
-29
lines changed

Cabal/src/Distribution/Simple/SetupHooks/Rule.hs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,17 @@ newtype instance Static User fnTy = UserStatic {userStaticPtr :: StaticPtr fnTy}
493493
newtype instance Static System fnTy = SystemStatic {userStaticKey :: StaticKey}
494494
deriving newtype (Eq, Ord, Show, Binary)
495495

496+
systemStatic :: Static User fnTy -> Static System fnTy
497+
systemStatic (UserStatic ptr) = SystemStatic (staticKey ptr)
498+
496499
instance Show (Static User fnTy) where
497-
showsPrec p ptr = showsPrec p (staticKey $ userStaticPtr ptr)
500+
showsPrec p ptr = showsPrec p (systemStatic ptr)
498501
instance Eq (Static User fnTy) where
499-
(==) = (==) `on` (staticKey . userStaticPtr)
502+
(==) = (==) `on` systemStatic
500503
instance Ord (Static User fnTy) where
501-
compare = compare `on` (staticKey . userStaticPtr)
504+
compare = compare `on` systemStatic
502505
instance Binary (Static User fnTy) where
503-
put (UserStatic ptr) = put (staticKey ptr)
506+
put = put . systemStatic
504507
get = do
505508
ptrKey <- get @StaticKey
506509
case unsafePerformIO $ unsafeLookupStaticPtr ptrKey of
@@ -525,7 +528,7 @@ type Command = CommandData User
525528
data CommandData (scope :: Scope) (arg :: Hs.Type) (res :: Hs.Type) = Command
526529
{ actionPtr :: !(Static scope (arg -> res))
527530
-- ^ The (statically-known) action to execute.
528-
, actionArg :: !(BinaryArg scope arg)
531+
, actionArg :: !(ScopedArgument scope arg)
529532
-- ^ The (possibly dynamic) argument to pass to the action.
530533
, cmdInstances :: !(Static scope (Dict (Binary arg, Show arg)))
531534
-- ^ Static evidence that the argument can be serialised and deserialised.
@@ -543,13 +546,13 @@ mkCommand
543546
mkCommand dict actionPtr arg =
544547
Command
545548
{ actionPtr = UserStatic actionPtr
546-
, actionArg = BinaryArg arg
549+
, actionArg = ScopedArgument arg
547550
, cmdInstances = UserStatic dict
548551
}
549552

550553
-- | Run a 'Command'.
551554
runCommand :: Command args res -> res
552-
runCommand (Command{actionPtr = UserStatic ptr, actionArg = BinaryArg arg}) =
555+
runCommand (Command{actionPtr = UserStatic ptr, actionArg = ScopedArgument arg}) =
553556
deRefStaticPtr ptr arg
554557

555558
-- | Commands to execute a rule:
@@ -626,7 +629,7 @@ of their choosing.
626629
627630
This all makes sense within the Hooks API, but when communicating with an
628631
external build system (such as cabal-install or HLS), these arguments are
629-
trated as opaque blobs of data (in particular if the Hooks are compiled into
632+
treated as opaque blobs of data (in particular if the Hooks are compiled into
630633
a separate executable, then the static pointers that contain the relevant
631634
instances for these user-chosen types can only be dereferenced from within that
632635
executable, and not on the side of the build system).
@@ -650,29 +653,29 @@ an 'Int64', we are nevertheless required to also serialise its size. This is bec
650653
on the build-system side, we don't have access to any of the types, and thus don't know
651654
how much to read in order to reconstruct the associated opaque 'ByteString'.
652655
To ensure we always serialise/deserialise including the length of the data,
653-
the 'BinaryArg' newtype is used, with a custom 'Binary' instance that always
656+
the 'ScopedArgument' newtype is used, with a custom 'Binary' instance that always
654657
incldues the length. We use this newtype:
655658
656659
- in the definition of 'CommandData', for arguments to rules,
657660
- in the definition of 'DepsRes', for the result of dynamic dependency computations.
658661
-}
659662

660-
newtype BinaryArg (scope :: Scope) arg = BinaryArg {getArg :: arg}
663+
newtype ScopedArgument (scope :: Scope) arg = ScopedArgument {getArg :: arg}
661664
deriving newtype (Eq, Ord, Show)
662665

663666
-- | Serialise/deserialise, always including the length of the payload.
664-
instance Binary arg => Binary (BinaryArg User arg) where
665-
put (BinaryArg arg) = put @LBS.ByteString (Binary.encode arg)
667+
instance Binary arg => Binary (ScopedArgument User arg) where
668+
put (ScopedArgument arg) = put @LBS.ByteString (Binary.encode arg)
666669
get = do
667670
dat <- get @LBS.ByteString
668671
case Binary.decodeOrFail dat of
669672
Left (_, _, err) -> fail err
670-
Right (_, _, res) -> return $ BinaryArg res
673+
Right (_, _, res) -> return $ ScopedArgument res
671674

672675
-- | Serialise and deserialise a raw ByteString, leaving it untouched.
673-
instance arg ~ LBS.ByteString => Binary (BinaryArg System arg) where
674-
put (BinaryArg arg) = put arg
675-
get = BinaryArg <$> get
676+
instance arg ~ LBS.ByteString => Binary (ScopedArgument System arg) where
677+
put (ScopedArgument arg) = put arg
678+
get = ScopedArgument <$> get
676679

677680
-- | A placeholder for a command that has been omitted, e.g. when we don't
678681
-- care about serialising/deserialising one particular command in a datatype.
@@ -681,7 +684,10 @@ data NoCmd (scope :: Scope) arg res = CmdOmitted
681684
deriving anyclass (Binary)
682685

683686
-- | A dynamic dependency command.
684-
newtype DynDepsCmd scope depsArg depsRes = DynDepsCmd {dynDepsCmd :: CommandData scope depsArg (IO ([Dependency], depsRes))}
687+
newtype DynDepsCmd scope depsArg depsRes = DynDepsCmd
688+
{ dynDepsCmd
689+
:: CommandData scope depsArg (IO ([Dependency], depsRes))
690+
}
685691

686692
deriving newtype instance Show (DynDepsCmd User depsArg depsRes)
687693
deriving newtype instance Eq (DynDepsCmd User depsArg depsRes)
@@ -694,9 +700,16 @@ deriving newtype instance
694700
=> Binary (DynDepsCmd System arg depsRes)
695701

696702
-- | The result of a dynamic dependency computation.
697-
newtype DepsRes (scope :: Scope) depsArg depsRes = DepsRes {depsRes :: BinaryArg scope depsRes}
703+
newtype DepsRes (scope :: Scope) depsArg depsRes = DepsRes
704+
{ depsRes
705+
:: ScopedArgument scope depsRes -- See Note [Hooks Binary instances]
706+
}
698707
deriving newtype (Show, Eq, Ord)
699708

709+
deriving newtype instance
710+
Binary (ScopedArgument scope depsRes)
711+
=> Binary (DepsRes scope depsArg depsRes)
712+
700713
-- | Both the rule command and the (optional) dynamic dependency command.
701714
type RuleCmds scope = RuleCommands scope DynDepsCmd CommandData
702715

@@ -743,7 +756,8 @@ runRuleDynDepsCmd = \case
743756
| Dict <- deRefStaticPtr instsPtr ->
744757
Just $ do
745758
(deps, depsRes) <- runCommand depsCmd
746-
return $ (deps, Binary.encode depsRes)
759+
-- See Note [Hooks Binary instances]
760+
return $ (deps, Binary.encode $ ScopedArgument @User depsRes)
747761

748762
-- | Project out the command for running the rule, passing in the result of
749763
-- the dependency computation if there was one.
@@ -783,7 +797,7 @@ ruleExecCmd
783797
DynamicRuleCommands
784798
{ dynamicRuleInstances = instsPtr
785799
, dynamicRuleCommand = cmd
786-
, dynamicDeps = DepsRes $ BinaryArg depsResBinary
800+
, dynamicDeps = DepsRes $ ScopedArgument depsResBinary
787801
, dynamicRuleTypeRep = tr
788802
}
789803

@@ -792,7 +806,7 @@ runRuleExecCmd :: RuleExecCmd User -> IO ()
792806
runRuleExecCmd = \case
793807
StaticRuleCommand{staticRuleCommand = cmd} -> runCommand cmd
794808
DynamicRuleCommands
795-
{ dynamicDeps = DepsRes (BinaryArg{getArg = res})
809+
{ dynamicDeps = DepsRes (ScopedArgument{getArg = res})
796810
, dynamicRuleCommand = cmd
797811
} ->
798812
runCommand cmd res
@@ -930,12 +944,6 @@ instance
930944
a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2
931945
_ == _ = False
932946

933-
instance Binary (BinaryArg scope depsRes) => Binary (DepsRes scope depsArgs depsRes) where
934-
put (DepsRes deps) = put $ Binary.encode deps -- See Note [put/get ByteString]
935-
get = do
936-
bs <- get
937-
return $ DepsRes $ Binary.decode bs -- See Note [put/get ByteString]
938-
939947
instance
940948
( forall arg res. Binary (ruleCmd User arg res)
941949
, forall depsArg depsRes. Binary depsRes => Binary (deps User depsArg depsRes)
@@ -1054,7 +1062,8 @@ instance
10541062
, dynamicRuleTypeRep = sTr
10551063
}
10561064

1057-
-- | Convenience function used for the pretty-printing @'Show' 'RuleBinary'@
1058-
-- instance.
1065+
--------------------------------------------------------------------------------
1066+
-- Showing rules
1067+
10591068
ruleBinary :: Rule -> RuleBinary
10601069
ruleBinary = Binary.decode . Binary.encode

0 commit comments

Comments
 (0)