error-messages icon indicating copy to clipboard operation
error-messages copied to clipboard

Add suggestions in "not a (visible) method"

Open monoidal opened this issue 3 years ago • 1 comments
trafficstars

If I refer to an unknown method in a class

module M where

import Data.Data (Data (gunfold))

data T

instance Data T where
  toConstr = undefined
  gunfolt = undefined

I get this error:

M.hs:8:3: error:
    ‘toConstr’ is not a (visible) method of class ‘Data’
  |
8 |   toConstr = undefined
  |   ^^^^^^^^

M.hs:9:3: error:
    ‘gunfolt’ is not a (visible) method of class ‘Data’
  |
9 |   gunfolt = undefined
  |   ^^^^^^^

Contrast with:

module M2 where

import Data.Data (Data (gunfold))

a = toConstr
b = gunfolt

where I get helpful suggestions:

M2.hs:5:5: error:
    • Variable not in scope: toConstr
    • Perhaps you want to add ‘toConstr’ to the import list
      in the import of ‘Data.Data’ (M2.hs:3:1-33).
  |
5 | a = toConstr
  |     ^^^^^^^^

M2.hs:6:5: error:
    • Variable not in scope: gunfolt
    • Perhaps you meant ‘gunfold’ (imported from Data.Data)
  |
6 | b = gunfolt
  |     ^^^^^^^

A cheap fix would be just to list all visible methods of the class in the error, classes usually don't have that many members.

Related: #13, about associated types.

monoidal avatar Aug 01 '22 15:08 monoidal

A related example, involving a qualified name, is available at https://gitlab.haskell.org/ghc/ghc/-/issues/20945.

data Foo a = Foo a deriving (Functor)

instance Applicative (Foo) where
  pure = Foo

gives

warning: [-Wmissing-methods]
    • No explicit implementation for
        either ‘<*>’ or ‘GHC.Base.liftA2’

Attempting to follow the suggestion,

instance Applicative Foo where
  pure = Foo
  GHC.Base.liftA2 f (Foo a) (Foo b) = Foo $ f a b

gives

    ‘GHC.Base.liftA2’ is not a (visible) method of class ‘Applicative’
   |
10 |   GHC.Base.liftA2 f (Foo a) (Foo b) = Foo $ f a b
   |   ^^^^^^^^^^^^^^^

    Qualified name in binding position: GHC.Base.liftA2
   |
10 |   GHC.Base.liftA2 f (Foo a) (Foo b) = Foo $ f a b
   |   ^^^^^^^^^^^^^^^

liftA2 is now be exported from Prelude in ghc master, but the general problem remains. The error message should suggest adding an import.

monoidal avatar Sep 12 '22 15:09 monoidal