@@ -205,10 +205,10 @@ func (u *unifier) join(x, y *TypeParam) bool {
205
205
return true
206
206
}
207
207
208
- // asTypeParam returns x.(*TypeParam) if x is a type parameter recorded with u.
208
+ // asBoundTypeParam returns x.(*TypeParam) if x is a type parameter recorded with u.
209
209
// Otherwise, the result is nil.
210
- func (u * unifier ) asTypeParam (x Type ) * TypeParam {
211
- if x , _ := x .(* TypeParam ); x != nil {
210
+ func (u * unifier ) asBoundTypeParam (x Type ) * TypeParam {
211
+ if x , _ := Unalias ( x ) .(* TypeParam ); x != nil {
212
212
if _ , found := u .handles [x ]; found {
213
213
return x
214
214
}
@@ -269,7 +269,7 @@ func (u *unifier) inferred(tparams []*TypeParam) []Type {
269
269
// asInterface returns the underlying type of x as an interface if
270
270
// it is a non-type parameter interface. Otherwise it returns nil.
271
271
func asInterface (x Type ) (i * Interface ) {
272
- if _ , ok := x .(* TypeParam ); ! ok {
272
+ if _ , ok := Unalias ( x ) .(* TypeParam ); ! ok {
273
273
i , _ = under (x ).(* Interface )
274
274
}
275
275
return i
@@ -291,11 +291,8 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
291
291
u .depth --
292
292
}()
293
293
294
- x = Unalias (x )
295
- y = Unalias (y )
296
-
297
294
// nothing to do if x == y
298
- if x == y {
295
+ if x == y || Unalias ( x ) == Unalias ( y ) {
299
296
return true
300
297
}
301
298
@@ -314,7 +311,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
314
311
// Ensure that if we have at least one
315
312
// - defined type, make sure one is in y
316
313
// - type parameter recorded with u, make sure one is in x
317
- if asNamed (x ) != nil || u .asTypeParam (y ) != nil {
314
+ if asNamed (x ) != nil || u .asBoundTypeParam (y ) != nil {
318
315
if traceInference {
319
316
u .tracef ("%s ≡ %s\t // swap" , y , x )
320
317
}
@@ -358,7 +355,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
358
355
// isTypeLit(x) is false and y was not changed above. In other
359
356
// words, if y was a defined type, it is still a defined type
360
357
// (relevant for the logic below).
361
- switch px , py := u .asTypeParam (x ), u .asTypeParam (y ); {
358
+ switch px , py := u .asBoundTypeParam (x ), u .asBoundTypeParam (y ); {
362
359
case px != nil && py != nil :
363
360
// both x and y are type parameters
364
361
if u .join (px , py ) {
@@ -449,7 +446,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
449
446
}
450
447
451
448
// x != y if we get here
452
- assert (x != y )
449
+ assert (x != y && Unalias ( x ) != Unalias ( y ) )
453
450
454
451
// If u.EnableInterfaceInference is set and we don't require exact unification,
455
452
// if both types are interfaces, one interface must have a subset of the
@@ -573,6 +570,10 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
573
570
emode |= exact
574
571
}
575
572
573
+ // Continue with unaliased types but don't lose original alias names, if any (go.dev/issue/67628).
574
+ xorig , x := x , Unalias (x )
575
+ yorig , y := y , Unalias (y )
576
+
576
577
switch x := x .(type ) {
577
578
case * Basic :
578
579
// Basic types are singletons except for the rune and byte
@@ -751,7 +752,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
751
752
case * TypeParam :
752
753
// x must be an unbound type parameter (see comment above).
753
754
if debug {
754
- assert (u .asTypeParam (x ) == nil )
755
+ assert (u .asBoundTypeParam (x ) == nil )
755
756
}
756
757
// By definition, a valid type argument must be in the type set of
757
758
// the respective type constraint. Therefore, the type argument's
@@ -774,13 +775,13 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
774
775
// need to take care of that case separately.
775
776
if cx := coreType (x ); cx != nil {
776
777
if traceInference {
777
- u .tracef ("core %s ≡ %s" , x , y )
778
+ u .tracef ("core %s ≡ %s" , xorig , yorig )
778
779
}
779
780
// If y is a defined type, it may not match against cx which
780
781
// is an underlying type (incl. int, string, etc.). Use assign
781
782
// mode here so that the unifier automatically takes under(y)
782
783
// if necessary.
783
- return u .nify (cx , y , assign , p )
784
+ return u .nify (cx , yorig , assign , p )
784
785
}
785
786
}
786
787
// x != y and there's nothing to do
@@ -789,7 +790,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
789
790
// avoid a crash in case of nil type
790
791
791
792
default :
792
- panic (sprintf (nil , true , "u.nify(%s, %s, %d)" , x , y , mode ))
793
+ panic (sprintf (nil , true , "u.nify(%s, %s, %d)" , xorig , yorig , mode ))
793
794
}
794
795
795
796
return false
0 commit comments