Skip to content

Commit

Permalink
[#24] Handles pattern match failues from fromExtension
Browse files Browse the repository at this point in the history
Problem: it turned out that the code for GHC extensions conversion that
we use from `haskell-src-meta` now does not handle the recently added
extensions, and on attempt to use the new LTS our interpolator fails
with pattern match error.

See #24 for
details.

Solution: use `spoon` to catch the pattern match failure.

Motivation: since the issued `haskell-src-meta` package version is
already on Hackage, there is not much we can do right now. Moreover,
some people wait for nyan-interpolation to work with the recent LTS.

So let's make it work at least somehow. I don't want to copy-paste the
canvas of code that `fromExtension` is, so I just use `teaspoon` to
catch the cases of extensions unhandled by `fromExtension` and ignore
them. As result, the most recent exceptions remain unsupported for now.

Separately I will try to fix `haskell-src-meta` and to make all the
extensions work the user will just need to bump their `haskell-src-meta`
dependency.
  • Loading branch information
Martoon-00 committed Apr 28, 2023
1 parent c6e9894 commit 7a574c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions full/nyan-interpolation.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ library
, haskell-src-exts
, haskell-src-meta
, nyan-interpolation-core
, spoon
, template-haskell
, text
default-language: Haskell2010
Expand Down Expand Up @@ -132,6 +133,7 @@ test-suite nyan-interpolation-tests
, haskell-src-meta
, nyan-interpolation
, nyan-interpolation-core
, spoon
, tasty
, tasty-hunit-compat
, template-haskell
Expand Down
1 change: 1 addition & 0 deletions full/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
- haskell-src-exts
- haskell-src-meta
- nyan-interpolation-core
- spoon
- template-haskell
- text

Expand Down
22 changes: 20 additions & 2 deletions full/src/Text/Interpolation/Nyan/Full.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import Language.Haskell.Meta.Syntax.Translate (toExp)
import Language.Haskell.TH (extsEnabled)

#if MIN_VERSION_haskell_src_meta(0,8,9)
import Control.Exception (Handler (..), PatternMatchFail (..))
import Control.Monad (join)
import Control.Spoon (teaspoonWithHandles)
import Data.Maybe (mapMaybe)
import Language.Haskell.Exts.Extension (Extension (..), Language (..))
import Language.Haskell.Exts.Parser (baseLanguage)
Expand All @@ -38,7 +41,11 @@ Known issues:
for the interpolated values.
With the modern version of @haskell-src-meta@, we do our best to be trasparent
and pick the extensions enabled in the module where interpolator is called
(some rare extensions may still be unsupported).
(some rare extensions may still be unsupported since they are not represented
in @haskell-src-exts@ or in @template-haskell@ packages).
* Some very modern extensions might be not allowed; if you face such issue,
try using the most recent version of @haskell-src-meta@.
-}
fullHaskellValueInterpolator :: ValueInterpolator
Expand All @@ -61,7 +68,18 @@ fullHaskellValueInterpolator = ValueInterpolator $ \txt -> do
where
providedExtensions =
#if MIN_VERSION_haskell_src_meta(0,8,9)
map EnableExtension . mapMaybe fromExtension
-- There is a period of time when fromExtension didn't handle
-- some cases in its pattern match.
-- See https://github.com/haskell-party/haskell-src-meta/issues/40
--
-- If some extension is unknown to it, we will just pretend it
-- does not exist.
let fromExtensionSafe ext =
join . teaspoonWithHandles
[Handler \(_ :: PatternMatchFail) -> pure Nothing] $
fromExtension ext

in map EnableExtension . mapMaybe fromExtensionSafe
#else
-- There is no easy way to do the conversion between template-haskell's
-- and haskell-src-exts's Extension types, so using only language-default
Expand Down
7 changes: 7 additions & 0 deletions full/tests/Test/Interpolator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ test_DefaultInterpolator = testGroup "Default interpolator"
[int|t|#d{succ . succ $ 5 + 7 `div` 2}|]
@?= "10"

, -- We want to ensure that at least basic extensions work in the interpolator
testGroup "Code with extensions works"
[ testCase "TypeApplications works" do
[int|t|#{id @Int 1}|]
@?= "1"
]

]

0 comments on commit 7a574c8

Please sign in to comment.