bind: use types.Unalias for consistency

In the previous CL go.dev/cl/740861, types.Unalias was mainly used to
resolve aliases. However, (*Type).Underlying was also unnecessarily
used. For consistency, stick to types.Unalias.

Updates golang/go#70698

Change-Id: I8a541c5fd06c8f66c563666749a691cce8a0784a
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/741600
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
diff --git a/bind/gen.go b/bind/gen.go
index 4c8e9bf..18c8037 100644
--- a/bind/gen.go
+++ b/bind/gen.go
@@ -394,17 +394,10 @@
 			g.errorf("unsupported basic type: %s", t)
 		}
 	case *types.Slice:
-		switch e := t.Elem().Underlying().(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				return "nbyteslice"
-			default:
-				g.errorf("unsupported slice type: %s", t)
-			}
-		default:
-			g.errorf("unsupported slice type: %s", t)
+		if isBytesSlice(t) {
+			return "nbyteslice"
 		}
+		g.errorf("unsupported slice type: %s", t)
 	case *types.Pointer:
 		if _, ok := types.Unalias(t.Elem()).(*types.Named); ok {
 			return g.cgoType(t.Elem())
@@ -504,10 +497,7 @@
 		}
 		return false
 	case *types.Slice:
-		switch e := t.Elem().Underlying().(type) {
-		case *types.Basic:
-			return e.Kind() == types.Uint8
-		}
+		return isBytesSlice(t)
 	case *types.Pointer:
 		switch t := types.Unalias(t.Elem()).(type) {
 		case *types.Named:
diff --git a/bind/gengo.go b/bind/gengo.go
index b192a0a..e374b03 100644
--- a/bind/gengo.go
+++ b/bind/gengo.go
@@ -111,17 +111,11 @@
 			g.Printf("%s := C.%s(%s)\n", toVar, g.cgoType(t), fromVar)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("%s := fromSlice(%s, %v)\n", toVar, fromVar, mode == modeRetained)
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("%s := fromSlice(%s, %v)\n", toVar, fromVar, mode == modeRetained)
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Pointer:
 		// TODO(crawshaw): test *int
 		// TODO(crawshaw): test **Generator
@@ -395,17 +389,11 @@
 			g.Printf("%s := %s(%s)\n", toVar, t.Underlying().String(), fromVar)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("%s := toSlice(%s, %v)\n", toVar, fromVar, mode == modeRetained)
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("%s := toSlice(%s, %v)\n", toVar, fromVar, mode == modeRetained)
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Pointer:
 		switch u := types.Unalias(t.Elem()).(type) {
 		case *types.Named:
diff --git a/bind/genjava.go b/bind/genjava.go
index 2b6b5d4..ce02bf7 100644
--- a/bind/genjava.go
+++ b/bind/genjava.go
@@ -129,12 +129,8 @@
 		}
 		return &java.Type{Kind: kind}
 	case *types.Slice:
-		switch e := T.Elem().(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				return &java.Type{Kind: java.Array, Elem: &java.Type{Kind: java.Byte}}
-			}
+		if isBytesSlice(T) {
+			return &java.Type{Kind: java.Array, Elem: &java.Type{Kind: java.Byte}}
 		}
 		return nil
 	case *types.Named:
@@ -907,17 +903,11 @@
 			g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("nbyteslice _%s = go_seq_from_java_bytearray(env, %s, %d);\n", varName, varName, toCFlag(mode == modeRetained))
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("nbyteslice _%s = go_seq_from_java_bytearray(env, %s, %d);\n", varName, varName, toCFlag(mode == modeRetained))
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Named:
 		switch u := t.Underlying().(type) {
 		case *types.Interface:
@@ -944,17 +934,11 @@
 			g.Printf("%s %s = (%s)%s;\n", g.jniType(t), toName, g.jniType(t), fromName)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("jbyteArray %s = go_seq_to_java_bytearray(env, %s, %d);\n", toName, fromName, toCFlag(mode == modeRetained))
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("jbyteArray %s = go_seq_to_java_bytearray(env, %s, %d);\n", toName, fromName, toCFlag(mode == modeRetained))
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Pointer:
 		// TODO(crawshaw): test *int
 		// TODO(crawshaw): test **Generator
@@ -1262,14 +1246,8 @@
 	switch t := types.Unalias(t).(type) {
 	case *types.Basic:
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				if mode == modeTransient {
-					g.Printf("go_seq_release_byte_array(env, %s, _%s.ptr);\n", varName, varName)
-				}
-			}
+		if isBytesSlice(t) && mode == modeTransient {
+			g.Printf("go_seq_release_byte_array(env, %s, _%s.ptr);\n", varName, varName)
 		}
 	}
 }
diff --git a/bind/genobjc.go b/bind/genobjc.go
index a9381cd..683df99 100644
--- a/bind/genobjc.go
+++ b/bind/genobjc.go
@@ -688,17 +688,11 @@
 			g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("nbyteslice _%s = go_seq_from_objc_bytearray(%s, %d);\n", varName, varName, toCFlag(mode == modeRetained))
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("nbyteslice _%s = go_seq_from_objc_bytearray(%s, %d);\n", varName, varName, toCFlag(mode == modeRetained))
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Named:
 		switch u := t.Underlying().(type) {
 		case *types.Interface:
@@ -755,17 +749,11 @@
 			g.Printf("%s %s = (%s)%s;\n", g.objcType(t), toName, g.objcType(t), fromName)
 		}
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				g.Printf("NSData *%s = go_seq_to_objc_bytearray(%s, %d);\n", toName, fromName, toCFlag(mode == modeRetained))
-			default:
-				g.errorf("unsupported type: %s", t)
-			}
-		default:
-			g.errorf("unsupported type: %s", t)
+		if isBytesSlice(t) {
+			g.Printf("NSData *%s = go_seq_to_objc_bytearray(%s, %d);\n", toName, fromName, toCFlag(mode == modeRetained))
+			return
 		}
+		g.errorf("unsupported type: %s", t)
 	case *types.Pointer:
 		switch t := types.Unalias(t.Elem()).(type) {
 		case *types.Named:
@@ -1035,18 +1023,12 @@
 func (g *ObjcGen) genRelease(varName string, t types.Type, mode varMode) {
 	switch t := types.Unalias(t).(type) {
 	case *types.Slice:
-		switch e := types.Unalias(t.Elem()).(type) {
-		case *types.Basic:
-			switch e.Kind() {
-			case types.Uint8: // Byte.
-				if mode == modeTransient {
-					// If the argument was not mutable, go_seq_from_objc_bytearray created a copy.
-					// Free it here.
-					g.Printf("if (![%s isKindOfClass:[NSMutableData class]]) {\n", varName)
-					g.Printf("  free(_%s.ptr);\n", varName)
-					g.Printf("}\n")
-				}
-			}
+		if isBytesSlice(t) && mode == modeTransient {
+			// If the argument was not mutable, go_seq_from_objc_bytearray created a copy.
+			// Free it here.
+			g.Printf("if (![%s isKindOfClass:[NSMutableData class]]) {\n", varName)
+			g.Printf("  free(_%s.ptr);\n", varName)
+			g.Printf("}\n")
 		}
 	}
 }
@@ -1290,7 +1272,7 @@
 }
 
 func (g *ObjcGen) objcParamType(t types.Type) string {
-	switch typ := t.Underlying().(type) {
+	switch typ := types.Unalias(t).(type) {
 	case *types.Basic:
 		switch typ.Kind() {
 		case types.String, types.UntypedString:
diff --git a/bind/types.go b/bind/types.go
index 9091076..4642d74 100644
--- a/bind/types.go
+++ b/bind/types.go
@@ -143,7 +143,7 @@
 }
 
 func isNullableType(t types.Type) bool {
-	t = t.Underlying()
+	t = types.Unalias(t)
 	return types.AssignableTo(types.Typ[types.UntypedNil].Underlying(), t) || t.String() == "string" // string is mapped to NSString*, which is nullable
 }
 
@@ -171,3 +171,8 @@
 	e := typePkgFirstElem(t)
 	return e == "Java" || e == "ObjC"
 }
+
+func isBytesSlice(t *types.Slice) bool {
+	e, ok := types.Unalias(t.Elem()).(*types.Basic)
+	return ok && e.Kind() == types.Byte
+}