Skip to content

Commit 7a574c8

Browse files
committed
[#24] Handles pattern match failues from fromExtension
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.
1 parent c6e9894 commit 7a574c8

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

full/nyan-interpolation.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ library
7474
, haskell-src-exts
7575
, haskell-src-meta
7676
, nyan-interpolation-core
77+
, spoon
7778
, template-haskell
7879
, text
7980
default-language: Haskell2010
@@ -132,6 +133,7 @@ test-suite nyan-interpolation-tests
132133
, haskell-src-meta
133134
, nyan-interpolation
134135
, nyan-interpolation-core
136+
, spoon
135137
, tasty
136138
, tasty-hunit-compat
137139
, template-haskell

full/package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies:
2424
- haskell-src-exts
2525
- haskell-src-meta
2626
- nyan-interpolation-core
27+
- spoon
2728
- template-haskell
2829
- text
2930

full/src/Text/Interpolation/Nyan/Full.hs

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import Language.Haskell.Meta.Syntax.Translate (toExp)
1919
import Language.Haskell.TH (extsEnabled)
2020

2121
#if MIN_VERSION_haskell_src_meta(0,8,9)
22+
import Control.Exception (Handler (..), PatternMatchFail (..))
23+
import Control.Monad (join)
24+
import Control.Spoon (teaspoonWithHandles)
2225
import Data.Maybe (mapMaybe)
2326
import Language.Haskell.Exts.Extension (Extension (..), Language (..))
2427
import Language.Haskell.Exts.Parser (baseLanguage)
@@ -38,7 +41,11 @@ Known issues:
3841
for the interpolated values.
3942
With the modern version of @haskell-src-meta@, we do our best to be trasparent
4043
and pick the extensions enabled in the module where interpolator is called
41-
(some rare extensions may still be unsupported).
44+
(some rare extensions may still be unsupported since they are not represented
45+
in @haskell-src-exts@ or in @template-haskell@ packages).
46+
47+
* Some very modern extensions might be not allowed; if you face such issue,
48+
try using the most recent version of @haskell-src-meta@.
4249
4350
-}
4451
fullHaskellValueInterpolator :: ValueInterpolator
@@ -61,7 +68,18 @@ fullHaskellValueInterpolator = ValueInterpolator $ \txt -> do
6168
where
6269
providedExtensions =
6370
#if MIN_VERSION_haskell_src_meta(0,8,9)
64-
map EnableExtension . mapMaybe fromExtension
71+
-- There is a period of time when fromExtension didn't handle
72+
-- some cases in its pattern match.
73+
-- See https://github.com/haskell-party/haskell-src-meta/issues/40
74+
--
75+
-- If some extension is unknown to it, we will just pretend it
76+
-- does not exist.
77+
let fromExtensionSafe ext =
78+
join . teaspoonWithHandles
79+
[Handler \(_ :: PatternMatchFail) -> pure Nothing] $
80+
fromExtension ext
81+
82+
in map EnableExtension . mapMaybe fromExtensionSafe
6583
#else
6684
-- There is no easy way to do the conversion between template-haskell's
6785
-- and haskell-src-exts's Extension types, so using only language-default

full/tests/Test/Interpolator.hs

+7
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ test_DefaultInterpolator = testGroup "Default interpolator"
3131
[int|t|#d{succ . succ $ 5 + 7 `div` 2}|]
3232
@?= "10"
3333

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

0 commit comments

Comments
 (0)