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:
17
_test/const19.go
Normal file
17
_test/const19.go
Normal 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
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user