[GHC] #14549: RecordWildCards doesn't work properly in repl

#14549: RecordWildCards doesn't work properly in repl -------------------------------------+------------------------------------- Reporter: akegalj | Owner: (none) Type: bug | Status: new Priority: lowest | Milestone: Component: GHCi | Version: 8.2.1 Keywords: repl, | Operating System: Unknown/Multiple recordwildcards | Architecture: | Type of failure: Incorrect result Unknown/Multiple | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- {{{#!hs Prelude> :set -XRecordWildCards Prelude> data Test = Test { t1 :: Int, t2 :: Int } deriving Show Prelude> t1 = 2 Prelude> Test { t2 = 2, ..} <interactive>:15:1: warning: [-Wmissing-fields] • Fields of 'Test' not initialised: t1 • In the expression: Test {t2 = 2, ..} In an equation for 'it': it = Test {t2 = 2, ..} <interactive>:15:1: warning: [-Wmissing-fields] • Fields of 'Test' not initialised: t1 • In the expression: Test {t2 = 2, ..} In an equation for 'it': it = Test {t2 = 2, ..} Test {t1 = *** Exception: <interactive>:15:1-18: Missing field in record construction t1 }}} I would expect above to return `Test {t1 = 2, t2 = 2}` instead of throwing an error. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14549 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14549: RecordWildCards doesn't work properly in repl -------------------------------------+------------------------------------- Reporter: akegalj | Owner: (none) Type: bug | Status: new Priority: lowest | Milestone: Component: GHCi | Version: 8.2.1 Resolution: | Keywords: repl, | recordwildcards Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by AntC): I would expect an error; the question is what error? If you write that code as a program, the compiler doesn't go past the second line: `Multiple declarations of ‘t1’` * The declaration for `Test` puts name `t1` in scope -- as a field selector function. * The `t1 = 2` (tries to) create a different declaration of `t1` ===> fail. In the repl, I guess the `t1 = 2` shadows `Test`'s declaration of `t1`, so you don't get a failure at that point. This works: {{{ data Test = Test { t1 :: Int, t2 :: Int } deriving Show tt = let t1 = 2 in Test { t2 = 2, ..} main = print tt }}} You might also get your code to work if you compiled the declaration for `Test` into a separate module and imported it. (In effect that's what I'm doing with the `let`.) Then `t1` would refer to your local variable, and wouldn't clash with imported `t1`. In an expression using record syntax `Test { t2 = 2, t1 = t1 }`, the lhs `t1` must be a field label, the rhs `t1` must be your local variable. Then `Test { t2 = 2, .. }` should work. In general, the repl is a tool for hacking quick-and-dirty. Don't expect that if it accepts some line of code you have an overall coherent program. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14549#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14549: RecordWildCards doesn't work properly in repl -------------------------------------+------------------------------------- Reporter: akegalj | Owner: (none) Type: bug | Status: closed Priority: lowest | Milestone: Component: GHCi | Version: 8.2.1 Resolution: invalid | Keywords: repl, | recordwildcards Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by akegalj): * status: new => closed * resolution: => invalid Comment:
I guess the t1 = 2 shadows Test's declaration of t1, so you don't get a failure at that point. ah, yes. I didn't think of that it could be name shadowing. It would be clear to me if I got warning about shadowing but I guess that is suppressed by default in the repl as otherways people would be annoyed seeing that error all the time - as it is a common use case for repl to shadow a variable.
You might also get your code to work if you compiled the declaration for Test into a separate module yes, that's why I got confused with the repl as it was working with compiled module.
Thank you for a very detailed explanation - it makes perfect sense now. Closing -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14549#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC