JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Limit unification of types and function argument types #459

Open JSAbrahams opened 1 year ago

JSAbrahams commented 1 year ago

Summary

In short, if we have a variable used within a function, say:

# some variable x is declared and used a bunch
f(x)

We can use type inference to get the type of x even if we don't assign it a type above. However, if we already know the type of x, then at most the check stage should check that it is a valid argument to the function. If we instead match it directly, we can get weird situations if the function signature specifies the argument is a union. Then the unification stage will also state that x is this union, which can lead to erroneous output:

Example

def f(x: {Int, Str}) -> Int => 10

def x := 10
f(x)

Will give the following:

# function definition here
x: Union[int, str] = 10
f(x)

While we expect:

x: int = 10
f(x)

Code quality

Move propagate logic to Constraint, moving logic away from Name which shouldn't be there.

Added Tests

WIP: Argument types still leak out unnecessarily in tests: