From: Robert Findley Date: Mon, 29 Nov 2021 22:39:19 +0000 (-0500) Subject: go/types, types2: handle case of no specific target types in conversion X-Git-Tag: go1.18beta1~149 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3ca57c7fb8bfc9b8b633f71a7aaa9de5fe76f63d;p=gostls13.git 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 Run-TryBot: Robert Findley Reviewed-by: Robert Griesemer TryBot-Result: Go Bot --- diff --git a/src/cmd/compile/internal/types2/conversions.go b/src/cmd/compile/internal/types2/conversions.go index 47f9ac0a5a..253868cf93 100644 --- a/src/cmd/compile/internal/types2/conversions.go +++ b/src/cmd/compile/internal/types2/conversions.go @@ -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 diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2 new file mode 100644 index 0000000000..0437e74a64 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2 @@ -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 */ ) +} diff --git a/src/go/types/conversions.go b/src/go/types/conversions.go index 5995d5920f..fb3771635d 100644 --- a/src/go/types/conversions.go +++ b/src/go/types/conversions.go @@ -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 diff --git a/src/go/types/testdata/fixedbugs/issue49864.go2 b/src/go/types/testdata/fixedbugs/issue49864.go2 new file mode 100644 index 0000000000..0437e74a64 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue49864.go2 @@ -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 */ ) +}