]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.18] go/types, types2: use correct underlying type in union set...
authorRobert Griesemer <gri@golang.org>
Mon, 14 Mar 2022 17:45:16 +0000 (10:45 -0700)
committerCherry Mui <cherryyz@google.com>
Mon, 4 Apr 2022 18:04:18 +0000 (18:04 +0000)
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>

src/cmd/compile/internal/types2/testdata/fixedbugs/issue51658.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/typeset.go
src/go/types/testdata/fixedbugs/issue51658.go2 [new file with mode: 0644]
src/go/types/typeset.go

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 (file)
index 0000000..c437c92
--- /dev/null
@@ -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
+}
index 8df89494352c546c0efe5c364698d3475d06cd8b..646b436685c7fdc60904ceeef65c82d7e54411c7 100644 (file)
@@ -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 (file)
index 0000000..04ce6a9
--- /dev/null
@@ -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
+}
index 6603383ea3cb7dbde14fc4d0545a23c85768f267..b33141ec3270390081ecf56b731e3f949ca24aa3 100644 (file)
@@ -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) {