Skip to content

Layout rules: missing implicit semicolon after `if`

Summary

The following program is accepted by GHC, where the condition True starts at the same column as the do-block:

-- GHC accepts, but this looks BAD according to the standard
main = do
  if
  True then print () else print ()

For comparison, the following variants aren't accepted, where instead we try to do the same thing with either of the then or else branches:

-- BAD, and GHC rightly rejects it
main = do
  if True then
  print () else print ()

-- BAD, and GHC rightly rejects it
main = do
  if True then print () else
  print ()

Reading Lexer.x, it might be that the logic around MultiWayIf layout is silencing the implicit semicolon insertion, but I'm not entirely sure.

Link to the relevant section of Haskell 2010 (the layout algorithm): https://www.haskell.org/onlinereport/haskell2010/haskellch10.html#x17-17500010.3

Is that actually valid? Or is this a known bug? (I couldn't find any mention of it in a cursory search.)

Environment

  • GHC version used: GHC 8.6. Read the code on master.
Edited by Xia Li-yao
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information