From: TimePath Date: Thu, 14 Apr 2016 07:56:28 +0000 (+1000) Subject: Invoke a compilation error when the cases of a switch do not match the switched type X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=refs%2Fheads%2FTimePath%2Fswitch;p=xonotic%2Fgmqcc.git Invoke a compilation error when the cases of a switch do not match the switched type --- diff --git a/ast.cpp b/ast.cpp index cf8ffc7..cb46a3d 100644 --- a/ast.cpp +++ b/ast.cpp @@ -2746,7 +2746,8 @@ bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out) if (m_cases.empty()) return true; - cmpinstr = type_eq_instr[irop->m_vtype]; + const auto switch_type = irop->m_vtype; + cmpinstr = type_eq_instr[switch_type]; if (cmpinstr >= VINSTR_END) { ast_type_to_string(m_operand, typestr, sizeof(typestr)); compile_error(m_context, "invalid type to perform a switch on: %s", typestr); @@ -2771,6 +2772,11 @@ bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out) if (swcase->m_value) { /* A regular case */ + if (swcase->m_value->m_vtype != switch_type) { + ast_type_to_string(swcase->m_value, typestr, sizeof(typestr)); + compile_error(m_context, "invalid type to perform a switch on: %s", typestr); + return false; + } /* generate the condition operand */ if (!swcase->m_value->codegen(func, false, &val)) return false;