fix: correctly return constant expressions in functions

In the case of a function returning a single value, a constant
result could be ignored, and the function would return a zero value.

Fixes #889.
This commit is contained in:
Marc Vertes
2020-10-13 17:02:04 +02:00
committed by GitHub
parent 9491e58920
commit b2b519c2fd
2 changed files with 21 additions and 1 deletions

17
_test/const19.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import (
"fmt"
"time"
)
func get10Hours() time.Duration {
return 10 * time.Hour
}
func main() {
fmt.Println(get10Hours().String())
}
// Output:
// 10h0m0s

View File

@@ -1869,7 +1869,10 @@ func _return(n *node) {
case 0:
n.exec = nil
case 1:
if child[0].kind == binaryExpr || isCall(child[0]) {
// This is an optimisation that is applied for binary expressions or function
// calls, but not for (binary) expressions involving const, as the values are not
// stored in the frame in that case.
if !child[0].rval.IsValid() && child[0].kind == binaryExpr || isCall(child[0]) {
n.exec = nil
} else {
v := values[0]