Add hints for unsolved HasField constraints
Tickets: #18776 (closed) #22382 (closed) #26480 (closed)
This MR adds hints and explanations for unsolved HasField constraints.
GHC will now provide additional explanations for an unsolved constraint of the form HasField fld_name rec_ty fld_ty; the details are laid out in Note [Error messages for unsolved HasField constraints], but briefly:
-
Provide similar name suggestions (e.g. mis-spelled field name) and import suggestions (record field not in scope). These result in actionable
GhcHints, which is helpful to provide code actions in HLS. -
Explain why GHC did not solve the constraint, e.g.:
-
fld_nameis not a string literal (e.g. it's a type variable). -
rec_tyis a TyCon without any fields, e.g.IntorBool. -
fld_tycontains existentials variables or foralls. - The record field is a pattern synonym field (GHC does not generate
HasFieldinstances for those). -
HasFieldis a custom TyCon, not actually the built-inHasFieldtypeclass fromGHC.Records.
-
On the way, we slightly refactor the mechanisms for import suggestions in GHC.Rename.Unbound.
This is to account for the fact that, for HasField, we don't care whether the field is imported qualified or
unqualified. importSuggestions was refactored, we now have sameQualImportSuggestions and anyQualImportSuggestions.