From c5540e53b1f692a8c977fd1e4ee0915eea66f999 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 5 Jan 2022 12:25:43 -0800 Subject: [PATCH] go/types, types2: ensure that signature type bounds are interfaces Do this by running verification for instantiated signatures later, after the delayed type parameter set-up had a chance to wrap type bounds in implicit interfaces where needed. Fixes #50450 Change-Id: If3ff7dc0be6af14af854830bfddb81112ac575cb Reviewed-on: https://go-review.googlesource.com/c/go/+/375737 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/call.go | 24 ++++++++++------- .../types2/testdata/fixedbugs/issue50450.go2 | 11 ++++++++ src/go/types/call.go | 26 +++++++++++-------- .../types/testdata/fixedbugs/issue50450.go2 | 11 ++++++++ 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 create mode 100644 src/go/types/testdata/fixedbugs/issue50450.go2 diff --git a/src/cmd/compile/internal/types2/call.go b/src/cmd/compile/internal/types2/call.go index ed8b67c607..d93805e9c7 100644 --- a/src/cmd/compile/internal/types2/call.go +++ b/src/cmd/compile/internal/types2/call.go @@ -74,17 +74,21 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs inst := check.instance(pos, typ, targs, check.bestContext(nil)).(*Signature) assert(len(xlist) <= len(targs)) - tparams := typ.TypeParams().list() - if i, err := check.verify(pos, tparams, targs); err != nil { - // best position for error reporting - pos := pos - if i < len(xlist) { - pos = syntax.StartPos(xlist[i]) + + // verify instantiation lazily (was issue #50450) + check.later(func() { + tparams := typ.TypeParams().list() + if i, err := check.verify(pos, tparams, targs); err != nil { + // best position for error reporting + pos := pos + if i < len(xlist) { + pos = syntax.StartPos(xlist[i]) + } + check.softErrorf(pos, "%s", err) + } else { + check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) } - check.softErrorf(pos, "%s", err) - } else { - check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) - } + }) return inst } diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 new file mode 100644 index 0000000000..bae3111578 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 @@ -0,0 +1,11 @@ +// Copyright 2022 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 + +type S struct{} + +func f[P S]() {} + +var _ = f[S] diff --git a/src/go/types/call.go b/src/go/types/call.go index 4156d56d9f..ec6efd2379 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -75,17 +75,21 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs inst := check.instance(pos, typ, targs, check.bestContext(nil)).(*Signature) assert(len(xlist) <= len(targs)) - tparams := typ.TypeParams().list() - if i, err := check.verify(pos, tparams, targs); err != nil { - // best position for error reporting - pos := pos - if i < len(xlist) { - pos = xlist[i].Pos() - } - check.softErrorf(atPos(pos), _InvalidTypeArg, "%s", err) - } else { - check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) - } + + // verify instantiation lazily (was issue #50450) + check.later(func() { + tparams := typ.TypeParams().list() + if i, err := check.verify(pos, tparams, targs); err != nil { + // best position for error reporting + pos := pos + if i < len(xlist) { + pos = xlist[i].Pos() + } + check.softErrorf(atPos(pos), _InvalidTypeArg, "%s", err) + } else { + check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) + } + }) return inst } diff --git a/src/go/types/testdata/fixedbugs/issue50450.go2 b/src/go/types/testdata/fixedbugs/issue50450.go2 new file mode 100644 index 0000000000..bae3111578 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue50450.go2 @@ -0,0 +1,11 @@ +// Copyright 2022 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 + +type S struct{} + +func f[P S]() {} + +var _ = f[S] -- 2.50.0