Skip to content

Commit bb077a5

Browse files
authored
Merge pull request #2208 from visualfc/typesalias
support gotypesalias
2 parents 6de6127 + 5acec58 commit bb077a5

File tree

8 files changed

+267
-50
lines changed

8 files changed

+267
-50
lines changed

.github/workflows/go.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,41 @@ jobs:
5252
uses: codecov/codecov-action@v5
5353
with:
5454
slug: goplus/gop
55+
56+
TestTypeAlias:
57+
strategy:
58+
matrix:
59+
os:
60+
- ubuntu-latest
61+
- windows-latest
62+
- macos-latest
63+
go:
64+
- 1.23.x
65+
- 1.24.x
66+
runs-on: ${{ matrix.os }}
67+
steps:
68+
- uses: actions/checkout@v4
69+
70+
- name: Set up Go
71+
uses: actions/setup-go@v5
72+
with:
73+
go-version: ${{ matrix.go }}
74+
75+
- name: Update golang.org/x/tools
76+
run: |
77+
go work init .
78+
go work edit -replace golang.org/x/tools=golang.org/x/[email protected]
79+
80+
- name: Test Go+ installer
81+
run: |
82+
git config --global user.email "[email protected]"
83+
git config --global user.name "build robot"
84+
go test -v cmd/make_test.go
85+
86+
- name: Run testcases
87+
run: go test -v -coverprofile="coverage.txt" -covermode=atomic ./...
88+
89+
- name: Codecov
90+
uses: codecov/codecov-action@v5
91+
with:
92+
slug: goplus/gop

cl/builtin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
9191
pkg.TryImport("strings")
9292
if ng.Types != nil {
9393
initMathBig(pkg, conf, ng)
94+
if obj := ng.Types.Scope().Lookup("Gop_ninteger"); obj != nil {
95+
if _, ok := obj.Type().(*types.Basic); !ok {
96+
conf.EnableTypesalias = true
97+
}
98+
}
9499
}
95100
initBuiltin(pkg, builtin, os, fmt, ng, iox, buil, reflect)
96101
gogen.InitBuiltin(pkg, builtin, conf)

cl/cltest/cltest.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ package cltest
1818

1919
import (
2020
"bytes"
21+
"fmt"
22+
"go/ast"
23+
"go/build"
24+
"go/types"
2125
"io/fs"
2226
"log"
2327
"os"
2428
"path"
2529
"strings"
2630
"testing"
2731

32+
goparser "go/parser"
33+
2834
"github.com/goplus/gogen"
2935
"github.com/goplus/gop/cl"
3036
"github.com/goplus/gop/parser"
@@ -205,4 +211,31 @@ func testFrom(t *testing.T, pkgDir, sel string) {
205211
DoFS(t, conf, fsx.Local, pkgDir, filter, "main", expected)
206212
}
207213

214+
func Go1Point() int {
215+
for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- {
216+
var version int
217+
if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil {
218+
continue
219+
}
220+
return version
221+
}
222+
panic("bad release tags")
223+
}
224+
225+
func EnableTypesalias() bool {
226+
ver := Go1Point()
227+
if ver < 22 {
228+
return false
229+
} else if ver > 22 {
230+
_, ok := types.Universe.Lookup("any").Type().(*types.Interface)
231+
return !ok
232+
} else {
233+
fset := token.NewFileSet()
234+
f, _ := goparser.ParseFile(fset, "a.go", "package p; type A = int", goparser.SkipObjectResolution)
235+
pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil)
236+
_, ok := pkg.Scope().Lookup("A").Type().(*types.Basic)
237+
return !ok
238+
}
239+
}
240+
208241
// -----------------------------------------------------------------------------

cl/compile.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,12 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
10401040
if debugLoad {
10411041
log.Println("==> Load > AliasType", name)
10421042
}
1043-
defs.AliasType(name, toType(ctx, t.Type), tName)
1043+
typ := defs.AliasType(name, toType(ctx, t.Type), tName)
1044+
if rec := ctx.recorder(); rec != nil {
1045+
if obj, ok := typ.(interface{ Obj() *types.TypeName }); ok {
1046+
rec.Def(tName, obj.Obj())
1047+
}
1048+
}
10441049
return
10451050
}
10461051
if debugLoad {

cl/compile_gop_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -933,14 +933,22 @@ func main() {
933933
}
934934

935935
func TestOverloadNamed(t *testing.T) {
936-
gopClTest(t, `
936+
var excepted string
937+
if gotypesalias {
938+
excepted = `package main
939+
937940
import "github.com/goplus/gop/cl/internal/overload/bar"
938941
939-
var a bar.Var[int]
940-
var b bar.Var[bar.M]
941-
c := bar.Var(string)
942-
d := bar.Var(bar.M)
943-
`, `package main
942+
var a bar.Var__0[int]
943+
var b bar.Var__1[bar.M]
944+
945+
func main() {
946+
c := bar.Gopx_Var_Cast__0[string]()
947+
d := bar.Gopx_Var_Cast__1[bar.M]()
948+
}
949+
`
950+
} else {
951+
excepted = `package main
944952
945953
import "github.com/goplus/gop/cl/internal/overload/bar"
946954
@@ -951,7 +959,16 @@ func main() {
951959
c := bar.Gopx_Var_Cast__0[string]()
952960
d := bar.Gopx_Var_Cast__1[map[string]any]()
953961
}
954-
`)
962+
`
963+
}
964+
gopClTest(t, `
965+
import "github.com/goplus/gop/cl/internal/overload/bar"
966+
967+
var a bar.Var[int]
968+
var b bar.Var[bar.M]
969+
c := bar.Var(string)
970+
d := bar.Var(bar.M)
971+
`, excepted)
955972
}
956973

957974
func TestMixedOverloadNamed(t *testing.T) {

cl/compile_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const (
3131
)
3232

3333
var (
34-
gblConfLine *cl.Config
34+
gblConfLine *cl.Config
35+
gotypesalias bool
3536
)
3637

3738
func init() {
@@ -45,6 +46,7 @@ func init() {
4546
NoFileLine: false,
4647
NoAutoGenMain: true,
4748
}
49+
gotypesalias = cltest.EnableTypesalias()
4850
}
4951

5052
func gopClNamedTest(t *testing.T, name string, gopcode, expected string) {
@@ -1955,13 +1957,15 @@ func main() {
19551957
}
19561958

19571959
func TestStructType(t *testing.T) {
1958-
gopClTest(t, `
1959-
type bar = foo
1960+
var expect string
1961+
if gotypesalias {
1962+
expect = `package main
19601963
1964+
type bar = foo
19611965
type foo struct {
19621966
p *bar
19631967
A int
1964-
B string "tag1:123"
1968+
B string ` + "`tag1:123`" + `
19651969
}
19661970
19671971
func main() {
@@ -1970,13 +1974,15 @@ func main() {
19701974
}
19711975
type b = a
19721976
}
1973-
`, `package main
1977+
`
1978+
} else {
1979+
expect = `package main
19741980
19751981
type bar = foo
19761982
type foo struct {
19771983
p *foo
19781984
A int
1979-
B string `+"`tag1:123`"+`
1985+
B string ` + "`tag1:123`" + `
19801986
}
19811987
19821988
func main() {
@@ -1985,7 +1991,24 @@ func main() {
19851991
}
19861992
type b = a
19871993
}
1988-
`)
1994+
`
1995+
}
1996+
gopClTest(t, `
1997+
type bar = foo
1998+
1999+
type foo struct {
2000+
p *bar
2001+
A int
2002+
B string "tag1:123"
2003+
}
2004+
2005+
func main() {
2006+
type a struct {
2007+
p *a
2008+
}
2009+
type b = a
2010+
}
2011+
`, expect)
19892012
}
19902013

19912014
func TestDeferGo(t *testing.T) {

cl/typeparams_test.go

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,56 @@ func main() {
136136
}
137137

138138
func TestTypeParamsType(t *testing.T) {
139+
var expect string
140+
if gotypesalias {
141+
expect = `package main
142+
143+
import "fmt"
144+
145+
type DataString = Data[string]
146+
type SliceString = Slice[[]string, string]
147+
148+
func main() {
149+
fmt.Println(Data[int]{1}.v)
150+
fmt.Println(DataString{"hello"}.v)
151+
fmt.Println(Data[int]{100}.v)
152+
fmt.Println(Data[string]{"hello"}.v)
153+
fmt.Println(Data[struct {
154+
X int
155+
Y int
156+
}]{}.v.X)
157+
v1 := Slice[[]int, int]{}
158+
v2 := SliceString{}
159+
v3 := Slice[[]int, int]{}
160+
v3.Append([]int{1, 2, 3, 4}...)
161+
v3.Append2([]int{1, 2, 3, 4}...)
162+
}
163+
`
164+
} else {
165+
expect = `package main
166+
167+
import "fmt"
168+
169+
type DataString = Data[string]
170+
type SliceString = Slice[[]string, string]
171+
172+
func main() {
173+
fmt.Println(Data[int]{1}.v)
174+
fmt.Println(Data[string]{"hello"}.v)
175+
fmt.Println(Data[int]{100}.v)
176+
fmt.Println(Data[string]{"hello"}.v)
177+
fmt.Println(Data[struct {
178+
X int
179+
Y int
180+
}]{}.v.X)
181+
v1 := Slice[[]int, int]{}
182+
v2 := Slice[[]string, string]{}
183+
v3 := Slice[[]int, int]{}
184+
v3.Append([]int{1, 2, 3, 4}...)
185+
v3.Append2([]int{1, 2, 3, 4}...)
186+
}
187+
`
188+
}
139189
gopMixedClTest(t, "main", `package main
140190
type Data[T any] struct {
141191
v T
@@ -178,29 +228,7 @@ v2 := SliceString{}
178228
v3 := Slice[[]int,int]{}
179229
v3.Append([1,2,3,4]...)
180230
v3.Append2([1,2,3,4]...)
181-
`, `package main
182-
183-
import "fmt"
184-
185-
type DataString = Data[string]
186-
type SliceString = Slice[[]string, string]
187-
188-
func main() {
189-
fmt.Println(Data[int]{1}.v)
190-
fmt.Println(Data[string]{"hello"}.v)
191-
fmt.Println(Data[int]{100}.v)
192-
fmt.Println(Data[string]{"hello"}.v)
193-
fmt.Println(Data[struct {
194-
X int
195-
Y int
196-
}]{}.v.X)
197-
v1 := Slice[[]int, int]{}
198-
v2 := Slice[[]string, string]{}
199-
v3 := Slice[[]int, int]{}
200-
v3.Append([]int{1, 2, 3, 4}...)
201-
v3.Append2([]int{1, 2, 3, 4}...)
202-
}
203-
`)
231+
`, expect)
204232
}
205233

206234
func TestTypeParamsComparable(t *testing.T) {
@@ -314,7 +342,13 @@ _ = At[int]
314342
}
315343

316344
func TestTypeParamsErrInferFunc(t *testing.T) {
317-
mixedErrorTest(t, `b.gop:2:5: cannot infer T2 (/foo/a.go:4:21)`, `
345+
var expect string
346+
if cltest.Go1Point() == 24 {
347+
expect = `b.gop:2:5: cannot infer T2 (declared at /foo/a.go:4:21)`
348+
} else {
349+
expect = `b.gop:2:5: cannot infer T2 (/foo/a.go:4:21)`
350+
}
351+
mixedErrorTest(t, expect, `
318352
package main
319353
320354
func Loader[T1 any, T2 any](p1 T1, p2 T2) T1 {

0 commit comments

Comments
 (0)