Handle file targets in missing home modules warning
authorHerbert Valerio Riedel <hvr@gnu.org>
Mon, 22 May 2017 15:59:56 +0000 (11:59 -0400)
committerBen Gamari <ben@smart-cactus.org>
Mon, 22 May 2017 16:41:20 +0000 (12:41 -0400)
When main module is listed on command line as a file, we should not
issue a warning about it. See Trac #13727

Reviewers: austin, bgamari, Yuras

Reviewed By: bgamari, Yuras

Subscribers: 23Skidoo, rwbarton, thomie

GHC Trac Issues: #13727

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

17 files changed:
compiler/main/GhcMake.hs
testsuite/tests/warnings/should_compile/T13727/Makefile [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727a.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727b.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727c.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727d.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727e.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727f.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727g.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727h.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727i.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727j.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/T13727k.stderr [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/all.T [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/src-exe/AltMain.hs [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/src-exe/Main.hs [new file with mode: 0644]
testsuite/tests/warnings/should_compile/T13727/src-lib/M1.hs [new file with mode: 0644]

index 60f38d4..a3b45e3 100644 (file)
@@ -164,11 +164,35 @@ warnMissingHomeModules :: GhcMonad m => HscEnv -> ModuleGraph -> m ()
 warnMissingHomeModules hsc_env mod_graph =
     when (wopt Opt_WarnMissingHomeModules dflags && not (null missing)) $
         logWarnings (listToBag [warn])
-    where
+  where
     dflags = hsc_dflags hsc_env
-    missing = filter (`notElem` targets) imports
-    imports = map (moduleName . ms_mod) mod_graph
-    targets = map (targetid_to_name . targetId) (hsc_targets hsc_env)
+    targets = map targetId (hsc_targets hsc_env)
+
+    is_known_module mod = any (is_my_target mod) targets
+
+    -- We need to be careful to handle the case where (possibly
+    -- path-qualified) filenames (aka 'TargetFile') rather than module
+    -- names are being passed on the GHC command-line.
+    --
+    -- For instance, `ghc --make src-exe/Main.hs` and
+    -- `ghc --make -isrc-exe Main` are supposed to be equivalent.
+    -- Note also that we can't always infer the associated module name
+    -- directly from the filename argument.  See Trac #13727.
+    is_my_target mod (TargetModule name)
+      = moduleName (ms_mod mod) == name
+    is_my_target mod (TargetFile target_file _)
+      | Just mod_file <- ml_hs_file (ms_location mod)
+      = target_file == mod_file ||
+           --  We can get a file target even if a module name was
+           --  originally specified in a command line because it can
+           --  be converted in guessTarget (by appending .hs/.lhs).
+           --  So let's convert it back and compare with module name
+           mkModuleName (fst $ splitExtension target_file)
+            == moduleName (ms_mod mod)
+    is_my_target _ _ = False
+
+    missing = map (moduleName . ms_mod) $
+      filter (not . is_known_module) mod_graph
 
     msg = text "Modules are not listed in command line: "
         <> sep (map ppr missing)
@@ -176,12 +200,6 @@ warnMissingHomeModules hsc_env mod_graph =
       (Reason Opt_WarnMissingHomeModules)
       (mkPlainErrMsg dflags noSrcSpan msg)
 
-    targetid_to_name (TargetModule name) = name
-    targetid_to_name (TargetFile file _) =
-      -- We can get a file even if module name in specified in command line
-      -- because it can be converted in guessTarget. So let's convert it back.
-      mkModuleName (fst $ splitExtension file)
-
 -- | Describes which modules of the module graph need to be loaded.
 data LoadHowMuch
    = LoadAllTargets
diff --git a/testsuite/tests/warnings/should_compile/T13727/Makefile b/testsuite/tests/warnings/should_compile/T13727/Makefile
new file mode 100644 (file)
index 0000000..1c39d1c
--- /dev/null
@@ -0,0 +1,3 @@
+TOP=../../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727a.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727a.stderr
new file mode 100644 (file)
index 0000000..64ad6d2
--- /dev/null
@@ -0,0 +1,6 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1
+[1 of 2] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 2] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+Linking src-exe/Main ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727b.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727b.stderr
new file mode 100644 (file)
index 0000000..64ad6d2
--- /dev/null
@@ -0,0 +1,6 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1
+[1 of 2] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 2] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+Linking src-exe/Main ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727c.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727c.stderr
new file mode 100644 (file)
index 0000000..0b9ac0e
--- /dev/null
@@ -0,0 +1,3 @@
+[1 of 2] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 2] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+Linking src-exe/Main ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727d.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727d.stderr
new file mode 100644 (file)
index 0000000..0b9ac0e
--- /dev/null
@@ -0,0 +1,3 @@
+[1 of 2] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 2] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+Linking src-exe/Main ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727e.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727e.stderr
new file mode 100644 (file)
index 0000000..0b9ac0e
--- /dev/null
@@ -0,0 +1,3 @@
+[1 of 2] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 2] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+Linking src-exe/Main ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727f.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727f.stderr
new file mode 100644 (file)
index 0000000..35bfae8
--- /dev/null
@@ -0,0 +1,7 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1 Main
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727g.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727g.stderr
new file mode 100644 (file)
index 0000000..35bfae8
--- /dev/null
@@ -0,0 +1,7 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1 Main
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727h.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727h.stderr
new file mode 100644 (file)
index 0000000..1832b38
--- /dev/null
@@ -0,0 +1,7 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727i.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727i.stderr
new file mode 100644 (file)
index 0000000..1832b38
--- /dev/null
@@ -0,0 +1,7 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: M1
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727j.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727j.stderr
new file mode 100644 (file)
index 0000000..f6d3197
--- /dev/null
@@ -0,0 +1,7 @@
+
+<no location info>: warning: [-Wmissing-home-modules]
+    Modules are not listed in command line: Main
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/T13727k.stderr b/testsuite/tests/warnings/should_compile/T13727/T13727k.stderr
new file mode 100644 (file)
index 0000000..c648d9b
--- /dev/null
@@ -0,0 +1,4 @@
+[1 of 3] Compiling M1               ( src-lib/M1.hs, src-lib/M1.o )
+[2 of 3] Compiling Main             ( src-exe/Main.hs, src-exe/Main.o )
+[3 of 3] Compiling AltMain          ( src-exe/AltMain.hs, src-exe/AltMain.o )
+Linking src-exe/AltMain ...
diff --git a/testsuite/tests/warnings/should_compile/T13727/all.T b/testsuite/tests/warnings/should_compile/T13727/all.T
new file mode 100644 (file)
index 0000000..1ca91bd
--- /dev/null
@@ -0,0 +1,20 @@
+# basic tests
+## complains about M1 only
+test('T13727a', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['Main', '-isrc-exe -isrc-lib -Wmissing-home-modules'])
+test('T13727b', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['src-exe/Main.hs', '-isrc-lib -Wmissing-home-modules'])
+## warning-free
+test('T13727c', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['Main', 'M1 -isrc-exe -isrc-lib -Wmissing-home-modules'])
+test('T13727d', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['src-exe/Main.hs', 'M1 -isrc-lib -Wmissing-home-modules'])
+test('T13727e', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['src-exe/Main.hs', 'src-lib/M1.hs -Wmissing-home-modules'])
+
+# tests involving -main-is
+## complains about M1 & Main
+test('T13727f', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['AltMain', '-main-is AltMain.alt_main -isrc-exe -isrc-lib -Wmissing-home-modules'])
+test('T13727g', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['src-exe/AltMain.hs', '-main-is AltMain.alt_main -isrc-exe -isrc-lib -Wmissing-home-modules'])
+## complains about M1 only
+test('T13727h', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['src-exe/AltMain.hs', '-main-is AltMain.alt_main ./src-exe/Main.hs -isrc-exe -isrc-lib -Wmissing-home-modules'])
+test('T13727i', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['AltMain', '-main-is AltMain.alt_main Main -isrc-exe -isrc-lib -Wmissing-home-modules'])
+## complains about Main only
+test('T13727j', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['AltMain', '-main-is AltMain.alt_main M1 -isrc-exe -isrc-lib -Wmissing-home-modules'])
+## warning-free
+test('T13727k', [extra_files(['src-exe/','src-lib/'])], multimod_compile, ['AltMain', '-main-is AltMain.alt_main M1 Main -isrc-exe -isrc-lib -Wmissing-home-modules'])
diff --git a/testsuite/tests/warnings/should_compile/T13727/src-exe/AltMain.hs b/testsuite/tests/warnings/should_compile/T13727/src-exe/AltMain.hs
new file mode 100644 (file)
index 0000000..850ae96
--- /dev/null
@@ -0,0 +1,6 @@
+module AltMain(alt_main) where
+
+import Main
+
+alt_main :: IO ()
+alt_main = Main.main
diff --git a/testsuite/tests/warnings/should_compile/T13727/src-exe/Main.hs b/testsuite/tests/warnings/should_compile/T13727/src-exe/Main.hs
new file mode 100644 (file)
index 0000000..0394c57
--- /dev/null
@@ -0,0 +1,6 @@
+module Main(main) where
+
+import M1 ()
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/warnings/should_compile/T13727/src-lib/M1.hs b/testsuite/tests/warnings/should_compile/T13727/src-lib/M1.hs
new file mode 100644 (file)
index 0000000..5d99c37
--- /dev/null
@@ -0,0 +1 @@
+module M1 where