From 7d61a567d554fa4ca80bd6b42c5247cc16c44776 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 2 Sep 2020 14:48:17 +0300 Subject: [PATCH] compiler: fix a bug with type conversion in switch It was incorrectly parsed as void call. --- pkg/compiler/func_scope.go | 5 +++++ pkg/compiler/switch_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index 2eca590fe..384e14992 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -108,6 +108,11 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool { if ok { c.voidCalls[ce] = false } + case *ast.SwitchStmt: + ce, ok := n.Tag.(*ast.CallExpr) + if ok { + c.voidCalls[ce] = false + } case *ast.CaseClause: for _, e := range n.List { ce, ok := e.(*ast.CallExpr) diff --git a/pkg/compiler/switch_test.go b/pkg/compiler/switch_test.go index ce90ee0c2..73c711b79 100644 --- a/pkg/compiler/switch_test.go +++ b/pkg/compiler/switch_test.go @@ -33,6 +33,21 @@ var switchTestCases = []testCase{ }`, big.NewInt(2), }, + { + "type conversion in tag", + `package main + type state int + func Main() int { + a := 1 + switch state(a) { + case 1: + return 42 + default: + return 11 + } + }`, + big.NewInt(42), + }, { "simple switch fail", `package main