Skip to content

Commit 67cbe2d

Browse files
committed
Test StructInstantiatedTypes
1 parent 324931b commit 67cbe2d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

internal/convgen/assign/basic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (fac Factory) TryBasic(x, y Object) (*BasicAssigner, bool) {
3232
}
3333

3434
if types.ConvertibleTo(x.Type().Type(), y.Type().Type()) {
35+
if x.Type().Basic.Info()&(types.IsInteger|types.IsUnsigned) != 0 && y.Type().Basic.Info() == types.IsString {
36+
// int or uint -> string conversion is not allowed
37+
return nil, false
38+
}
39+
3540
as.convertible = true
3641
as.x = x
3742
as.y = y
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//go:build convgen
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/sublee/convgen"
10+
)
11+
12+
type (
13+
X[T any] struct{ Value T }
14+
Y[T any] struct{ Value T }
15+
)
16+
17+
var mod = convgen.New(convgen.ImportFunc(strconv.Itoa))
18+
19+
func Int8_Int32(x X[int8]) (y Y[int32]) { convgen.Struct(mod, x, &y); return }
20+
func Int_String(x X[int]) (y Y[string]) { convgen.Struct(mod, x, &y); return }
21+
22+
type XX struct {
23+
Int8_Int32 X[int8]
24+
Int_String X[int]
25+
}
26+
27+
type YY struct {
28+
Int8_Int32 Y[int32]
29+
Int_String Y[string]
30+
}
31+
32+
func XX_YY(x XX) (y YY) { convgen.Struct(mod, x, &y); return }
33+
34+
func main() {
35+
// Output: int32 123
36+
fmt.Printf("%[1]T %[1]v\n", Int8_Int32(X[int8]{Value: 123}).Value)
37+
38+
// Output: string 123
39+
fmt.Printf("%[1]T %[1]v\n", Int_String(X[int]{Value: 123}).Value)
40+
41+
// Output: main.YY{Int8_Int32:main.Y[int32]{Value:123}, Int_String:main.Y[string]{Value:"123"}}
42+
fmt.Printf("%#v\n", XX_YY(XX{
43+
Int8_Int32: X[int8]{Value: 123},
44+
Int_String: X[int]{Value: 123},
45+
}))
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int32 123
2+
string 123
3+
main.YY{Int8_Int32:main.Y[int32]{Value:123}, Int_String:main.Y[string]{Value:"123"}}

0 commit comments

Comments
 (0)