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

Field names should be rejected if they duplicate another field name #123

Closed
chwarr opened this issue Jan 19, 2016 · 2 comments
Closed

Field names should be rejected if they duplicate another field name #123

chwarr opened this issue Jan 19, 2016 · 2 comments
Labels

Comments

@chwarr
Copy link
Member

chwarr commented Jan 19, 2016

The following .bond file should fail gbc code generation, as it contains two fields with the same name.

namespace duplicate_field_names;

struct ShouldBeRejected
{
    0: bool DuplicateFieldName;
    1: bool DuplicateFieldName;
}

However, Bond Compiler 0.4.0.1 accepts this .bond file and generates C++ and C# code that does not compile.

Here's an excerpt of the generated C# code.

namespace duplicate_field_names
{
    using System.Collections.Generic;

    [global::Bond.Schema]
    [System.CodeDom.Compiler.GeneratedCode("gbc", "0.4.0.1")]
    public partial class ShouldBeRejected
    {
        [global::Bond.Id(0)]
        public bool DuplicateFieldName { get; set; }

        [global::Bond.Id(1)]
        public bool DuplicateFieldName { get; set; }

        public ShouldBeRejected()
            : this("duplicate_field_names.ShouldBeRejected", "ShouldBeRejected")
        {}

        protected ShouldBeRejected(string fullName, string name)
        {

        }
    }
} // duplicate_field_names
@chwarr chwarr added the bug label Jan 19, 2016
@lalo
Copy link
Member

lalo commented Jan 25, 2016

For this one I was thinking of this simple change in Parser.h line 235:

    unique p = do
        fields' <- p
        case findDuplicates fields' fieldOrdinal ++ findDuplicates fields' fieldName of
            [] -> return fields'
            Field {..}:_ -> fail $ "Duplicate definition of the field with ordinal " ++ show fieldOrdinal ++
                " and field name " ++ show fieldName
      where
        findDuplicates xs ft = deleteFirstsBy (ordinal ft) xs (nubBy (ordinal ft) xs)
        ordinal ft = (==) `on` ft

The bad thing is that the error message will display both values as possible duplications when only one is duplicated. If we want a more specific error message for each case I can refactor it in some other way that allows this.

@chwarr
Copy link
Member Author

chwarr commented Jan 26, 2016

If we change the message to "Duplicate definition of the field with either ordinal 0 or field name foo", the text will cover both cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants