From 1dcb89a4544a768d1759d8a4f0cd48d1992fd876 Mon Sep 17 00:00:00 2001 From: teodanciu Date: Thu, 9 Jan 2025 14:26:49 +0000 Subject: [PATCH] Improve error handling in constrained `genFromSpec` * use the existing error handling mechanism from constrained * remove usage of unsafePerformIO * remove incorrectly placed `catch` and catching of `AnyException` --- libs/constrained-generators/src/Constrained/Base.hs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libs/constrained-generators/src/Constrained/Base.hs b/libs/constrained-generators/src/Constrained/Base.hs index b1d804b8441..0ea3445599a 100644 --- a/libs/constrained-generators/src/Constrained/Base.hs +++ b/libs/constrained-generators/src/Constrained/Base.hs @@ -51,7 +51,6 @@ module Constrained.Base where import Control.Applicative import Control.Arrow (first) -import Control.Exception (SomeException, catch) import Control.Monad import Control.Monad.Identity import Control.Monad.Writer (Writer, runWriter, tell) @@ -76,7 +75,6 @@ import GHC.Real import GHC.Stack import GHC.TypeLits import Prettyprinter -import System.IO.Unsafe import System.Random import System.Random.Stateful import Test.QuickCheck hiding (Args, Fun, forAll) @@ -1285,12 +1283,10 @@ envFromPred env p = case p of -- | A version of `genFromSpecT` that simply errors if the generator fails genFromSpec :: forall fn a. (HasCallStack, HasSpec fn a) => Specification fn a -> Gen a genFromSpec spec = do - res <- strictGen $ explain1 "Calling genFromSpec" $ do - r <- genFromSpecT spec - unsafePerformIO $ - r `seq` - pure (pure r) `catch` \(e :: SomeException) -> pure (fatalError (pure $ show e)) - errorGE $ fmap pure res + res <- do + let genT = genFromSpecT @fn @a @GE spec + catchGen genT + pure $ either (error . show . NE.toList) id res -- | A version of `genFromSpecT` that takes a seed and a size and gives you a result genFromSpecWithSeed ::