Skip to content

Commit c77f5a7

Browse files
committed
loadOriginHeaders: use parent stack instead of special-casing reentrant calls
1 parent 88f6fbd commit c77f5a7

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

dhall/src/Dhall/Import.hs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ import Control.Monad.IO.Class (MonadIO (..))
168168
import Control.Monad.Morph (hoist)
169169
import Control.Monad.State.Strict (MonadState, StateT)
170170
import Data.ByteString (ByteString)
171-
import Data.List.NonEmpty (NonEmpty (..))
171+
import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
172+
import Data.Maybe (fromMaybe)
172173
import Data.Text (Text)
173174
import Data.Typeable (Typeable)
174175
import Data.Void (Void, absurd)
@@ -1050,15 +1051,14 @@ defaultOriginHeaders = do
10501051
originHeadersLoader :: IO (Expr Src Import) -> StateT Status IO OriginHeaders
10511052
originHeadersLoader headersExpr = do
10521053

1053-
-- Load the headers using a parallel state with an empty impport chain.
1054-
-- We also set _loadOriginHeaders to prevent reentrant loads.
1054+
-- Load the headers using the parent stack, which should always be a local
1055+
-- import (we only load headers for the first remote import)
10551056

10561057
status <- State.get
10571058

1058-
let headerLoadStatus = status {
1059-
_stack = pure (NonEmpty.last (_stack status)),
1060-
_loadOriginHeaders = reentrantLoad
1061-
}
1059+
let parentStack = fromMaybe abortEmptyStack (nonEmpty (NonEmpty.tail (_stack status)))
1060+
1061+
let headerLoadStatus = status { _stack = parentStack }
10621062

10631063
(headers, _) <- liftIO (State.runStateT doLoad headerLoadStatus)
10641064

@@ -1067,14 +1067,7 @@ originHeadersLoader headersExpr = do
10671067

10681068
return headers
10691069
where
1070-
1071-
-- The builtin Cycle error should make this unnecessary,
1072-
-- but loadWith raises ReferentiallyOpaque before we have a chance to
1073-
-- raise a Cycle, and the former is caught by dhall's `?` operator.
1074-
reentrantLoad = do
1075-
Status { _stack } <- State.get
1076-
let (Chained parent) = NonEmpty.head _stack
1077-
throwMissingImport (Imported _stack (Cycle parent))
1070+
abortEmptyStack = Core.internalError "Origin headers loaded with an empty stack"
10781071

10791072
doLoad = do
10801073
partialExpr <- liftIO headersExpr

0 commit comments

Comments
 (0)