From 7aee762f3afe4ff40f7365082d45a32f52328e74 Mon Sep 17 00:00:00 2001 From: Jasper Yujin Geer Date: Wed, 10 Aug 2022 17:39:21 -0700 Subject: Throw error when untyped shift expressions have non-integral type hints --- src/check_expr.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/check_expr.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index b2f3567ba..4dab26572 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -2505,11 +2505,20 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ x->expr->tav.is_lhs = true; } x->mode = Addressing_Value; - if (type_hint && is_type_integer(type_hint)) { - x->type = type_hint; + if (type_hint) { + if (is_type_integer(type_hint)) { + x->type = type_hint; + } else { + gbString expr_str = expr_to_string(node); + gbString to_type = type_to_string(type_hint); + error(node, "Cannot convert untyped expression '%s' to '%s'", expr_str, to_type); + gb_string_free(expr_str); + gb_string_free(to_type); + x->mode = Addressing_Invalid; + return; + } } // x->value = x_val; - return; } } @@ -2522,7 +2531,7 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ // TODO(bill): Should we support shifts for fixed arrays and #simd vectors? if (!is_type_integer(x->type)) { - gbString err_str = expr_to_string(y->expr); + gbString err_str = expr_to_string(x->expr); error(node, "Shift operand '%s' must be an integer", err_str); gb_string_free(err_str); x->mode = Addressing_Invalid; -- cgit v1.2.3 From 5b621d5be18523939ded57cba24b3b3d19b774bd Mon Sep 17 00:00:00 2001 From: Jasper Yujin Geer Date: Wed, 10 Aug 2022 18:07:49 -0700 Subject: More accurate error message --- src/check_expr.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/check_expr.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 4dab26572..83bec20ae 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -2509,10 +2509,10 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ if (is_type_integer(type_hint)) { x->type = type_hint; } else { - gbString expr_str = expr_to_string(node); + gbString x_str = expr_to_string(x->expr); gbString to_type = type_to_string(type_hint); - error(node, "Cannot convert untyped expression '%s' to '%s'", expr_str, to_type); - gb_string_free(expr_str); + error(node, "Conversion of shifted operand '%s' to '%s' is not allowed", x_str, to_type); + gb_string_free(x_str); gb_string_free(to_type); x->mode = Addressing_Invalid; return; -- cgit v1.2.3 From 57dd5ec4db2a8d4ea40a605a885e6a432d3ec568 Mon Sep 17 00:00:00 2001 From: Jasper Yujin Geer Date: Wed, 10 Aug 2022 18:25:29 -0700 Subject: Added back missing return statement --- src/check_expr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/check_expr.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 83bec20ae..f6c94466b 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -2515,10 +2515,10 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ gb_string_free(x_str); gb_string_free(to_type); x->mode = Addressing_Invalid; - return; } } // x->value = x_val; + return; } } -- cgit v1.2.3