Fix #16219: TemplateHaskell causes indefinite package build error
authorEdward Z. Yang <ezyang@fb.com>
Fri, 1 Feb 2019 01:00:50 +0000 (20:00 -0500)
committerBen Gamari <ben@smart-cactus.org>
Fri, 1 Feb 2019 01:02:12 +0000 (20:02 -0500)
It should work to write an indefinite package using TemplateHaskell,
so long as all of the actual TH code lives outside of the package.
However, cleverness we had to build TH code even when building
with -fno-code meant that we attempted to build object code for
modules in an indefinite package, even when the signatures were
not instantiated.  This patch disables said logic in the event
that an indefinite package is being typechecked.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Test Plan: validate

Reviewers: simonpj, bgamari

Reviewed By: bgamari

Subscribers: rwbarton, carter

GHC Trac Issues: #16219

Differential Revision: https://phabricator.haskell.org/D5475

compiler/main/GhcMake.hs
compiler/main/Packages.hs
testsuite/tests/backpack/cabal/T16219/LICENSE [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/Makefile [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/Setup.hs [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/all.T [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/backpack-issue.cabal [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/library-a-impl/A.hs [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/library-a/A/Sig.hsig [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/library-a/B.hs [new file with mode: 0644]
testsuite/tests/backpack/cabal/T16219/library-b/C.hs [new file with mode: 0644]

index 85925b3..a6fe5c7 100644 (file)
@@ -2045,6 +2045,9 @@ enableCodeGenForTH target nodemap =
         , ms_hspp_opts = dflags@DynFlags
           {hscTarget = HscNothing}
         } <- ms
+      -- Don't enable codegen for TH on indefinite packages; we
+      -- can't compile anything anyway! See #16219.
+      , not (isIndefinite dflags)
       , ms_mod `Set.member` needs_codegen_set
       = do
         let new_temp_file suf dynsuf = do
index 70c9c26..44258de 100644 (file)
@@ -58,6 +58,7 @@ module Packages (
         pprPackages,
         pprPackagesSimple,
         pprModuleMap,
+        isIndefinite,
         isDllName
     )
 where
diff --git a/testsuite/tests/backpack/cabal/T16219/LICENSE b/testsuite/tests/backpack/cabal/T16219/LICENSE
new file mode 100644 (file)
index 0000000..9488e98
--- /dev/null
@@ -0,0 +1,30 @@
+Copyright (c) 2019, Isaac Elliott
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Isaac Elliott nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/testsuite/tests/backpack/cabal/T16219/Makefile b/testsuite/tests/backpack/cabal/T16219/Makefile
new file mode 100644 (file)
index 0000000..e98af4b
--- /dev/null
@@ -0,0 +1,18 @@
+TOP=/../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+SETUP='$(PWD)/Setup' -v0
+CONFIGURE=$(SETUP) configure $(CABAL_MINIMAL_BUILD) --with-ghc='$(TEST_HC)' --ghc-options='$(TEST_HC_OPTS)' --package-db='$(PWD)/tmp.d' --prefix='$(PWD)/inst'
+
+T16219: clean
+       '$(GHC_PKG)' init tmp.d
+       '$(TEST_HC)' $(TEST_HC_OPTS) -v0 --make Setup
+       $(CONFIGURE)
+       $(SETUP) build
+ifneq "$(CLEANUP)" ""
+       $(MAKE) -s --no-print-directory clean
+endif
+
+clean :
+       $(RM) -rf tmp.d inst dist Setup$(exeext)
diff --git a/testsuite/tests/backpack/cabal/T16219/Setup.hs b/testsuite/tests/backpack/cabal/T16219/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/backpack/cabal/T16219/all.T b/testsuite/tests/backpack/cabal/T16219/all.T
new file mode 100644 (file)
index 0000000..29dd8a4
--- /dev/null
@@ -0,0 +1,9 @@
+if config.cleanup:
+   cleanup = 'CLEANUP=1'
+else:
+   cleanup = 'CLEANUP=0'
+
+test('T16219',
+     extra_files(['Setup.hs', 'backpack-issue.cabal', 'library-a', 'library-a-impl', 'library-b']),
+     run_command,
+     ['$MAKE -s --no-print-directory T16219 ' + cleanup])
diff --git a/testsuite/tests/backpack/cabal/T16219/backpack-issue.cabal b/testsuite/tests/backpack/cabal/T16219/backpack-issue.cabal
new file mode 100644 (file)
index 0000000..4c3f673
--- /dev/null
@@ -0,0 +1,47 @@
+-- Initial backpack-issue.cabal generated by cabal init.  For further
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name:                backpack-issue
+version:             0.1.0.0
+-- synopsis:
+-- description:
+license:             BSD3
+license-file:        LICENSE
+author:              Isaac Elliott
+maintainer:          isaace71295@gmail.com
+-- copyright:
+-- category:
+build-type:          Simple
+extra-source-files:  CHANGELOG.md
+cabal-version:       >=2
+
+library library-a
+  signatures:          A.Sig
+  exposed-modules:     B
+  build-depends:       base >=4.12 && <4.13
+  hs-source-dirs:      library-a
+  default-language:    Haskell2010
+
+library library-a-impl
+  exposed-modules:     A
+  build-depends:       base >=4.12 && <4.13
+  hs-source-dirs:      library-a-impl
+  default-language:    Haskell2010
+
+library library-b
+  exposed-modules:     C
+  build-depends:       base >=4.12 && <4.13
+                     , library-a
+  hs-source-dirs:      library-b
+  default-language:    Haskell2010
+  extensions:          TemplateHaskell
+
+library
+  mixins:              library-a requires (A.Sig as A)
+                     , library-b requires (A.Sig as A)
+  reexported-modules:  A, B, C
+  build-depends:       base >=4.12 && <4.13
+                     , library-a
+                     , library-a-impl
+                     , library-b
+  default-language:    Haskell2010
diff --git a/testsuite/tests/backpack/cabal/T16219/library-a-impl/A.hs b/testsuite/tests/backpack/cabal/T16219/library-a-impl/A.hs
new file mode 100644 (file)
index 0000000..6598c36
--- /dev/null
@@ -0,0 +1,6 @@
+module A where
+
+newtype A = A { unA :: Int }
+
+showA :: A -> String
+showA = show . unA
diff --git a/testsuite/tests/backpack/cabal/T16219/library-a/A/Sig.hsig b/testsuite/tests/backpack/cabal/T16219/library-a/A/Sig.hsig
new file mode 100644 (file)
index 0000000..ce90fea
--- /dev/null
@@ -0,0 +1,5 @@
+signature A.Sig where
+
+data A
+
+showA :: A -> String
diff --git a/testsuite/tests/backpack/cabal/T16219/library-a/B.hs b/testsuite/tests/backpack/cabal/T16219/library-a/B.hs
new file mode 100644 (file)
index 0000000..ad89656
--- /dev/null
@@ -0,0 +1,6 @@
+module B where
+
+import A.Sig
+
+exclaimA :: A -> String
+exclaimA = (++ "!") . showA
diff --git a/testsuite/tests/backpack/cabal/T16219/library-b/C.hs b/testsuite/tests/backpack/cabal/T16219/library-b/C.hs
new file mode 100644 (file)
index 0000000..4d4f7e8
--- /dev/null
@@ -0,0 +1,10 @@
+{-# language TemplateHaskell #-}
+module C where
+
+import Data.Char (toUpper)
+
+import A.Sig
+import B
+
+veryExclaimA :: A -> String
+veryExclaimA = fmap toUpper . exclaimA