Commit cb6d8589 authored by Ömer Sinan Ağacan's avatar Ömer Sinan Ağacan

Slighly improve infix con app pattern errors

Given this program:

    main = do
      f $ do
        a <- return 3
          c <- do
          return 5

GHC previously gave this error message:

    Main.hs:2:7: error:
        Parse error in pattern: do a <- return 3 c
        Possibly caused by a missing 'do'?
      |
    2 |   f $ do
      |       ^^...

What happened is GHC considered the whole `f $ do a <- return 3 c` as a
pattern. When parsed as an expression it becomes an infix application of
`($)`, and GHC checks left and right hand sides before checking if `($)`
is a valid infix constructor name, and shows the first error it got.

If instead we first check if the infix op is valid in pattern context,
the error message becomes much clearer:

    Main.hs:2:3: error:
        Parse error in pattern: f $ do a <- return 3 c
        Possibly caused by a missing 'do'?
      |
    2 |   f $ do
      |   ^^^^^^...

This may not entirely fix #11188 but I think it's an improvement.

Reviewers: bgamari

Reviewed By: bgamari

Subscribers: rwbarton, thomie, carter

GHC Trac Issues: #11188

Differential Revision: https://phabricator.haskell.org/D4497
parent 152055a1
......@@ -984,12 +984,13 @@ checkAPat msg loc e0 = do
| extopt LangExt.NPlusKPatterns opts && (plus == plus_RDR)
-> return (mkNPlusKPat (L nloc n) (L lloc lit))
OpApp l op _fix r -> do l <- checkLPat msg l
r <- checkLPat msg r
case op of
L cl (HsVar (L _ c)) | isDataOcc (rdrNameOcc c)
-> return (ConPatIn (L cl c) (InfixCon l r))
_ -> patFail msg loc e0
OpApp l (L cl (HsVar (L _ c))) _fix r
| isDataOcc (rdrNameOcc c) -> do
l <- checkLPat msg l
r <- checkLPat msg r
return (ConPatIn (L cl c) (InfixCon l r))
OpApp _l _op _fix _r -> patFail msg loc e0
HsPar e -> checkLPat msg e >>= (return . ParPat)
ExplicitList _ _ es -> do ps <- mapM (checkLPat msg) es
......
main = do
f $ do
a <- return 3
c <- do
return 5
InfixAppPatErr.hs:2:3: error:
Parse error in pattern: f $ do a <- return 3 c
Possibly caused by a missing 'do'?
......@@ -115,3 +115,5 @@ test('NumericUnderscoresFail0',
grep_errmsg(r'^NumericUnderscoresFail0.hs:'), compile_fail, [''])
test('NumericUnderscoresFail1',
grep_errmsg(r'^NumericUnderscoresFail1.hs:'), compile_fail, [''])
test('InfixAppPatErr', normal, compile_fail, [''])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment