jfecher / ante

A safe, easy systems language
http://antelang.org
MIT License
1.9k stars 79 forks source link

Draft: Issue #92 Allow `Maybe` type to be retuned to `If` if no `else` is defined #143

Closed anoojpatel closed 1 year ago

anoojpatel commented 2 years ago

This PR attempts to add MAYBE type into the builtins.rs for Ast::If

anoojpatel commented 1 year ago

I got some time to take a crack at the parser desugaring and got this so far. . I'm seeming to run into codegen issues

➜ ante test.an
thread 'main' panicked at '- inst5 (jump block3(v3)): arg 0 (v3) has type f64, expected i8
- inst5 (jump block3(v3)): mismatched argument count for `jump block3(v3)`: got 1, expected 3
- inst7 (jump block3(v7)): mismatched argument count for `jump block3(v7)`: got 1, expected 3
', src/cranelift_backend/context.rs:194:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[1]    53622 abort      ante test.an

Should I be trying to infer the type differently? My approach is to desugar into unit and then on the typing side, infer the then, and check if otherwise returns unit (in the case we have no else) to then return the custom Maybe. does approach make sense?

jfecher commented 1 year ago

I've implemented this in the branch if-sugar and now am not sure if the feature is wanted or not. It causes issues with some common patterns such as:

loop () ->
    if c then
        ...
        recur ()

Seen in e.g. print_string and several other functions in the prelude alone. When desugared, the if changes to if c then ... Some (recur ()) else None which is problematic since if we say the loop returns a type a then Some (recur ()) of type Maybe a is returned, we then have a = Maybe a which is a type error that can easily confuse users since it is hidden in the syntax sugar.