fumieval / extensible

Extensible records, variants, structs, effects, tangles
BSD 3-Clause "New" or "Revised" License
128 stars 20 forks source link

build with GHC 9.10.1 #39

Open miguel-negrao opened 5 days ago

miguel-negrao commented 5 days ago

Currently doesn't build with GHC 9.10.1

Building library for extensible-0.9...
src/Data/Extensible/Dictionary.hs:6:14: warning: [GHC-53692] [-Wdeprecated-flags]
    -XTypeInType is deprecated: use -XDataKinds and -XPolyKinds instead
  |
6 | {-# LANGUAGE TypeInType #-}
  |              ^^^^^^^^^^

src/Data/Extensible/Field.hs:7:39: warning: [GHC-53692] [-Wdeprecated-flags]
    -XTypeInType is deprecated: use -XDataKinds and -XPolyKinds instead
  |
7 | {-# LANGUAGE UndecidableSuperClasses, TypeInType #-}
  |                                       ^^^^^^^^^^

[ 1 of 19] Compiling Data.Extensible.Internal.Rig ( src/Data/Extensible/Internal/Rig.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Internal/Rig.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Internal/Rig.dyn_o )
[ 2 of 19] Compiling Data.Extensible.Wrapper ( src/Data/Extensible/Wrapper.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Wrapper.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Wrapper.dyn_o )
[ 3 of 19] Compiling Data.Extensible.Class ( src/Data/Extensible/Class.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Class.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Class.dyn_o )
[ 4 of 19] Compiling Data.Extensible.Sum ( src/Data/Extensible/Sum.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Sum.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Sum.dyn_o )
[ 5 of 19] Compiling Data.Extensible.Struct ( src/Data/Extensible/Struct.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Struct.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Struct.dyn_o )
src/Data/Extensible/Struct.hs:61:1: warning: [GHC-66111] [-Wunused-imports]
    The import of ‘Data.Kind’ is redundant
      except perhaps to import instances from ‘Data.Kind’
    To import instances alone, use: import Data.Kind()
   |
61 | import Data.Kind (Type)
   | ^^^^^^^^^^^^^^^^^^^^^^^

[ 6 of 19] Compiling Data.Extensible.Product ( src/Data/Extensible/Product.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Product.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Product.dyn_o )
[ 7 of 19] Compiling Data.Extensible.Plain ( src/Data/Extensible/Plain.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Plain.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Plain.dyn_o )
[ 8 of 19] Compiling Data.Extensible.Match ( src/Data/Extensible/Match.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Match.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Match.dyn_o )
[ 9 of 19] Compiling Data.Extensible.Inclusion ( src/Data/Extensible/Inclusion.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Inclusion.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Inclusion.dyn_o )
[10 of 19] Compiling Data.Extensible.Nullable ( src/Data/Extensible/Nullable.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Nullable.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Nullable.dyn_o )
[11 of 19] Compiling Data.Extensible.Tangle ( src/Data/Extensible/Tangle.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Tangle.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Tangle.dyn_o )
src/Data/Extensible/Tangle.hs:21:1: warning: [GHC-66111] [-Wunused-imports]
    The import of ‘Control.Applicative’ is redundant
      except perhaps to import instances from ‘Control.Applicative’
    To import instances alone, use: import Control.Applicative()
   |
21 | import Control.Applicative
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^

[12 of 19] Compiling Data.Extensible.Field ( src/Data/Extensible/Field.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Field.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Field.dyn_o )
src/Data/Extensible/Field.hs:244:1: warning: [GHC-90177] [-Worphans]
    Orphan class instance: instance (k ~ l) => IsLabel k (Proxy l)
    Suggested fix:
      Move the instance declaration to the module of the class or of the type, or
      wrap the type with a newtype and declare the instance on the new type.
    |
244 | instance k ~ l => IsLabel k (Proxy l) where
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

[13 of 19] Compiling Data.Extensible.TH ( src/Data/Extensible/TH.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/TH.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/TH.dyn_o )
[14 of 19] Compiling Data.Extensible.Record ( src/Data/Extensible/Record.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Record.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Record.dyn_o )
[15 of 19] Compiling Data.Extensible.Label ( src/Data/Extensible/Label.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Label.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Label.dyn_o )
[16 of 19] Compiling Data.Extensible.GetOpt ( src/Data/Extensible/GetOpt.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/GetOpt.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/GetOpt.dyn_o )
src/Data/Extensible/GetOpt.hs:36:1: warning: [GHC-66111] [-Wunused-imports]
    The import of ‘Data.List’ is redundant
      except perhaps to import instances from ‘Data.List’
    To import instances alone, use: import Data.List()
   |
36 | import Data.List (foldl')
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

[17 of 19] Compiling Data.Extensible.Dictionary ( src/Data/Extensible/Dictionary.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Dictionary.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Dictionary.dyn_o )
[18 of 19] Compiling Data.Extensible.Bits ( src/Data/Extensible/Bits.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Bits.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible/Bits.dyn_o )
src/Data/Extensible/Bits.hs:141:10: error: [GHC-39999]
    • Could not deduce ‘KnownNat (BitWidth (h (TargetOf x)))’
        arising from the superclasses of an instance declaration
      from the context: FromBits r (h (TargetOf x))
        bound by the instance declaration
        at src/Data/Extensible/Bits.hs:141:10-72
      Possible fix:
        If the constraint looks soluble from a superclass of the instance context,
        read 'Undecidable instances and loopy superclasses' in the user manual
    • In the instance declaration for ‘FromBits r (Field h x)’
    |
141 | instance (Bits r, FromBits r (h (TargetOf x))) => FromBits r (Field h x) where
    |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[19 of 19] Compiling Data.Extensible  ( src/Data/Extensible.hs, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible.o, /home/miguel/Development/Haskell/projects/SC/related/extensible/dist-newstyle/build/x86_64-linux/ghc-9.10.1/extensible-0.9/build/Data/Extensible.dyn_o )
Error: [Cabal-7125]
Failed to build extensible-0.9.
miguel-negrao commented 1 day ago

This would fix it:

diff --git a/src/Data/Extensible/Bits.hs b/src/Data/Extensible/Bits.hs
index 51708a9..b44f07b 100644
--- a/src/Data/Extensible/Bits.hs
+++ b/src/Data/Extensible/Bits.hs
@@ -138,7 +138,7 @@ instance FromBits r a => FromBits r (Const a b) where
   fromBits = Const . fromBits
   toBits = toBits . getConst

-instance (Bits r, FromBits r (h (TargetOf x))) => FromBits r (Field h x) where
+instance (Bits r, KnownNat (BitWidth (h (TargetOf x))), FromBits r (h (TargetOf x))) => FromBits r (Field h x) where
   type BitWidth (Field h x) = BitWidth (h (TargetOf x))
   fromBits = Field . fromBits
   toBits = toBits . getField

ChatGPT gives the following explanation to why GHC can no longer deduce KnownNat automatically:

The reason you're encountering this error in GHC 9.10.1, whereas it didn't appear in GHC 9.8 and earlier versions, is due to a change in how GHC handles superclass constraints in instance declarations starting from GHC 9.6 and onwards. This change affects the way GHC infers and requires superclass constraints in the context of instance declarations.

Background:

In earlier versions of GHC (before 9.6), the compiler would automatically expand superclass constraints of constraints present in the instance context. This means that if you had a constraint like FromBits r a in your instance context, GHC would automatically consider its superclass constraints (like KnownNat (BitWidth a)) when resolving instance declarations.

Change in GHC 9.6 and Later:

Starting from GHC 9.6, the compiler's behavior regarding superclass constraints became more conservative to improve type inference reliability and avoid potential issues like infinite loops in the type checker. Specifically, GHC no longer automatically expands superclass constraints of constraints in instance contexts during instance resolution.

Impact on Your Code:

In your code, the FromBits class has a superclass constraint KnownNat (BitWidth a):

class (Bits r, KnownNat (BitWidth a)) => FromBits r a where
  type BitWidth a :: Nat
  fromBits :: r -> a
  toBits :: a -> r

In your instance declaration for FromBits r (Field h x), you have:

instance (Bits r, FromBits r (h (TargetOf x))) => FromBits r (Field h x) where
  type BitWidth (Field h x) = BitWidth (h (TargetOf x))
  fromBits = Field . fromBits
  toBits = toBits . getField

Under GHC 9.8 and earlier, GHC would automatically deduce KnownNat (BitWidth (h (TargetOf x))) from the FromBits r (h (TargetOf x)) constraint because it would expand the superclass constraints of FromBits. However, in GHC 9.10.1, this automatic expansion doesn't happen, so GHC cannot deduce the required KnownNat constraint unless you specify it explicitly.

Why GHC Changed This Behavior:

What You Need to Do:

To fix the issue in GHC 9.10.1, you need to explicitly include the KnownNat constraint in your instance declaration:

instance (Bits r, KnownNat (BitWidth (h (TargetOf x))), FromBits r (h (TargetOf x))) => FromBits r (Field h x) where
  type BitWidth (Field h x) = BitWidth (h (TargetOf x))
  fromBits = Field . fromBits
  toBits = toBits . getField

By adding KnownNat (BitWidth (h (TargetOf x))) to the context, you provide GHC with the necessary constraint that it cannot infer automatically in the newer version.

Summary:

Additional Reference:

For more details on this change, you can refer to the GHC User's Guide section on Superclass constraints. It explains how GHC handles superclass constraints and the rationale behind requiring them to be explicit in certain contexts.

Conclusion:

The change in GHC's behavior regarding superclass constraints in instance declarations is the reason you're seeing this error in GHC 9.10.1. By adjusting your code to include the necessary constraints explicitly, you align with the new requirements and ensure your code remains compatible with future GHC versions.