Prevent -optc arguments from being duplicated in reverse order (#17471)
authorRyan Scott <ryan.gl.scott@gmail.com>
Wed, 13 Nov 2019 15:35:11 +0000 (10:35 -0500)
committerMarge Bot <ben+marge-bot@smart-cactus.org>
Sat, 23 Nov 2019 23:56:00 +0000 (18:56 -0500)
This reverts a part of commit
7bc5d6c6578ab9d60a83b81c7cc14819afef32ba that causes all arguments
to `-optc` (and `-optcxx`) to be passed twice to the C/C++ compiler,
once in reverse order and then again in the correct order. While
passing duplicate arguments is usually harmless it can cause breakage
in this pattern, which is employed by Hackage libraries in the wild:

```
ghc Foo.hs foo.c -optc-D -optcFOO
```

As `FOO -D -D FOO` will cause compilers to error.

Fixes #17471.

compiler/main/SysTools/Tasks.hs
testsuite/tests/ffi/should_run/T17471.hs [new file with mode: 0644]
testsuite/tests/ffi/should_run/T17471.stdout [new file with mode: 0644]
testsuite/tests/ffi/should_run/T17471_c.c [new file with mode: 0644]
testsuite/tests/ffi/should_run/all.T

index 5b0cb1c..96a5b29 100644 (file)
@@ -127,10 +127,9 @@ runCc mLanguage dflags args = traceToolCommand dflags "cc" $ do
     Nothing -> ([], userOpts_c)
     Just language -> ([Option "-x", Option languageName], opts)
       where
-        s = settings dflags
         (languageName, opts) = case language of
-          LangC      -> ("c",             sOpt_c s ++ userOpts_c)
-          LangCxx    -> ("c++",           sOpt_cxx s ++ userOpts_cxx)
+          LangC      -> ("c",             userOpts_c)
+          LangCxx    -> ("c++",           userOpts_cxx)
           LangObjc   -> ("objective-c",   userOpts_c)
           LangObjcxx -> ("objective-c++", userOpts_cxx)
           LangAsm    -> ("assembler",     [])
diff --git a/testsuite/tests/ffi/should_run/T17471.hs b/testsuite/tests/ffi/should_run/T17471.hs
new file mode 100644 (file)
index 0000000..bcaebc5
--- /dev/null
@@ -0,0 +1,9 @@
+{-# LANGUAGE ForeignFunctionInterface #-}
+module Main where
+
+import Foreign.C.Types
+
+foreign import ccall "foo" foo :: IO CInt
+
+main :: IO ()
+main = foo >>= print
diff --git a/testsuite/tests/ffi/should_run/T17471.stdout b/testsuite/tests/ffi/should_run/T17471.stdout
new file mode 100644 (file)
index 0000000..d00491f
--- /dev/null
@@ -0,0 +1 @@
+1
diff --git a/testsuite/tests/ffi/should_run/T17471_c.c b/testsuite/tests/ffi/should_run/T17471_c.c
new file mode 100644 (file)
index 0000000..64a9445
--- /dev/null
@@ -0,0 +1,7 @@
+int foo() {
+#if defined(FOO)
+  return 1;
+#else
+  return 0;
+#endif
+}
index d379191..a0984a2 100644 (file)
@@ -213,3 +213,6 @@ test('PrimFFIWord16', [omit_ways(['ghci'])], compile_and_run, ['PrimFFIWord16_c.
 test('T493', [omit_ways(['ghci'])], compile_and_run, ['T493_c.c'])
 
 test('UnliftedNewtypesByteArrayOffset', [omit_ways(['ghci'])], compile_and_run, ['UnliftedNewtypesByteArrayOffset_c.c'])
+
+test('T17471', [omit_ways(['ghci'])], compile_and_run,
+     ['T17471_c.c -optc-D -optcFOO'])