haskell / haskell/error-messages

Better error message for out of scope data constructor if a type with the same name exists

Open
#533 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
76
Forks
19
PR merge metrics
No merged PRs in 30d

Description

Inspired by [this stackoverflow question](https://stackoverflow.com/q/74259720/15207568),

I imagine a common mistake newcomers make when learning about pattern matching is that they use the name of the type instead of the name of constructors. I guess especially people coming from C-style languages are used to the prefix `Typename x` way of writing function arguments. I think we can detect this case and save them the trouble of turning to a book or the internet for help.

In particular, I'd propose to add a check for the not in scope error for data constructors. If the identifier does match a valid type, then we can suggest that perhaps the user meant to use a constructor of that type. We could list all possible constructors (limited to 5 if there are too many perhaps).

The problematic code in the stackoverflow question is this:

```haskell
data Mood = Blah | Woot

instance Show Mood where
show(Mood x) = "Mood: " ++ show(x)
```

Currently GHC produces the message:

```
T7.hs:4:10: error: Not in scope: data constructor ‘Mood’
|
4 | show(Mood x) = "Mood: " ++ show(x)
| ^^^^
```

I'd propose:

```
T7.hs:4:10: error: Not in scope: data constructor ‘Mood’
Perhaps you intended to pattern match on the type ‘Mood’
For that you can use its data constructors: ‘Blah’, ‘Woot’
|
4 | show(Mood x) = "Mood: " ++ show(x)
| ^^^^
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.