rf: type unification for type patterns
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 692
- Forks
- 29
- PR merge metrics
- No merged PRs in 30d
Description
The normal semantics for var t T in an ex rule is that t will match any expression whose type is assignable to T. In the example below, i and v are both assignable to I, which implements interface { Equal(I) bool }, so I would expect all of the equals to be rewritten. However, currently only the first gets rewritten:
ex {
type T interface { Equal(T) bool }
var t1, t2 T
t1 == t2 -> t1.Equal(t2)
}
-- x.go --
package p
type I interface { Equal(I) bool }
var i I
type V int
func (V) Equal(I) bool
var v V
var _ I = v
var _ = i == i
var _ = i == v
var _ = v == i
var _ = v == v
I suspect matcher.matchWildcardType needs to be extended to support type unification rather than requiring strict type identity for back matches.
There's probably some subtlety here for preferring that the unification to prefer unifying to I rather than interface { Equal(I) bool }. Might want to learn from how types2 handles this.
In the mean time, it's possible to manually workaround this without too many additional rules:
ex {
type A interface{ Equal(A) bool }
type B interface{ Equal(A) bool }
var a1, a2 A
var b1, b2 B
a1 == a2 -> a1.Equal(a2)
a1 == b2 -> a1.Equal(b2)
b1 == a2 -> b1.Equal(a2)
b1 == b2 -> b1.Equal(b2)
}
-- x.go --
package p
type I interface{ Equal(I) bool }
var i I
type V int
func (V) Equal(I) bool
var v V
var _ I = v
var _ = i == i
var _ = i == v
var _ = v == i
var _ = v == v
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with matcher.matchWildcardType and the provided ex rule and x.go example. Compare the needed type unification behavior with how types2 handles unification, including preference for I over the expanded interface. Done means all four equality expressions are rewritten by the single rule.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100