From: Robert Griesemer Date: Tue, 20 Feb 2024 23:40:44 +0000 (-0800) Subject: go/types: generate conversions.go from types2 source X-Git-Tag: go1.23rc1~1171 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cc276a768feddf6ae1d82b581e0d975a8990d18a;p=gostls13.git go/types: generate conversions.go from types2 source 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 Auto-Submit: Robert Griesemer LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/conversions.go b/src/cmd/compile/internal/types2/conversions.go index 8027092c6c..286fad578a 100644 --- a/src/cmd/compile/internal/types2/conversions.go +++ b/src/cmd/compile/internal/types2/conversions.go @@ -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 != "" { diff --git a/src/cmd/compile/internal/types2/util.go b/src/cmd/compile/internal/types2/util.go index 9ffef1314d..3718c6aeaf 100644 --- a/src/cmd/compile/internal/types2/util.go +++ b/src/cmd/compile/internal/types2/util.go @@ -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 diff --git a/src/go/types/conversions.go b/src/go/types/conversions.go index 2be17eeb12..89043f2c46 100644 --- a/src/go/types/conversions.go +++ b/src/go/types/conversions.go @@ -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 diff --git a/src/go/types/generate_test.go b/src/go/types/generate_test.go index 371106c6e6..7399acf872 100644 --- a/src/go/types/generate_test.go +++ b/src/go/types/generate_test.go @@ -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") }, diff --git a/src/go/types/util.go b/src/go/types/util.go index ea0dd7b071..4d6613ea51 100644 --- a/src/go/types/util.go +++ b/src/go/types/util.go @@ -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