Skip to content

Commit 41fe746

Browse files
committed
go/types, types2: use correct underlying type in union set computation
Fixes #51658. Change-Id: Ibf415d7e12849b8f50b58d74713613d4e65bc347 Reviewed-on: https://go-review.googlesource.com/c/go/+/392575 Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 8419ec2 commit 41fe746

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type F { // ERROR syntax error
8+
float64
9+
} // ERROR syntax error
10+
11+
func _[T F | int](x T) {
12+
_ = x == 0 // don't crash when recording type of 0
13+
}
14+
15+
// test case from issue
16+
17+
type FloatType { // ERROR syntax error
18+
float32 | float64
19+
} // ERROR syntax error
20+
21+
type IntegerType interface {
22+
int8 | int16 | int32 | int64 | int |
23+
uint8 | uint16 | uint32 | uint64 | uint
24+
}
25+
26+
type ComplexType interface {
27+
complex64 | complex128
28+
}
29+
30+
type Number interface {
31+
FloatType | IntegerType | ComplexType
32+
}
33+
34+
func GetDefaultNumber[T Number](value, defaultValue T) T {
35+
if value == 0 {
36+
return defaultValue
37+
}
38+
return value
39+
}

src/cmd/compile/internal/types2/typeset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos syn
406406
// For now we don't permit type parameters as constraints.
407407
assert(!isTypeParam(t.typ))
408408
terms = computeInterfaceTypeSet(check, pos, ui).terms
409-
} else if t.typ == Typ[Invalid] {
409+
} else if u == Typ[Invalid] {
410410
continue
411411
} else {
412412
if t.tilde && !Identical(t.typ, u) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type F { // ERROR expected type
8+
float64
9+
} // ERROR expected declaration
10+
11+
func _[T F | int](x T) {
12+
_ = x == 0 // don't crash when recording type of 0
13+
}
14+
15+
// test case from issue
16+
17+
type FloatType { // ERROR expected type
18+
float32 | float64
19+
} // ERROR expected declaration
20+
21+
type IntegerType interface {
22+
int8 | int16 | int32 | int64 | int |
23+
uint8 | uint16 | uint32 | uint64 | uint
24+
}
25+
26+
type ComplexType interface {
27+
complex64 | complex128
28+
}
29+
30+
type Number interface {
31+
FloatType | IntegerType | ComplexType
32+
}
33+
34+
func GetDefaultNumber[T Number](value, defaultValue T) T {
35+
if value == 0 {
36+
return defaultValue
37+
}
38+
return value
39+
}

src/go/types/typeset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos tok
406406
// For now we don't permit type parameters as constraints.
407407
assert(!isTypeParam(t.typ))
408408
terms = computeInterfaceTypeSet(check, pos, ui).terms
409-
} else if t.typ == Typ[Invalid] {
409+
} else if u == Typ[Invalid] {
410410
continue
411411
} else {
412412
if t.tilde && !Identical(t.typ, u) {

0 commit comments

Comments
 (0)