Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have a warning about the GeneralizedNewtypeDeriving extension when using the Safe extension if I do not use the first one?

Tags:

haskell

I enabled the Safe extension, and now I have this warning:

<no location info>: warning: [GHC-98887]
    -XGeneralizedNewtypeDeriving is not allowed in Safe Haskell; ignoring -XGeneralizedNewtypeDeriving

However, I don't use that in the first place.

like image 946
Seeker Avatar asked Jan 25 '26 21:01

Seeker


1 Answers

Starting with GHC 9.2.1, the GHC2021 language variant was made the default. It enabled a large set of extensions, including GeneralizedNewtypeDeriving.

Since GeneralizedNewtypeDeriving conflicts with the Safe extension, if you enable Safe with these newer GHC versions then you get a warning and GeneralizedNewtypeDeriving ends up being disabled.

This was actually documented in the 9.2.1 release notes but disappeared in later version of the documentation. The advice given in the release notes is:

Because GHC2021 includes GeneralizedNewtypeDeriving, which is not safe for Safe Haskell, users of Safe Haskell are advised to use Haskell2010 explicitly.

So, that's one solution for getting rid of the warning. You can compile with the GHC -XHaskell2010 flag, add a default-language: Haskell2010 clause to your .cabal file, or add a pragma at the top of your module, like so:

{-# LANGUAGE Haskell2010 #-}
{-# LANGUAGE Safe #-}

module Okay where
...

You can also take the more conservative approach of keeping the rest of GHC2021 but disabling GeneralizedNewtypeDeriving using:

{-# LANGUAGE Safe #-}
{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}

module Unsafe where
...

The order of the two pragmas doesn't matter, so use whichever you prefer.

like image 158
K. A. Buhr Avatar answered Jan 28 '26 23:01

K. A. Buhr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!