]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: remove structuralString in favor of inlined code
authorRobert Findley <rfindley@google.com>
Tue, 16 Nov 2021 04:29:25 +0000 (23:29 -0500)
committerRobert Findley <rfindley@google.com>
Tue, 16 Nov 2021 15:16:54 +0000 (15:16 +0000)
This is a clean port of CL 363154 from types2 to go/types.

Change-Id: I26c18767041db096390e84ba9200ec69b66778d0
Reviewed-on: https://go-review.googlesource.com/c/go/+/364234
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/builtins.go
src/go/types/type.go

index b767128367bc1c4794c958fc81bc26fabee02388..c1932232aaf757c0b55327613a04948b86663c8a 100644 (file)
@@ -339,7 +339,26 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                if y.mode == invalid {
                        return
                }
-               src, _ := structuralString(y.typ).(*Slice)
+               // src, _ := structuralType(y.typ).(*Slice); but also accepts strings
+               var src *Slice
+               var elem Type // == src.elem if valid
+               if underIs(y.typ, func(u Type) bool {
+                       switch u := u.(type) {
+                       case *Basic:
+                               if isString(u) && (elem == nil || Identical(elem, universeByte)) {
+                                       elem = universeByte
+                                       return true
+                               }
+                       case *Slice:
+                               if elem == nil || Identical(elem, u.elem) {
+                                       elem = u.elem
+                                       return true
+                               }
+                       }
+                       return false
+               }) {
+                       src = NewSlice(elem)
+               }
 
                if dst == nil || src == nil {
                        check.invalidArg(x, _InvalidCopy, "copy expects slice arguments; found %s and %s", x, &y)
index 8f23fb530d1b7bbdb1d740eadd59a0e662793fe1..e26d8189d17f1f6146d5afea010e8a426e578bfa 100644 (file)
@@ -80,30 +80,6 @@ func structuralType(typ Type) Type {
        return nil
 }
 
-// structuralString is like structuralType but also considers []byte
-// and string as "identical". In this case, if successful, the result
-// is always []byte.
-func structuralString(typ Type) Type {
-       var su Type
-       if underIs(typ, func(u Type) bool {
-               if isString(u) {
-                       u = NewSlice(universeByte)
-               }
-               if su != nil {
-                       u = match(su, u)
-                       if u == nil {
-                               return false
-                       }
-               }
-               // su == nil || match(su, u) != nil
-               su = u
-               return true
-       }) {
-               return su
-       }
-       return nil
-}
-
 // If t is a defined type, asNamed returns that type (possibly after resolving it), otherwise it returns nil.
 func asNamed(t Type) *Named {
        e, _ := t.(*Named)