Skip to content

Commit 3776465

Browse files
dmitshurgopherbot
authored andcommitted
text/template: clarify error when too few or too many return values
Prior to CL 561115, calling a function without any return values would print "function called with 0 args; should be 1 or 2". Afterwards, the error message became "too many return values". Keep the improvement of referring to return values rather than args, and bring back clarity about their actual and permitted numbers. Change-Id: I2c014e4633208cc7052fac265a995a8f2fe68151 Reviewed-on: https://go-review.googlesource.com/c/go/+/588355 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a2eae66 commit 3776465

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/text/template/exec_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ type T struct {
8181
NilOKFunc func(*int) bool
8282
ErrFunc func() (string, error)
8383
PanicFunc func() string
84-
InvalidReturnCountFunc func() (string, error, int)
84+
TooFewReturnCountFunc func()
85+
TooManyReturnCountFunc func() (string, error, int)
8586
InvalidReturnTypeFunc func() (string, bool)
8687
// Template to test evaluation of templates.
8788
Tmpl *Template
@@ -170,7 +171,8 @@ var tVal = &T{
170171
NilOKFunc: func(s *int) bool { return s == nil },
171172
ErrFunc: func() (string, error) { return "bla", nil },
172173
PanicFunc: func() string { panic("test panic") },
173-
InvalidReturnCountFunc: func() (string, error, int) { return "", nil, 0 },
174+
TooFewReturnCountFunc: func() {},
175+
TooManyReturnCountFunc: func() (string, error, int) { return "", nil, 0 },
174176
InvalidReturnTypeFunc: func() (string, bool) { return "", false },
175177
Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
176178
}
@@ -1746,16 +1748,22 @@ func TestFunctionCheckDuringCall(t *testing.T) {
17461748
wantErr: "error calling call: wrong number of args for .VariadicFuncInt: got 0 want at least 1",
17471749
},
17481750
{
1749-
name: "call invalid return number func",
1750-
input: `{{call .InvalidReturnCountFunc}}`,
1751+
name: "call too few return number func",
1752+
input: `{{call .TooFewReturnCountFunc}}`,
17511753
data: tVal,
1752-
wantErr: "error calling call: too many return values for .InvalidReturnCountFunc",
1754+
wantErr: "error calling call: function .TooFewReturnCountFunc has 0 return values; should be 1 or 2",
1755+
},
1756+
{
1757+
name: "call too many return number func",
1758+
input: `{{call .TooManyReturnCountFunc}}`,
1759+
data: tVal,
1760+
wantErr: "error calling call: function .TooManyReturnCountFunc has 3 return values; should be 1 or 2",
17531761
},
17541762
{
17551763
name: "call invalid return type func",
17561764
input: `{{call .InvalidReturnTypeFunc}}`,
17571765
data: tVal,
1758-
wantErr: "error calling call: invalid function signature for .InvalidReturnTypeFunc: second argument should be error; is bool",
1766+
wantErr: "error calling call: invalid function signature for .InvalidReturnTypeFunc: second return value should be error; is bool",
17591767
},
17601768
{
17611769
name: "call pipeline",

src/text/template/funcs.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,16 @@ func addFuncs(out, in FuncMap) {
110110

111111
// goodFunc reports whether the function or method has the right result signature.
112112
func goodFunc(name string, typ reflect.Type) error {
113-
numOut := typ.NumOut()
114-
115113
// We allow functions with 1 result or 2 results where the second is an error.
116-
switch {
114+
switch numOut := typ.NumOut(); {
117115
case numOut == 1:
118116
return nil
119117
case numOut == 2 && typ.Out(1) == errorType:
120118
return nil
121119
case numOut == 2:
122-
return fmt.Errorf("invalid function signature for %s: second argument should be error; is %s", name, typ.Out(1))
120+
return fmt.Errorf("invalid function signature for %s: second return value should be error; is %s", name, typ.Out(1))
123121
default:
124-
return fmt.Errorf("too many return values for %s", name)
122+
return fmt.Errorf("function %s has %d return values; should be 1 or 2", name, typ.NumOut())
125123
}
126124
}
127125

0 commit comments

Comments
 (0)