go/types: first cut at type-checking type parameters with operators

- implemented for comparisons for now
- can type-check generic min
- also: rebased on top of Go tip

Change-Id: Id35582a59c4cddcb2b4ae9c7d7154ef8e41580ab
This commit is contained in:
Robert Griesemer 2019-09-12 17:28:57 -07:00
parent dc4f9fb373
commit cfbeecaabb
3 changed files with 30 additions and 2 deletions

View File

@ -62,8 +62,13 @@ func isUntyped(typ Type) bool {
}
func isOrdered(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsOrdered != 0
switch t := typ.Underlying().(type) {
case *Basic:
return t.info&IsOrdered != 0
case *TypeParam:
return t.contr.ifaceAt(t.index).is(isOrdered)
}
return false
}
func isConstType(typ Type) bool {

View File

@ -116,3 +116,16 @@ var _ T2(int32)
var _ T2 /* ERROR not satisfied */ (int64)
var _ T2(int)
var _ T2(struct{x int})
// Use of contracts in parameterized functions
contract SignedInteger(T) {
T int8, int16, int32, int64, int
}
func min(type T SignedInteger)(x, y T) T {
if x < y {
return x
}
return y
}

View File

@ -254,6 +254,16 @@ type Interface struct {
types []Type // for contracts
}
// is reports whether interface t represents types that all satisfy pred.
func (t *Interface) is(pred func(Type) bool) bool {
for _, t := range t.types {
if !pred(t) {
return false
}
}
return true
}
// emptyInterface represents the empty (completed) interface
var emptyInterface = Interface{allMethods: markComplete}