]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: allow conversion from slice to array ptr
authorJosh Bleecher Snyder <josharian@gmail.com>
Sun, 14 Mar 2021 21:27:28 +0000 (14:27 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 21 Apr 2021 00:54:01 +0000 (00:54 +0000)
These match the changes to cmd/compile/internal/types2 in CL 301650.

Updates #395

Change-Id: I1e85b6355c8c8fdba0996c26a2505c65fab908d6
Reviewed-on: https://go-review.googlesource.com/c/go/+/301651
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/api_test.go
src/go/types/conversions.go
src/go/types/stdlib_test.go

index 6998fc0a0d57360d62651d5eab00f08dfa3bf1ac..3438d790246cb46ad131ff5ef1036fd7b8fa83f2 100644 (file)
@@ -1430,6 +1430,9 @@ func TestConvertibleTo(t *testing.T) {
                {newDefined(new(Struct)), new(Struct), true},
                {newDefined(Typ[Int]), new(Struct), false},
                {Typ[UntypedInt], Typ[Int], true},
+               {NewSlice(Typ[Int]), NewPointer(NewArray(Typ[Int], 10)), true},
+               {NewSlice(Typ[Int]), NewArray(Typ[Int], 10), false},
+               {NewSlice(Typ[Int]), NewPointer(NewArray(Typ[Uint], 10)), false},
                // Untyped string values are not permitted by the spec, so the below
                // behavior is undefined.
                {Typ[UntypedString], Typ[String], true},
index d93ff465bb853a3173527c808e606ffe818e5d26..e977d0db1f516c63d824de417db4451e6e30df49 100644 (file)
@@ -133,6 +133,18 @@ func (x *operand) convertibleTo(check *Checker, T Type) bool {
                return true
        }
 
+       // "x is a slice, T is a pointer-to-array type,
+       // and the slice and array types have identical element types."
+       if s := asSlice(V); s != nil {
+               if p := asPointer(T); p != nil {
+                       if a := asArray(p.Elem()); a != nil {
+                               if check.identical(s.Elem(), a.Elem()) {
+                                       return true
+                               }
+                       }
+               }
+       }
+
        return false
 }
 
index 8f9218c864aebfce3e7301667fa6e5a0977d06af..29f71137df8394256a7770186f40ece33692ae7b 100644 (file)
@@ -163,10 +163,6 @@ func TestStdTest(t *testing.T) {
                "embedfunc.go",   // tests //go:embed
                "embedvers.go",   // tests //go:embed
                "linkname2.go",   // go/types doesn't check validity of //go:xxx directives
-
-               "convert2.go",     // temporary: go/types doesn't know yet about converting from slices to array pointers
-               "convert4.go",     // temporary: go/types doesn't know yet about converting from slices to array pointers
-               "escape_slice.go", // temporary: go/types doesn't know yet about converting from slices to array pointers
        )
 }