Skip to content

Commit 8ce01ac

Browse files
authored
Bump go.mod to 1.23 and remove alias replacements (#248)
Alias replacements derived from syntax were introduced in #220 as a way to ensure the aliases used in source code were also used. This helped ensure packages mode worked on go1.22, which didn't have explicit alias node support in the `go/types` package. Alias replacements have a couple issues: * They flat out replace any would-be references to an underlying type with an alias type. * They don't properly handle aliases to generic type instantiations (ref: #243) Now that go1.24 is released, we can bump `go.mod` to go1.23, which means we can ensure `go/types` has an explicit `types.Alias` node for type aliases, and we can remove the alias replacement logic.
1 parent 6568d88 commit 8ce01ac

File tree

9 files changed

+29
-132
lines changed

9 files changed

+29
-132
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
go:
1313
strategy:
1414
matrix:
15-
go-version: [1.22.x, 1.23.x] # oldest version that can build go mock and official supported go versions
15+
go-version: [1.23.x, 1.24.x] # oldest version that can build go mock and official supported go versions
1616
os: [ubuntu-latest]
1717
runs-on: ${{ matrix.os }}
1818
steps:
@@ -71,4 +71,4 @@ jobs:
7171

7272
- name: Run Bazel tests
7373
run: |
74-
cd bazel && bazel test //...
74+
cd bazel && bazel test //...

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.uber.org/mock
22

3-
go 1.22
3+
go 1.23
44

55
require (
66
github.com/stretchr/testify v1.9.0

mockgen/internal/tests/alias/mock/interfaces.go

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

mockgen/internal/tests/generics/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.uber.org/mock/mockgen/internal/tests/generics
22

3-
go 1.22
3+
go 1.23
44

55
replace go.uber.org/mock => ../../../..
66

mockgen/internal/tests/package_mode/mock/interfaces.go

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

mockgen/internal/tests/typed/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.uber.org/mock/mockgen/internal/tests/typed
22

3-
go 1.22
3+
go 1.23
44

55
replace go.uber.org/mock => ../../../..
66

mockgen/package_mode.go

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"flag"
66
"fmt"
7-
"go/ast"
87
"go/types"
98
"strings"
109

@@ -18,20 +17,6 @@ var (
1817

1918
type packageModeParser struct {
2019
pkgName string
21-
22-
// Mapping from underlying types to aliases used within the package source.
23-
//
24-
// We prefer to use aliases used in the source rather than underlying type names
25-
// as those may be unexported or internal.
26-
// TODO(joaks): Once mock is Go1.23+ only, we can remove this
27-
// as the casing for types.Alias will automatically handle this
28-
// in all cases.
29-
aliasReplacements map[types.Type]aliasReplacement
30-
}
31-
32-
type aliasReplacement struct {
33-
name string
34-
pkg string
3520
}
3621

3722
func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*model.Package, error) {
@@ -42,8 +27,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
4227
return nil, fmt.Errorf("load package: %w", err)
4328
}
4429

45-
p.buildAliasReplacements(pkg)
46-
4730
interfaces, err := p.extractInterfacesFromPackage(pkg, ifaces)
4831
if err != nil {
4932
return nil, fmt.Errorf("extract interfaces from package: %w", err)
@@ -56,90 +39,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
5639
}, nil
5740
}
5841

59-
// buildAliasReplacements finds and records any references to aliases
60-
// within the given package's source.
61-
// These aliases will be preferred when parsing types
62-
// over the underlying name counterparts, as those may be unexported / internal.
63-
//
64-
// If a type has more than one alias within the source package,
65-
// the latest one to be inspected will be the one used for mapping.
66-
// This is fine, since all aliases and their underlying types are interchangeable
67-
// from a type-checking standpoint.
68-
func (p *packageModeParser) buildAliasReplacements(pkg *packages.Package) {
69-
p.aliasReplacements = make(map[types.Type]aliasReplacement)
70-
71-
// checkIdent checks if the given identifier exists
72-
// in the given package as an alias, and adds it to
73-
// the alias replacements map if so.
74-
checkIdent := func(pkg *types.Package, ident string) bool {
75-
scope := pkg.Scope()
76-
if scope == nil {
77-
return true
78-
}
79-
obj := scope.Lookup(ident)
80-
if obj == nil {
81-
return true
82-
}
83-
objTypeName, ok := obj.(*types.TypeName)
84-
if !ok {
85-
return true
86-
}
87-
if !objTypeName.IsAlias() {
88-
return true
89-
}
90-
typ := objTypeName.Type()
91-
if typ == nil {
92-
return true
93-
}
94-
p.aliasReplacements[typ] = aliasReplacement{
95-
name: objTypeName.Name(),
96-
pkg: pkg.Path(),
97-
}
98-
return false
99-
100-
}
101-
102-
for _, f := range pkg.Syntax {
103-
fileScope, ok := pkg.TypesInfo.Scopes[f]
104-
if !ok {
105-
continue
106-
}
107-
ast.Inspect(f, func(node ast.Node) bool {
108-
109-
// Simple identifiers: check if it is an alias
110-
// from the source package.
111-
if ident, ok := node.(*ast.Ident); ok {
112-
return checkIdent(pkg.Types, ident.String())
113-
}
114-
115-
// Selector expressions: check if it is an alias
116-
// from the package represented by the qualifier.
117-
selExpr, ok := node.(*ast.SelectorExpr)
118-
if !ok {
119-
return true
120-
}
121-
122-
x, sel := selExpr.X, selExpr.Sel
123-
xident, ok := x.(*ast.Ident)
124-
if !ok {
125-
return true
126-
}
127-
128-
xObj := fileScope.Lookup(xident.String())
129-
pkgName, ok := xObj.(*types.PkgName)
130-
if !ok {
131-
return true
132-
}
133-
134-
xPkg := pkgName.Imported()
135-
if xPkg == nil {
136-
return true
137-
}
138-
return checkIdent(xPkg, sel.String())
139-
})
140-
}
141-
}
142-
14342
func (p *packageModeParser) loadPackage(packageName string) (*packages.Package, error) {
14443
var buildFlagsSet []string
14544
if *buildFlags != "" {
@@ -300,14 +199,6 @@ func (p *packageModeParser) parseType(t types.Type) (model.Type, error) {
300199
pkg = object.Obj().Pkg().Path()
301200
}
302201

303-
// If there was an alias to this type used somewhere in the source,
304-
// use that alias instead of the underlying type,
305-
// since the underlying type might be unexported.
306-
if alias, ok := p.aliasReplacements[t]; ok {
307-
name = alias.name
308-
pkg = alias.pkg
309-
}
310-
311202
// TypeArgs method not available for aliases in go1.22
312203
genericType, ok := t.(interface{ TypeArgs() *types.TypeList })
313204
if !ok || genericType.TypeArgs() == nil {

mockgen/package_mode_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ func Test_packageModeParser_parsePackage(t *testing.T) {
314314
Name: "AddHumans",
315315
In: []*model.Parameter{
316316
{
317-
Type: model.PredeclaredType("int"),
317+
Type: &model.NamedType{
318+
Package: "go.uber.org/mock/mockgen/internal/tests/package_mode",
319+
Type: "HumansCount",
320+
},
318321
},
319322
},
320323
Out: []*model.Parameter{
@@ -333,7 +336,10 @@ func Test_packageModeParser_parsePackage(t *testing.T) {
333336
Name: "HumanPopulation",
334337
Out: []*model.Parameter{
335338
{
336-
Type: model.PredeclaredType("int"),
339+
Type: &model.NamedType{
340+
Package: "go.uber.org/mock/mockgen/internal/tests/package_mode",
341+
Type: "HumansCount",
342+
},
337343
},
338344
},
339345
},
@@ -444,13 +450,13 @@ func TestAliases(t *testing.T) {
444450
In: []*model.Parameter{{
445451
Type: &model.NamedType{
446452
Package: "go.uber.org/mock/mockgen/internal/tests/alias",
447-
Type: "FooerAlias",
453+
Type: "Fooer",
448454
},
449455
}},
450456
Out: []*model.Parameter{{
451457
Type: &model.NamedType{
452458
Package: "go.uber.org/mock/mockgen/internal/tests/alias",
453-
Type: "FooerAlias",
459+
Type: "Fooer",
454460
},
455461
}},
456462
}},

tools/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/uber-go/mock/tools
22

3-
go 1.22
3+
go 1.23
44

55
require github.com/stretchr/testify v1.9.0
66

0 commit comments

Comments
 (0)