From 800c7bca41fdaece948da442ab06d9d09f168970 Mon Sep 17 00:00:00 2001 From: TimePath Date: Thu, 14 Apr 2016 17:56:28 +1000 Subject: [PATCH] Invoke a compilation error when the cases of a switch do not match the switched type --- ast.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; -- 2.39.2