[Git][ghc/ghc][wip/sand-witch/flip-set-field] Flip the order of arguments of setField (24668)

Andrei Borzenkov (@sand-witch) gitlab at gitlab.haskell.org
Fri Dec 13 14:05:23 UTC 2024



Andrei Borzenkov pushed to branch wip/sand-witch/flip-set-field at Glasgow Haskell Compiler / GHC


Commits:
f64c8e1f by Andrei Borzenkov at 2024-12-13T18:05:05+04:00
Flip the order of arguments of setField (24668)

GHC Proposal 583 "HasField redesign" specifies the
order of a setField function as this:

  setField :: forall fld a b. SetField fld a b. b -> a -> a

This patch flips the application order to match the spec.

- - - - -


6 changed files:

- compiler/GHC/Rename/Expr.hs
- docs/users_guide/9.14.1-notes.rst
- docs/users_guide/exts/overloaded_record_update.rst
- testsuite/tests/parser/should_fail/RecordDotSyntaxFail10.hs
- testsuite/tests/parser/should_fail/RecordDotSyntaxFail13.hs
- testsuite/tests/parser/should_run/RecordDotSyntax1.hs


Changes:

=====================================
compiler/GHC/Rename/Expr.hs
=====================================
@@ -2844,10 +2844,11 @@ mkGetField :: Name -> LHsExpr GhcRn -> LocatedAn NoEpAnns FieldLabelString -> Hs
 mkGetField get_field arg field = unLoc (head $ mkGet get_field [arg] field)
 
 -- mkSetField a field b calculates a set_field @field expression.
--- e.g mkSetSetField a field b = set_field @"field" a b (read as "set field 'field' on a to b").
+-- e.g mkSetSetField a field b = set_field @"field" a b (read as "set field 'field' to a on b").
+-- NB: the order of aruments is specified by GHC Proposal 583: HasField redesign.
 mkSetField :: Name -> LHsExpr GhcRn -> LocatedAn NoEpAnns FieldLabelString -> LHsExpr GhcRn -> HsExpr GhcRn
 mkSetField set_field a (L _ (FieldLabelString field)) b =
-  genHsApp (genHsApp (genHsVar set_field `genAppType` genHsTyLit field)  a) b
+  genHsApp (genHsApp (genHsVar set_field `genAppType` genHsTyLit field) b) a
 
 mkGet :: Name -> [LHsExpr GhcRn] -> LocatedAn NoEpAnns FieldLabelString -> [LHsExpr GhcRn]
 mkGet get_field l@(r : _) (L _ (FieldLabelString field)) =


=====================================
docs/users_guide/9.14.1-notes.rst
=====================================
@@ -12,7 +12,7 @@ Language
 ~~~~~~~~
 
 * ``-Wincomplete-record-selectors`` is now part of `-Wall`, as specified
-  by `GHC Proposal 516: add warning for incomplete record selectors <https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0516-incomplete-record-selectors.rst>_`.
+  by `GHC Proposal 516: add warning for incomplete record selectors <https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0516-incomplete-record-selectors.rst>`_.
   Hence, if a library is compiled with ``-Werror``, compilation may now fail. Solution: fix the library.
   Workaround: add ``-Werror=no-incomplete-record-selectors``.
 
@@ -25,6 +25,19 @@ Language
   :extension:`TypeAbstractions`. The warning flag``deprecated-type-abstractions``
   has also been removed from the compiler.
 
+* :extension:`OverloadedRecordUpdate` now passes the arguments to a ``setField`` function
+  in the flipped order, as specicfied by `GHC Proposal 583: HasField redesign <https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0583-hasfield-redesign.rst>`_.
+
+  Previously GHC expected ``setField`` to have this type: ::
+
+    setField :: forall (fld :: Symbol) a r. r -> a -> r
+
+  And that's what GHC expects now: ::
+
+    setField :: forall (fld :: Symbol) a r. a -> r -> r
+
+  That will break the combination of :extension:`OverloadedRecordUpdate` with :extension:`RebindableSyntax`.
+
 Compiler
 ~~~~~~~~
 


=====================================
docs/users_guide/exts/overloaded_record_update.rst
=====================================
@@ -30,8 +30,8 @@ Example:
 
   getField :: forall x r a . HasField x r a => r -> a
   getField = snd . hasField @x -- Note: a.x = is getField @"x" a.
-  setField :: forall x r a . HasField x r a => r -> a -> r
-  setField = fst . hasField @x -- Note : a{x = b} is setField @"x" a b.
+  setField :: forall x r a . HasField x r a => a -> r -> r
+  setField b a = fst (hasField @x a) b -- Note : a{x = b} is setField @"x" b a.
 
   data Person = Person { name :: String } deriving Show
   instance HasField "name" Person String where


=====================================
testsuite/tests/parser/should_fail/RecordDotSyntaxFail10.hs
=====================================
@@ -11,8 +11,8 @@ class HasField x r a | x r -> a where
 getField :: forall x r a . HasField x r a => r -> a
 getField = snd . hasField @x -- Note: a.x = is getField @"x" a.
 
-setField :: forall x r a . HasField x r a => r -> a -> r
-setField = fst . hasField @x -- Note : a{x = b} is setField @"x" a b.
+setField :: forall x r a . HasField x r a => a -> r -> r
+setField b a = fst (hasField @x a) b -- Note : a{x = b} is setField @"x" b a.
 
 -- 'Foo' has 'foo' field of type 'Bar'
 data Foo = Foo { foo :: Bar } deriving (Show, Eq)


=====================================
testsuite/tests/parser/should_fail/RecordDotSyntaxFail13.hs
=====================================
@@ -13,8 +13,8 @@ class HasField x r a | x r -> a where
 getField :: forall x r a . HasField x r a => r -> a
 getField = snd . hasField @x -- Note: a.x = is getField @"x" a.
 
-setField :: forall x r a . HasField x r a => r -> a -> r
-setField = fst . hasField @x -- Note : a{x = b} is setField @"x" a b.
+setField :: forall x r a . HasField x r a => a -> r -> r
+setField b a = fst (hasField @x a) b -- Note : a{x = b} is setField @"x" b a.
 
 -- 'Foo' has 'foo' field of type 'Int'
 data Foo = Foo { foo :: Int } deriving (Show, Eq)


=====================================
testsuite/tests/parser/should_run/RecordDotSyntax1.hs
=====================================
@@ -21,8 +21,8 @@ class HasField x r a | x r -> a where
 getField :: forall x r a . HasField x r a => r -> a
 getField = snd . hasField @x -- Note: a.x = is getField @"x" a.
 
-setField :: forall x r a . HasField x r a => r -> a -> r
-setField = fst . hasField @x -- Note : a{x = b} is setField @"x" a b.
+setField :: forall x r a . HasField x r a => a -> r -> r
+setField b a = fst (hasField @x a) b -- Note : a{x = b} is setField @"x" b a.
 
 -- 'Foo' has 'foo' field of type 'Bar'
 data Foo = Foo { foo :: Bar } deriving (Show, Eq)



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f64c8e1fbb00c228f166a9ac28e520c1b9d9642b

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f64c8e1fbb00c228f166a9ac28e520c1b9d9642b
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20241213/48121f97/attachment-0001.html>


More information about the ghc-commits mailing list