JSAbrahams / mamba

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

Type error points to cause #425

Closed JSAbrahams closed 1 year ago

JSAbrahams commented 1 year ago

Relevant issues

Summary

For Type error messages, print the cause of an error as well. We point to the location with which we're trying to unify for:

For instance, if we have source (file: list.mamba):

def g := [ 1, 2 ]
def h := [ 4, 9 * 9 mod 3 ]

def i := [ x | x in h, x + 0, x > 3 ]
def j := [ (x, 0) | x in h, x > 0 ]
def k := [ x ^ 2 | x in 0..= 10 ]

We get the following type error message now:

Error: We expected a Bool, but Int does not define __bool__(Int)
 ──→ list.mamba:4:24
   4 | def i := [ x | x in h, x + 0, x > 3 ]
                              ^^^^^
     └─→ In comprehension condition

In the above example, the error message is generated using:

  • The expected against which it is compared, in this case a Bool.
  • The entity being accessed, in this case Int.
  • The function being used, in this case __bool__ because we expect a truthy, as well as its arguments.
  • The underlying cause, which we generate using the message of the constraint itself giving context. Constraint messages are typically generated in the generate stage, which allows us to easily define the context in which they were generated.
  • We use the source and position to print there the ^ arrows in the correct spot.

Lastly, we also streamline print error logic so that it is stored in a single location in common. Both simpler logic, and less chance of inconsistent error messages. Display of TypeErr, ParseErr, and UnimplementedErr all make use of this new logic.

codecov[bot] commented 1 year ago

Codecov Report

Merging #425 (7e440b9) into develop (4596563) will increase coverage by 0.31%. The diff coverage is 73.97%.

@@             Coverage Diff             @@
##           develop     #425      +/-   ##
===========================================
+ Coverage    88.10%   88.41%   +0.31%     
===========================================
  Files          110      110              
  Lines        12042    12019      -23     
===========================================
+ Hits         10610    10627      +17     
+ Misses        1432     1392      -40     
Impacted Files Coverage Δ
src/generate/result.rs 26.66% <33.33%> (+4.08%) :arrow_up:
src/check/constrain/unify/ty.rs 84.04% <64.28%> (+5.82%) :arrow_up:
src/common/result.rs 70.31% <67.27%> (-11.51%) :arrow_down:
src/parse/result.rs 43.07% <72.72%> (-0.07%) :arrow_down:
src/check/result.rs 82.22% <85.71%> (+9.28%) :arrow_up:
src/check/constrain/unify/function.rs 81.48% <90.90%> (+4.90%) :arrow_up:
src/parse/iterator.rs 95.61% <100.00%> (ø)
JSAbrahams commented 1 year ago

We expect [...] seems a bit off. Just say Expected [...]

JSAbrahams commented 1 year ago

We have enough helper methods now. We can start using them properly as we implement others functionality and fix bugs.

JSAbrahams commented 1 year ago

Ah, didn't realize I hadn't pushed the commit address the Expected comment. Will fix in future.