From 4f8c26d2c605f24cdeb1a4c7154662b2552640ef Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 8 Nov 2017 21:44:10 -0500 Subject: [PATCH] fix enum sizes too large closes #598 --- src/analyze.cpp | 2 +- src/codegen.cpp | 13 ++++++++--- test/cases/enum.zig | 54 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 0dc221408dfd..7d46ebc90863 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1389,7 +1389,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) { return; } - TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count); + TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1); TypeTableEntry *tag_type_entry = create_enum_tag_type(g, enum_type, tag_int_type); enum_type->data.enumeration.tag_type = tag_type_entry; diff --git a/src/codegen.cpp b/src/codegen.cpp index 976b20405e18..38a1a2cbeac0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2666,9 +2666,16 @@ static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, IrExecutable *executable if (ir_want_debug_safety(g, &instruction->base)) { TypeTableEntry *enum_type = enum_tag_type->data.enum_tag.enum_type; size_t field_count = enum_type->data.enumeration.src_field_count; - LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(enum_tag_value)); - LLVMValueRef end_val = LLVMConstInt(LLVMTypeOf(enum_tag_value), field_count, false); - add_bounds_check(g, enum_tag_value, LLVMIntUGE, zero, LLVMIntULT, end_val); + + // if the field_count can't fit in the bits of the enum_tag_type, then it can't possibly + // be the wrong value + BigInt field_bi; + bigint_init_unsigned(&field_bi, field_count); + TypeTableEntry *tag_int_type = enum_tag_type->data.enum_tag.int_type; + if (bigint_fits_in_bits(&field_bi, tag_int_type->data.integral.bit_count, false)) { + LLVMValueRef end_val = LLVMConstInt(LLVMTypeOf(enum_tag_value), field_count, false); + add_bounds_check(g, enum_tag_value, LLVMIntEQ, nullptr, LLVMIntULT, end_val); + } } LLVMValueRef indices[] = { diff --git a/test/cases/enum.zig b/test/cases/enum.zig index 492d450fd7c3..f2d8ded0d6a3 100644 --- a/test/cases/enum.zig +++ b/test/cases/enum.zig @@ -136,3 +136,57 @@ const AlignTestEnum = enum { A: [9]u8, B: u64, }; + +const ValueCount0 = enum {}; +const ValueCount1 = enum { I0 }; +const ValueCount2 = enum { I0, I1 }; +const ValueCount256 = enum { + I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, + I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31, + I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47, + I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63, + I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79, + I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95, + I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109, + I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123, + I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137, + I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151, + I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165, + I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179, + I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193, + I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207, + I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221, + I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235, + I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249, + I250, I251, I252, I253, I254, I255 +}; +const ValueCount257 = enum { + I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, + I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31, + I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47, + I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63, + I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79, + I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95, + I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109, + I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123, + I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137, + I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151, + I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165, + I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179, + I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193, + I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207, + I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221, + I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235, + I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249, + I250, I251, I252, I253, I254, I255, I256 +}; + +test "enum sizes" { + comptime { + assert(@sizeOf(ValueCount0) == 0); + assert(@sizeOf(ValueCount1) == 0); + assert(@sizeOf(ValueCount2) == 1); + assert(@sizeOf(ValueCount256) == 1); + assert(@sizeOf(ValueCount257) == 2); + } +}