JSAbrahams / mamba

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

Add branching model to constraint builder #430

Closed JSAbrahams closed 1 year ago

JSAbrahams commented 1 year ago

Relevant issues

Summary

Another addition to the constraint builder, similar to #416 . This time, we implement a new branching model which allows us to now finally properly check converging paths:

We now only create branches for:

This means that we don't unnecessarily create new sets when entering classes, for loops, while loops, etc. Only when we have diverging execution paths. Shadowing of variables is now no longer maintained partially by different sets, but instead by shadowing of variables in:

As a result of this refactor, we also moved some var mapping logic to the builder to make it easier to change. This means that Expected::from(AST) has been greatly simplified. In fact, there was no reason for it to return a result now.

Destruction of variables

Due to the change in behavior, we had to add destruction variables in the environment in case we are no longer allowed to access them:

In other locations where we use a type alias we should also destruct variables. For instance, if in match if we in future allow type aliases to be used there.

Propagate assigns

If we assign to a variable ,that variable has a type. Normally we annotate this with a type. We now also do this even when we propagate an assign into a match or if:

Example with if:

def my_var := if True then
    print("then")
    10
else
    print("else")
    20

Generates:

if True:
    print("then")
    my_var: int = 10
else:
    print("else")
    my_var: int = 20

Before:

if True:
    print("then")
    my_var = 10
else:
    print("else")
    my_var = 20
codecov[bot] commented 1 year ago

Codecov Report

Merging #430 (47478a7) into develop (25f6ef4) will increase coverage by 0.25%. The diff coverage is 96.51%.

@@             Coverage Diff             @@
##           develop     #430      +/-   ##
===========================================
+ Coverage    88.34%   88.59%   +0.25%     
===========================================
  Files          110      110              
  Lines        11997    11956      -41     
===========================================
- Hits         10599    10593       -6     
+ Misses        1398     1363      -35     
Impacted Files Coverage Δ
src/check/constrain/generate/mod.rs 95.08% <ø> (ø)
src/check/constrain/mod.rs 100.00% <ø> (+28.88%) :arrow_up:
src/parse/collection.rs 89.51% <ø> (+2.79%) :arrow_up:
src/check/constrain/unify/function.rs 78.26% <12.50%> (-3.23%) :arrow_down:
src/check/constrain/generate/collection.rs 94.25% <92.59%> (-2.53%) :arrow_down:
src/generate/convert/mod.rs 95.19% <95.00%> (+0.58%) :arrow_up:
src/check/constrain/generate/env.rs 98.80% <95.23%> (+4.36%) :arrow_up:
src/check/constrain/constraint/builder.rs 96.50% <97.60%> (+10.50%) :arrow_up:
src/check/constrain/constraint/expected.rs 90.24% <100.00%> (+2.12%) :arrow_up:
src/check/constrain/constraint/iterator.rs 84.61% <100.00%> (-1.60%) :arrow_down:
... and 59 more