diff options
author | Robert Griesemer <gri@golang.org> | 2022-03-14 10:45:16 -0700 |
---|---|---|
committer | Cherry Mui <cherryyz@google.com> | 2022-04-04 18:04:18 +0000 |
commit | 9d738480d2e56dc88dc2f53fb6220eb8a63964c6 (patch) | |
tree | 7795fe6b992fe483f3fa9620fe201668855f4ea2 | |
parent | 32ff9b5de66ad2fa852b82f06897543e4b872105 (diff) | |
download | go-9d738480d2e56dc88dc2f53fb6220eb8a63964c6.tar.gz go-9d738480d2e56dc88dc2f53fb6220eb8a63964c6.zip |
[release-branch.go1.18] go/types, types2: use correct underlying type in union set computation
Fixes #51665.
Change-Id: Ibf415d7e12849b8f50b58d74713613d4e65bc347
Reviewed-on: https://go-review.googlesource.com/c/go/+/392575
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/392577
TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r-- | src/cmd/compile/internal/types2/testdata/fixedbugs/issue51658.go2 | 39 | ||||
-rw-r--r-- | src/cmd/compile/internal/types2/typeset.go | 2 | ||||
-rw-r--r-- | src/go/types/testdata/fixedbugs/issue51658.go2 | 39 | ||||
-rw-r--r-- | src/go/types/typeset.go | 2 |
4 files changed, 80 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51658.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51658.go2 new file mode 100644 index 0000000000..c437c92d29 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51658.go2 @@ -0,0 +1,39 @@ +// Copyright 2022 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. + +package p + +type F { // ERROR syntax error + float64 +} // ERROR syntax error + +func _[T F | int](x T) { + _ = x == 0 // don't crash when recording type of 0 +} + +// test case from issue + +type FloatType { // ERROR syntax error + float32 | float64 +} // ERROR syntax error + +type IntegerType interface { + int8 | int16 | int32 | int64 | int | + uint8 | uint16 | uint32 | uint64 | uint +} + +type ComplexType interface { + complex64 | complex128 +} + +type Number interface { + FloatType | IntegerType | ComplexType +} + +func GetDefaultNumber[T Number](value, defaultValue T) T { + if value == 0 { + return defaultValue + } + return value +} diff --git a/src/cmd/compile/internal/types2/typeset.go b/src/cmd/compile/internal/types2/typeset.go index 8df8949435..646b436685 100644 --- a/src/cmd/compile/internal/types2/typeset.go +++ b/src/cmd/compile/internal/types2/typeset.go @@ -406,7 +406,7 @@ func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos syn // For now we don't permit type parameters as constraints. assert(!isTypeParam(t.typ)) terms = computeInterfaceTypeSet(check, pos, ui).terms - } else if t.typ == Typ[Invalid] { + } else if u == Typ[Invalid] { continue } else { if t.tilde && !Identical(t.typ, u) { diff --git a/src/go/types/testdata/fixedbugs/issue51658.go2 b/src/go/types/testdata/fixedbugs/issue51658.go2 new file mode 100644 index 0000000000..04ce6a9760 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue51658.go2 @@ -0,0 +1,39 @@ +// Copyright 2022 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. + +package p + +type F { // ERROR expected type + float64 +} // ERROR expected declaration + +func _[T F | int](x T) { + _ = x == 0 // don't crash when recording type of 0 +} + +// test case from issue + +type FloatType { // ERROR expected type + float32 | float64 +} // ERROR expected declaration + +type IntegerType interface { + int8 | int16 | int32 | int64 | int | + uint8 | uint16 | uint32 | uint64 | uint +} + +type ComplexType interface { + complex64 | complex128 +} + +type Number interface { + FloatType | IntegerType | ComplexType +} + +func GetDefaultNumber[T Number](value, defaultValue T) T { + if value == 0 { + return defaultValue + } + return value +} diff --git a/src/go/types/typeset.go b/src/go/types/typeset.go index 6603383ea3..b33141ec32 100644 --- a/src/go/types/typeset.go +++ b/src/go/types/typeset.go @@ -406,7 +406,7 @@ func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos tok // For now we don't permit type parameters as constraints. assert(!isTypeParam(t.typ)) terms = computeInterfaceTypeSet(check, pos, ui).terms - } else if t.typ == Typ[Invalid] { + } else if u == Typ[Invalid] { continue } else { if t.tilde && !Identical(t.typ, u) { |