Opened 4 years ago

Last modified 4 years ago

#10707 new bug

-fth-dec-file outputs invalid case clauses

Reported by: Fabian Owned by: bollmann
Priority: normal Milestone:
Component: Template Haskell Version: 7.10.1
Keywords: newcomer Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Other Test Case:
Blocked By: Blocking:
Related Tickets: #10701 Differential Rev(s):
Wiki Page:

Description (last modified by Fabian)

instance Data.Aeson.Types.Class.ToJSON Language.Haskell.TH.Syntax.Name where
  Data.Aeson.Types.Class.toJSON x
    = \ k_a94l v_a94m
        -> case k_a94l of {
             GHC.Base.Just "" -> GHC.Err.undefined
             GHC.Base.Nothing -> GHC.Err.undefined }
        (GHC.Base.Just "test") "test2"

Attachments (1)

Test.hs (1.2 KB) - added by Fabian 4 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 4 years ago by Fabian

Description: modified (diff)

comment:2 Changed 4 years ago by goldfire

Component: CompilerTemplate Haskell
Keywords: newcomer added
Milestone: 7.12.1

Thanks for the report. No great care has been made to ensure that pretty-printed Template Haskell can be parsed, but this has been requested before. Straightforward to fix -- all the code is in Language.Haskell.TH.Ppr. Anyone care to submit a patch?

Changed 4 years ago by Fabian

Attachment: Test.hs added

comment:3 Changed 4 years ago by rwbarton

I'm not sure what you mean by "invalid case clauses", but it seems to me that the error is that

case e of { ...alts... } foo

is not valid syntax for an application, it must be parenthesized like

(case e of { ...alts... }) foo

A problem with the case expression itself in its context, not the case clauses.

comment:4 in reply to:  3 ; Changed 4 years ago by Fabian

Replying to rwbarton:

I'm not sure what you mean by "invalid case clauses", but it seems to me that the error is that

case e of { ...alts... } foo

is not valid syntax for an application, it must be parenthesized like

(case e of { ...alts... }) foo

A problem with the case expression itself in its context, not the case clauses.

I wasn't sure exactly what was wrong with the output

comment:5 Changed 4 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:6 Changed 4 years ago by thomie

Milestone: 8.0.1

comment:7 in reply to:  4 Changed 4 years ago by bollmann

Replying to Fabian:

Replying to rwbarton:

I'm not sure what you mean by "invalid case clauses", but it seems to me that the error is that

case e of { ...alts... } foo

is not valid syntax for an application, it must be parenthesized like

(case e of { ...alts... }) foo

A problem with the case expression itself in its context, not the case clauses.

I wasn't sure exactly what was wrong with the output

According to Fabian's specified AST (see the attached Test.hs), the problem rather seems to be missing parentheses around the lambda abstraction as well as missing semi-colons to separate the cases. That is, instead of pretty-printing

instance Data.Aeson.Types.Class.ToJSON Language.Haskell.TH.Syntax.Name where
  Data.Aeson.Types.Class.toJSON x
    = \ k_a94l v_a94m
        -> case k_a94l of {
             GHC.Base.Just "" -> GHC.Err.undefined
             GHC.Base.Nothing -> GHC.Err.undefined }
        (GHC.Base.Just "test") "test2"

the code should be pretty-printed as:

instance Data.Aeson.Types.Class.ToJSON Language.Haskell.TH.Syntax.Name where
  Data.Aeson.Types.Class.toJSON x
    = (\ k_a94l v_a94m
        -> case k_a94l of {
             GHC.Base.Just "" -> GHC.Err.undefined;   -- note the ';' here
             GHC.Base.Nothing -> GHC.Err.undefined }) -- note the parentheses
        (GHC.Base.Just "test") "test2"

That should be easy to fix. I'll have a look.

comment:8 Changed 4 years ago by bollmann

Owner: set to bollmann

comment:9 Changed 4 years ago by bollmann

Upon a first investigation, it seems that the pretty printer in Language.Haskell.TH.Ppr functions correctly (at least concerning the parentheses around the lambda abstraction).

It hence seems as if the pretty printer that is actually called when dumping the splice via -dth-dec-file is not Language.Haskell.TH.Ppr.pprint? I will investigate this further...

comment:10 Changed 4 years ago by goldfire

That may well be true. If I recall, the other pretty-printer to look at is the one for HsExpr, in hsSyn/HsExpr.hs. It's quite possible that this ticket will be fixed when #10854 is complete.

comment:11 Changed 4 years ago by bollmann

@goldfire: good reference, thanks! Indeed, applying a lambda abstraction to another term was not correctly parenthesized when converting from TH Exps to HsExprs in hsSyn/Convert.hs. However, this has been fixed in #10603.

Thus, it would only remain to put semicolons between the different alternatives of the HsCase expression in order to make this ticket's pretty printed code parse again. It seems to me that adding these ';' could be done by adjusting pprMatches in hsSyn/HsExpr. But I'm not sure if this rather ad-hoc approach is right, more so since I suspect that there might be other Haskell constructs where the pretty-printer does not print ';' between multiple case alternatives.

Any ideas on how to proceed best with this?

Last edited 4 years ago by bollmann (previous) (diff)
Note: See TracTickets for help on using tickets.