4

I'm writing a TH library and I have a situation where I'm doing some module-wide stuff. In a nutshell, I'm collecting information from all quasiquotes and putQ-ing it into Q.

Then, once the Haskell module has been typechecked, I look up all this state and do some stuff with it. I'm managing that by recording this final action in using addModFinalizer.

Now, the problem: sometimes, this final action can fail. If it does, I want to report a compile error. Doing this with fail, I get error messages that look like:

Main.hs:1:1: error: My message of something going wrong with the whole module...
  |
1 | {-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
  | ^

As you can see, GHC decides to associate my error message to the top of the file. Is there any way I can tell GHC not to associate my error with a bit of code? I'd prefer something like:

Main.hs: error: My message of something going wrong with the whole module...
Alec
  • 31,829
  • 7
  • 67
  • 114
  • By the way, I am aware that in general the sort of error reporting I'm looking for is unhelpful to the end user - fear not, they will have more error reporting logs before this broad error - just not coming from GHC. – Alec Dec 01 '17 at 17:25
  • 1
    The only thing I can think of is 'faking' the error message with something like `runIO (System.Exit.die "fail")`. Depending on your specific use case, this could be entirely unsatisfactory, e.g. with `-fdefer-type-errors` compilation of subsequent modules should continue after one fails, but this would kill the entire process. At least, GHC stills seems to do cleanup after compilation even if you kill it unceremoniously like this. Alternatively, consider adding a disclaimer at the bottom of the error stating that the location below is entirely bogus. – user2407038 Dec 01 '17 at 18:03

0 Answers0