You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: incorrect type set intersection computation #57486
I read the new semantic changes several times. Maybe I still haven't got it.
I don't get why the type set of C2 is not [int]. I don't think it is relevant to the 1.20 semantic changes.
My understanding is that C2 is only satisfied by [2]any only when [2]any is used as a value type (but for the above example, it is not anyway), not a type constraint. (That is why the F1 function fails to compile, which is an expected behavior.)
type non_comparable interface {
[2]any | int
}
type C2 interface {
comparable
non_comparable
}
non_comparable does not implement comparable, because not all types in the type set of non_comparable are comparable. So there's no type that both implement comparable and does not implement comparable, so C2's type set is empty.
@go101
(from my understanding)
The real constraint is interface{[2]any}
It has to implement comparable but does not implement the constraint strictly comparable. (easily understood since implementation is type set inclusion)
Strictly comparable is different from comparable in that it asserts that the members of its type set are themselves implementing comparable instead of just satisfying it.
(i.e. the members of stricly comparable are themselves strictly comparable)
According to my current reading of the spec at tip, strictly comparable does not matter here because [2]any is not an interface. [2]any satisfies comparable because it has the comparison operators. We have type set inclusion. So the type parameter constraint you are using actually implements C2. All is fine.
@go101 In that case, C2 is reducible/can be rewritten to interface{~int}.
Embedding the comparable constraint enforces that any type argument has to be(means "has to satisfy") comparable. Via the conjunction/embedding, C2 enforces that any potential type argument is comparable.
C2 is just not the canonical form of the expressed constraint. ~[]byte can be eliminated.
Passing a type inferred variable of type []byte to G2 doesn't compile (it's not comparable). It's normal. :)
changed the title [-]cmd/compile: incorrectly allow to use a type parameter as type argument (not very sure it is a bug or not)[/-][+]cmd/compile: incorrect type set intersection computation[/+]on Dec 28, 2022
Activity
cuonglm commentedon Dec 28, 2022
F2compile ok, becauseC2is an empty type set.There is no type that both implement
comparableand is an array[2]any.ianlancetaylor commentedon Dec 28, 2022
Note that in 1.20
C2is satisfied by[2]any, per #56548.It's not clear to me why this compiles with 1.18 and 1.19. CC @griesemer .
cuonglm commentedon Dec 28, 2022
@ianlancetaylor
C2is an empty type set, the same as:zigo101 commentedon Dec 28, 2022
@cuonglm
But why doesn't the type set of C2 contain
int?I looks the calculated type set of C2 in the following code is indeed
[[2]any, struct{x any}, int], which is obviously wrong.cuonglm commentedon Dec 28, 2022
@go101 the semantic for go1.20 was changed, see Ian's comment above.
zigo101 commentedon Dec 28, 2022
I read the new semantic changes several times. Maybe I still haven't got it.
I don't get why the type set of C2 is not
[int]. I don't think it is relevant to the 1.20 semantic changes.And per @ianlancetaylor 's
My understanding is that C2 is only satisfied by
[2]anyonly when[2]anyis used as a value type (but for the above example, it is not anyway), not a type constraint. (That is why theF1function fails to compile, which is an expected behavior.)cuonglm commentedon Dec 28, 2022
You can see
C2as:non_comparabledoes not implementcomparable, because not all types in the type set ofnon_comparablearecomparable. So there's no type that both implementcomparableand does not implementcomparable, soC2's type set is empty.See: https://go.dev/ref/spec#Type_constraints
zigo101 commentedon Dec 28, 2022
I don't think the interpretation is right, if the compilation behavior is right for the following program.
atdiar commentedon Dec 28, 2022
@go101
(from my understanding)
The real constraint is
interface{[2]any}It has to implement
comparablebut does not implement the constraint strictly comparable. (easily understood since implementation is type set inclusion)Strictly comparableis different fromcomparablein that it asserts that the members of its type set are themselves implementingcomparableinstead of just satisfying it.(i.e. the members of stricly comparable are themselves strictly comparable)
According to my current reading of the spec at tip, strictly comparable does not matter here because
[2]anyis not an interface.[2]anysatisfies comparable because it has the comparison operators. We have type set inclusion. So the type parameter constraint you are using actually implements C2. All is fine.The compiler is correct.
zigo101 commentedon Dec 28, 2022
Okay, now the only I don't understand is why the
_ = t == tline doesn't compile? (edit: I'm still confused in several points, ;))cuonglm commentedon Dec 28, 2022
@go101 you're right. I was wrong that the type set is empty, it's actually contain
[2]anyandint. Running gotype with tracing enabled:atdiar commentedon Dec 28, 2022
@go101 In that case,
C2is reducible/can be rewritten tointerface{~int}.Embedding the
comparableconstraint enforces that any type argument has to be(means "has to satisfy") comparable. Via the conjunction/embedding, C2 enforces that any potential type argument is comparable.C2 is just not the canonical form of the expressed constraint.
~[]bytecan be eliminated.Passing a type inferred variable of type []byte to G2 doesn't compile (it's not comparable). It's normal. :)
zigo101 commentedon Dec 28, 2022
@atdiar
More specifically, I mean why the
_ = t == tline in the following code doesn't compile.atdiar commentedon Dec 28, 2022
@go101 ah yes, you're right, I actually don't know. Looks like a bug to me as well.
cc @ianlancetaylor @griesemer
18 remaining items
griesemer commentedon Dec 28, 2022
This is now fixed (but not yet submitted). As @ianlancetaylor correctly noted, this should not have worked with Go 1.18 or Go 1.19.
@gopherbot please consider this for backport to 1.18 and 1.19. This is a compiler bug.
gopherbot commentedon Dec 28, 2022
Backport issue(s) opened: #57498 (for 1.18), #57499 (for 1.19).
Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.
[-]cmd/compile: incorrectly allow to use a type parameter as type argument (not very sure it is a bug or not)[/-][+]cmd/compile: incorrect type set intersection computation[/+]gopherbot commentedon Dec 29, 2022
Change https://go.dev/cl/460015 mentions this issue:
[release-branch.go1.18] go/types, types2: use strict comparability for type set intersectiongopherbot commentedon Dec 29, 2022
Change https://go.dev/cl/460016 mentions this issue:
[release-branch.go1.19] go/types, types2: use strict comparability for type set intersectiongo/types, types2: use strict comparability for type set intersection