Skip to content

Commit

Permalink
all: various test fixes related to any
Browse files Browse the repository at this point in the history
Fix several tests that were failing at CL 368254 due to inconsistent
formatting of standard library types across Go versions. Generally this
is addressed by replacing interface{} with any, though the mechanism
varies.

Where possible, prefer to update the test data to accurately reflect
1.18 formatting.

Change-Id: Ia92cd565d2ba1dd464891093b78c636fcaff44bc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/370655
Trust: Robert Findley <[email protected]>
Run-TryBot: Robert Findley <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
findleyr committed Dec 9, 2021
1 parent e06c107 commit 27fc764
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions cmd/guru/guru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func doQuery(out io.Writer, q *query, json bool) {
}

for _, output := range outputs {
// Replace occurrences of interface{} with any, for consistent output
// across go 1.18 and earlier.
output = strings.ReplaceAll(output, "interface{}", "any")
fmt.Fprintf(out, "%s\n", output)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/guru/testdata/src/implements/main.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface type FG
implements F

-------- @implements slice --------
slice type []int implements only interface{}
slice type []int implements only any

-------- @implements C --------
pointer type *C
Expand Down
2 changes: 1 addition & 1 deletion cmd/guru/testdata/src/pointsto/main.golden
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ this map[string]*int may point to these objects:

Error: pointer analysis wants an expression of reference type; got ()
-------- @pointsto var-ref-s-f --------
this interface{} may contain these dynamic types:
this any may contain these dynamic types:
chan bool, may point to:
makechan

Expand Down
2 changes: 1 addition & 1 deletion cmd/guru/testdata/src/reflection/main.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ this reflect.Value may contain these dynamic types:
makemap

-------- @pointsto p1 --------
this interface{} may contain these dynamic types:
this any may contain these dynamic types:
*bool, may point to:
reflection.b
*int, may point to:
Expand Down
8 changes: 5 additions & 3 deletions go/gcexportdata/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/tools/go/gcexportdata"
)
Expand Down Expand Up @@ -59,16 +60,17 @@ func ExampleRead() {
fmt.Printf("Package members: %s...\n", members[:5])
println := pkg.Scope().Lookup("Println")
posn := fset.Position(println.Pos())
posn.Line = 123 // make example deterministic
fmt.Printf("Println type: %s\n", println.Type())
posn.Line = 123 // make example deterministic
typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any") // go 1.18+ uses the 'any' alias
fmt.Printf("Println type: %s\n", typ)
fmt.Printf("Println location: %s\n", slashify(posn))

// Output:
//
// Package path: fmt
// Export data: fmt.a
// Package members: [Errorf Formatter Fprint Fprintf Fprintln]...
// Println type: func(a ...interface{}) (n int, err error)
// Println type: func(a ...any) (n int, err error)
// Println location: $GOROOT/src/fmt/print.go:123:1
}

Expand Down
11 changes: 7 additions & 4 deletions go/ssa/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func main() {
// golang.org/x/tools/cmd/ssadump.
//
func Example_buildPackage() {
// Replace interface{} with any for this test.
ssa.SetNormalizeAnyForTesting(true)
defer ssa.SetNormalizeAnyForTesting(false)
// Parse the source files.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "hello.go", hello, parser.ParseComments)
Expand Down Expand Up @@ -105,11 +108,11 @@ func Example_buildPackage() {
// # Location: hello.go:8:6
// func main():
// 0: entry P:0 S:0
// t0 = new [1]interface{} (varargs) *[1]interface{}
// t1 = &t0[0:int] *interface{}
// t2 = make interface{} <- string ("Hello, World!":string) interface{}
// t0 = new [1]any (varargs) *[1]any
// t1 = &t0[0:int] *any
// t2 = make any <- string ("Hello, World!":string) any
// *t1 = t2
// t3 = slice t0[:] []interface{}
// t3 = slice t0[:] []any
// t4 = fmt.Println(t3...) (n int, err error)
// return
}
Expand Down
11 changes: 10 additions & 1 deletion go/ssa/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io"
"reflect"
"sort"
"strings"

"golang.org/x/tools/go/types/typeutil"
)
Expand All @@ -38,8 +39,16 @@ func relName(v Value, i Instruction) string {
return v.Name()
}

// normalizeAnyFortesting controls whether we replace occurrences of
// interface{} with any. It is only used for normalizing test output.
var normalizeAnyForTesting bool

func relType(t types.Type, from *types.Package) string {
return types.TypeString(t, types.RelativeTo(from))
s := types.TypeString(t, types.RelativeTo(from))
if normalizeAnyForTesting {
s = strings.ReplaceAll(s, "interface{}", "any")
}
return s
}

func relString(m Member, from *types.Package) string {
Expand Down
10 changes: 10 additions & 0 deletions go/ssa/testhelper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ssa

// SetNormalizeAnyForTesting is exported here for external tests.
func SetNormalizeAnyForTesting(normalize bool) {
normalizeAnyForTesting = normalize
}
8 changes: 7 additions & 1 deletion refactor/eg/eg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test(t *testing.T) {
if err != nil {
if shouldFail == nil {
t.Errorf("NewTransformer(%s): %s", filename, err)
} else if want := constant.StringVal(shouldFail.Val()); !strings.Contains(err.Error(), want) {
} else if want := constant.StringVal(shouldFail.Val()); !strings.Contains(normalizeAny(err.Error()), want) {
t.Errorf("NewTransformer(%s): got error %q, want error %q", filename, err, want)
}
} else if shouldFail != nil {
Expand Down Expand Up @@ -173,3 +173,9 @@ func Test(t *testing.T) {
}
}
}

// normalizeAny replaces occurrences of interface{} with any, for consistent
// output.
func normalizeAny(s string) string {
return strings.ReplaceAll(s, "interface{}", "any")
}
2 changes: 1 addition & 1 deletion refactor/eg/testdata/expr_type_mismatch.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// rules this replacement should be ok, but types.Assignable doesn't work
// in the expected way (elementwise assignability) for tuples.
// Perhaps that's even a type-checker bug?
const shouldFail = "(n int, err error) is not a safe replacement for (key interface{}, err error)"
const shouldFail = "(n int, err error) is not a safe replacement for (key any, err error)"

func before() (interface{}, error) { return x509.ParsePKCS8PrivateKey(nil) }
func after() (interface{}, error) { return fmt.Print() }

0 comments on commit 27fc764

Please sign in to comment.