Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception signature syntax is not ideal #53

Open
osa1 opened this issue Jan 15, 2025 · 1 comment
Open

Exception signature syntax is not ideal #53

osa1 opened this issue Jan 15, 2025 · 1 comment
Labels

Comments

@osa1
Copy link
Member

osa1 commented Jan 15, 2025

This syntax is not in main yet, but it will be soon.

In the exceptions branch (#51) we use braces for exceptions of a function:

fn forEachWithControlFlow(vec: Vec[t], f: Fn(t): {Break, ..r}): {..r}

The exception type is normally a type like any other except we require it to be a variant so that throwing functions will compose.

But we can't use the normal type syntax with brackets for variants as that makes it impossible to distinguish exception type from return type when we specify only one of them:

fn f1(): []     # is `[]` the exception type or return type?

We currently only accept a special kind of variant syntax where delimiters are braces instead of brackets.

Ideally there should be one variant syntax, so we should use [].

Another issue is requiring only variants in the exception type position also makes it impossible to have a function like:

fn f2(): r ()

Instead we have to do:

fn f3(): {..r} ()

Which is the same thing if we only allow throwing variants (which we do currently), but more verbose.

I spent way too much time trying to come up with a syntax that lalrpop will accept. For now I'll merge this syntax as-is but we should fix this soon.

One of the other syntax that I've tried is with ~ prefix for the exception type, e.g.

fn forEachWithControlFlow(vec: Vec[t], f: Fn(t): ~[Break, ..r]): ~[..r]

However that causes LR(1) issues, if the exception type is A and a [ follows:

fn f(): ~A []

The parser can reduce A as a type, or shift [ and try to parse a type argument like in A[U32].

So we need two things:

  • Have different syntax for parameter lists and variants (Use different delimiters for type args and variants #42) to avoid ambiguities when two types are separate by only whitespace.
  • Mark the exception types in signatures to distinguish exception type from return type when there's only one type after :. I'm thinking ! may be an option for this: !errs.
    • Or: require two or zero types, i.e. if you specify one of the return type of exception type you specify the other as well.
      I don't like this too much as I'd like to infer exception types in the future.
@osa1 osa1 added the syntax label Jan 15, 2025
@osa1
Copy link
Member Author

osa1 commented Jan 19, 2025

Have different syntax for parameter lists and variants (#42) to avoid ambiguities when two types are separate by only whitespace.

Not possible with lalrpop, but if we could distinguish A[ from A [ (with whitespace in between) I think we could solve this ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant