rsc / rsc/rf

rf: type unification for type patterns

Open
#6 0 comments 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.