trill-lang / trill

A type safe, compiled language inspired by (and written in) Swift
MIT License
275 stars 15 forks source link

[WIP] Resolve overloads through the constraint system #61

Closed harlanhaskins closed 4 years ago

harlanhaskins commented 7 years ago

This patch tracks the rewrite of the overload resolution engine in terms of our new constraint solver.

Ideally, this overload resolver will punish coercions in order to arrive at the most optimal overload for a given function call.

harlanhaskins commented 7 years ago

Aw yeah, the build is super broken right now! 😄

segiddins commented 7 years ago

Lol I'm guessing you broke compiling the stdlib

harlanhaskins commented 7 years ago

All overload resolution is broken because the constraint generator is relying on decls being wired up. Not sure how to fix this just yet...

segiddins commented 7 years ago

To summarize the state of this PR: Harlan and I have discussed how to fix the regressions, and the solution we've converged upon is that type solving needs to be done at the statement, rather than expression, level, to allow for cases in which expressions have sub-expressions with multiple possible types -- (see for example what happens with ternaries). For this to happen, the overload resolution code has to be juggled about to play nicely with expressions not being fully types until the enclosing statement has been fully visited.

CodaFi commented 7 years ago

type solving needs to be done at the statement, rather than expression, level, to allow for cases in which expressions have sub-expressions with multiple possible types -- (see for example what happens with ternaries)

The rule for this is that you type check both branches and if they yield two disjunctions you can either force the user to specify a contextual type or have some merge heuristic (try to take an intersection and see if you get a singular set, maybe?). Otherwise you can use whatever branch that doesn't have a disjunct in it as a contextual type and keep solving.

Solving at the statement level should only be done if you intend to start propagating solution sets between statements.