exp/template: do not take address of interface variable to find methods.
Also allow struct values as "with" targets.
R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4809086
diff --git a/src/pkg/exp/template/exec_test.go b/src/pkg/exp/template/exec_test.go
index 415f170..b788474 100644
--- a/src/pkg/exp/template/exec_test.go
+++ b/src/pkg/exp/template/exec_test.go
@@ -43,6 +43,8 @@
Empty2 interface{}
Empty3 interface{}
Empty4 interface{}
+ // Non-empty interface.
+ NonEmptyInterface I
// Pointers
PI *int
PSI *[]int
@@ -69,13 +71,14 @@
{"one": 1, "two": 2},
{"eleven": 11, "twelve": 12},
},
- Empty1: 3,
- Empty2: "empty2",
- Empty3: []int{7, 8},
- Empty4: &U{"UinEmpty"},
- PI: newInt(23),
- PSI: newIntSlice(21, 22, 23),
- Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
+ Empty1: 3,
+ Empty2: "empty2",
+ Empty3: []int{7, 8},
+ Empty4: &U{"UinEmpty"},
+ NonEmptyInterface: new(T),
+ PI: newInt(23),
+ PSI: newIntSlice(21, 22, 23),
+ Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
}
// A non-empty interface.
@@ -358,8 +361,12 @@
// Must separate dot and receiver; otherwise args are evaluated with dot set to variable.
{"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true},
// Do not loop endlessly in indirect for non-empty interfaces.
- // The bug appears with *interface only; this is supposed to fail (cannot invoke Method0), but terminate.
- {"bug1", "{{.Method0}}", "", &iVal, false},
+ // The bug appears with *interface only; looped forever.
+ {"bug1", "{{.Method0}}", "M0", &iVal, true},
+ // Was taking address of interface field, so method set was empty.
+ {"bug2", "{{$.NonEmptyInterface.Method0}}", "M0", tVal, true},
+ // Struct values were not legal in with - mere oversight.
+ {"bug4", "{{with $}}{{.Method0}}{{end}}", "M0", tVal, true},
}
func zeroArgs() string {