Skip to content

Commit a351cd8

Browse files
authored
Implement new fall-back behavior for ? (#2203)
… and also track other standard changes, like parsing multiple shebang lines
1 parent 88ad565 commit a351cd8

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

dhall/dhall.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Extra-Source-Files:
159159
dhall-lang/Prelude/Natural/toDouble
160160
dhall-lang/Prelude/Natural/toInteger
161161
dhall-lang/Prelude/Natural/*.dhall
162+
dhall-lang/Prelude/NonEmpty/*.dhall
162163
dhall-lang/Prelude/Operator/package.dhall
163164
dhall-lang/Prelude/Optional/all
164165
dhall-lang/Prelude/Optional/any

dhall/src/Dhall/Import.hs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE DeriveAnyClass #-}
34
{-# LANGUAGE DeriveDataTypeable #-}
@@ -7,6 +8,7 @@
78
{-# LANGUAGE RankNTypes #-}
89
{-# LANGUAGE RecordWildCards #-}
910
{-# LANGUAGE ScopedTypeVariables #-}
11+
{-# LANGUAGE TypeApplications #-}
1012
{-# LANGUAGE ViewPatterns #-}
1113

1214
{-# OPTIONS_GHC -Wall #-}
@@ -167,6 +169,7 @@ import Data.List.NonEmpty (NonEmpty (..))
167169
import Data.Text (Text)
168170
import Data.Typeable (Typeable)
169171
import Data.Void (Void, absurd)
172+
import Dhall.TypeCheck (TypeError)
170173

171174
import Dhall.Syntax
172175
( Chunks (..)
@@ -201,13 +204,15 @@ import Lens.Family.State.Strict (zoom)
201204

202205
import qualified Codec.CBOR.Write as Write
203206
import qualified Codec.Serialise
207+
import qualified Control.Exception as Exception
204208
import qualified Control.Monad.State.Strict as State
205209
import qualified Control.Monad.Trans.Maybe as Maybe
206210
import qualified Data.ByteString
207211
import qualified Data.ByteString.Lazy
208212
import qualified Data.CaseInsensitive
209213
import qualified Data.Foldable
210214
import qualified Data.List.NonEmpty as NonEmpty
215+
import qualified Data.Maybe as Maybe
211216
import qualified Data.Text as Text
212217
import qualified Data.Text.Encoding
213218
import qualified Data.Text.IO
@@ -1157,11 +1162,29 @@ loadWith expr₀ = case expr₀ of
11571162

11581163
ImportAlt a b -> loadWith a `catch` handler₀
11591164
where
1160-
handler₀ (SourcedException (Src begin _ text₀) (MissingImports es₀)) =
1161-
loadWith b `catch` handler₁
1165+
is :: forall e . Exception e => SomeException -> Bool
1166+
is exception = Maybe.isJust (Exception.fromException @e exception)
1167+
1168+
isNotResolutionError exception =
1169+
is @(Imported (TypeError Src Void)) exception
1170+
|| is @(Imported Cycle ) exception
1171+
|| is @(Imported HashMismatch ) exception
1172+
|| is @(Imported ParseError ) exception
1173+
1174+
handler₀ exception₀@(SourcedException (Src begin _ text₀) (MissingImports es₀))
1175+
| any isNotResolutionError es₀ =
1176+
throwM exception₀
1177+
| otherwise = do
1178+
loadWith b `catch` handler₁
11621179
where
1163-
handler₁ (SourcedException (Src _ end text₁) (MissingImports es₁)) =
1164-
throwM (SourcedException (Src begin end text₂) (MissingImports (es₀ ++ es₁)))
1180+
handler₁ exception₁@(SourcedException (Src _ end text₁) (MissingImports es₁))
1181+
| any isNotResolutionError es₁ =
1182+
throwM exception₁
1183+
| otherwise =
1184+
-- Fix the source span for the error message to encompass both
1185+
-- alternatives, since both are equally to blame for the
1186+
-- failure if neither succeeds.
1187+
throwM (SourcedException (Src begin end text₂) (MissingImports (es₀ ++ es₁)))
11651188
where
11661189
text₂ = text₀ <> " ? " <> text₁
11671190

dhall/tests/Dhall/Test/Import.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ failureTest path = do
156156
let pathString = Text.unpack path
157157

158158
Tasty.HUnit.testCase pathString (do
159-
text <- Text.IO.readFile pathString
160-
161-
actualExpr <- Core.throws (Parser.exprFromText mempty text)
159+
actualExpr <- do
160+
Core.throws (Parser.exprFromText mempty (Test.Util.toDhallPath path))
162161

163162
succeeded <- Exception.catch @SomeException
164163
(do _ <- Test.Util.load actualExpr

0 commit comments

Comments
 (0)