Improve "No instance for HasField" error messages
The built-in HasField x r a constraint is solved automatically whenever x is a field label that belongs to the type r (except if the field is not in scope, or for a few other corner cases, e.g. the field type is existential, higher-rank or unboxed). If it is not solved automatically, however, the error message is typically just
No instance for (HasField "foo" T ()) arising from ...
which is rather uninformative. It would be better to customise the error reporting:
- If the type
ris not a record type at all, we could mention that fact. - If the type
ris a record type, but does not have the named field, we could suggest similarly-named fields (like how the renamer suggests possibilities for not-in-scope identifiers). - If the field does belong to the type, but is not in scope, we could say that and suggest importing it.
- If the field type is existential, higher-rank or unboxed, we could explain that such fields cannot be used with
HasField.