summaryrefslogtreecommitdiff
path: root/src/text
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2021-09-29 10:50:18 -0700
committerIan Lance Taylor <iant@golang.org>2021-09-29 18:38:00 +0000
commit82ac9ab83a47468046bda9bf6c4676a9695fae24 (patch)
tree079eb045ea5cf64a1acfd23c571faf46109afac2 /src/text
parente48cf0db4edb139901946fcee1497bd539229d71 (diff)
downloadgo-git-82ac9ab83a47468046bda9bf6c4676a9695fae24.tar.gz
text/template: check final value for short-circuit and/or
There was a bug in the short-circuit code for and/or added in CL 321490: it ignored the value passed in by an earlier pipeline. For #31103 Change-Id: Ic31f4d7cedfe563ef968cbb712ecfb2413c42eb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/353130 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/text')
-rw-r--r--src/text/template/exec.go5
-rw-r--r--src/text/template/exec_test.go4
2 files changed, 8 insertions, 1 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go
index e03920964e..fce3b0abbf 100644
--- a/src/text/template/exec.go
+++ b/src/text/template/exec.go
@@ -721,9 +721,12 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
for _, arg := range args {
v = s.evalArg(dot, argType, arg).Interface().(reflect.Value)
if truth(v) == (name == "or") {
- break
+ return v
}
}
+ if final != missingVal {
+ v = s.validateType(final, argType)
+ }
return v
}
diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go
index 93fd54e84d..a0432a588d 100644
--- a/src/text/template/exec_test.go
+++ b/src/text/template/exec_test.go
@@ -485,6 +485,10 @@ var execTests = []execTest{
{"and short-circuit", "{{and 1 0 (die)}}", "0", nil, true},
{"or short-circuit2", "{{or 0 0 (die)}}", "", nil, false},
{"and short-circuit2", "{{and 1 1 (die)}}", "", nil, false},
+ {"and pipe-true", "{{1 | and 1}}", "1", nil, true},
+ {"and pipe-false", "{{0 | and 1}}", "0", nil, true},
+ {"or pipe-true", "{{1 | or 0}}", "1", nil, true},
+ {"or pipe-false", "{{0 | or 0}}", "0", nil, true},
{"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true},
{"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true},