]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: generate conversions.go from types2 source
authorRobert Griesemer <gri@golang.org>
Tue, 20 Feb 2024 23:40:44 +0000 (15:40 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 21 Feb 2024 17:12:09 +0000 (17:12 +0000)
This CL reduces the amount of code that needs to be maintained
manually by about 300 LOC.

Change-Id: I749e47668e90e77e99109005fbe19045d4a5ad29
Reviewed-on: https://go-review.googlesource.com/c/go/+/565437
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/util.go
src/go/types/conversions.go
src/go/types/generate_test.go
src/go/types/util.go

index 8027092c6cd6d2a4595834a982b26bf6d6fa5647..286fad578a92f4238ff2ad35c0573535cd7b796b 100644 (file)
@@ -98,17 +98,18 @@ func (check *Checker) conversion(x *operand, T Type) {
        // given a type explicitly by a constant declaration or conversion,...".
        if isUntyped(x.typ) {
                final := T
-               // - For conversions to interfaces, except for untyped nil arguments,
-               //   use the argument's default type.
+               // - For conversions to interfaces, except for untyped nil arguments
+               //   and isTypes2, use the argument's default type.
                // - For conversions of untyped constants to non-constant types, also
                //   use the default type (e.g., []byte("foo") should report string
                //   not []byte as type for the constant "foo").
+               // - If !isTypes2, keep untyped nil for untyped nil arguments.
                // - For constant integer to string conversions, keep the argument type.
                //   (See also the TODO below.)
-               if x.typ == Typ[UntypedNil] {
+               if isTypes2 && x.typ == Typ[UntypedNil] {
                        // ok
-               } else if isNonTypeParamInterface(T) || constArg && !isConstType(T) {
-                       final = Default(x.typ)
+               } else if isNonTypeParamInterface(T) || constArg && !isConstType(T) || !isTypes2 && x.isNil() {
+                       final = Default(x.typ) // default type of untyped nil is untyped nil
                } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
                        final = x.typ
                }
@@ -227,7 +228,7 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
                return false
        }
 
-       errorf := func(format string, args ...interface{}) {
+       errorf := func(format string, args ...any) {
                if check != nil && cause != nil {
                        msg := check.sprintf(format, args...)
                        if *cause != "" {
index 9ffef1314d953e24bca61fa122b4b2df5ff89537..3718c6aeaf0c5fc67f974a88aa8b2a7301ac1343 100644 (file)
@@ -11,6 +11,8 @@ package types2
 
 import "cmd/compile/internal/syntax"
 
+const isTypes2 = true
+
 // cmpPos compares the positions p and q and returns a result r as follows:
 //
 // r <  0: p is before q
index 2be17eeb1261b2146bd698ec7743df5272b0a289..89043f2c46134ff3ca4f571cab10d3c547b648d7 100644 (file)
@@ -1,3 +1,5 @@
+// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
+
 // Copyright 2012 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.
@@ -98,14 +100,17 @@ func (check *Checker) conversion(x *operand, T Type) {
        // given a type explicitly by a constant declaration or conversion,...".
        if isUntyped(x.typ) {
                final := T
-               // - For conversions to interfaces, use the argument's default type.
+               // - For conversions to interfaces, except for untyped nil arguments
+               //   and isTypes2, use the argument's default type.
                // - For conversions of untyped constants to non-constant types, also
                //   use the default type (e.g., []byte("foo") should report string
                //   not []byte as type for the constant "foo").
-               // - Keep untyped nil for untyped nil arguments.
+               // - If !isTypes2, keep untyped nil for untyped nil arguments.
                // - For constant integer to string conversions, keep the argument type.
                //   (See also the TODO below.)
-               if isNonTypeParamInterface(T) || constArg && !isConstType(T) || x.isNil() {
+               if isTypes2 && x.typ == Typ[UntypedNil] {
+                       // ok
+               } else if isNonTypeParamInterface(T) || constArg && !isConstType(T) || !isTypes2 && x.isNil() {
                        final = Default(x.typ) // default type of untyped nil is untyped nil
                } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
                        final = x.typ
index 371106c6e6abfd8b53f29f7203a31e0cce32d2fa..7399acf872fe2f990f12b32c52ecbe0809623f80 100644 (file)
@@ -115,6 +115,7 @@ var filemap = map[string]action{
        "const.go":        func(f *ast.File) { fixTokenPos(f) },
        "context.go":      nil,
        "context_test.go": nil,
+       "conversions.go":  nil,
        "errsupport.go":   nil,
        "gccgosizes.go":   nil,
        "gcsizes.go":      func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },
index ea0dd7b07133a2078e714ea9a8361ffb1db8ae04..4d6613ea51f388c0b71a9287de3c371e13f5471c 100644 (file)
@@ -14,6 +14,8 @@ import (
        "go/token"
 )
 
+const isTypes2 = false
+
 // cmpPos compares the positions p and q and returns a result r as follows:
 //
 // r <  0: p is before q