Skip to content

Commit

Permalink
fix: correctly return constant expressions in functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored Oct 13, 2020
1 parent 9491e58 commit b2b519c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions _test/const19.go
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b2b519c

Please sign in to comment.