Skip to content

Add message for GHC-72771 "Mismatched default method signature" #524

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

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions message-index/messages/GHC-72771/example/after/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeOperators #-}

module Lib where

import GHC.Generics

class Enumerate a where
enumerate :: [a]
default enumerate :: (Int ~ a) => [a]
enumerate = [1 ..]
11 changes: 11 additions & 0 deletions message-index/messages/GHC-72771/example/before/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeOperators #-}

module Lib where

import GHC.Generics

class Enumerate a where
enumerate :: [a]
default enumerate :: (Int ~ a) => a
enumerate = [1 ..]
25 changes: 25 additions & 0 deletions message-index/messages/GHC-72771/example/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Default type signature does not match
---

## Error Message
```
Lib.hs:10:11: error: [GHC-72771]
• The default type signature for enumerate: (Int ~ a) => a
does not match its corresponding non-default type signature
• When checking the class method:
enumerate :: forall a. Enumerate a => [a]
In the class declaration for ‘Enumerate’
|
10 | default enumerate :: (Int ~ a) => a
| ^^^^^^^^^
```

## Explanation

Here, we have defined an `Enumerate` typeclass over some type `a`, its instance `enumerate` simply has the signature `[a]`.
At the same time, we define a default method for `enumerate`, which is applied in case the `a` is of type `Int`.
But, this default method has the type signature `a`, which is not equivalent to `[a]`, therefore this error is thrown.

To resolve this error, we need to define the default method signature to match the method signature.
Therefore, we define the default method signature to produce `[a]` instead of `a`.
8 changes: 8 additions & 0 deletions message-index/messages/GHC-72771/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Mismatched default method signature
summary: The provided default method implementation's type signature does not match the corresponding method type signature.
severity: error
introduced: 9.8.1
---

This error means that there is a type class with some method `m` for which a [default signature](https://downloads.haskell.org/ghc/9.12.1/docs/users_guide/exts/default_signatures.html) was defined, but that default signature does not match the type signature of `m`.