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

Duplicate union(enum) variant labels of differing types do not result in an error #730

Closed
tiehuis opened this Issue Jan 30, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@tiehuis
Member

tiehuis commented Jan 30, 2018

const Foo = union(enum) {
    Bar,
    Bar: usize.

    fn go(self: &const Foo) bool {
        switch (*self) {
            Foo.Bar => |inner| {
                return inner == 0;
            },
            else => {
                return false;
            }
        }
    }
};

test "example" {
    const a = Foo.Bar;
    _ = a.go();
}

This compiles with the following error:

$ zig test t.zig
/tmp/t.zig:8:33: error: integer value 0 cannot be implicitly casted to type 'void'
                return inner == 0;
                                ^

We should get a warning regarding the duplicate tagname instead. Reversing the order of declaration makes the following code work. I presume we just are not checking for duplicate tag names and are taking the first present value as the actual.

// This compiles
const Foo = union(enum) {
    Bar: usize,
    Bar,

    fn go(self: &const Foo) bool {
        switch (*self) {
            Foo.Bar => |inner| {
                return inner == 0;
            },
            else => {
                return false;
            }
        }
    }
};

test "example" {
    const a = Foo { .Bar = 0 };
    _ = a.go();
}


Also probably related, but when the tagnames are of identical types.

const Foo = union(enum) {
    Bar,
    Bar,

    fn go(self: &const Foo) void {}
};

test "example" {
    const a = Foo.Bar;
    _ = a.go();
}
$ zig test t.zig
lld: error: undefined symbol: __unnamed_6
>>> referenced by t.zig:8 (/tmp/t.zig:8)
>>>               ./zig-cache/test.o:(example)

lld: error: undefined symbol: __unnamed_6
>>> referenced by test
>>>               ./zig-cache/test.o:(__unnamed_190)

@tiehuis tiehuis added the bug label Jan 30, 2018

@andrewrk andrewrk added this to the 0.2.0 milestone Jan 30, 2018

@andrewrk andrewrk closed this in 3ef6a00 Jan 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment