Skip to content

Commit cfbe6cd

Browse files
adonovangopherbot
authored andcommitted
cmd/compile/internal/types2: port CL 576975 to types2
This CL ports to types2 the (passing) test from CL 576975, which fixed a bug in go/types. Updates #66704 Updates #65294 Change-Id: Icdf77e39ed177d9f9ecc435d5125f02f2ee4dd0f Reviewed-on: https://go-review.googlesource.com/c/go/+/579015 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent b107d95 commit cfbe6cd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/cmd/compile/internal/types2/api_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -2985,3 +2985,28 @@ func TestTooNew(t *testing.T) {
29852985
}
29862986
}
29872987
}
2988+
2989+
// This is a regression test for #66704.
2990+
func TestUnaliasTooSoonInCycle(t *testing.T) {
2991+
t.Setenv("GODEBUG", "gotypesalias=1")
2992+
const src = `package a
2993+
2994+
var x T[B] // this appears to cause Unalias to be called on B while still Invalid
2995+
2996+
type T[_ any] struct{}
2997+
type A T[B]
2998+
type B = T[A]
2999+
`
3000+
3001+
f := mustParse(src)
3002+
pkg, err := new(Config).Check("a", []*syntax.File{f}, nil)
3003+
if err != nil {
3004+
t.Fatal(err)
3005+
}
3006+
3007+
B := pkg.Scope().Lookup("B")
3008+
got, want := Unalias(B.Type()).String(), "a.T[a.A]"
3009+
if got != want {
3010+
t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want)
3011+
}
3012+
}

src/cmd/compile/internal/types2/decl.go

+5
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
512512
// TODO(gri) Should be able to use nil instead of Typ[Invalid] to mark
513513
// the alias as incomplete. Currently this causes problems
514514
// with certain cycles. Investigate.
515+
//
516+
// NOTE(adonovan): to avoid the Invalid being prematurely observed
517+
// by (e.g.) a var whose type is an unfinished cycle,
518+
// Unalias does not memoize if Invalid. Perhaps we should use a
519+
// special sentinel distinct from Invalid.
515520
alias := check.newAlias(obj, Typ[Invalid])
516521
setDefType(def, alias)
517522

0 commit comments

Comments
 (0)