Skip to content

Commit 5ab8f90

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: operand.convertibleTo must consider Alias types
Fixes regression from Go 1.22. Fixes #67540. For #67547. Change-Id: I61f642970c6a9bd8567654bb5ecf645ae77b3bcc Reviewed-on: https://go-review.googlesource.com/c/go/+/587159 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 2bf686d commit 5ab8f90

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,16 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
139139
return true
140140
}
141141

142-
// "V and T have identical underlying types if tags are ignored
143-
// and V and T are not type parameters"
144-
V := x.typ
142+
origT := T
143+
V := Unalias(x.typ)
144+
T = Unalias(T)
145145
Vu := under(V)
146146
Tu := under(T)
147147
Vp, _ := V.(*TypeParam)
148148
Tp, _ := T.(*TypeParam)
149+
150+
// "V and T have identical underlying types if tags are ignored
151+
// and V and T are not type parameters"
149152
if IdenticalIgnoreTags(Vu, Tu) && Vp == nil && Tp == nil {
150153
return true
151154
}
@@ -267,7 +270,7 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
267270
}
268271
x.typ = V.typ
269272
if !x.convertibleTo(check, T, cause) {
270-
errorf("cannot convert %s (in %s) to type %s", V.typ, Vp, T)
273+
errorf("cannot convert %s (in %s) to type %s", V.typ, Vp, origT)
271274
return false
272275
}
273276
return true

src/go/types/conversions.go

+7-4
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

+40
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,43 @@ func _[P map[int]int]() {
2626
var m A
2727
clear(m) // don't report an error for m
2828
}
29+
30+
type S1 struct {
31+
x int "S1.x"
32+
}
33+
34+
type S2 struct {
35+
x int "S2.x"
36+
}
37+
38+
func _[P1 S1, P2 S2]() {
39+
type A = P1
40+
var p A
41+
_ = P2(p) // conversion must be valid
42+
}
43+
44+
func _[P1 S1, P2 S2]() {
45+
var p P1
46+
type A = P2
47+
_ = A(p) // conversion must be valid
48+
}
49+
50+
func _[P int | string]() {
51+
var p P
52+
type A = int
53+
// preserve target type name A in error messages when using Alias types
54+
// (test are run with and without Alias types enabled, so we need to
55+
// keep both A and int in the error message)
56+
_ = A(p /* ERRORx "cannot convert string .* to type (A|int)" */)
57+
}
58+
59+
// Test case for go.dev/issue/67540.
60+
func _() {
61+
type (
62+
S struct{}
63+
A = *S
64+
T S
65+
)
66+
var p A
67+
_ = (*T)(p)
68+
}

0 commit comments

Comments
 (0)