Skip to content

Commit ab41822

Browse files
committed
cmd/internal/testdir: add a -gomodversion flag
Adds a -gomodversion flag to testdir. This sets the go version in generated go.mod files. This is just runindir tests at the moment. This is a building block so that tests can be written for exported type parameterized aliases (like reproducing #68526). This also adds a test that uses this feature. A type parameterized alias is used so aliastypeparams and gotypesalias must be enabled. gotypesalias is enabled by the go module version. The alias is not exported and will not appear in exportdata. The test shows the package containing the alias can be imported. This encapsulates the level of support of type parameterized aliases in 1.23. Updates #68526 Updates #68778 Change-Id: I8e20df6baa178e1d427d0fff627a16714d9c3b18 Reviewed-on: https://go-review.googlesource.com/c/go/+/604102 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent b292799 commit ab41822

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

src/cmd/internal/testdir/testdir_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ func (t test) run() error {
543543

544544
goexp := goExperiment
545545
godebug := goDebug
546+
gomodvers := ""
546547

547548
// collect flags
548549
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
@@ -583,6 +584,10 @@ func (t test) run() error {
583584
godebug += args[0]
584585
runenv = append(runenv, "GODEBUG="+godebug)
585586

587+
case "-gomodversion": // set the GoVersion in generated go.mod files (just runindir ATM)
588+
args = args[1:]
589+
gomodvers = args[0]
590+
586591
default:
587592
flags = append(flags, args[0])
588593
}
@@ -900,7 +905,11 @@ func (t test) run() error {
900905
t.Fatal(err)
901906
}
902907

903-
modFile := fmt.Sprintf("module %s\ngo 1.14\n", modName)
908+
modVersion := gomodvers
909+
if modVersion == "" {
910+
modVersion = "1.14"
911+
}
912+
modFile := fmt.Sprintf("module %s\ngo %s\n", modName, modVersion)
904913
if err := os.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
905914
t.Fatal(err)
906915
}

test/fixedbugs/issue68526.dir/a/a.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build goexperiment.aliastypeparams
6+
7+
package a
8+
9+
// TODO(#68778): enable once type parameterized aliases are allowed in exportdata.
10+
// type A[T any] = struct{ F T }
11+
12+
type B = struct{ F int }
13+
14+
func F() B {
15+
type a[T any] = struct{ F T }
16+
return a[int]{}
17+
}

test/fixedbugs/issue68526.dir/main.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build goexperiment.aliastypeparams
6+
7+
package main
8+
9+
import (
10+
"issue68526.dir/a"
11+
)
12+
13+
func main() {
14+
unexported()
15+
// exported()
16+
}
17+
18+
func unexported() {
19+
var want struct{ F int }
20+
21+
if any(want) != any(a.B{}) || any(want) != any(a.F()) {
22+
panic("zero value of alias and concrete type not identical")
23+
}
24+
}
25+
26+
// TODO(#68778): enable once type parameterized aliases are allowed in exportdata.
27+
28+
// func exported() {
29+
// var (
30+
// astr a.A[string]
31+
// aint a.A[int]
32+
// )
33+
34+
// if any(astr) != any(struct{ F string }{}) || any(aint) != any(struct{ F int }{}) {
35+
// panic("zero value of alias and concrete type not identical")
36+
// }
37+
38+
// if any(astr) == any(aint) {
39+
// panic("zero value of struct{ F string } and struct{ F int } are not distinct")
40+
// }
41+
42+
// if got := fmt.Sprintf("%T", astr); got != "struct { F string }" {
43+
// panic(got)
44+
// }
45+
// }

test/fixedbugs/issue68526.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// runindir -goexperiment aliastypeparams -gomodversion "1.23"
2+
3+
// Copyright 2024 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 ignored

0 commit comments

Comments
 (0)