#bug in `switch` or in MultiArrayList?

1 messages Β· Page 1 of 1 (latest)

somber widget
#

ive just found a pretty bizarre bug and im not sure if its in MultiArrayList or the compiler

const Foo = union(enum) {
    a: u32,
    b: usize,
};
var list = std.MultiArrayList(Foo){};
try list.append(std.heap.page_allocator, .{ .a = 69 });
switch (list.get(0)) {
    .a, .b => |ab| std.debug.print("{}\n", .{ab}),
}

naturally this prints 69 as you'd expect, but if you just flip the order of the switch cases

switch (list.get(0)) {
    .b, .a => |ab| std.debug.print("{}\n", .{ab}),
}

it now prints 139899969732677

#

noticed that in the first one ab is resolved as a u32 but in the second its a usize, ig for some reason it just chooses the first one it sees which causes a bunch of random bits to be read?

#

similar thing happens if you init the union as a b thats out of range of a u32 then swap the cases

#

goes away if you inline .b, .a which makes sense

potent burrow
#

it's supposed to error without inline when the payload types don't match

somber widget
#

yeah i knew that for incompatible types e.g. if either a or b is signed. guess someone forgot to add this error since generally u32 and usize work together :p

gray gulch
#

you mind making an issue about it. i would love to solve this and pr πŸ˜„

somber widget
#

sure

gray gulch
#

πŸ™

somber widget
#

second useful thing sinon has done πŸ™ πŸ™

gray gulch
#

truly