summaryrefslogtreecommitdiff
path: root/testsuite/tests/warnings
diff options
context:
space:
mode:
authorHerbert Valerio Riedel <hvr@gnu.org>2016-01-24 14:46:44 +0100
committerHerbert Valerio Riedel <hvr@gnu.org>2016-01-24 21:46:13 +0100
commitfd6dd41c67f3bd23bbf074357219cfd251eb53d6 (patch)
treeb74609f5d6752ff10c5aae242949248687b45508 /testsuite/tests/warnings
parent2c6fe5b8a854f06ea9574f7dca545b4c2d35b811 (diff)
downloadhaskell-fd6dd41c67f3bd23bbf074357219cfd251eb53d6.tar.gz
Implement `-Wnoncanonical-monadfail-instances` warning
The MonadFail proposal implemented so far via #10751 only warns about missing `MonadFail` instances based on existence of failible pattern matches in `do`-blocks. However, based on the noncanonical Monad warnings implemented via #11150 we can provide a different mechanism for detecting missing `MonadFail` instances quite cheaply. That is, by checking for canonical `fail` definitions. In the case of `Monad`/`MonadFail`, we define the canonical implementation of `fail` to be such that the soft-deprecated method shall (iff overridden) be defined in terms of the non-deprecated method. Consequently, in case of `MonadFail`, the `Monad(fail)` method shall be defined as alias of the `MonadFail(fail)` method. This allows us at some distant point in the future to remove `fail` from the `Monad` class, while having GHC ignore/tolerate such literal canonical method definitions. Reviewed By: bgamari, RyanGlScott Differential Revision: https://phabricator.haskell.org/D1838
Diffstat (limited to 'testsuite/tests/warnings')
-rw-r--r--testsuite/tests/warnings/should_compile/T11128b.hs64
-rw-r--r--testsuite/tests/warnings/should_compile/T11128b.stderr10
-rw-r--r--testsuite/tests/warnings/should_compile/all.T1
3 files changed, 75 insertions, 0 deletions
diff --git a/testsuite/tests/warnings/should_compile/T11128b.hs b/testsuite/tests/warnings/should_compile/T11128b.hs
new file mode 100644
index 0000000000..2cca9a53e0
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T11128b.hs
@@ -0,0 +1,64 @@
+{-# LANGUAGE DeriveFunctor #-}
+{-# OPTIONS_GHC -Wnoncanonical-monadfail-instances #-}
+
+-- | Test noncanonical-monadfail-instances warnings
+module T11128b where
+
+import Control.Applicative as A
+import Control.Monad as M
+import Control.Monad.Fail as MF
+
+----------------------------------------------------------------------------
+-- minimal definition
+
+data T0 a = T0 a deriving Functor
+
+instance A.Applicative T0 where
+ pure = T0
+ (<*>) = M.ap
+
+instance M.Monad T0 where
+ (>>=) = undefined
+
+instance MF.MonadFail T0 where
+ fail = error "fail"
+
+----------------------------------------------------------------------------
+-- trigger all 2 warnings
+
+data T1 a = T1 a deriving Functor
+
+instance A.Applicative T1 where
+ pure = return
+ (<*>) = M.ap
+ (*>) = (M.>>)
+
+instance M.Monad T1 where
+ (>>=) = undefined
+ return = T1
+ (>>) = undefined
+ fail = error "fail"
+
+instance MF.MonadFail T1 where
+ fail = M.fail
+
+----------------------------------------------------------------------------
+-- backward compat canonical defintion
+
+data T2 a = T2 a deriving Functor
+
+instance Applicative T2 where
+ pure = T2
+ (<*>) = ap
+ (*>) = undefined
+
+instance M.Monad T2 where
+ (>>=) = undefined
+ return = pure
+ (>>) = (*>)
+ fail = MF.fail
+
+instance MF.MonadFail T2 where
+ fail = error "fail"
+
+----------------------------------------------------------------------------
diff --git a/testsuite/tests/warnings/should_compile/T11128b.stderr b/testsuite/tests/warnings/should_compile/T11128b.stderr
new file mode 100644
index 0000000000..57aa22beea
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T11128b.stderr
@@ -0,0 +1,10 @@
+
+T11128b.hs:40:5: warning:
+ Noncanonical ‘fail’ definition detected
+ in the instance declaration for ‘Monad T1’.
+ Either remove definition for ‘fail’ or define as ‘fail = Control.Monad.Fail.fail’
+
+T11128b.hs:43:5: warning:
+ Noncanonical ‘fail = Control.Monad.fail’ definition detected
+ in the instance declaration for ‘MonadFail T1’.
+ Move definition from ‘Control.Monad.fail’ to ‘fail’
diff --git a/testsuite/tests/warnings/should_compile/all.T b/testsuite/tests/warnings/should_compile/all.T
index a2b1860ba4..2e7132213c 100644
--- a/testsuite/tests/warnings/should_compile/all.T
+++ b/testsuite/tests/warnings/should_compile/all.T
@@ -7,6 +7,7 @@ test('T9230', normal, compile_without_flag('-fno-warn-tabs'), [''])
test('T10908', normal, compile, [''])
test('T11077', normal, compile, ['-fwarn-missing-exported-sigs'])
test('T11128', normal, compile, [''])
+test('T11128b', normal, compile, [''])
test('PluralS', normal, compile, [''])
test('DeprU',