Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate record types to have no duplicate fields. #1352

Closed
wants to merge 1 commit into from

Conversation

mgold
Copy link
Contributor

@mgold mgold commented Apr 19, 2016

This is a redo of #1324: detecting duplicate fields in record types, whether in annotations or aliases. The detection happens during validation, so there's a very nice error and it's possible to get more than one at a time.

Example message:

-- DUPLICATE FIELD ---------------------------------------------------- test.elm

This record type has more than one field named `foo`.

9│ g : List { foo : String, foo : Int } -> Int
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
There can only be one. Do some renaming to make sure the names are distinct!

Notice that it detected the duplicate even when nested in a list in a function, and also provides the exact location. This also works for aliases:

23│ type alias Foo = List { foo : String, foo : Int } -> Int
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^

The implementation is adding another value constructor to Syntax.Error an detecting it in Validate. As we've seen, we need to recursively traverse raw types to ensure there are no invalid records hiding.

Adds two tests. Renames the value constructor for record values with duplicate fields for matching.

Record types (in annotations and aliases) should not have duplicate
fields. This commit adds tests, detects duplicates, and reports them.
@evancz
Copy link
Member

evancz commented Mar 6, 2018

In my development branch, I am getting the following error messages for those cases:

-- NAME CLASH --------------------------------------------------------- temp.elm

This record has multiple `foo` fields.

4| g : List { foo : String, foo : Int } -> Int
              ^^^           ^^^
How can I know which one you want? Rename one of them!
-- NAME CLASH --------------------------------------------------------- temp.elm

This record has multiple `foo` fields.

9| type alias Foo = List { foo : String, foo : Int } -> Int
                           ^^^           ^^^
How can I know which one you want? Rename one of them!

The "duplicate name" errors should be much more precise in general though!

@evancz evancz closed this Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants