Skip to content

Add hints for unsolved HasField constraints

sheaf requested to merge sheaf/ghc:T26480 into master

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:

  1. 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.

  2. Explain why GHC did not solve the constraint, e.g.:

    • fld_name is not a string literal (e.g. it's a type variable).
    • rec_ty is a TyCon without any fields, e.g. Int or Bool.
    • fld_ty contains existentials variables or foralls.
    • The record field is a pattern synonym field (GHC does not generate HasField instances for those).
    • HasField is a custom TyCon, not actually the built-in HasField typeclass from GHC.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.

Edited by sheaf

Merge request reports

Loading