Fix #9438 by converting a panic to an error message
authorRyan Scott <ryan.gl.scott@gmail.com>
Fri, 13 Apr 2018 15:33:54 +0000 (11:33 -0400)
committerBen Gamari <ben@smart-cactus.org>
Fri, 13 Apr 2018 15:33:59 +0000 (11:33 -0400)
Previously, GHC was quite eager to panic whenever it was fed
an archive file when `DYNAMIC_GHC_PROGRAMS=YES`. This ought to be an
explicit error message instead, so this patch accomplishes just that.

Test Plan: make test TEST=T14708

Reviewers: Phyx, hvr, bgamari

Reviewed By: Phyx

Subscribers: thomie, carter

GHC Trac Issues: #9438, #14708, #15032

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

compiler/ghci/Linker.hs
testsuite/.gitignore
testsuite/tests/ghci/linking/Makefile
testsuite/tests/ghci/linking/T14708.hs [new file with mode: 0644]
testsuite/tests/ghci/linking/T14708.stderr [new file with mode: 0644]
testsuite/tests/ghci/linking/add.c [new file with mode: 0644]
testsuite/tests/ghci/linking/all.T

index a91df32..8d0338a 100644 (file)
@@ -71,7 +71,10 @@ import System.Win32.Info (getSystemDirectory)
 
 import Exception
 
-import Foreign (Ptr) -- needed for 2nd stage
+-- needed for 2nd stage
+#if STAGE >= 2
+import Foreign (Ptr)
+#endif
 
 {- **********************************************************************
 
@@ -504,9 +507,17 @@ preloadLib hsc_env lib_paths framework_paths pls lib_spec = do
        = do b <- doesFileExist name
             if not b then return False
                      else do if dynamicGhc
-                                 then panic "Loading archives not supported"
+                                 then throwGhcExceptionIO $
+                                      CmdLineError dynamic_msg
                                  else loadArchive hsc_env name
                              return True
+      where
+        dynamic_msg = unlines
+          [ "User-specified static library could not be loaded ("
+            ++ name ++ ")"
+          , "Loading static libraries is not supported in this configuration."
+          , "Try using a dynamic library instead."
+          ]
 
 
 {- **********************************************************************
index e6934f9..7c4453e 100644 (file)
@@ -776,6 +776,7 @@ mk/ghcconfig*_test___spaces_ghc*.exe.mk
 /tests/ghci/linking/dir004/
 /tests/ghci/linking/dir005/
 /tests/ghci/linking/dir006/
+/tests/ghci/linking/T14708scratch/
 /tests/ghci/prog001/C.hs
 /tests/ghci/prog001/D.hs
 /tests/ghci/prog002/A.hs
index 793152d..793998e 100644 (file)
@@ -40,11 +40,11 @@ ghcilink002 :
 ghcilink003 :
        echo ":q" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -lstdc++
 
-# Test 4: 
+# Test 4:
 #   package P
 #      library-dirs: `pwd`/dir004
 #      extra-libraries: foo
-#   with 
+#   with
 #    dir004/libfoo.a
 
 LOCAL_PKGCONF004=dir004/local.package.conf
@@ -68,11 +68,11 @@ ghcilink004 :
        "$(AR)" cqs dir004/libfoo.a dir004/foo.o
        echo "test" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -package-db $(LOCAL_PKGCONF004) -package test TestLink.hs
 
-# Test 5: 
+# Test 5:
 #   package P
 #      library-dirs: `pwd`/dir005
 #      extra-libraries: foo
-#   with 
+#   with
 #    dir005/libfoo.so
 
 LOCAL_PKGCONF005=dir005/ghcilink005.package.conf
@@ -96,7 +96,7 @@ ghcilink005 :
        "$(TEST_HC)" -no-auto-link-packages -shared -o dir005/$(call DLL,foo) dir005/foo.o
        echo "test" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -package-db $(LOCAL_PKGCONF005) -package test TestLink.hs
 
-# Test 6: 
+# Test 6:
 #   package P
 #      extra-libraries: stdc++
 
@@ -126,3 +126,11 @@ endif
 T3333:
        "$(TEST_HC)" -c T3333.c -o T3333.o
        echo "weak_test 10" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) T3333.hs T3333.o
+
+.PHONY: T14708
+T14708:
+       $(RM) -rf T14708scratch
+       mkdir T14708scratch
+       "$(TEST_HC)" -c add.c -o T14708scratch/add.o
+       "$(AR)" cqs T14708scratch/libadd.a T14708scratch/add.o
+       -"$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -LT14708scratch -ladd T14708.hs
diff --git a/testsuite/tests/ghci/linking/T14708.hs b/testsuite/tests/ghci/linking/T14708.hs
new file mode 100644 (file)
index 0000000..377b6b5
--- /dev/null
@@ -0,0 +1,2 @@
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/ghci/linking/T14708.stderr b/testsuite/tests/ghci/linking/T14708.stderr
new file mode 100644 (file)
index 0000000..fabbdb4
--- /dev/null
@@ -0,0 +1,4 @@
+<command line>: User-specified static library could not be loaded (T14708scratch/libadd.a)
+Loading static libraries is not supported in this configuration.
+Try using a dynamic library instead.
+
diff --git a/testsuite/tests/ghci/linking/add.c b/testsuite/tests/ghci/linking/add.c
new file mode 100644 (file)
index 0000000..538880c
--- /dev/null
@@ -0,0 +1 @@
+int add2(int x, int y) { return x + y; }
index 124b3a4..f9617c5 100644 (file)
@@ -31,3 +31,11 @@ test('T3333',
       unless(opsys('linux') or opsys('darwin') or ghc_dynamic(),
              expect_broken(3333))],
      run_command, ['$MAKE -s --no-print-directory T3333'])
+
+test('T14708',
+     [extra_files(['T14708.hs', 'add.c']),
+      unless(doing_ghci, skip),
+      unless(ghc_dynamic(), skip),
+      extra_clean(['T14708scratch/*', 'T14708'])],
+     run_command,
+     ['$MAKE -s --no-print-directory T14708'])