Don't allow deriving-via when the via type is identical to the type intsances are being derived for
Summary
Currently we can use DerivingVia
to derive an instance via the very type that we're trying to define an instance for:
❯ ghci
GHCi, version 8.10.3: https://www.haskell.org/ghc/ :? for help
Prelude> :set -XDerivingVia
Prelude> data Color = Red | Green | Blue deriving Show via Color
Prelude> show Red
"
This is nonsensical and just leads to infinite recursion at runtime.
While there are plenty of other ways one can shoot themselves in their foot, I suggest that we at least notice this case. I just accidentally introduced this into some code at work when I forget to provide a newtype
. I meant to write:
data Foo = ...
deriving (FromField) via ReadShow Foo
but instead wrote
data Foo = ...
deriving (FromField) via Foo
Expected behavior
At least trigger a warning, if there are legitimate reasons we might want to have data Foo = ... deriving C via Foo
. If there are no legitimate reasons to write this, a compile time error would be emitted.
Environment
- GHC version used: 8.10.3