Skip to content

Commit 3ff08ca

Browse files
committed
Document GHC-02256
1 parent 2574280 commit 3ff08ca

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE DuplicateRecordFields #-}
2+
module AmbiguousA where
3+
4+
data Foo = Foo { foo :: Int }
5+
6+
data Bar = Bar { foo :: Int }
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module AmbiguousB where
2+
3+
import qualified AmbiguousA as Foo (Foo(..))
4+
import qualified AmbiguousA as Bar (Bar(..))
5+
6+
blah :: Bar.Bar -> Bar.Bar
7+
blah bar = bar { Bar.foo = 5 }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE DuplicateRecordFields #-}
2+
module Ambiguous where
3+
4+
data Foo = Foo { foo :: Int }
5+
6+
data Bar = Bar { foo :: Int }
7+
8+
blah :: Bar -> Bar
9+
blah bar = bar { foo = 5 }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Ambiguous Record Update
3+
---
4+
5+
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`.
6+
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.
7+
8+
```
9+
before/Ambiguous.hs:9:18: warning: [GHC-02256] [-Wambiguous-fields]
10+
The record update bar {foo = 5} with type Bar is ambiguous.
11+
This will not be supported by -XDuplicateRecordFields in future releases of GHC.
12+
|
13+
9 | blah bar = bar { foo = 5 }
14+
|
15+
```
16+
17+
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.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Ambiguous record update
3+
summary: Record update with duplicate field names is ambiguous
4+
severity: warning
5+
flag: -Wambiguous-fields
6+
introduced: 9.6.1
7+
---
8+
9+
With the `DuplicateRecordFields` extension enabled it is possible to use the same field name in multiple records within the same module.
10+
This can lead to problems when using field accessors to access a field of a record.

0 commit comments

Comments
 (0)