Skip to content

Commit

Permalink
[dev.go2go] go/types: use the underlying type of type list entries
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
griesemer committed Jun 20, 2020
1 parent c0c872e commit 0a03088
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/go/types/testdata/typeinst2.go2
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ type _ interface {
type struct{f int}, struct{g int}, struct /* ERROR duplicate type */ {f int}
}

// Interface type lists can contain any type, incl. *Named types.
// Verify that we use the underlying type to compute the operational type.
type MyInt int
func add1(type T interface{type MyInt})(x T) T {
return x + 1
}

type MyString string
func double(type T interface{type MyInt, MyString})(x T) T {
return x + x
}

// Embedding of interfaces with type lists leads to interfaces
// with type lists that are the intersection of the embedded
// type lists.
Expand Down
5 changes: 3 additions & 2 deletions src/go/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,8 @@ func optype(typ Type) Type {
// (type T interface { type T }).
// See also issue #39680.
if u := t.Bound().allTypes; u != nil && u != typ {
return u
// u != typ and u is a type parameter => u.Under() != typ, so this is ok
return u.Under()
}
return theTop
}
Expand Down Expand Up @@ -979,7 +980,7 @@ func (t *Struct) Under() Type { return t }
func (t *Pointer) Under() Type { return t }
func (t *Tuple) Under() Type { return t }
func (t *Signature) Under() Type { return t }
func (t *Sum) Under() Type { return t }
func (t *Sum) Under() Type { return t } // TODO(gri) is this correct?
func (t *Interface) Under() Type { return t }
func (t *Map) Under() Type { return t }
func (t *Chan) Under() Type { return t }
Expand Down

0 comments on commit 0a03088

Please sign in to comment.