purescript / purescript-record

Functions for working with records and polymorphic labels
BSD 3-Clause "New" or "Revised" License
70 stars 31 forks source link

Bug: `union` causes type confusion #59

Closed maurges closed 4 years ago

maurges commented 4 years ago

Using "purescript": "^0.13.6" and package set psc-0.13.6-20200423

Here's my foot gun:

main :: Effect Unit
main = do
    Console.log "1"
    let x = union {y: 5} {y: "5"}
    Console.log "2"
    let s = show x
    Console.log "3"
    Console.log s

Causes TypeError: s.replace is not a function after logging 2.

Changing to let x = union {y: "5"} {y: 5} gets rid of error and produces expected representation: { y: "5", y: 5 }

maurges commented 4 years ago

Well, "expected" is a bad word, since I was expecting it to fail in all cases with duplicated field names with different types. It also does fail with something like union {y: {y: "y"}} {y: "y"} and produces garbage with union {y: {y: "y"}} {y: 5}

natefaubion commented 4 years ago

I don't think this is a bug in union per se. This bug happens because the Show implementation for records uses unsafeGet indiscriminately from a RowList, and does not consider whether there are duplicates.

hdgarrood commented 4 years ago

That seems accurate to me. Shall we move this to prelude then?