mirror of
https://github.com/golang/go
synced 2024-11-19 13:14:42 -07:00
cf1ec17360
Deal with case where a certain instantiation of a generic function/method leads to an unsatisfiable type assertion or type case. In that case, the compiler was causing a fatal error while trying to create an impossible itab for the dictionary. To deal with that case, allow ITabLsym() to create a dummy itab even when the concrete type doesn't implement the interface. This dummy itab is analogous to the "negative" itabs created on-the-fly by the runtime. We will use the dummy itab in type asserts and type switches in instantiations that use that dictionary entry. Since the dummy itab can never be used for any real value at runtime (since the concrete type doesn't implement the interface), there will always be a failure for the corresponding type assertion or a non-match for the corresponding type-switch case. Fixes #50002 Change-Id: I1df05b1019533e1fc93dd7ab29f331a74fab9202 Reviewed-on: https://go-review.googlesource.com/c/go/+/369894 Reviewed-by: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
// run -gcflags=-G=3
|
|
|
|
// 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.
|
|
|
|
// Test for cases where certain instantiations of a generic function (F in this
|
|
// example) will always fail on a type assertion or mismatch on a type case.
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type S struct{}
|
|
|
|
func (S) M() byte {
|
|
return 0
|
|
}
|
|
|
|
type I[T any] interface {
|
|
M() T
|
|
}
|
|
|
|
func F[T, A any](x I[T], shouldMatch bool) {
|
|
switch x.(type) {
|
|
case A:
|
|
if !shouldMatch {
|
|
fmt.Printf("wanted mis-match, got match")
|
|
}
|
|
default:
|
|
if shouldMatch {
|
|
fmt.Printf("wanted match, got mismatch")
|
|
}
|
|
}
|
|
|
|
_, ok := x.(A)
|
|
if ok != shouldMatch {
|
|
fmt.Printf("ok: got %v, wanted %v", ok, shouldMatch)
|
|
}
|
|
|
|
if !shouldMatch {
|
|
defer func() {
|
|
if shouldMatch {
|
|
fmt.Printf("Shouldn't have panicked")
|
|
}
|
|
recover()
|
|
}()
|
|
}
|
|
_ = x.(A)
|
|
if !shouldMatch {
|
|
fmt.Printf("Should have panicked")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// Test instantiation where the type switch/type asserts can't possibly succeed
|
|
// (since string does not implement I[byte]).
|
|
F[byte, string](S{}, false)
|
|
|
|
// Test instantiation where the type switch/type asserts should succeed
|
|
// (since S does implement I[byte])
|
|
F[byte, S](S{}, true)
|
|
F[byte, S](I[byte](S{}), true)
|
|
}
|