-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/types, types2: use exact unification for component types
This change defines two unification modes used to control unification: - assign set when unifying types involved in an assignment - exact if set, types unify if they can be made identical Currently, unification is inexact: when a defined type is compared against a type literal, the underlying type of the defined type is considered. When channel types are compared, the channel direction is ignored. And when defined types are compared where one (or both) are interfaces, interface unification is used. By contrast, exact unification requires types to match exactly: if they can be unified, the types must be identical (with suitable type arguments). Exact unification is required when comparing component types. For instance, when unifying func(x P) with func(x Q), the two signatures unify only if P is identical to Q per Go's assignment rules. Until now we have ignored exact unification and made due with inexact unification everywhere, even for component types. In some cases this led to infinite recursions in the unifier, which we guarded against with a depth limit (and unification failure). Go's assignmemt rules allow inexact matching at the top-level but require exact matching for element types. This change passes 'assign' to the unifier when unifying parameter against argument types because those follow assignment rules. When comparing constraints, inexact unification is used as before. In 'assign' mode, when comparing element types, the unifyier is called recursively, this time with the 'exact' mode set, causing element types to be compared exactly. If unification succeeds for element types, they are identical (with suitable type arguments). This change fixes #60460. It also fixes a bug in the test for issue #60377. We also don't need to rely anymore on the recursion depth limit (a temporary fix) for #59740. Finally, because we use exact unification when comparing element types which are channels, errors caused by assignment failures (due to inexact inference which succeeded when it shouldn't have) now produce the correct inference error. Fixes #60460. For #60377. For #59740. Change-Id: Icb6a9b4dbd34294f99328a06d52135cb499cab85 Reviewed-on: https://go-review.googlesource.com/c/go/+/498895 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
- Loading branch information
Showing
7 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package p | ||
|
||
// Simplified (representative) test case. | ||
|
||
func _() { | ||
f(R1{}) | ||
} | ||
|
||
func f[T any](R[T]) {} | ||
|
||
type R[T any] interface { | ||
m(R[T]) | ||
} | ||
|
||
type R1 struct{} | ||
|
||
func (R1) m(R[int]) {} | ||
|
||
// Test case from issue. | ||
|
||
func _() { | ||
r := newTestRules() | ||
NewSet(r) | ||
r2 := newTestRules2() | ||
NewSet(r2) | ||
} | ||
|
||
type Set[T any] struct { | ||
rules Rules[T] | ||
} | ||
|
||
func NewSet[T any](rules Rules[T]) Set[T] { | ||
return Set[T]{ | ||
rules: rules, | ||
} | ||
} | ||
|
||
func (s Set[T]) Copy() Set[T] { | ||
return NewSet(s.rules) | ||
} | ||
|
||
type Rules[T any] interface { | ||
Hash(T) int | ||
Equivalent(T, T) bool | ||
SameRules(Rules[T]) bool | ||
} | ||
|
||
type testRules struct{} | ||
|
||
func newTestRules() Rules[int] { | ||
return testRules{} | ||
} | ||
|
||
func (r testRules) Hash(val int) int { | ||
return val % 16 | ||
} | ||
|
||
func (r testRules) Equivalent(val1 int, val2 int) bool { | ||
return val1 == val2 | ||
} | ||
|
||
func (r testRules) SameRules(other Rules[int]) bool { | ||
_, ok := other.(testRules) | ||
return ok | ||
} | ||
|
||
type testRules2 struct{} | ||
|
||
func newTestRules2() Rules[string] { | ||
return testRules2{} | ||
} | ||
|
||
func (r testRules2) Hash(val string) int { | ||
return 16 | ||
} | ||
|
||
func (r testRules2) Equivalent(val1 string, val2 string) bool { | ||
return val1 == val2 | ||
} | ||
|
||
func (r testRules2) SameRules(other Rules[string]) bool { | ||
_, ok := other.(testRules2) | ||
return ok | ||
} |