Skip to content

Commit a7de684

Browse files
committed
cmd/compile/internal/noder: stop creating TUNION types
In the types1 universe under the unified frontend, we never need to worry about type parameter constraints, so we only see pure interfaces. However, we might still see interfaces that contain union types, because of interfaces like "interface{ any | int }" (equivalent to just "any"). We can handle these without needing to actually represent type unions within types1 by simply mapping any union to "any". Updates golang#57410. Change-Id: I5e4efcf0339edbb01f4035c54fb6fb1f9ddc0c65 Reviewed-on: https://go-review.googlesource.com/c/go/+/458619 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 627f128 commit a7de684

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/cmd/compile/internal/noder/reader.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,33 @@ func (r *reader) doTyp() *types.Type {
519519
}
520520

521521
func (r *reader) unionType() *types.Type {
522-
terms := make([]*types.Type, r.Len())
523-
tildes := make([]bool, len(terms))
524-
for i := range terms {
525-
tildes[i] = r.Bool()
526-
terms[i] = r.typ()
522+
// In the types1 universe, we only need to handle value types.
523+
// Impure interfaces (i.e., interfaces with non-trivial type sets
524+
// like "int | string") can only appear as type parameter bounds,
525+
// and this is enforced by the types2 type checker.
526+
//
527+
// However, type unions can still appear in pure interfaces if the
528+
// type union is equivalent to "any". E.g., typeparam/issue52124.go
529+
// declares variables with the type "interface { any | int }".
530+
//
531+
// To avoid needing to represent type unions in types1 (since we
532+
// don't have any uses for that today anyway), we simply fold them
533+
// to "any". As a consistency check, we still read the union terms
534+
// to make sure this substitution is safe.
535+
536+
pure := false
537+
for i, n := 0, r.Len(); i < n; i++ {
538+
_ = r.Bool() // tilde
539+
term := r.typ()
540+
if term.IsEmptyInterface() {
541+
pure = true
542+
}
543+
}
544+
if !pure {
545+
base.Fatalf("impure type set used in value type")
527546
}
528-
return types.NewUnion(terms, tildes)
547+
548+
return types.Types[types.TINTER]
529549
}
530550

531551
func (r *reader) interfaceType() *types.Type {

test/typeparam/issue52124.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
package p
88

9-
type I interface{ any | int }
9+
type Any any
10+
11+
type I interface{ Any | int }
1012

1113
var (
1214
X I = 42

0 commit comments

Comments
 (0)