Skip to content

Commit 35f4d3d

Browse files
tbidneBinderDavid
authored andcommitted
Add NoFieldSelectors example to GHC-88464
1 parent 6050f8a commit 35f4d3d

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE NoFieldSelectors #-}
2+
3+
module A (Foo (..)) where
4+
5+
data Foo = MkFoo { fint :: Int, fchar :: Char }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Example3 where
2+
3+
import A (Foo (MkFoo, fint, fchar))
4+
5+
getFooInt :: Foo -> Int
6+
getFooInt (MkFoo i _) = i -- this is fine
7+
8+
-- Creation and updates below are fine
9+
10+
mkFoo :: Foo
11+
mkFoo =
12+
MkFoo
13+
{ fint = 0,
14+
fchar = 'a'
15+
}
16+
17+
setFooInt :: Foo -> Int -> Foo
18+
setFooInt f x = f { fint = x }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE NoFieldSelectors #-}
2+
3+
module A (Foo (..)) where
4+
5+
data Foo = MkFoo { fint :: Int, fchar :: Char }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Example3 where
2+
3+
import A (Foo (MkFoo, fint, fchar))
4+
5+
getFooInt :: Foo -> Int
6+
getFooInt = fint -- this is the problem
7+
8+
-- Creation and updates below are fine
9+
10+
mkFoo :: Foo
11+
mkFoo =
12+
MkFoo
13+
{ fint = 0,
14+
fchar = 'a'
15+
}
16+
17+
setFooInt :: Foo -> Int -> Foo
18+
setFooInt f x = f { fint = x }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Attempted to use a field selector disabled with NoFieldSelectors
3+
---
4+
5+
## Error Message
6+
7+
```
8+
Example3.hs:14:13: error: [GHC-88464]
9+
Variable not in scope: fint :: Foo -> Int
10+
Suggested fix:
11+
Notice that ‘fint’ is a field selector belonging to the type ‘Foo’
12+
that has been suppressed by NoFieldSelectors.
13+
|
14+
14 | getFooInt = fint
15+
| ^^^^
16+
```
17+
18+
## Description
19+
20+
This example attempts to use the field selector `fint`, despite it being disabled at the definition site (`A.hs`) with `NoFieldSelectors`. This fix is to use pattern matching instead.
21+
22+
Notice that record creation and updates still work with `NoFieldSelectors`.

0 commit comments

Comments
 (0)