Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestions in "not a (visible) method" #526

Open
monoidal opened this issue Aug 1, 2022 · 1 comment
Open

Add suggestions in "not a (visible) method" #526

monoidal opened this issue Aug 1, 2022 · 1 comment

Comments

@monoidal
Copy link
Member

monoidal commented Aug 1, 2022

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
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant