Skip to content

Commit

Permalink
Document GHC-02256
Browse files Browse the repository at this point in the history
  • Loading branch information
BinderDavid committed May 16, 2024
1 parent 2574280 commit 18a7b18
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE DuplicateRecordFields #-}
module AmbiguousA where

data Foo = Foo { foo :: Int }

data Bar = Bar { foo :: Int }

Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -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 }
17 changes: 17 additions & 0 deletions message-index/messages/GHC-02256/ambiguous/index.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions message-index/messages/GHC-02256/index.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 18a7b18

Please sign in to comment.