morganstanley / hobbes

A language and an embedded JIT compiler
http://hobbes.readthedocs.io/
Apache License 2.0
1.16k stars 105 forks source link

It is possible to create a record with duplicate field names #355

Closed adam-antonik closed 4 years ago

adam-antonik commented 4 years ago
double :: (x={lbl:h*()}, {lbl:h*x}=y) => x -> y
double = newPrim()

>:t double({a=1})
{ a:int, a:int }

This example segfaults straightaway when called, but a slightly more convoluted version can actually get us an object with duplicated fields. Would it be possible for this to be caught as a unification error in type-checking?

kthielen commented 4 years ago

Oh that's a good one! I had accounted for this in one place, but obviously not deeply enough. You're right, we need to reject the construction of record types with duplicate field names entirely (however they're arrived at). It should never get to the stage where a user sees {a:int,a:int}.

kthielen commented 4 years ago

OK, I fixed this here: https://github.com/Morgan-Stanley/hobbes/pull/360

And I added some tests to verify it. Now it won't be possible to construct record or variant types with duplicate names, no matter how roundabout the process of deciding those names is.

Thanks for pointing this out.

kthielen commented 4 years ago

Thanks adam, I've got this fix merged to master and added some tests to verify that such types (records and variants with duplicate field/constructor names) are rejected.