From 18a7b180204f1b39d7882ff44009deb86f8a9f07 Mon Sep 17 00:00:00 2001 From: David Binder Date: Thu, 16 May 2024 12:19:42 +0200 Subject: [PATCH] Document GHC-02256 --- .../GHC-02256/ambiguous/after/AmbiguousA.hs | 7 +++++++ .../GHC-02256/ambiguous/after/AmbiguousB.hs | 7 +++++++ .../GHC-02256/ambiguous/before/AmbiguousA.hs | 9 +++++++++ .../messages/GHC-02256/ambiguous/index.md | 17 +++++++++++++++++ message-index/messages/GHC-02256/index.md | 10 ++++++++++ 5 files changed, 50 insertions(+) create mode 100644 message-index/messages/GHC-02256/ambiguous/after/AmbiguousA.hs create mode 100644 message-index/messages/GHC-02256/ambiguous/after/AmbiguousB.hs create mode 100644 message-index/messages/GHC-02256/ambiguous/before/AmbiguousA.hs create mode 100644 message-index/messages/GHC-02256/ambiguous/index.md create mode 100644 message-index/messages/GHC-02256/index.md diff --git a/message-index/messages/GHC-02256/ambiguous/after/AmbiguousA.hs b/message-index/messages/GHC-02256/ambiguous/after/AmbiguousA.hs new file mode 100644 index 00000000..250c9133 --- /dev/null +++ b/message-index/messages/GHC-02256/ambiguous/after/AmbiguousA.hs @@ -0,0 +1,7 @@ +{-# LANGUAGE DuplicateRecordFields #-} +module AmbiguousA where + +data Foo = Foo { foo :: Int } + +data Bar = Bar { foo :: Int } + diff --git a/message-index/messages/GHC-02256/ambiguous/after/AmbiguousB.hs b/message-index/messages/GHC-02256/ambiguous/after/AmbiguousB.hs new file mode 100644 index 00000000..62a003a7 --- /dev/null +++ b/message-index/messages/GHC-02256/ambiguous/after/AmbiguousB.hs @@ -0,0 +1,7 @@ +module AmbiguousB where + +import qualified AmbiguousA as Foo (Foo(..)) +import qualified AmbiguousA as Bar (Bar(..)) + +blah :: Bar.Bar -> Bar.Bar +blah bar = bar { Bar.foo = 5 } diff --git a/message-index/messages/GHC-02256/ambiguous/before/AmbiguousA.hs b/message-index/messages/GHC-02256/ambiguous/before/AmbiguousA.hs new file mode 100644 index 00000000..bdc30a2b --- /dev/null +++ b/message-index/messages/GHC-02256/ambiguous/before/AmbiguousA.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE DuplicateRecordFields #-} +module Ambiguous where + +data Foo = Foo { foo :: Int } + +data Bar = Bar { foo :: Int } + +blah :: Bar -> Bar +blah bar = bar { foo = 5 } diff --git a/message-index/messages/GHC-02256/ambiguous/index.md b/message-index/messages/GHC-02256/ambiguous/index.md new file mode 100644 index 00000000..efcb3957 --- /dev/null +++ b/message-index/messages/GHC-02256/ambiguous/index.md @@ -0,0 +1,17 @@ +--- +title: Ambiguous Record Update +--- + +In this example we declare two records `Foo` and `Bar` which each contain a field with the same name `foo`. In the function `blah` we use this name to update the field in a record of type `Bar`. +The compiler can successfully detect that the field `foo` that we mention in the record update can only refer to the field of the record `Bar`, but the mechanism that GHC uses for this will be deprecated in a future release, and for this reason a warning is emitted. + +``` +before/Ambiguous.hs:9:18: warning: [GHC-02256] [-Wambiguous-fields] + The record update bar {foo = 5} with type Bar is ambiguous. + This will not be supported by -XDuplicateRecordFields in future releases of GHC. + | +9 | blah bar = bar { foo = 5 } + | +``` + +In order to make your code compile without any warnings you can put the declarations of both records in a separate module, and use qualified imports and qualified names to make it unambiguous which record field you mean. diff --git a/message-index/messages/GHC-02256/index.md b/message-index/messages/GHC-02256/index.md new file mode 100644 index 00000000..cfe7fd26 --- /dev/null +++ b/message-index/messages/GHC-02256/index.md @@ -0,0 +1,10 @@ +--- +title: Ambiguous record update +summary: Record update with duplicate field names is ambiguous +severity: warning +flag: -Wambiguous-fields +introduced: 9.6.1 +--- + +With the `DuplicateRecordFields` extension enabled it is possible to use the same field name in multiple records within the same module. +This can lead to problems when using field accessors to access a field of a record.