haskell / haskell/error-messages
Language extensions don't work
- Dominant language
- No language data
- Stars
- 76
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
[/u/crmills_2000 posted the following code on Reddit](https://reddit.com/r/haskell/comments/pfxgd4/monthly_hask_anything_september_2021/hd3jxjt/):
```haskell
module Lib ( someFunc) where
{-# LANGUAGE CPP, DeriveGeneric, OverloadedStrings, ScopedTypeVariables #-}
{-# LANGUAGE DeriveGeneric #-}
import Data.Text (Text)
import Data.Vector (Vector)
import GHC.Generics (Generic)
import qualified Data.ByteString.Lazy as BL
import Data.Csv
data Person = Person { name :: !Text , salary :: !Int }
deriving ( Show)
instance FromNamedRecord Person where
parseNamedRecord r = Person <$> r .: "name" <*> r .: "salary"
main :: IO ()
main = do
csvData <- BL.readFile "salaries.csv"
case decodeByName csvData of
Left err -> putStrLn err
Right v -> 0 -- V.forM_ v $ \ (name, salary :: Int) ->
-- putStrLn $ name ++ " earns " ++ show salary ++ " dollars"
someFunc :: IO ()
someFunc = putStrLn "someFunc"
```
This produces the errors:
```
T.hs:16:40: error:
• Couldn't match expected type ‘Data.ByteString.Internal.ByteString’
with actual type ‘[Char]’
• In the second argument of ‘(.:)’, namely ‘"name"’
In the second argument of ‘(<$>)’, namely ‘r .: "name"’
In the first argument of ‘(<*>)’, namely ‘Person <$> r .: "name"’
|
16 | parseNamedRecord r = Person <$> r .: "name" <*> r .: "salary"
| ^^^^^^
T.hs:16:56: error:
• Couldn't match expected type ‘Data.ByteString.Internal.ByteString’
with actual type ‘[Char]’
• In the second argument of ‘(.:)’, namely ‘"salary"’
In the second argument of ‘(<*>)’, namely ‘r .: "salary"’
In the expression: Person <$> r .: "name" <*> r .: "salary"
|
16 | parseNamedRecord r = Person <$> r .: "name" <*> r .: "salary"
| ^^^^^^^^
```
That is quite surprising because the `OverloadedStrings` extension seems to be enabled. However, the extensions are written below the `module ... where` declaration, so they are silently ignored.
I really don't know what the best way to solve this is. Maybe we should add an "important" warning about the misplaced language pragmas which is not overwritten by the errors? Or perhaps if `OverloadedStrings` would be suggested then the user at least knows that the extension is not detected by the compiler.
Edit: In this case the user reporting this issue mentions this is one of the first programs they have written in Haskell, so they have probably not heard of the OverloadedStrings extension before and just copied it from somewhere, so I don't know if it would be possible to have them fix the issue on their own with just a better error message. We can try and perhaps help some people who know slightly more, but maybe I'm just too trigger-happy reporting these.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the misplaced-pragmas example in T.hs and confirm how the compiler reports the OverloadedStrings errors. Then read the issue discussion to determine whether the intended change is an important warning, an extension suggestion, or another diagnostic; the work is done when that behavior is agreed and verified for this example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100