[commit: ghc] master: Better warning message for orphan instances (Ticket #9178) (877a957)

git at git.haskell.org git at git.haskell.org
Mon Jun 9 13:15:24 UTC 2014


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/877a9577280f4b6aa6ef47f7a27d9b741b8234cf/ghc

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

commit 877a9577280f4b6aa6ef47f7a27d9b741b8234cf
Author: Dr. Heinrich Hördegen <hoerdegen at energiefluss.info>
Date:   Fri Jun 6 19:11:27 2014 +0200

    Better warning message for orphan instances (Ticket #9178)
    
    Including a test case.


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

877a9577280f4b6aa6ef47f7a27d9b741b8234cf
 compiler/iface/MkIface.lhs                                       | 7 +++++++
 .../tests/{annotations => warnings}/should_compile/Makefile      | 0
 testsuite/tests/warnings/should_compile/T9178.hs                 | 9 +++++++++
 testsuite/tests/warnings/should_compile/T9178.stderr             | 8 ++++++++
 testsuite/tests/warnings/should_compile/T9178DataType.hs         | 5 +++++
 testsuite/tests/warnings/should_compile/all.T                    | 3 +++
 6 files changed, 32 insertions(+)

diff --git a/compiler/iface/MkIface.lhs b/compiler/iface/MkIface.lhs
index f2d2058..760f349 100644
--- a/compiler/iface/MkIface.lhs
+++ b/compiler/iface/MkIface.lhs
@@ -879,6 +879,13 @@ instOrphWarn :: DynFlags -> PrintUnqualified -> ClsInst -> WarnMsg
 instOrphWarn dflags unqual inst
   = mkWarnMsg dflags (getSrcSpan inst) unqual $
     hang (ptext (sLit "Orphan instance:")) 2 (pprInstanceHdr inst)
+    $$ text "To avoid this"
+    $$ nest 4 (vcat posibilities)
+  where
+    posibilities =
+      text "move the instance declaration to the module of the class or of the type, or" :
+      text "wrap the type with a newtype and declare the instance on the new type." :
+      []
 
 ruleOrphWarn :: DynFlags -> PrintUnqualified -> Module -> IfaceRule -> WarnMsg
 ruleOrphWarn dflags unqual mod rule
diff --git a/testsuite/tests/annotations/should_compile/Makefile b/testsuite/tests/warnings/should_compile/Makefile
similarity index 100%
copy from testsuite/tests/annotations/should_compile/Makefile
copy to testsuite/tests/warnings/should_compile/Makefile
diff --git a/testsuite/tests/warnings/should_compile/T9178.hs b/testsuite/tests/warnings/should_compile/T9178.hs
new file mode 100644
index 0000000..9171381
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T9178.hs
@@ -0,0 +1,9 @@
+
+
+module T9178 where
+
+import T9178DataType
+
+
+instance Show T9178_Type where
+         show _ = undefined
\ No newline at end of file
diff --git a/testsuite/tests/warnings/should_compile/T9178.stderr b/testsuite/tests/warnings/should_compile/T9178.stderr
new file mode 100644
index 0000000..6f4b6c0
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T9178.stderr
@@ -0,0 +1,8 @@
+[1 of 2] Compiling T9178DataType    ( T9178DataType.hs, T9178DataType.o )
+[2 of 2] Compiling T9178            ( T9178.hs, T9178.o )
+
+T9178.hs:8:10: Warning:
+    Orphan instance: instance Show T9178_Type
+    To avoid this
+        move the instance declaration to the module of the class or of the type, or
+        wrap the type with a newtype and declare the instance on the new type.
diff --git a/testsuite/tests/warnings/should_compile/T9178DataType.hs b/testsuite/tests/warnings/should_compile/T9178DataType.hs
new file mode 100644
index 0000000..e274117
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T9178DataType.hs
@@ -0,0 +1,5 @@
+
+
+module T9178DataType where
+
+data T9178_Type
diff --git a/testsuite/tests/warnings/should_compile/all.T b/testsuite/tests/warnings/should_compile/all.T
new file mode 100644
index 0000000..f6747bf
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/all.T
@@ -0,0 +1,3 @@
+test('T9178', extra_clean(['T9178.o', 'T9178DataType.o',
+			   'T9178.hi', 'T9178DataType.hi']),
+	      multimod_compile, ['T9178', '-Wall'])
\ No newline at end of file



More information about the ghc-commits mailing list