Commit 503219e3 authored by Ryan Scott's avatar Ryan Scott Committed by Ben Gamari

Warn about missing instance methods that start with an underscore

Previously, GHC would not warn whenever there was a class instance that
didn't implement a class method whose name begins with an underscore.

Fixes #12959.

Test Plan: make test TEST=WarnMinimal

Reviewers: austin, bgamari, simonpj

Reviewed By: bgamari, simonpj

Subscribers: thomie

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

GHC Trac Issues: #12959
parent 1ec632f8
...@@ -293,12 +293,10 @@ tcClassMinimalDef _clas sigs op_info ...@@ -293,12 +293,10 @@ tcClassMinimalDef _clas sigs op_info
(\bf -> addWarnTc NoReason (warningMinimalDefIncomplete bf)) (\bf -> addWarnTc NoReason (warningMinimalDefIncomplete bf))
return mindef return mindef
where where
-- By default require all methods without a default -- By default require all methods without a default implementation
-- implementation whose names don't start with '_'
defMindef :: ClassMinimalDef defMindef :: ClassMinimalDef
defMindef = mkAnd [ noLoc (mkVar name) defMindef = mkAnd [ noLoc (mkVar name)
| (name, _, Nothing) <- op_info | (name, _, Nothing) <- op_info ]
, not (startsWithUnderscore (getOccName name)) ]
instantiateMethod :: Class -> Id -> [TcType] -> TcType instantiateMethod :: Class -> Id -> [TcType] -> TcType
-- Take a class operation, say -- Take a class operation, say
......
...@@ -100,6 +100,18 @@ Compiler ...@@ -100,6 +100,18 @@ Compiler
- The Mingw-w64 toolchain for the Windows version of GHC has been updated. GHC now uses - The Mingw-w64 toolchain for the Windows version of GHC has been updated. GHC now uses
`GCC 6.2.0` and `binutils 2.27`. `GCC 6.2.0` and `binutils 2.27`.
- Previously, :ghc-flag:`-Wmissing-methods` would not warn whenever a type
class method beginning with an underscore was not implemented in an instance.
For instance, this code would compile without any warnings: ::
class Foo a where
_Bar :: a -> Int
instance Foo Int
:ghc-flag:`-Wmissing-methods` will now warn that ``_Bar`` is not implemented
in the ``Foo Int`` instance.
GHCi GHCi
~~~~ ~~~~
......
...@@ -87,7 +87,7 @@ class Cheater a where -- WARNING LINE ...@@ -87,7 +87,7 @@ class Cheater a where -- WARNING LINE
class Cheater2 a where class Cheater2 a where
_cheater2 :: a _cheater2 :: a
{-# MINIMAL #-} -- no warning {-# MINIMAL #-} -- warning!
class Cheater3 a where -- WARNING LINE class Cheater3 a where -- WARNING LINE
cheater3, cheater3b :: a cheater3, cheater3b :: a
...@@ -100,7 +100,7 @@ instance Num Bool where -- WARNING LINE ...@@ -100,7 +100,7 @@ instance Num Bool where -- WARNING LINE
class NoExplicit a where class NoExplicit a where
needed :: a needed :: a
_optional :: a _alsoNeeded :: a
instance NoExplicit Int where -- WARNING LINE instance NoExplicit Int where -- WARNING LINE
......
...@@ -30,6 +30,12 @@ WarnMinimal.hs:84:1: warning: ...@@ -30,6 +30,12 @@ WarnMinimal.hs:84:1: warning:
but there is no default implementation. but there is no default implementation.
• In the class declaration for ‘Cheater’ • In the class declaration for ‘Cheater’
WarnMinimal.hs:88:1: warning:
• The MINIMAL pragma does not require:
‘_cheater2’
but there is no default implementation.
• In the class declaration for ‘Cheater2’
WarnMinimal.hs:92:1: warning: WarnMinimal.hs:92:1: warning:
• The MINIMAL pragma does not require: • The MINIMAL pragma does not require:
‘cheater3b’ ‘cheater3b’
...@@ -45,7 +51,7 @@ WarnMinimal.hs:99:10: warning: [-Wmissing-methods (in -Wdefault)] ...@@ -45,7 +51,7 @@ WarnMinimal.hs:99:10: warning: [-Wmissing-methods (in -Wdefault)]
WarnMinimal.hs:105:10: warning: [-Wmissing-methods (in -Wdefault)] WarnMinimal.hs:105:10: warning: [-Wmissing-methods (in -Wdefault)]
• No explicit implementation for • No explicit implementation for
‘needed’ ‘needed’ and ‘_alsoNeeded’
• In the instance declaration for ‘NoExplicit Int’ • In the instance declaration for ‘NoExplicit Int’
WarnMinimal.hs:116:10: warning: [-Wmissing-methods (in -Wdefault)] WarnMinimal.hs:116:10: warning: [-Wmissing-methods (in -Wdefault)]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment