go/types, types2: handle case of no specific target types in conversion

Avoid a panic by handling the case of no specific target type in a type
parameter to type parameter conversions.

Fixes #49864

Change-Id: I117dd80cc9d47c8c1e168f1caf0f281726270c84
Reviewed-on: https://go-review.googlesource.com/c/go/+/367616
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Robert Findley 2021-11-29 17:39:19 -05:00
parent 186f375ecf
commit 3ca57c7fb8
4 changed files with 24 additions and 0 deletions

View File

@ -233,6 +233,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
}
x.typ = V.typ
return Tp.is(func(T *term) bool {
if T == nil {
return false // no specific types
}
if !x.convertibleTo(check, T.typ, cause) {
errorf("cannot convert %s (in %s) to %s (in %s)", V.typ, Vp, T.typ, Tp)
return false

View File

@ -0,0 +1,9 @@
// Copyright 2021 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.
package p
func _[P ~int, Q any](p P) {
_ = Q(p /* ERROR cannot convert */ )
}

View File

@ -224,6 +224,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
}
x.typ = V.typ
return Tp.is(func(T *term) bool {
if T == nil {
return false // no specific types
}
if !x.convertibleTo(check, T.typ, cause) {
errorf("cannot convert %s (in %s) to %s (in %s)", V.typ, Vp, T.typ, Tp)
return false

View File

@ -0,0 +1,9 @@
// Copyright 2021 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.
package p
func _[P ~int, Q any](p P) {
_ = Q(p /* ERROR cannot convert */ )
}