Skip to content

Commit 6170e98

Browse files
committed
fix: Do not execute cell when there is variable shadowing.
1 parent 1ac41ee commit 6170e98

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

src/Sabela/Handlers.hs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ executeSingleCell st gen cid = do
158158
nb <- readMVar (stNotebook st)
159159
let metas = collectMetadata nb
160160
allCode = filter (\c -> cellType c == CodeCell) (nbCells nb)
161+
codePosMap = M.fromList (zip (map cellId (nbCells nb)) [1 ..])
161162
(defMap, _) = Topo.buildDefMap allCode
162163
(topoResult, redefMap) = Topo.computeTopoOrder allCode
163164
skipIds = Topo.trCycleIds topoResult `S.union` M.keysSet redefMap
@@ -173,9 +174,11 @@ executeSingleCell st gen cid = do
173174
st
174175
defMap
175176
(M.filterWithKey (\k _ -> k == cid) redefMap)
177+
codePosMap
176178
broadcastCycleErrors
177179
st
178180
(S.intersection (Topo.trCycleIds topoResult) (S.singleton cid))
181+
codePosMap
179182
else runAndBroadcast st gen cell
180183
still' <- isCurrentGen st gen
181184
when still' $ broadcast st EvExecutionDone
@@ -191,9 +194,10 @@ executeFullRestart st gen = do
191194
ok <- installAndRestart st gen needed
192195
when ok $ do
193196
let (defMap, _) = Topo.buildDefMap allCode
197+
codePosMap = M.fromList (zip (map cellId (nbCells nb)) [1 ..])
194198
(topoResult, redefMap) = Topo.computeTopoOrder allCode
195-
broadcastRedefinitionErrors st defMap redefMap
196-
broadcastCycleErrors st (Topo.trCycleIds topoResult)
199+
broadcastRedefinitionErrors st defMap redefMap codePosMap
200+
broadcastCycleErrors st (Topo.trCycleIds topoResult) codePosMap
197201
let skipIds = Topo.trCycleIds topoResult `S.union` M.keysSet redefMap
198202
toRun = filter (\c -> not (S.member (cellId c) skipIds)) (Topo.trOrdered topoResult)
199203
runCellList st gen toRun
@@ -386,6 +390,7 @@ executeAffected st gen editedCid = do
386390
depsOk = neededD `S.isSubsetOf` (installed `S.union` globalDeps)
387391
extsOk = neededE == instExts
388392
(defMap, _) = Topo.buildDefMap allCode
393+
codePosMap = M.fromList (zip (map cellId (nbCells nb)) [1 ..])
389394

390395
case (mSess, depsOk && extsOk) of
391396
(Just _, True) -> do
@@ -416,8 +421,8 @@ executeAffected st gen editedCid = do
416421
++ show (take 10 (S.toList uses))
417422
++ (if S.size uses > 10 then "..." else "")
418423

419-
broadcastRedefinitionErrors st defMap redefMap
420-
broadcastCycleErrors st (Topo.trCycleIds topoResult)
424+
broadcastRedefinitionErrors st defMap redefMap codePosMap
425+
broadcastCycleErrors st (Topo.trCycleIds topoResult) codePosMap
421426
let skipIds = Topo.trCycleIds topoResult `S.union` M.keysSet redefMap
422427
toRun = filter (\c -> not (S.member (cellId c) skipIds)) (Topo.trOrdered topoResult)
423428
runCellList st gen toRun
@@ -428,8 +433,8 @@ executeAffected st gen editedCid = do
428433
ok <- installAndRestart st gen needed
429434
when ok $ do
430435
let (topoResult, redefMap) = Topo.computeTopoOrder allCode
431-
broadcastRedefinitionErrors st defMap redefMap
432-
broadcastCycleErrors st (Topo.trCycleIds topoResult)
436+
broadcastRedefinitionErrors st defMap redefMap codePosMap
437+
broadcastCycleErrors st (Topo.trCycleIds topoResult) codePosMap
433438
let skipIds = Topo.trCycleIds topoResult `S.union` M.keysSet redefMap
434439
toRun = filter (\c -> not (S.member (cellId c) skipIds)) (Topo.trOrdered topoResult)
435440
runCellList st gen toRun
@@ -440,26 +445,33 @@ executeAffected st gen editedCid = do
440445
ok <- installAndRestart st gen needed
441446
when ok $ do
442447
let (topoResult, redefMap) = Topo.computeTopoOrder allCode
443-
broadcastRedefinitionErrors st defMap redefMap
444-
broadcastCycleErrors st (Topo.trCycleIds topoResult)
448+
broadcastRedefinitionErrors st defMap redefMap codePosMap
449+
broadcastCycleErrors st (Topo.trCycleIds topoResult) codePosMap
445450
let skipIds = Topo.trCycleIds topoResult `S.union` M.keysSet redefMap
446451
toRun = filter (\c -> not (S.member (cellId c) skipIds)) (Topo.trOrdered topoResult)
447452
runCellList st gen toRun
448453

449454
broadcastCellError :: AppState -> Int -> Text -> IO ()
450455
broadcastCellError st cid msg = do
451456
let err = CellError{ceLine = Nothing, ceCol = Nothing, ceMessage = msg}
457+
modifyMVar_ (stNotebook st) $ \nb ->
458+
pure nb{nbCells = map (clearCell cid msg) (nbCells nb)}
452459
broadcast st (EvCellResult cid [] (Just msg) [err])
460+
where
461+
clearCell targetCid errMsg c
462+
| cellId c == targetCid =
463+
c{cellOutputs = [], cellError = Just errMsg, cellDirty = False}
464+
| otherwise = c
453465

454466
broadcastRedefinitionErrors ::
455-
AppState -> M.Map Text Int -> M.Map Int [Text] -> IO ()
456-
broadcastRedefinitionErrors st defMap redefMap =
467+
AppState -> M.Map Text Int -> M.Map Int [Text] -> M.Map Int Int -> IO ()
468+
broadcastRedefinitionErrors st defMap redefMap codePosMap =
457469
forM_ (M.toList redefMap) $ \(cid, names) -> do
458470
let msgs =
459471
[ "'"
460472
<> name
461473
<> "' is already defined in cell "
462-
<> T.pack (show origCid)
474+
<> T.pack (show (M.findWithDefault origCid origCid codePosMap))
463475
<> " (which takes precedence)"
464476
| name <- names
465477
, Just origCid <- [M.lookup name defMap]
@@ -472,12 +484,15 @@ broadcastRedefinitionErrors st defMap redefMap =
472484
<> ". Remove the duplicate to resolve this conflict."
473485
broadcastCellError st cid combined
474486

475-
broadcastCycleErrors :: AppState -> S.Set Int -> IO ()
476-
broadcastCycleErrors st cycleIds
487+
broadcastCycleErrors :: AppState -> S.Set Int -> M.Map Int Int -> IO ()
488+
broadcastCycleErrors st cycleIds codePosMap
477489
| S.null cycleIds = pure ()
478490
| otherwise = do
479491
let cids = S.toList cycleIds
480-
cycleMsg = T.intercalate ", " (map (T.pack . show) cids)
492+
cycleMsg =
493+
T.intercalate
494+
", "
495+
(map (\c -> T.pack (show (M.findWithDefault c c codePosMap))) cids)
481496
msg =
482497
"This cell is part of a circular dependency and cannot be executed."
483498
<> " Cells in the cycle: ["

0 commit comments

Comments
 (0)