From 3fd481043bb58292f067e419da2956aed185d339 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 6 Jan 2020 16:05:53 -0800 Subject: [PATCH] go/types: must have at least one type when checking type list properties Fixes bug with generic min being accepted even though the contract or interface bound doesn't enumerate any types (or is missing). Change-Id: Icdfc62fbd2b73ece397d5b5f1ebe27e52ed9b32f --- src/go/types/testdata/typeparams.go2 | 20 +++++++++++++++----- src/go/types/type.go | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/go/types/testdata/typeparams.go2 b/src/go/types/testdata/typeparams.go2 index b069b24f62..98c5c18927 100644 --- a/src/go/types/testdata/typeparams.go2 +++ b/src/go/types/testdata/typeparams.go2 @@ -37,15 +37,25 @@ func swapswap(type A, B)(a A, b B) (A, B) { return swap(B, A)(b, a) } -// type F(type A, B) func(A, B) (B, A) +type F(type A, B) func(A, B) (B, A) -func min(type T)(x, y T) T { - //if x < y { - // return x - //} +func min(type T interface{ type int })(x, y T) T { + if x < y { + return x + } return y } +func _(type T interface{type int, float32})(x, y T) bool { return x < y } +func _(type T)(x, y T) bool { return x /* ERROR cannot compare */ < y } +func _(type T interface{type int, float32, bool})(x, y T) bool { return x /* ERROR cannot compare */ < y } + +func _(type T C1)(x, y T) bool { return x /* ERROR cannot compare */ < y } +func _(type T C2)(x, y T) bool { return x < y } + +contract C1(T) {} +contract C2(T) { T int, float32 } + func new(type T)() *T { var x T return &x diff --git a/src/go/types/type.go b/src/go/types/type.go index af56e87892..7536ab716b 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -259,7 +259,7 @@ func (t *Interface) is(pred func(Type) bool) bool { return false } } - return true + return len(t.allTypes) > 0 // we must have at least one type! (was bug) } // emptyInterface represents the empty (completed) interface