TTG: Handling Source Locations

Richard Eisenberg rae at cs.brynmawr.edu
Sat Feb 9 16:19:11 UTC 2019


Hi devs,

I just came across [TTG: Handling Source Locations], as I was poking around in RdrHsSyn and found wondrous things like (dL->L wiz waz) all over the place.

General outline: https://ghc.haskell.org/trac/ghc/wiki/ImplementingTreesThatGrow/HandlingSourceLocations <https://ghc.haskell.org/trac/ghc/wiki/ImplementingTreesThatGrow/HandlingSourceLocations>
Phab diff: https://phabricator.haskell.org/D5036 <https://phabricator.haskell.org/D5036>
Trac ticket: https://ghc.haskell.org/trac/ghc/ticket/15495 <https://ghc.haskell.org/trac/ghc/ticket/15495>
Commit: https://gitlab.haskell.org/ghc/ghc/commit/509d5be69c7507ba5d0a5f39ffd1613a59e73eea <https://gitlab.haskell.org/ghc/ghc/commit/509d5be69c7507ba5d0a5f39ffd1613a59e73eea>

I see why this change is wanted and how the new version works.

It seems to me, though, that this move makes us *less typed*. That is, it would be very easy (and disastrous) to forget to match on a location node. For example, I can now do this:

> foo :: LPat p -> ...
> foo (VarPat ...) = ...

Note that I have declared that foo takes a located pat, but then I forgot to extract the location with dL. This would type-check, but it would fail. Previously, the type checker would ensure that I didn't forget to match on the L constructor. This error would get caught after some poking about, because foo just wouldn't work.

However, worse, we might forget to *add* a location when downstream functions expect one. This would be harder to detect, for two reasons:
1. The problem is caught at deconstruction, and figuring out where an object was constructed can be quite hard.
2. The problem might silently cause trouble, because dL won't actually fail on a node missing a location -- it just gives noSrcSpan. So the problem would manifest as a subtle degradation in the quality of an error message, perhaps not caught until several patches (or years!) later.

So I'm uncomfortable with this direction of travel.

Has this aspect of this design been brought up before? I have to say I don't have a great solution to suggest. Perhaps the best I can think of is to make Located a type family. It would branch on the type index to HsSyn types, introducing a Located node for GhcPass but not for other types. This Isn't really all that extensible (I think) and it gives special status to GHC's usage of the AST. But it seems to solve the immediate problems without the downside above.

Sorry for reopening something that has already been debated, but (unless I'm missing something) the current state of affairs seems like a potential wellspring of subtle bugs.

Thanks,
Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20190209/a1797fa2/attachment.html>


More information about the ghc-devs mailing list