Fix #345: Panic when using enum as map key

Also add a little map demo.
This commit is contained in:
Jeroen van Rijn
2019-03-02 13:21:01 +01:00
parent a126d2ba16
commit b6d6eb6ae2
2 changed files with 58 additions and 3 deletions

View File

@@ -530,7 +530,7 @@ parametric_polymorphism :: proc() {
// `I` is the type of N
// `T` is the type passed
fmt.printf("Generating an array of type %v from the value %v of type %v\n",
typeid_of(type_of(res)), N, typeid_of(I));
typeid_of(type_of(res)), N, typeid_of(I));
for i in 0..N-1 {
res[i] = T(i*i);
}
@@ -691,6 +691,61 @@ using_enum :: proc() {
fmt.println(len(Foo));
}
map_type :: proc() {
fmt.println("# map type");
// enums of type u16, u32, i16 & i32 also work
Enum_u8 :: enum u8 {
A = 0,
B = 1 << 8 - 1,
}
Enum_u64 :: enum u64 {
A = 0,
B = 1 << 64 - 1,
}
Enum_i32 :: enum i32 {
A = 0,
B = -(1 << 31),
}
Enum_i64 :: enum i64 {
A = 0,
B = -(1 << 63),
}
map_u8: map[Enum_u8]u8;
map_u8[Enum_u8.A] = u8(Enum_u8.B);
assert(map_u8[Enum_u8.A] == u8(Enum_u8.B));
fmt.println(map_u8);
map_u64: map[Enum_u64]u64;
map_u64[Enum_u64.A] = u64(Enum_u64.B);
assert(map_u64[Enum_u64.A] == u64(Enum_u64.B));
fmt.println(map_u64);
map_i8: map[Enum_i8]i8;
map_i8[Enum_i8.A] = i8(Enum_i8.B);
assert(map_i8[Enum_i8.A] == i8(Enum_i8.B));
fmt.println(map_i8);
map_i64: map[Enum_i64]i64;
map_i64[Enum_i64.A] = i64(Enum_i64.B);
assert(map_i64[Enum_i64.A] == i64(Enum_i64.B));
fmt.println(map_i64);
demo_struct :: struct {
member: Enum_i64,
}
map_string: map[string]demo_struct;
map_string["Hellope!"] = demo_struct{Enum_i64.B};
assert(map_string["Hellope!"].member == Enum_i64.B);
assert("Hellope?" notin map_string);
fmt.println(map_string);
fmt.println("Hellope! in map_string:", "Hellope!" in map_string);
fmt.println("Hellope? in map_string:", "Hellope?" in map_string);
}
explicit_procedure_overloading :: proc() {
fmt.println("# explicit procedure overloading");
@@ -884,6 +939,7 @@ main :: proc() {
array_programming();
named_proc_return_parameters();
using_enum();
map_type();
explicit_procedure_overloading();
complete_switch();
cstring_example();

View File

@@ -3216,8 +3216,7 @@ irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
if (is_type_integer(t)) {
ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), ir_emit_conv(proc, key, hash_type));
} else if (is_type_enum(t)) {
irValue *e = ir_emit_bitcast(proc, key, t_uint);
ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), ir_emit_conv(proc, e, hash_type));
ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), ir_emit_conv(proc, key, hash_type));
} else if (is_type_typeid(t)) {
irValue *i = ir_emit_bitcast(proc, key, t_uint);
ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), ir_emit_conv(proc, i, hash_type));