Skip to content

Reject nested foralls/contexts in instance types more consistently

Ryan Scott requested to merge wip/T18240 into master

GHC is very wishy-washy about rejecting instance declarations with nested foralls or contexts that are surrounded by outermost parentheses. This can even lead to some strange interactions with ScopedTypeVariables, as demonstrated in #18240 (closed). This patch makes GHC more consistently reject instance types with nested foralls/contexts so as to prevent these strange interactions.

On the implementation side, this patch tweaks splitLHsInstDeclTy and getLHsInstDeclHead to not look through parentheses, which can be semantically significant. I've added a Note [No nested foralls or contexts in instance types] in GHC.Hs.Type to explain why. This also introduces a no_nested_foralls_contexts_err function in GHC.Rename.HsType to catch nested foralls/contexts in instance types. This function is now used in rnClsInstDecl (for ordinary instance declarations) and rnSrcDerivDecl (for standalone deriving declarations), the latter of which fixes #18271 (closed).

On the documentation side, this adds a new "Formal syntax for instance declaration types" section to the GHC User's Guide that presents a BNF-style grammar for what is and isn't allowed in instance types.

Fixes #18240 (closed).

Merge request reports