1
0
mirror of https://github.com/golang/go synced 2024-11-14 14:20:30 -07:00
go/test/typeparam/issue47713.go
Dan Scales 5b64381155 cmd/compile: fix naming of types inside instantiations
Issues 47713 and 47877 were both due to problems with the names used for
instantiated functions/methods, which must be in sync with the names
used by types2.

 - Switched to using NameString() for writing out type arguments in
   instantiation names. This ensures that we are always adding the
   package to type names even for the local package. Previously, we were
   explicitly adding the package name for local packages, but that
   doesn't handle the case when the local type is embedded inside a
   pointer or slice type. By switching to NameString(), we fix #47713.

 - types1 and types2 write out 'interface {' differently (vs.
   'interface{') and we were already handling that. But we needed to add
   similar code to handle 'struct {' vs 'struct{'. This fixes issue
   #47877.

While fixing these bugs, I also moved some duplicated code (which
include some of the changes above) into a common function addTargs(). I
also moved InstType() name to subr.go, and renamed: MakeInstName ->
MakeFuncInstSym and MakeDictName -> MakeDictSym.

Also removed a couple of ".inst..inst." prefix checks which are
irrelvant now, since we don't add ".inst." anymore to function
instantiations.

Fixes #47713
Fixes #47877
Fixes #47922

Change-Id: I19e9a073451f3ababd8ec31b6608cd79ba8cba36
Reviewed-on: https://go-review.googlesource.com/c/go/+/344613
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-24 18:30:13 +00:00

53 lines
902 B
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.
package main
import (
"encoding"
"fmt"
)
type Seralizable interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
type SerDeString string
func (s *SerDeString) UnmarshalBinary(in []byte) error {
*s = SerDeString(in)
return nil
}
func (s SerDeString) MarshalBinary() ([]byte, error) {
return []byte(s), nil
}
type GenericSerializable[T Seralizable] struct {
Key string
Value T
}
func (g GenericSerializable[T]) Send() {
out, err := g.Value.MarshalBinary()
if err != nil {
panic("bad")
}
var newval SerDeString
newval.UnmarshalBinary(out)
fmt.Printf("Sent %s\n", newval)
}
func main() {
val := SerDeString("asdf")
x := GenericSerializable[*SerDeString]{
Value: &val,
}
x.Send()
}