Skip to content

Bogus origin reported with RebindableSyntax

Consider this (discovered when investigating #19918 (closed)):

{-# LANGUAGE RebindableSyntax #-}

module Foo where

import Prelude( Bool )

class Wombat a

ifThenElse :: Wombat a => Bool -> a -> a -> a
ifThenElse _ ok _ = ok

foo :: ()
foo = if True then () else ()

We get this bad error

Foo.hs:13:7: error:
    • No instance for (Wombat ())
        << This should not appear in error messages. If you see this
        in an error message, please report a bug mentioning ‘if expression’ at
        https://gitlab.haskell.org/ghc/ghc/wikis/report-a-bug >>
    • In the expression: if True then () else ()
      In an equation for ‘foo’: foo = if True then () else ()
   |
13 | foo = if True then () else ()
   |       ^^^^^^^^^^^^^^^^^^^^^^^

Diagnosis

GHC expands the if to a call of ifThenElse. But the CtOrigin for constraints arising from this call generated here, in GHC.Tc.Gen.App.tcInstFun:

    fun_orig = exprCtOrigin (case fun_ctxt of
                               VAExpansion e _ -> e
                               VACall e _ _    -> e)

Here we have a VAExpansion, so we'll get exprCtOrigin (HsIf ...). And that is good: the origin is the user-visible if-then-else.

Alas we have

exprCtOrigin (HsIf {})           = Shouldn'tHappenOrigin "if expression"

But it should happen! Solution: an origin for these expansions. Then the error message can say

Foo.hs:13:7: error:
    • No instance for (Wombat ())
        arising from the expansion of an if-then-else expression
    • In the expression: if True then () else ()
      In an equation for ‘foo’: foo = if True then () else ()
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information