Better type errors for NamedRoutes
without Generic
instance
#1641
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If you forget to derive
Generic
for your API types in the context of named routes, you get rather unhelpful error messages. For example, considerwhich yields
for me on GHC 9.2.4. One might spot the
Rep
type family here fromGHC.Generics
, but it is certainly not obvious at all, the first message in particular. This can be particularly bad when you have many nested APIS, residing in completely different modules.In this PR, we use a trick from this blog post: https://blog.csongor.co.uk/report-stuck-families/
With it, the first error message gets much simpler:
The error messages for other snippets such as
also improve in the same manner.
I added the error-reporting constraint as a superclass constraint to the various instances of
NamedRoutes
. There might be further places to apply this pattern, or even a way to also make the second error message better (naively addingErrorIfNoGeneric
to e.g.GenericServant
does not work, as then certain call sites oftoServant
/fromServant
then no longer compile).As this approach relies non-trivially on type family evaluation semantics, I can't outrule that this might yield type errors in contexts where one tries to use
NamedRoutes
with a not-yet-concreteapi
, but playing around, I couldn't find anything, and a relatively large internal code base using servant and various add-on packages also compiled fine.