[commit: ghc] ghc-7.10: Don't overwrite input file by default (ed4de44)

git at git.haskell.org git at git.haskell.org
Thu Feb 12 16:14:50 UTC 2015


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-7.10
Link       : http://ghc.haskell.org/trac/ghc/changeset/ed4de443b31bc383cc7194bef75d27c3a487e8de/ghc

>---------------------------------------------------------------

commit ed4de443b31bc383cc7194bef75d27c3a487e8de
Author: Phil Ruffwind <rf at rufflewind.com>
Date:   Mon Feb 9 13:39:12 2015 -0600

    Don't overwrite input file by default
    
    Summary:
    If the default filename of the output executable coincides with that of main
    source file, throw an error instead of silently clobbering the input file.
    
    Reviewers: austin
    
    Reviewed By: austin
    
    Subscribers: thomie
    
    Differential Revision: https://phabricator.haskell.org/D642
    
    GHC Trac Issues: #9930
    
    (cherry picked from commit 78833ca6305f0875add94351592e141c032cd088)


>---------------------------------------------------------------

ed4de443b31bc383cc7194bef75d27c3a487e8de
 compiler/main/GhcMake.hs                   | 18 +++++++++++++-----
 testsuite/tests/ghc-e/should_fail/Makefile |  3 +++
 testsuite/tests/ghc-e/should_fail/T9930    |  1 +
 testsuite/tests/ghc-e/should_fail/all.T    |  3 +++
 4 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/compiler/main/GhcMake.hs b/compiler/main/GhcMake.hs
index cd670b3..5f3e315 100644
--- a/compiler/main/GhcMake.hs
+++ b/compiler/main/GhcMake.hs
@@ -427,14 +427,22 @@ guessOutputFile = modifySession $ \env ->
             ml_hs_file (ms_location ms)
         name = fmap dropExtension mainModuleSrcPath
 
+        name_exe = do
 #if defined(mingw32_HOST_OS)
-        -- we must add the .exe extention unconditionally here, otherwise
-        -- when name has an extension of its own, the .exe extension will
-        -- not be added by DriverPipeline.exeFileName.  See #2248
-        name_exe = fmap (<.> "exe") name
+          -- we must add the .exe extention unconditionally here, otherwise
+          -- when name has an extension of its own, the .exe extension will
+          -- not be added by DriverPipeline.exeFileName.  See #2248
+          name' <- fmap (<.> "exe") name
 #else
-        name_exe = name
+          name' <- name
 #endif
+          mainModuleSrcPath' <- mainModuleSrcPath
+          -- #9930: don't clobber input files (unless they ask for it)
+          if name' == mainModuleSrcPath'
+            then throwGhcException . UsageError $
+                 "default output name would overwrite the input file; " ++
+                 "must specify -o explicitly"
+            else Just name'
     in
     case outputFile dflags of
         Just _ -> env
diff --git a/testsuite/tests/ghc-e/should_fail/Makefile b/testsuite/tests/ghc-e/should_fail/Makefile
index 7a02f7b..897ed2a 100644
--- a/testsuite/tests/ghc-e/should_fail/Makefile
+++ b/testsuite/tests/ghc-e/should_fail/Makefile
@@ -13,3 +13,6 @@ T9905fail2:
 
 T9905fail3:
 	'$(TEST_HC)' $(TEST_HC_OPTS) -ignore-dot-ghci -e "import Prelude (+)" # syntax error
+
+T9930fail:
+	'$(TEST_HC)' $(TEST_HC_OPTS) -ignore-dot-ghci -x hs T9930
diff --git a/testsuite/tests/ghc-e/should_fail/T9930 b/testsuite/tests/ghc-e/should_fail/T9930
new file mode 100644
index 0000000..45846a9
--- /dev/null
+++ b/testsuite/tests/ghc-e/should_fail/T9930
@@ -0,0 +1 @@
+main = undefined
diff --git a/testsuite/tests/ghc-e/should_fail/all.T b/testsuite/tests/ghc-e/should_fail/all.T
index 07dc614..d9064d2 100644
--- a/testsuite/tests/ghc-e/should_fail/all.T
+++ b/testsuite/tests/ghc-e/should_fail/all.T
@@ -11,3 +11,6 @@ test('T9905fail2', [exit_code(2), req_interp, ignore_output], run_command,
 
 test('T9905fail3', [exit_code(2), req_interp, ignore_output], run_command,
      ['$MAKE --no-print-directory -s T9905fail3'])
+
+test('T9930fail', [exit_code(2), ignore_output], run_command,
+     ['$MAKE --no-print-directory -s T9930fail'])



More information about the ghc-commits mailing list