Skip to content

Commit 8d464e4

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: operand.AssignableTo must consider Alias types
Fixes regression from Go 1.22. For #67547. Change-Id: Id0d07d6b24e1eab6ed1c7476d9d9b82d28aee80a Reviewed-on: https://go-review.googlesource.com/c/go/+/587161 Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 44079f3 commit 8d464e4

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ func (x *operand) assignableTo(check *Checker, T Type, cause *string) (bool, Cod
260260
return true, 0 // avoid spurious errors
261261
}
262262

263-
V := x.typ
263+
origT := T
264+
V := Unalias(x.typ)
265+
T = Unalias(T)
264266

265267
// x's type is identical to T
266268
if Identical(V, T) {
@@ -386,7 +388,7 @@ func (x *operand) assignableTo(check *Checker, T Type, cause *string) (bool, Cod
386388
x.typ = V.typ
387389
ok, code = x.assignableTo(check, T, cause)
388390
if !ok {
389-
errorf("cannot assign %s (in %s) to %s", V.typ, Vp, T)
391+
errorf("cannot assign %s (in %s) to %s", V.typ, Vp, origT)
390392
return false
391393
}
392394
return true

src/go/types/operand.go

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal/types/testdata/fixedbugs/issue67547.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,28 @@ func _[P int | string]() {
5353
// preserve target type name A in error messages when using Alias types
5454
// (test are run with and without Alias types enabled, so we need to
5555
// keep both A and int in the error message)
56-
_ = A(p /* ERRORx "cannot convert string .* to type (A|int)" */)
56+
_ = A(p /* ERRORx `cannot convert string \(in P\) to type (A|int)` */)
57+
}
58+
59+
func _[P struct{ x int }]() {
60+
var x struct{ x int }
61+
type A = P
62+
var _ A = x // assignment must be valid
63+
}
64+
65+
func _[P struct{ x int }]() {
66+
type A = P
67+
var x A
68+
var _ struct{ x int } = x // assignment must be valid
69+
}
70+
71+
func _[P []int | struct{}]() {
72+
type A = []int
73+
var a A
74+
var p P
75+
// preserve target type name A in error messages when using Alias types
76+
a = p // ERRORx `cannot assign struct{} \(in P\) to (A|\[\]int)`
77+
_ = a
5778
}
5879

5980
// Test case for go.dev/issue/67540.

0 commit comments

Comments
 (0)