Skip to content

Commit beaba07

Browse files
authored
Merge pull request #25 from avnik/fix-order-of-promoted-type-tuples
Fix order of promoted type tuples in resulting TH
2 parents fb660a1 + 363f15a commit beaba07

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

haskell-src-meta/src/Language/Haskell/Meta/Syntax/Translate.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ instance ToType (Exts.Type l) where
424424
Exts.PromotedString _ _ s -> TH.LitT $ TH.StrTyLit s
425425
Exts.PromotedCon _ _q n -> TH.PromotedT $ toName n
426426
Exts.PromotedList _ _q ts -> foldr (\t pl -> TH.PromotedConsT `TH.AppT` toType t `TH.AppT` pl) TH.PromotedNilT ts
427-
Exts.PromotedTuple _ ts -> foldr (\t pt -> pt `TH.AppT` toType t) (TH.PromotedTupleT $ length ts) ts
427+
Exts.PromotedTuple _ ts -> foldl (\pt t -> pt `TH.AppT` toType t) (TH.PromotedTupleT $ length ts) ts
428428
Exts.PromotedUnit _ -> TH.PromotedT ''()
429429
toType (Exts.TyEquals _ t1 t2) = TH.EqualityT `TH.AppT` toType t1 `TH.AppT` toType t2
430430
toType t@Exts.TySplice{} = noTH "toType" t
@@ -601,10 +601,10 @@ instance ToDec (Exts.Decl l) where
601601

602602
toDec (Exts.AnnPragma _ ann) = TH.PragmaD (TH.AnnP (target ann) (expann ann))
603603
where
604-
target (Exts.Ann _ n _) = TH.ValueAnnotation (toName n)
604+
target (Exts.Ann _ n _) = TH.ValueAnnotation (toName n)
605605
target (Exts.TypeAnn _ n _) = TH.TypeAnnotation (toName n)
606606
target (Exts.ModuleAnn _ _) = TH.ModuleAnnotation
607-
expann (Exts.Ann _ _ e) = toExp e
607+
expann (Exts.Ann _ _ e) = toExp e
608608
expann (Exts.TypeAnn _ _ e) = toExp e
609609
expann (Exts.ModuleAnn _ e) = toExp e
610610

haskell-src-meta/tests/Main.hs

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
14
module Main where
25

3-
import qualified Control.Monad.Fail as Fail
4-
import qualified Language.Haskell.Exts as Exts
5-
import qualified Language.Haskell.Exts.Extension as Extension
6-
import qualified Language.Haskell.Exts.Parser as Parser
6+
import qualified Control.Monad.Fail as Fail
7+
import Data.Data (Data, cast, gfoldl)
8+
import Data.Functor.Const
9+
(Const (Const, getConst))
10+
import qualified Language.Haskell.Exts as Exts
11+
import qualified Language.Haskell.Exts.Extension as Extension
12+
import qualified Language.Haskell.Exts.Parser as Parser
713
import Language.Haskell.Meta.Parse
8-
import qualified Language.Haskell.TH as TH
9-
import Test.HUnit (Assertion, (@?=))
14+
import Language.Haskell.Meta.Syntax.Translate
15+
import qualified Language.Haskell.TH as TH
16+
import Test.HUnit (Assertion, (@?=))
1017
import Test.Tasty
1118
(TestTree, defaultMain, testGroup)
12-
import Test.Tasty.HUnit (testCase)
19+
import Test.Tasty.HUnit (testCase)
1320

1421
type Test = TestTree
1522

@@ -19,12 +26,46 @@ main = defaultMain (testGroup "unit" tests)
1926
tests :: [Test]
2027
tests = [ derivingClausesTest
2128
, typeAppTest
29+
, orderInTypeTuples
2230
]
2331

2432
derivingClausesTest :: Test
2533
derivingClausesTest = testCase "Deriving clauses preserved" $
2634
roundTripDecls "data Foo = Foo deriving (A, B, C)"
2735

36+
orderInTypeTuples :: Test
37+
orderInTypeTuples =
38+
testCase "Ensure that type tuples reconstructed in proper order" $ do
39+
expected @?= actual
40+
where
41+
expected :: [TH.TyLit]
42+
expected = collectAll (toExp parsed)
43+
actual = [TH.StrTyLit "a", TH.StrTyLit "b"]
44+
45+
parsed :: Exts.Exp Exts.SrcSpanInfo
46+
parsed = case Exts.parseExpWithMode mode "foo @'(\"a\", \"b\")" of
47+
Exts.ParseOk v -> v
48+
e -> error $ show e
49+
mode :: Exts.ParseMode
50+
mode = Exts.defaultParseMode {
51+
Exts.extensions = [
52+
Exts.EnableExtension Exts.TypeApplications
53+
, Exts.EnableExtension Exts.DataKinds
54+
]
55+
}
56+
57+
collectAll :: (Data a, Data b) => a -> [b]
58+
collectAll = ($ []) . go
59+
where
60+
go :: forall a b. (Data a, Data b) => a -> [b] -> [b]
61+
go = \x ->
62+
case cast x of
63+
Just x' -> (x' :)
64+
Nothing -> getConst $ gfoldl ap (const $ Const id) x
65+
where
66+
ap :: Data x => Const ([b] -> [b]) (x -> y) -> x -> Const ([b] -> [b]) y
67+
ap (Const acc) x = Const $ acc . go x
68+
2869
typeAppMode :: Exts.ParseMode
2970
typeAppMode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications] }
3071

scripts/format/make-stylish.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ find . \
55
-name "*.hs" \
66
-and -not -path "*/.stack-work/*" \
77
-and -not -path "*/dist/*" \
8-
-and -not -path "*/dist-newstyle/*" \
8+
-and -not -path "*/*dist-newstyle*/*" \
99
-exec stylish-haskell -i {} \;

0 commit comments

Comments
 (0)