go/types, types2: refuse pointer to array as argument to "clear" builtin

The accepted proposal only permits map and slice types.

Updates #56351

Change-Id: I95cf4c856a5ecfcdf564601b6215eda3cb6ba86b
Reviewed-on: https://go-review.googlesource.com/c/go/+/463075
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Cuong Manh Le 2023-01-21 14:53:47 +07:00 committed by Robert Griesemer
parent dc9d219cad
commit 8c37d486c8
7 changed files with 7 additions and 19 deletions

View file

@ -240,15 +240,11 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
} }
if !underIs(x.typ, func(u Type) bool { if !underIs(x.typ, func(u Type) bool {
switch u := u.(type) { switch u.(type) {
case *Map, *Slice: case *Map, *Slice:
return true return true
case *Pointer:
if _, ok := under(u.base).(*Array); ok {
return true
} }
} check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map or slice", x)
check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map, slice, or array pointer", x)
return false return false
}) { }) {
return return

View file

@ -42,8 +42,6 @@ var builtinCalls = []struct {
{"clear", `var m map[float64]int; clear(m)`, `func(map[float64]int)`}, {"clear", `var m map[float64]int; clear(m)`, `func(map[float64]int)`},
{"clear", `var s []byte; clear(s)`, `func([]byte)`}, {"clear", `var s []byte; clear(s)`, `func([]byte)`},
{"clear", `var p *[10]int; clear(p)`, `func(*[10]int)`},
{"clear", `var s P; clear(s)`, `func(P)`},
{"close", `var c chan int; close(c)`, `func(chan int)`}, {"close", `var c chan int; close(c)`, `func(chan int)`},
{"close", `var c chan<- chan string; close(c)`, `func(chan<- chan string)`}, {"close", `var c chan<- chan string; close(c)`, `func(chan<- chan string)`},

View file

@ -241,15 +241,11 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
} }
if !underIs(x.typ, func(u Type) bool { if !underIs(x.typ, func(u Type) bool {
switch u := u.(type) { switch u.(type) {
case *Map, *Slice: case *Map, *Slice:
return true return true
case *Pointer:
if _, ok := under(u.base).(*Array); ok {
return true
} }
} check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map or slice", x)
check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map, slice, or array pointer", x)
return false return false
}) { }) {
return return

View file

@ -42,8 +42,6 @@ var builtinCalls = []struct {
{"clear", `var m map[float64]int; clear(m)`, `func(map[float64]int)`}, {"clear", `var m map[float64]int; clear(m)`, `func(map[float64]int)`},
{"clear", `var s []byte; clear(s)`, `func([]byte)`}, {"clear", `var s []byte; clear(s)`, `func([]byte)`},
{"clear", `var p *[10]int; clear(p)`, `func(*[10]int)`},
{"clear", `var s P; clear(s)`, `func(P)`},
{"close", `var c chan int; close(c)`, `func(chan int)`}, {"close", `var c chan int; close(c)`, `func(chan int)`},
{"close", `var c chan<- chan string; close(c)`, `func(chan<- chan string)`}, {"close", `var c chan<- chan string; close(c)`, `func(chan<- chan string)`},

View file

@ -1432,7 +1432,7 @@ const (
_ // not used anymore _ // not used anymore
// InvalidClear occurs when clear is called with an argument // InvalidClear occurs when clear is called with an argument
// that is not of map, slice, or pointer-to-array type. // that is not of map or slice type.
// //
// Example: // Example:
// func _(x int) { // func _(x int) {

View file

@ -144,7 +144,7 @@ func clear1() {
var m map[float64]string var m map[float64]string
var s []byte var s []byte
clear(a /* ERROR "cannot clear a" */) clear(a /* ERROR "cannot clear a" */)
clear(&a) clear(&/* ERROR "cannot clear &a" */a)
clear(m) clear(m)
clear(s) clear(s)
clear([]int{}) clear([]int{})

View file

@ -14,7 +14,7 @@ func _[T any](x T) {
clear(x /* ERROR "cannot clear x" */) clear(x /* ERROR "cannot clear x" */)
} }
func _[T ~map[int]string | ~[]byte | ~*[10]int](x T) { func _[T ~map[int]string | ~[]byte](x T) {
clear(x) clear(x)
} }