Skip to content

Commit 9997474

Browse files
committed
Fix order of promoted type tuples in resulting TH
Also add a test illustrating a problem.
1 parent 9b9f31f commit 9997474

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ instance ToType (Exts.Type l) where
437437
Exts.PromotedString _ _ s -> TH.LitT $ TH.StrTyLit s
438438
Exts.PromotedCon _ _q n -> TH.PromotedT $ toName n
439439
Exts.PromotedList _ _q ts -> foldr (\t pl -> TH.PromotedConsT `TH.AppT` toType t `TH.AppT` pl) TH.PromotedNilT ts
440-
Exts.PromotedTuple _ ts -> foldr (\t pt -> pt `TH.AppT` toType t) (TH.PromotedTupleT $ length ts) ts
440+
Exts.PromotedTuple _ ts -> foldl (\pt t -> pt `TH.AppT` toType t) (TH.PromotedTupleT $ length ts) ts
441441
Exts.PromotedUnit _ -> TH.PromotedT ''()
442442
toType (Exts.TyEquals _ t1 t2) = TH.EqualityT `TH.AppT` toType t1 `TH.AppT` toType t2
443443
toType t@Exts.TySplice{} = noTH "toType" t

haskell-src-meta/tests/Main.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qualified Language.Haskell.Exts as Exts
77
import qualified Language.Haskell.Exts.Extension as Extension
88
import qualified Language.Haskell.Exts.Parser as Parser
99
import Language.Haskell.Meta.Parse
10+
import Language.Haskell.Meta.Syntax.Translate
1011
import qualified Language.Haskell.TH as TH
1112
-- import Test.Framework
1213
-- import Test.Framework.Providers.HUnit
@@ -24,12 +25,22 @@ tests = [ derivingClausesTest
2425
#if MIN_VERSION_template_haskell(2,12,0)
2526
, typeAppTest
2627
#endif
28+
, orderInTypeTuples
2729
]
2830

2931
derivingClausesTest :: Test
3032
derivingClausesTest = testCase "Deriving clauses preserved" $
3133
roundTripDecls "data Foo = Foo deriving (A, B, C)"
3234

35+
-- Very ugly test, illustrating incorrect order of promoted type tuples reconstructed into TH
36+
orderInTypeTuples :: Test
37+
orderInTypeTuples = testCase "Ensure that type tuples reconstructed in proper order" $ do
38+
let Parser.ParseOk parsed = show . toExp <$> Parser.parseExpWithMode mode "getField @'(\"a\", \"b\", \"c\") localVar"
39+
parsed @?= expected
40+
where
41+
mode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications, Extension.EnableExtension Extension.DataKinds ] }
42+
expected = "(AppE (AppTypeE (VarE getField) (AppT (AppT (AppT (PromotedTupleT 3) (LitT (StrTyLit \"a\"))) (LitT (StrTyLit \"b\"))) (LitT (StrTyLit \"c\")))) (VarE localVar))"
43+
3344
typeAppMode :: Exts.ParseMode
3445
typeAppMode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications] }
3546

0 commit comments

Comments
 (0)