Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new fall-back behavior for ? #2203

Merged
merged 10 commits into from
Jun 11, 2021
2 changes: 1 addition & 1 deletion dhall/dhall-lang
Submodule dhall-lang updated 36 files
+17 −31 Prelude/JSON/renderAs.dhall
+9 −0 Prelude/NonEmpty/Type.dhall
+32 −0 Prelude/NonEmpty/all.dhall
+32 −0 Prelude/NonEmpty/any.dhall
+58 −0 Prelude/NonEmpty/concat.dhall
+53 −0 Prelude/NonEmpty/concatMap.dhall
+12 −0 Prelude/NonEmpty/head.dhall
+31 −0 Prelude/NonEmpty/index.dhall
+39 −0 Prelude/NonEmpty/indexed.dhall
+16 −0 Prelude/NonEmpty/last.dhall
+15 −0 Prelude/NonEmpty/length.dhall
+17 −0 Prelude/NonEmpty/make.dhall
+28 −0 Prelude/NonEmpty/map.dhall
+55 −0 Prelude/NonEmpty/package.dhall
+33 −0 Prelude/NonEmpty/reverse.dhall
+97 −0 Prelude/NonEmpty/shifted.dhall
+13 −0 Prelude/NonEmpty/singleton.dhall
+16 −0 Prelude/NonEmpty/toList.dhall
+56 −0 Prelude/NonEmpty/unzip.dhall
+61 −0 Prelude/NonEmpty/zip.dhall
+3 −0 Prelude/package.dhall
+15 −0 nixops/packages/Prelude_20_2_0.nix
+1 −0 nixops/store.nix
+46 −16 standard/imports.md
+1 −0 tests/import/data/doesNotParse.dhall
+1 −1 tests/import/failure/unit/DontRecoverCycle.dhall
+3 −0 tests/import/failure/unit/DontRecoverHashMismatch.dhall
+1 −0 tests/import/failure/unit/DontRecoverParseError.dhall
+1 −1 tests/import/failure/unit/DontRecoverTypeError.dhall
+0 −1 tests/import/success/unit/AlternativeHashMismatchA.dhall
+0 −1 tests/import/success/unit/AlternativeHashMismatchB.dhall
+0 −1 tests/import/success/unit/AlternativeParseErrorA.dhall
+0 −1 tests/import/success/unit/AlternativeParseErrorB.dhall
+0 −1 tests/import/success/unit/AlternativeTypeErrorA.dhall
+0 −1 tests/import/success/unit/AlternativeTypeErrorB.dhall
+86 −0 tests/type-inference/success/preludeB.dhall
31 changes: 27 additions & 4 deletions dhall/src/Dhall/Import.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
Expand All @@ -7,6 +8,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -Wall #-}
Expand Down Expand Up @@ -167,6 +169,7 @@ import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import Data.Typeable (Typeable)
import Data.Void (Void, absurd)
import Dhall.TypeCheck (TypeError)

import Dhall.Syntax
( Chunks (..)
Expand Down Expand Up @@ -201,13 +204,15 @@ import Lens.Family.State.Strict (zoom)

import qualified Codec.CBOR.Write as Write
import qualified Codec.Serialise
import qualified Control.Exception as Exception
import qualified Control.Monad.State.Strict as State
import qualified Control.Monad.Trans.Maybe as Maybe
import qualified Data.ByteString
import qualified Data.ByteString.Lazy
import qualified Data.CaseInsensitive
import qualified Data.Foldable
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Maybe as Maybe
import qualified Data.Text as Text
import qualified Data.Text.Encoding
import qualified Data.Text.IO
Expand Down Expand Up @@ -1157,11 +1162,29 @@ loadWith expr₀ = case expr₀ of

ImportAlt a b -> loadWith a `catch` handler₀
where
handler₀ (SourcedException (Src begin _ text₀) (MissingImports es₀)) =
loadWith b `catch` handler₁
is :: forall e . Exception e => SomeException -> Bool
is exception = Maybe.isJust (Exception.fromException @e exception)

isNotResolutionError exception =
is @(Imported (TypeError Src Void)) exception
|| is @(Imported Cycle ) exception
|| is @(Imported HashMismatch ) exception
|| is @(Imported ParseError ) exception

handler₀ exception₀@(SourcedException (Src begin _ text₀) (MissingImports es₀))
| any isNotResolutionError es₀ =
throwM exception₀
| otherwise = do
loadWith b `catch` handler₁
where
handler₁ (SourcedException (Src _ end text₁) (MissingImports es₁)) =
throwM (SourcedException (Src begin end text₂) (MissingImports (es₀ ++ es₁)))
handler₁ exception₁@(SourcedException (Src _ end text₁) (MissingImports es₁))
| any isNotResolutionError es₁ =
throwM exception₁
| otherwise =
-- Fix the source span for the error message to encompass both
-- alternatives, since both are equally to blame for the
-- failure if neither succeeds.
throwM (SourcedException (Src begin end text₂) (MissingImports (es₀ ++ es₁)))
where
text₂ = text₀ <> " ? " <> text₁

Expand Down
5 changes: 2 additions & 3 deletions dhall/tests/Dhall/Test/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ failureTest path = do
let pathString = Text.unpack path

Tasty.HUnit.testCase pathString (do
text <- Text.IO.readFile pathString

actualExpr <- Core.throws (Parser.exprFromText mempty text)
actualExpr <- do
Core.throws (Parser.exprFromText mempty (Test.Util.toDhallPath path))

succeeded <- Exception.catch @SomeException
(do _ <- Test.Util.load actualExpr
Expand Down