Skip to content

Commit 42057e9

Browse files
korzhaodanscales
authored andcommitted
cmd/compile: save the note of fields when translating struct
Fixes #48317 Change-Id: I756ae6253022870071004332dd8f49169307f7e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/349013 Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Dan Scales <[email protected]> Trust: Dan Scales <[email protected]>
1 parent 960d036 commit 42057e9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/cmd/compile/internal/typecheck/subr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ func (ts *Tsubster) tstruct(t *types.Type, force bool) *types.Type {
13121312
// the type param, not the instantiated type).
13131313
newfields[i] = types.NewField(f.Pos, f.Sym, t2)
13141314
newfields[i].Embedded = f.Embedded
1315+
newfields[i].Note = f.Note
13151316
if f.IsDDD() {
13161317
newfields[i].SetIsDDD(true)
13171318
}

test/typeparam/issue48317.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run -gcflags="-G=3"
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"encoding/json"
11+
)
12+
13+
type A[T any] struct {
14+
F1 string `json:"t1"`
15+
F2 T `json:"t2"`
16+
B B `json:"t3"`
17+
}
18+
19+
type B struct {
20+
F4 int `json:"t4"`
21+
}
22+
23+
func a[T any]() {
24+
data := `{"t1":"1","t2":2,"t3":{"t4":4}}`
25+
a1 := A[T]{}
26+
if err := json.Unmarshal([]byte(data), &a1); err != nil {
27+
panic(err)
28+
}
29+
if bytes, err := json.Marshal(&a1); err != nil {
30+
panic(err)
31+
} else if string(bytes) != data {
32+
panic(string(bytes))
33+
}
34+
}
35+
36+
func main() {
37+
a[int]()
38+
}

0 commit comments

Comments
 (0)