[Haskell-cafe] application of the record field may fail

Johannes Waldmann johannes.waldmann at htwk-leipzig.de
Fri May 9 13:54:48 UTC 2025


Dear Cafe,

(ignore following email, but perhaps the observation is still useful,
the answer is to use `-Weverything`, which gives more than `-Wall`. Obviously.)


For two constructors with fields of identical type but different name,

data T = A {foo::()} | B {f00 ::()}

GHC detects a typo in the field name

ghci> f x = case x of A{f00 = f} -> f ; B {} -> ()
<interactive>:2:19: error: [GHC-53822]
     • Constructor ‘A’ does not have field ‘f00’

Good. Now changing the program slightly,

ghci> f x = case x of A{} -> f00 x ; B {} -> ()

the program becomes statically correct.
Can we get a warning here? (to detect the typo).

This example is close to real code,
not  foo  and  f00  but  something_longer  and  something_longerr.
Yes I shoudn't do this, and make names more descriptive/distinct.
And types! It's only a typo because it's not a type error.


[EDIT] the answer is yes, we can, with -Weverything,

<interactive>:9:24: warning: [GHC-17335] [-Wincomplete-record-selectors]
     The application of the record field ‘f00’ may fail for the following constructors: A

this sounds over-cautious, because it _will_ fail (if evaluated at all).


- J.W.



More information about the Haskell-Cafe mailing list