Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for Go 1.16 #8015

Merged
merged 9 commits into from
Feb 18, 2021
Next Next commit
tpl/internal: Synch Go templates fork with Go 1.16dev
bep committed Feb 18, 2021

Verified

This commit was signed with the committer’s verified signature.
bep Bjørn Erik Pedersen
commit 5daa64aeeee580179f7bc26fa17e1c6e07a2c90a
2 changes: 1 addition & 1 deletion scripts/fork_go_templates/main.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ import (

func main() {
// TODO(bep) git checkout tag
// The current is built with Go version b68fa57c599720d33a2d735782969ce95eabf794 / go1.15dev
// The current is built with Go version da54dfb6a1f3bef827b9ec3780c98fde77a97d11 / go1.16dev
fmt.Println("Forking ...")
defer fmt.Println("Done ...")

1 change: 1 addition & 0 deletions tpl/internal/go_templates/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ const KnownEnv = `
GOSUMDB
GOTMPDIR
GOTOOLDIR
GOVCS
GOWASM
GO_EXTLINK_ENABLED
PKG_CONFIG
2 changes: 1 addition & 1 deletion tpl/internal/go_templates/fmtsort/sort.go
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ func compare(aVal, bVal reflect.Value) int {
default:
return -1
}
case reflect.Ptr:
case reflect.Ptr, reflect.UnsafePointer:
a, b := aVal.Pointer(), bVal.Pointer()
switch {
case a < b:
22 changes: 22 additions & 0 deletions tpl/internal/go_templates/fmtsort/sort_test.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
"reflect"
"strings"
"testing"
"unsafe"
)

var compareTests = [][]reflect.Value{
@@ -32,6 +33,7 @@ var compareTests = [][]reflect.Value{
ct(reflect.TypeOf(complex128(0+1i)), -1-1i, -1+0i, -1+1i, 0-1i, 0+0i, 0+1i, 1-1i, 1+0i, 1+1i),
ct(reflect.TypeOf(false), false, true),
ct(reflect.TypeOf(&ints[0]), &ints[0], &ints[1], &ints[2]),
ct(reflect.TypeOf(unsafe.Pointer(&ints[0])), unsafe.Pointer(&ints[0]), unsafe.Pointer(&ints[1]), unsafe.Pointer(&ints[2])),
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
@@ -118,6 +120,10 @@ var sortTests = []sortTest{
pointerMap(),
"PTR0:0 PTR1:1 PTR2:2",
},
{
unsafePointerMap(),
"UNSAFEPTR0:0 UNSAFEPTR1:1 UNSAFEPTR2:2",
},
{
map[toy]string{{7, 2}: "72", {7, 1}: "71", {3, 4}: "34"},
"{3 4}:34 {7 1}:71 {7 2}:72",
@@ -159,6 +165,14 @@ func sprintKey(key reflect.Value) string {
}
}
return "PTR???"
case "unsafe.Pointer":
ptr := key.Interface().(unsafe.Pointer)
for i := range ints {
if ptr == unsafe.Pointer(&ints[i]) {
return fmt.Sprintf("UNSAFEPTR%d", i)
}
}
return "UNSAFEPTR???"
case "chan int":
c := key.Interface().(chan int)
for i := range chans {
@@ -185,6 +199,14 @@ func pointerMap() map[*int]string {
return m
}

func unsafePointerMap() map[unsafe.Pointer]string {
m := make(map[unsafe.Pointer]string)
for i := 2; i >= 0; i-- {
m[unsafe.Pointer(&ints[i])] = fmt.Sprint(i)
}
return m
}

func chanMap() map[chan int]string {
m := make(map[chan int]string)
for i := 2; i >= 0; i-- {
16 changes: 8 additions & 8 deletions tpl/internal/go_templates/htmltemplate/clone_test.go
Original file line number Diff line number Diff line change
@@ -10,15 +10,15 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"strings"
"sync"
"testing"

"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
)

func TestAddParseTree(t *testing.T) {
func TestAddParseTreeHTML(t *testing.T) {
root := Must(New("root").Parse(`{{define "a"}} {{.}} {{template "b"}} {{.}} "></a>{{end}}`))
tree, err := parse.Parse("t", `{{define "b"}}<a href="{{end}}`, "", "", nil, nil)
if err != nil {
@@ -174,7 +174,7 @@ func TestCloneThenParse(t *testing.T) {
t.Error("adding a template to a clone added it to the original")
}
// double check that the embedded template isn't available in the original
err := t0.ExecuteTemplate(ioutil.Discard, "a", nil)
err := t0.ExecuteTemplate(io.Discard, "a", nil)
if err == nil {
t.Error("expected 'no such template' error")
}
@@ -188,13 +188,13 @@ func TestFuncMapWorksAfterClone(t *testing.T) {

// get the expected error output (no clone)
uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
wantErr := uncloned.Execute(ioutil.Discard, nil)
wantErr := uncloned.Execute(io.Discard, nil)

// toClone must be the same as uncloned. It has to be recreated from scratch,
// since cloning cannot occur after execution.
toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
cloned := Must(toClone.Clone())
gotErr := cloned.Execute(ioutil.Discard, nil)
gotErr := cloned.Execute(io.Discard, nil)

if wantErr.Error() != gotErr.Error() {
t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr)
@@ -216,7 +216,7 @@ func TestTemplateCloneExecuteRace(t *testing.T) {
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
if err := tmpl.Execute(ioutil.Discard, "data"); err != nil {
if err := tmpl.Execute(io.Discard, "data"); err != nil {
panic(err)
}
}
@@ -240,7 +240,7 @@ func TestCloneGrowth(t *testing.T) {
tmpl = Must(tmpl.Clone())
Must(tmpl.Parse(`{{define "B"}}Text{{end}}`))
for i := 0; i < 10; i++ {
tmpl.Execute(ioutil.Discard, nil)
tmpl.Execute(io.Discard, nil)
}
if len(tmpl.DefinedTemplates()) > 200 {
t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates()))
@@ -260,7 +260,7 @@ func TestCloneRedefinedName(t *testing.T) {
for i := 0; i < 2; i++ {
t2 := Must(t1.Clone())
t2 = Must(t2.New(fmt.Sprintf("%d", i)).Parse(page))
err := t2.Execute(ioutil.Discard, nil)
err := t2.Execute(io.Discard, nil)
if err != nil {
t.Fatal(err)
}
6 changes: 3 additions & 3 deletions tpl/internal/go_templates/htmltemplate/content_test.go
Original file line number Diff line number Diff line change
@@ -404,11 +404,11 @@ func TestTypedContent(t *testing.T) {
}

// Test that we print using the String method. Was issue 3073.
type stringer struct {
type myStringer struct {
v int
}

func (s *stringer) String() string {
func (s *myStringer) String() string {
return fmt.Sprintf("string=%d", s.v)
}

@@ -421,7 +421,7 @@ func (s *errorer) Error() string {
}

func TestStringer(t *testing.T) {
s := &stringer{3}
s := &myStringer{3}
b := new(bytes.Buffer)
tmpl := Must(New("x").Parse("{{.}}"))
if err := tmpl.Execute(b, s); err != nil {
2 changes: 2 additions & 0 deletions tpl/internal/go_templates/htmltemplate/escape.go
Original file line number Diff line number Diff line change
@@ -125,6 +125,8 @@ func (e *escaper) escape(c context, n parse.Node) context {
switch n := n.(type) {
case *parse.ActionNode:
return e.escapeAction(c, n)
case *parse.CommentNode:
return c
case *parse.IfNode:
return e.escapeBranch(c, &n.BranchNode, "if")
case *parse.ListNode:
2 changes: 1 addition & 1 deletion tpl/internal/go_templates/htmltemplate/escape_test.go
Original file line number Diff line number Diff line change
@@ -1825,7 +1825,7 @@ func TestIndirectPrint(t *testing.T) {
}

// This is a test for issue 3272.
func TestEmptyTemplate(t *testing.T) {
func TestEmptyTemplateHTML(t *testing.T) {
page := Must(New("page").ParseFiles(os.DevNull))
if err := page.ExecuteTemplate(os.Stdout, "page", "nothing"); err == nil {
t.Fatal("expected error")
Loading