Skip to content

Commit 0a03088

Browse files
committed
[dev.go2go] go/types: use the underlying type of type list entries
Type lists can contain defined types, make sure we use their underlying types when computing operational types. Change-Id: I8b6b11302f6b28977916e495f1486cc7b352e859 Reviewed-on: https://go-review.googlesource.com/c/go/+/239163 Reviewed-by: Robert Griesemer <[email protected]>
1 parent c0c872e commit 0a03088

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/go/types/testdata/typeinst2.go2

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ type _ interface {
172172
type struct{f int}, struct{g int}, struct /* ERROR duplicate type */ {f int}
173173
}
174174

175+
// Interface type lists can contain any type, incl. *Named types.
176+
// Verify that we use the underlying type to compute the operational type.
177+
type MyInt int
178+
func add1(type T interface{type MyInt})(x T) T {
179+
return x + 1
180+
}
181+
182+
type MyString string
183+
func double(type T interface{type MyInt, MyString})(x T) T {
184+
return x + x
185+
}
186+
175187
// Embedding of interfaces with type lists leads to interfaces
176188
// with type lists that are the intersection of the embedded
177189
// type lists.

src/go/types/type.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ func optype(typ Type) Type {
828828
// (type T interface { type T }).
829829
// See also issue #39680.
830830
if u := t.Bound().allTypes; u != nil && u != typ {
831-
return u
831+
// u != typ and u is a type parameter => u.Under() != typ, so this is ok
832+
return u.Under()
832833
}
833834
return theTop
834835
}
@@ -979,7 +980,7 @@ func (t *Struct) Under() Type { return t }
979980
func (t *Pointer) Under() Type { return t }
980981
func (t *Tuple) Under() Type { return t }
981982
func (t *Signature) Under() Type { return t }
982-
func (t *Sum) Under() Type { return t }
983+
func (t *Sum) Under() Type { return t } // TODO(gri) is this correct?
983984
func (t *Interface) Under() Type { return t }
984985
func (t *Map) Under() Type { return t }
985986
func (t *Chan) Under() Type { return t }

0 commit comments

Comments
 (0)