GHC issueshttps://gitlab.haskell.org/ghc/ghc/-/issues2019-07-07T18:03:36Zhttps://gitlab.haskell.org/ghc/ghc/-/issues/15645TypeChecking of Monad patterns incorrect with RebindableSyntax and Overloaded...2019-07-07T18:03:36ZNeil MitchellTypeChecking of Monad patterns incorrect with RebindableSyntax and OverloadedStringsUsing GHC 8.4.3, I'd expect the following to work:
```hs
{-# LANGUAGE RebindableSyntax, OverloadedStrings #-}
module Fail where
import Prelude hiding (fail)
foo x = do
Just y <- x
return y
newtype Text = Text String
fail ::...Using GHC 8.4.3, I'd expect the following to work:
```hs
{-# LANGUAGE RebindableSyntax, OverloadedStrings #-}
module Fail where
import Prelude hiding (fail)
foo x = do
Just y <- x
return y
newtype Text = Text String
fail :: Text -> a
fail (Text x) = error x
fromString :: String -> Text
fromString = Text
```
But it fails with:
```
Fail.hs:8:5-15: error:
* Couldn't match expected type `[Char]' with actual type `Text'
* In a stmt of a 'do' block: Just y <- x
In the expression:
do Just y <- x
return y
In an equation for `foo':
foo x
= do Just y <- x
return y
|
8 | Just y <- x
| ^^^^^^^^^^^
```
Given the enabled extensions, I'd expect `foo` to desugar as:
```hs
foo x = x >>= \v -> case v of
Just y -> return y
_ -> fail (fromString "pattern match error")
```
But looking at TcMatches.tcMonadFailOp it checks the fail operation (which is literally `fail`) takes an argument of type tyString (e.g. `[Char]`). One way around that would be to make the "fail-op" being passed around be `fail . fromString` if the appropriate extensions are enabled.
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | -------------------- |
| Version | 8.4.3 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | ndmitchell@gmail.com |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"TypeChecking of Monad patterns incorrect with RebindableSyntax and OverloadedStrings","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"8.6.1","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.4.3","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":["ndmitchell@gmail.com"],"type":"Bug","description":"Using GHC 8.4.3, I'd expect the following to work:\r\n\r\n{{{#!hs\r\n{-# LANGUAGE RebindableSyntax, OverloadedStrings #-}\r\n\r\nmodule Fail where\r\n\r\nimport Prelude hiding (fail)\r\n\r\nfoo x = do\r\n Just y <- x\r\n return y\r\n\r\nnewtype Text = Text String\r\n\r\nfail :: Text -> a\r\nfail (Text x) = error x\r\n\r\nfromString :: String -> Text\r\nfromString = Text\r\n}}}\r\n\r\nBut it fails with:\r\n\r\n{{{\r\nFail.hs:8:5-15: error:\r\n * Couldn't match expected type `[Char]' with actual type `Text'\r\n * In a stmt of a 'do' block: Just y <- x\r\n In the expression:\r\n do Just y <- x\r\n return y\r\n In an equation for `foo':\r\n foo x\r\n = do Just y <- x\r\n return y\r\n |\r\n8 | Just y <- x\r\n | ^^^^^^^^^^^\r\n}}}\r\n\r\nGiven the enabled extensions, I'd expect {{{foo}}} to desugar as:\r\n\r\n{{{#!hs\r\nfoo x = x >>= \\v -> case v of\r\n Just y -> return y\r\n _ -> fail (fromString \"pattern match error\")\r\n}}}\r\n\r\nBut looking at TcMatches.tcMonadFailOp it checks the fail operation (which is literally {{{fail}}}) takes an argument of type tyString (e.g. {{{[Char]}}}). One way around that would be to make the \"fail-op\" being passed around be {{{fail . fromString}}} if the appropriate extensions are enabled.","type_of_failure":"OtherFailure","blocking":[]} -->8.8.1Shayne Fletchershayne@shaynefletcher.orgShayne Fletchershayne@shaynefletcher.org