From 92655582d0d3b739a1fc88c73cc49a24eb57f845 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 15 Nov 2021 11:47:55 -0500 Subject: [PATCH] cmd/compile/internal/types2: add a check for nil reason in assignableTo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A recent change to error message formatting was missing a nil check. Fixes #49592 Change-Id: Ic1843e0277ba75eec0e8e41fe34b59c323d7ea31 Reviewed-on: https://go-review.googlesource.com/c/go/+/364034 Trust: Robert Findley Trust: Dan Scales Trust: Daniel Martí Run-TryBot: Robert Findley Reviewed-by: Dan Scales TryBot-Result: Go Bot --- src/cmd/compile/internal/types2/operand.go | 4 +++- .../types2/testdata/fixedbugs/issue49592.go2 | 11 +++++++++++ src/go/types/testdata/fixedbugs/issue49592.go2 | 11 +++++++++++ test/fixedbugs/issue49592.go | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/cmd/compile/internal/types2/testdata/fixedbugs/issue49592.go2 create mode 100644 src/go/types/testdata/fixedbugs/issue49592.go2 create mode 100644 test/fixedbugs/issue49592.go diff --git a/src/cmd/compile/internal/types2/operand.go b/src/cmd/compile/internal/types2/operand.go index 8a905f3fd00..6581d80323b 100644 --- a/src/cmd/compile/internal/types2/operand.go +++ b/src/cmd/compile/internal/types2/operand.go @@ -316,7 +316,9 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er // not an interface. if check != nil && check.conf.CompilerErrorMessages { if isInterfacePtr(Tu) { - *reason = check.sprintf("%s does not implement %s (%s is pointer to interface, not interface)", x.typ, T, T) + if reason != nil { + *reason = check.sprintf("%s does not implement %s (%s is pointer to interface, not interface)", x.typ, T, T) + } return false, _InvalidIfaceAssign } if Vi, _ := Vu.(*Interface); Vi != nil && Vp == nil { diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49592.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49592.go2 new file mode 100644 index 00000000000..846deaa89aa --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49592.go2 @@ -0,0 +1,11 @@ +// 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 _() { + var x *interface{} + var y interface{} + _ = x == y +} diff --git a/src/go/types/testdata/fixedbugs/issue49592.go2 b/src/go/types/testdata/fixedbugs/issue49592.go2 new file mode 100644 index 00000000000..846deaa89aa --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue49592.go2 @@ -0,0 +1,11 @@ +// 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 _() { + var x *interface{} + var y interface{} + _ = x == y +} diff --git a/test/fixedbugs/issue49592.go b/test/fixedbugs/issue49592.go new file mode 100644 index 00000000000..8b5612943a7 --- /dev/null +++ b/test/fixedbugs/issue49592.go @@ -0,0 +1,13 @@ +// compile + +// 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 _() { + var x *interface{} + var y interface{} + _ = x == y +}