compiler: do not DROP return value with type assertion

The same problem as with IF. Example:

```
func foo() interface{} {
    ...
}

func Main() int{} {
   return foo().(int)    <--- panic here
}
```
This commit is contained in:
Anna Shaleva 2020-10-12 19:00:07 +03:00
parent dbce3c9a19
commit f8d5c40928
2 changed files with 20 additions and 0 deletions

View file

@ -105,6 +105,11 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool {
return false
}
}
case *ast.TypeAssertExpr:
ce, ok := n.X.(*ast.CallExpr)
if ok {
c.voidCalls[ce] = false
}
case *ast.BinaryExpr:
return false
case *ast.RangeStmt:

View file

@ -123,3 +123,18 @@ func TestNamedReturn(t *testing.T) {
t.Run("EmptyReturn", runCase("", big.NewInt(1), big.NewInt(2)))
t.Run("AnotherVariable", runCase("b, c", big.NewInt(2), big.NewInt(3)))
}
func TestTypeAssertReturn(t *testing.T) {
src := `
package main
func foo() interface{} {
return 5
}
func Main() int {
return foo().(int)
}
`
eval(t, src, big.NewInt(5))
}