Opened 5 years ago

Last modified 4 years ago

#10330 new bug

Better Template Haskell error message locations

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

Description

Consider this TH splice

f x = $( [| let g x = (True && 'x', x) in g True |] )

or more generally

f x = $( g [| x |])

Two problems

  1. If there is a type error in the spliced-in code, there is no record of the splice in the error message:
    TH.hs:5:10:
        Couldn't match expected type ‘Bool’ with actual type ‘Char’
        In the second argument of ‘(&&)’, namely ‘'x'’
        In the expression: (True && 'x')
        In the expression: ((True && 'x'), x_a2qf)
    
    I'd like to see
    TH.hs:5:10:
        Couldn't match expected type ‘Bool’ with actual type ‘Char’
        In the second argument of ‘(&&)’, namely ‘'x'’
        In the expression: (True && 'x')
        In the splice: $( [| let g x = ... in g True |] )   <------- NB
    
  1. (Less important, and harder to fix.) The error location (column 10) is that of the splice (column 10), not where the reported error really is (column 31).

Fixing this would mean round-tripping source location info through TH syntax. Moreover, since the constructed code may be combined from quoted snippets spread over some TH libraries, it's far from clear that the location info would help.

Thoughts about fixing (1). This can only arise for untyped splices. (For typed splices we can't get type errors in spliced code.) So the renamer should produce a syntax tree with a variant of HsPar saying "here is where I spliced in an expression". Same think for patterns and types; for declarations it would be a bit more fiddly.

Change History (3)

comment:1 Changed 5 years ago by goldfire

In my opinion, (2) above is more important than (1). The singletons package encourages wrapping up lots of code in one big splice and quote. ($(singletons [d| data Foo = ... ; bar :: ...; bar = ...; ... |])) Because of the bug reported here, users get no location information attached to often-baroque errors.

Agreed that (2) is quite a bit harder to fix than (1), though. (2) would seem to require a full overhaul of the TH AST and would break many clients of TH.

comment:2 Changed 5 years ago by ezyang

In that case, let me record one possible workaround for problem (2), which is #10331; however, this doesn't help users of the old TH API.

comment:3 Changed 4 years ago by ezyang

Cc: ezyang added
Note: See TracTickets for help on using tickets.