Skip to content

Commit 65ba586

Browse files
committed
interpreter: add chewing gum for stack traces of do-blocks
This restores the stack trace compromised in the previous commit, at the cost of making a small mess. This is its own commit in order to be able to revert it easily later. This works by stuffing a Maybe String into VDo, populating it from the stack trace goop, and feeding it to a wrapper around interpretStmts. As an instance of chewing gum, it could be worse...
1 parent e563f84 commit 65ba586

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

intTests/test_sawscript_builtins/fail2.log.good

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Loading file "fail2.saw"
22
Stack trace:
3+
"f" (fail2.saw:5:1-5:2)
34
"fail" (fail2.saw:4:17-4:21)
45
requested failure
56

saw-central/src/SAWCentral/Value.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ data Value
301301
-- | Returned value in unspecified monad
302302
| VReturn Value
303303
-- | Not-yet-executed do-block in unspecified monad
304-
| VDo SS.Pos LocalEnv [SS.Stmt]
304+
--
305+
-- The string is a hack hook for the current implementation of
306+
-- stack traces. See the commit message that added it for further
307+
-- information. XXX: to be removed along with the stack trace code
308+
| VDo SS.Pos (Maybe String) LocalEnv [SS.Stmt]
305309
-- | Single monadic bind in unspecified monad.
306310
-- This exists only to support the "for" builtin; see notes there
307311
-- for why this is so. XXX: remove it once that's no longer needed
@@ -476,7 +480,7 @@ showsPrecValue opts nenv p v =
476480
VTerm t -> showString (SAWCorePP.showTermWithNames opts nenv (ttTerm t))
477481
VType sig -> showString (pretty sig)
478482
VReturn v' -> showString "return " . showsPrecValue opts nenv (p + 1) v'
479-
VDo pos _env stmts ->
483+
VDo pos _name _env stmts ->
480484
let e = SS.Block pos stmts in
481485
shows (PP.pretty e)
482486
VBindOnce v1 v2 ->

saw-script/src/SAWScript/Interpreter.hs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,8 @@ withStackTraceFrame str val =
425425
withStackTraceFrame str `fmap` doProofScript m
426426
in
427427
VProofScript m'
428-
VDo _pos _env _stmts ->
429-
-- Blah. Let's just ignore it for now... XXX.
430-
-- (but with luck we'll be able to throw this code out before anyone cares)
431-
val
428+
VDo pos _ env stmts ->
429+
VDo pos (Just str) env stmts
432430
VBindOnce v1 v2 ->
433431
let v1' = withStackTraceFrame str v1
434432
v2' = withStackTraceFrame str v2
@@ -515,7 +513,7 @@ interpretExpr expr =
515513
VArray <$> traverse interpretExpr es
516514
SS.Block pos stmts -> do
517515
env <- getLocalEnv
518-
return $ VDo pos env stmts
516+
return $ VDo pos Nothing env stmts
519517
SS.Tuple _ es ->
520518
VTuple <$> traverse interpretExpr es
521519
SS.Record _ bs ->
@@ -622,8 +620,8 @@ interpretStmts stmts =
622620
-- (or whichever value for whichever monad)
623621
let v'' :: m Value = return v'
624622
return $ mkValue v''
625-
VDo _blkpos env stmts' ->
626-
withLocalEnvAny env (interpretStmts stmts')
623+
VDo _blkpos mname env stmts' ->
624+
withLocalEnvAny env (interpretStmts' mname stmts')
627625
VBindOnce m1 v2 -> do
628626
v1 <- actionFromValue m1
629627
m2 <- liftTopLevel $ applyValue "Value in a VBindOnce, via force" v2 v1
@@ -689,6 +687,20 @@ interpretStmts stmts =
689687
let env' = LocalTypedef (getVal name) ty : env
690688
withLocalEnv env' (interpretStmts ss)
691689

690+
-- Wrapper around interpretStmts for inserting a stack trace frame, in the
691+
-- old style. XXX remove along with the old stack trace code...
692+
interpretStmts' :: InterpreterMonad m => Maybe String -> [SS.Stmt] -> m Value
693+
interpretStmts' mname stmts = do
694+
trace <- liftTopLevel $ gets rwStackTrace
695+
case mname of
696+
Nothing -> pure ()
697+
Just name -> liftTopLevel $ modify (\rw -> rw { rwStackTrace = name : trace })
698+
v <- interpretStmts stmts
699+
case mname of
700+
Nothing -> pure ()
701+
Just _ -> liftTopLevel $ modify (\rw -> rw { rwStackTrace = trace })
702+
return v
703+
692704
-- Execute a top-level bind.
693705
processStmtBind ::
694706
InterpreterMonad m =>
@@ -1025,8 +1037,8 @@ instance IsValue a => IsValue (TopLevel a) where
10251037
instance FromValue a => FromValue (TopLevel a) where
10261038
fromValue (VTopLevel action) = fmap fromValue action
10271039
fromValue (VReturn v) = return (fromValue v)
1028-
fromValue (VDo _pos env stmts) = do
1029-
v <- withLocalEnv env (interpretStmts stmts)
1040+
fromValue (VDo _pos mname env stmts) = do
1041+
v <- withLocalEnv env (interpretStmts' mname stmts)
10301042
fromValue v
10311043
fromValue (VBindOnce m1 v2) = do
10321044
v1 <- fromValue m1
@@ -1044,8 +1056,8 @@ instance FromValue a => FromValue (ProofScript a) where
10441056
-- the type system should keep this from happening.
10451057
fromValue (VTopLevel m) = ProofScript (lift (lift (fmap fromValue m)))
10461058
fromValue (VReturn v) = return (fromValue v)
1047-
fromValue (VDo _pos env stmts) = do
1048-
v <- withLocalEnvProof env (interpretStmts stmts)
1059+
fromValue (VDo _pos mname env stmts) = do
1060+
v <- withLocalEnvProof env (interpretStmts' mname stmts)
10491061
fromValue v
10501062
fromValue (VBindOnce m1 v2) = ProofScript $ do
10511063
v1 <- unProofScript (fromValue m1)
@@ -1059,8 +1071,8 @@ instance IsValue a => IsValue (LLVMCrucibleSetupM a) where
10591071
instance FromValue a => FromValue (LLVMCrucibleSetupM a) where
10601072
fromValue (VLLVMCrucibleSetup m) = fmap fromValue m
10611073
fromValue (VReturn v) = return (fromValue v)
1062-
fromValue (VDo _pos env stmts) = do
1063-
v <- withLocalEnvLLVM env (interpretStmts stmts)
1074+
fromValue (VDo _pos mname env stmts) = do
1075+
v <- withLocalEnvLLVM env (interpretStmts' mname stmts)
10641076
fromValue v
10651077
fromValue (VBindOnce m1 v2) = LLVMCrucibleSetupM $ do
10661078
v1 <- runLLVMCrucibleSetupM (fromValue m1)
@@ -1074,8 +1086,8 @@ instance IsValue a => IsValue (JVMSetupM a) where
10741086
instance FromValue a => FromValue (JVMSetupM a) where
10751087
fromValue (VJVMSetup m) = fmap fromValue m
10761088
fromValue (VReturn v) = return (fromValue v)
1077-
fromValue (VDo _pos env stmts) = do
1078-
v <- withLocalEnvJVM env (interpretStmts stmts)
1089+
fromValue (VDo _pos mname env stmts) = do
1090+
v <- withLocalEnvJVM env (interpretStmts' mname stmts)
10791091
fromValue v
10801092
fromValue (VBindOnce m1 v2) = JVMSetupM $ do
10811093
v1 <- runJVMSetupM (fromValue m1)
@@ -1089,8 +1101,8 @@ instance IsValue a => IsValue (MIRSetupM a) where
10891101
instance FromValue a => FromValue (MIRSetupM a) where
10901102
fromValue (VMIRSetup m) = fmap fromValue m
10911103
fromValue (VReturn v) = return (fromValue v)
1092-
fromValue (VDo _pos env stmts) = do
1093-
v <- withLocalEnvMIR env (interpretStmts stmts)
1104+
fromValue (VDo _pos mname env stmts) = do
1105+
v <- withLocalEnvMIR env (interpretStmts' mname stmts)
10941106
fromValue v
10951107
fromValue (VBindOnce m1 v2) = MIRSetupM $ do
10961108
v1 <- runMIRSetupM (fromValue m1)

0 commit comments

Comments
 (0)