Skip to content

Commit

Permalink
fix(semantic): collision between external and fresh type vars
Browse files Browse the repository at this point in the history
Fixes #263.

Type inference replaces any external free type variables with new
fresh type variables. The result is a new but completely equivalent
type expression. Previously during the substitution process
collisions could occur between the names of old type variables and
the names of the new freshly generated type variables. This would
render the new type expression invalid.

To remedy this problem, fresh type variables are now generated
before being substituted into the original type expression. If
there is a collision, that type variable is discarded and a new
one is generated.
  • Loading branch information
jlapacik committed Dec 7, 2018
1 parent a4b2653 commit f5c6aa7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion semantic/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func (v ConstraintGenerator) typeof(n Node) (PolyType, error) {
ftv := n.ExternType.freeVars(nil)
subst := make(Substitution, len(ftv))
for _, tv := range ftv {
subst[tv] = v.cs.f.Fresh()
f := v.cs.f.Fresh()
for ftv.contains(f) {
f = v.cs.f.Fresh()
}
subst[tv] = f
}
t := subst.ApplyType(n.ExternType)
// Check if this type knows about its kind constraints
Expand Down

0 comments on commit f5c6aa7

Please sign in to comment.