Skip to content

Commit 2b36238

Browse files
authored
Merge pull request #281 from liggitt/alias
Preserve existing behavior with go1.22/1.23 alias type parsing
2 parents f173c7c + a2ede17 commit 2b36238

File tree

10 files changed

+116
-2
lines changed

10 files changed

+116
-2
lines changed

v2/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ all:
22
go build ./...
33

44
test:
5-
go test -race ./...
5+
GODEBUG=gotypesalias=0 go test -race ./... -count=1
6+
GODEBUG=gotypesalias=1 go test -race ./... -count=1
67

78
# We verify for the maximum version of the go directive as 1.20
89
# here because the oldest go directive that exists on our supported
910
# release branches in k/k is 1.20.
1011
verify:
11-
./hack/verify-examples.sh
12+
GODEBUG=gotypesalias=0 ./hack/verify-examples.sh
13+
GODEBUG=gotypesalias=1 ./hack/verify-examples.sh
1214
./hack/verify-go-directive.sh 1.20

v2/examples/kilroy/testdata/simple/file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type ExportedStruct struct {
99
PI *int
1010
S string
1111
PS *string
12+
AI AliasInt
13+
AS AliasStruct
1214
}
1315

1416
type privateString string
@@ -21,3 +23,11 @@ type privateStruct struct {
2123
s string
2224
ps *string
2325
}
26+
27+
type AliasInt = int
28+
29+
type UnderlyingStruct struct {
30+
}
31+
type AliasStruct = UnderlyingStruct
32+
33+
type AliasUnused = int

v2/examples/kilroy/testdata/simple/generated.kilroy.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/examples/pointuh/testdata/results/simple/generated.pointuh.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/examples/pointuh/testdata/simple/file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type ExportedStruct struct {
99
PI *int
1010
S string
1111
PS *string
12+
AI AliasInt
13+
AS AliasStruct
1214
}
1315

1416
type privateString string
@@ -21,3 +23,11 @@ type privateStruct struct {
2123
s string
2224
ps *string
2325
}
26+
27+
type AliasInt = int
28+
29+
type UnderlyingStruct struct {
30+
}
31+
type AliasStruct = UnderlyingStruct
32+
33+
type AliasUnused = int

v2/examples/tracer/testdata/simple/file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type ExportedStruct struct {
99
PI *int
1010
S string
1111
PS *string
12+
AI AliasInt
13+
AS AliasStruct
1214
}
1315

1416
type privateString string
@@ -21,3 +23,11 @@ type privateStruct struct {
2123
s string
2224
ps *string
2325
}
26+
27+
type AliasInt = int
28+
29+
type UnderlyingStruct struct {
30+
}
31+
type AliasStruct = UnderlyingStruct
32+
33+
type AliasUnused = int

v2/examples/tracer/testdata/simple/out.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: int
55
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedInt
66
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedString
77
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedStruct
8+
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.UnderlyingStruct
89
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateInt
910
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateString
1011
DBG: FilterFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateStruct
@@ -13,6 +14,7 @@ DBG: GeneratorsFunc{k8s.io/gengo/v2/examples/tracer/testdata/simple}
1314
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedInt
1415
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedString
1516
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedStruct
17+
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.UnderlyingStruct
1618
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateInt
1719
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateString
1820
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Filter: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateStruct
@@ -23,6 +25,7 @@ DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.Init
2325
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedInt
2426
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedString
2527
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.ExportedStruct
28+
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.UnderlyingStruct
2629
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateInt
2730
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateString
2831
DBG: tracerGenerator{k8s.io/gengo/v2/examples/tracer/testdata/simple}.GenerateType: k8s.io/gengo/v2/examples/tracer/testdata/simple.privateStruct

v2/parser/parse.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"golang.org/x/tools/go/packages"
32+
3233
"k8s.io/gengo/v2/types"
3334
"k8s.io/klog/v2"
3435
)
@@ -642,6 +643,12 @@ func (p *Parser) walkType(u types.Universe, useName *types.Name, in gotypes.Type
642643
name = *useName
643644
}
644645

646+
// Handle alias types conditionally on go1.22+.
647+
// Inline this once the minimum supported version is go1.22
648+
if out := p.walkAliasType(u, in); out != nil {
649+
return out
650+
}
651+
645652
switch t := in.(type) {
646653
case *gotypes.Struct:
647654
out := u.Type(name)

v2/parser/parse_122.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build go1.22
2+
// +build go1.22
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package parser
21+
22+
import (
23+
gotypes "go/types"
24+
25+
"k8s.io/gengo/v2/types"
26+
)
27+
28+
func (p *Parser) walkAliasType(u types.Universe, in gotypes.Type) *types.Type {
29+
if t, isAlias := in.(*gotypes.Alias); isAlias {
30+
return p.walkType(u, nil, gotypes.Unalias(t))
31+
}
32+
return nil
33+
}

v2/parser/parse_pre_122.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build !go1.22
2+
// +build !go1.22
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package parser
21+
22+
import (
23+
gotypes "go/types"
24+
25+
"k8s.io/gengo/v2/types"
26+
)
27+
28+
func (p *Parser) walkAliasType(u types.Universe, in gotypes.Type) *types.Type {
29+
return nil
30+
}

0 commit comments

Comments
 (0)