Skip to content

Confusing error and location when nesting lambdas and do-blocks

Summary

I was in the process of refactoring a large pattern match of the form:

{-# LANGUAGE UnicodeSyntax  #-}

module BadErrorLocation where

import Data.Functor.Identity (Identity(..))

data T = A | B | C | D

f t =
  \ x  do
    case t of
      A  do
        a  runIdentity (Identity 0)
        return a
      B  do
        a  runIdentity (Identity 0)
        return a
      C  do
        a  runIdentity (Identity 0)
        return a
      D  do oops
        a  runIdentity (Identity 0)
        return a
BadErrorLocation.hs:12:11: error: do-notation in pattern
   |
12 |       A → do
   |           ^^^...

Notice how I accidentally left an oops on the do line of the D branch. Unfortunately, this reports an error on the A branch line. In my real example, there were of course a couple dozen lines between these two, so figuring out where the error was became sort of a binary search on the AST...

Steps to reproduce

Just drop the above in a file.

Expected behavior

Some error on the D → do oops line ideally. No error on the A → do line.

If you remove the \ x → do block altogether, you'll see the error reported is better.

Environment

  • GHC version used: Confirmed on:
  • 8.10.7
  • 9.2.2
Edited by Valentin Robert
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information