go/types: make "make" work for generic types

Passes all.bash.

Change-Id: I524878e0299e12633c633540cc8f76deab81ce48
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/755783
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2020-05-27 12:14:44 -07:00
parent 7116b1df5f
commit 188ddaf79c
4 changed files with 86 additions and 10 deletions

View File

@ -448,20 +448,42 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
return
}
var min int // minimum number of arguments
switch T.Under().(type) {
case *Slice:
min = 2
case *Map, *Chan:
min = 1
default:
min, max := -1, 10
var valid func(t Type) bool
valid = func(t Type) bool {
var m int
switch t := t.Under().(type) {
case *Slice:
m = 2
case *Map, *Chan:
m = 1
case *TypeParam:
return t.Bound().is(valid)
default:
return false
}
if m > min {
min = m
}
if m+1 < max {
max = m + 1
}
return true
}
if !valid(T) {
check.invalidArg(arg0.Pos(), "cannot make %s; type must be slice, map, or channel", arg0)
return
}
if nargs < min || min+1 < nargs {
check.errorf(call.Pos(), "%v expects %d or %d arguments; found %d", call, min, min+1, nargs)
if nargs < min || max < nargs {
if min == max {
check.errorf(call.Pos(), "%v expects %d arguments; found %d", call, min, nargs)
} else {
check.errorf(call.Pos(), "%v expects %d or %d arguments; found %d", call, min, max, nargs)
}
return
}
types := []Type{T}
var sizes []int64 // constant integer arguments, if any
for _, arg := range call.Args[1:] {

View File

@ -82,6 +82,7 @@ var tests = [][]string{
{"testdata/expr3.src"},
{"testdata/methodsets.src"},
{"testdata/shifts.src"},
{"testdata/builtins.go2"},
{"testdata/builtins.src"},
{"testdata/conversions.src"},
{"testdata/conversions2.src"},

View File

@ -512,7 +512,7 @@ func (check *Checker) convertUntyped(x *operand, target Type) {
}
// In case of a type parameter, conversion must succeed against
// all types enumerated by the the type parameter bound.
// all types enumerated by the type parameter bound.
if t := target.TypeParam(); t != nil {
types := t.Bound().allTypes
if len(types) == 0 {

53
src/go/types/testdata/builtins.go2 vendored Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file tests built-in calls on generic types.
package builtins
type Bmc interface {
type map[rune]string, chan int
}
type Bms interface {
type map[string]int, []int
}
type Bcs interface {
type chan bool, []float64
}
type Bss interface {
type []int, []string
}
func _(type T) () {
_ = make(T /* ERROR invalid argument */ )
_ = make(T /* ERROR invalid argument */ , 10)
_ = make(T /* ERROR invalid argument */ , 10, 20)
}
func _(type T Bmc) () {
_ = make(T)
_ = make(T, 10)
_ = make /* ERROR expects 1 or 2 arguments */ (T, 10, 20)
}
func _(type T Bms) () {
_ = make /* ERROR expects 2 arguments */ (T)
_ = make(T, 10)
_ = make /* ERROR expects 2 arguments */ (T, 10, 20)
}
func _(type T Bcs) () {
_ = make /* ERROR expects 2 arguments */ (T)
_ = make(T, 10)
_ = make /* ERROR expects 2 arguments */ (T, 10, 20)
}
func _(type T Bss) () {
_ = make /* ERROR expects 2 or 3 arguments */ (T)
_ = make(T, 10)
_ = make(T, 10, 20)
}