Explain why a type is defaulted
This comes from https://github.com/haskell/error-messages/issues/56.
In the program
x = 5
GHC will report (with suitable flags)
Scratch.hs:32:5: warning: [-Wtype-defaults]
* Defaulting the following constraint to type `Integer'
Num a0 arising from the literal `5'
* In the expression: 5
In an equation for `x': x = 5
|
32 | x = 5
| ^
This message leaves out a critical piece of information: why the constraint was defaulted. I suggest
Scratch.hs:32:5: warning: [-Wtype-defaults]
* Defaulting a0 to type `Integer' to satisfy the following constraint:
Num a0 arising from the literal `5'
This constraint could not be part of the context on 'x'
because of the monomorphism restriction.
* In the expression: 5
In an equation for `x': x = 5
|
32 | x = 5
| ^
The new message has several improvements over the existing one:
- It is more literally correct. The old message talks about defaulting a constraint to
Integer
, which doesn't really make sense. - The new message relates the constraint directly to
x
, which is otherwise somewhat hard to discover. - The new message informs the user that the monomorphism restriction is in play, should they wish to disable it.
Addressing (1) is dead easy: just change the text.
Addressing (2) and (3) is harder: I think the way to do it is to have a new CtOrigin
constructor MonomorphismRestrictionOrigin :: CtOrigin -> Id -> CtOrigin
that wraps an existing origin so that the above message can be printed. Arranging to create this new CtOrigin
is a bit tricky: the place to do it seems to be decideMonoTyVars
(which is what deals with the MR), but that would require threading some extra information around.