Skip to content

Commit c187443

Browse files
committed
go/types: allow conversion from slice to array ptr
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 <[email protected]> Trust: Robert Griesemer <[email protected]> Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent faa4fa1 commit c187443

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/go/types/api_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,9 @@ func TestConvertibleTo(t *testing.T) {
14301430
{newDefined(new(Struct)), new(Struct), true},
14311431
{newDefined(Typ[Int]), new(Struct), false},
14321432
{Typ[UntypedInt], Typ[Int], true},
1433+
{NewSlice(Typ[Int]), NewPointer(NewArray(Typ[Int], 10)), true},
1434+
{NewSlice(Typ[Int]), NewArray(Typ[Int], 10), false},
1435+
{NewSlice(Typ[Int]), NewPointer(NewArray(Typ[Uint], 10)), false},
14331436
// Untyped string values are not permitted by the spec, so the below
14341437
// behavior is undefined.
14351438
{Typ[UntypedString], Typ[String], true},

src/go/types/conversions.go

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ func (x *operand) convertibleTo(check *Checker, T Type) bool {
133133
return true
134134
}
135135

136+
// "x is a slice, T is a pointer-to-array type,
137+
// and the slice and array types have identical element types."
138+
if s := asSlice(V); s != nil {
139+
if p := asPointer(T); p != nil {
140+
if a := asArray(p.Elem()); a != nil {
141+
if check.identical(s.Elem(), a.Elem()) {
142+
return true
143+
}
144+
}
145+
}
146+
}
147+
136148
return false
137149
}
138150

src/go/types/stdlib_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ func TestStdTest(t *testing.T) {
163163
"embedfunc.go", // tests //go:embed
164164
"embedvers.go", // tests //go:embed
165165
"linkname2.go", // go/types doesn't check validity of //go:xxx directives
166-
167-
"convert2.go", // temporary: go/types doesn't know yet about converting from slices to array pointers
168-
"convert4.go", // temporary: go/types doesn't know yet about converting from slices to array pointers
169-
"escape_slice.go", // temporary: go/types doesn't know yet about converting from slices to array pointers
170166
)
171167
}
172168

0 commit comments

Comments
 (0)