1
0
mirror of https://github.com/golang/go synced 2024-11-26 05:37:57 -07:00

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 <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>
This commit is contained in:
Robert Griesemer 2024-02-20 15:40:44 -08:00 committed by Gopher Robot
parent 3374dad1d9
commit cc276a768f
5 changed files with 20 additions and 9 deletions

View File

@ -98,17 +98,18 @@ func (check *Checker) conversion(x *operand, T Type) {
// given a type explicitly by a constant declaration or conversion,...". // given a type explicitly by a constant declaration or conversion,...".
if isUntyped(x.typ) { if isUntyped(x.typ) {
final := T final := T
// - For conversions to interfaces, except for untyped nil arguments, // - For conversions to interfaces, except for untyped nil arguments
// use the argument's default type. // and isTypes2, use the argument's default type.
// - For conversions of untyped constants to non-constant types, also // - For conversions of untyped constants to non-constant types, also
// use the default type (e.g., []byte("foo") should report string // use the default type (e.g., []byte("foo") should report string
// not []byte as type for the constant "foo"). // 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. // - For constant integer to string conversions, keep the argument type.
// (See also the TODO below.) // (See also the TODO below.)
if x.typ == Typ[UntypedNil] { if isTypes2 && x.typ == Typ[UntypedNil] {
// ok // ok
} else if isNonTypeParamInterface(T) || constArg && !isConstType(T) { } else if isNonTypeParamInterface(T) || constArg && !isConstType(T) || !isTypes2 && x.isNil() {
final = Default(x.typ) final = Default(x.typ) // default type of untyped nil is untyped nil
} else if x.mode == constant_ && isInteger(x.typ) && allString(T) { } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
final = x.typ final = x.typ
} }
@ -227,7 +228,7 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
return false return false
} }
errorf := func(format string, args ...interface{}) { errorf := func(format string, args ...any) {
if check != nil && cause != nil { if check != nil && cause != nil {
msg := check.sprintf(format, args...) msg := check.sprintf(format, args...)
if *cause != "" { if *cause != "" {

View File

@ -11,6 +11,8 @@ package types2
import "cmd/compile/internal/syntax" import "cmd/compile/internal/syntax"
const isTypes2 = true
// cmpPos compares the positions p and q and returns a result r as follows: // cmpPos compares the positions p and q and returns a result r as follows:
// //
// r < 0: p is before q // r < 0: p is before q

View 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. // Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // 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,...". // given a type explicitly by a constant declaration or conversion,...".
if isUntyped(x.typ) { if isUntyped(x.typ) {
final := T 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 // - For conversions of untyped constants to non-constant types, also
// use the default type (e.g., []byte("foo") should report string // use the default type (e.g., []byte("foo") should report string
// not []byte as type for the constant "foo"). // 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. // - For constant integer to string conversions, keep the argument type.
// (See also the TODO below.) // (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 final = Default(x.typ) // default type of untyped nil is untyped nil
} else if x.mode == constant_ && isInteger(x.typ) && allString(T) { } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
final = x.typ final = x.typ

View File

@ -115,6 +115,7 @@ var filemap = map[string]action{
"const.go": func(f *ast.File) { fixTokenPos(f) }, "const.go": func(f *ast.File) { fixTokenPos(f) },
"context.go": nil, "context.go": nil,
"context_test.go": nil, "context_test.go": nil,
"conversions.go": nil,
"errsupport.go": nil, "errsupport.go": nil,
"gccgosizes.go": nil, "gccgosizes.go": nil,
"gcsizes.go": func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") }, "gcsizes.go": func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },

View File

@ -14,6 +14,8 @@ import (
"go/token" "go/token"
) )
const isTypes2 = false
// cmpPos compares the positions p and q and returns a result r as follows: // cmpPos compares the positions p and q and returns a result r as follows:
// //
// r < 0: p is before q // r < 0: p is before q