DanielXMoore / Civet

A TypeScript superset that favors more types and less typing
https://civet.dev
MIT License
1.49k stars 31 forks source link

Pattern Catching Gone Wrong #1485

Open bbrk24 opened 2 days ago

bbrk24 commented 2 days ago

What on earth is this miscompile

try
  foo()
catch e <? RangeError
  bar(e)
(() => {
  try {
    return foo();
  } catch (e) {
    return;
  }
})() instanceof RangeError(bar(e));
bbrk24 commented 2 days ago

Oh, I see what's happening. Adding parens and changing whitespace for clarity, though I'm not sure if this version still parses:

(
  try
    foo
  catch e
) <? RangeError bar(e)
edemaine commented 2 days ago

It's like this:

(try
  foo()
catch e) <? RangeError
  bar(e)

It's not that crazy, given what patterns are currently supported... But obviously not what you wanted. I think mainly we need to implement this form of pattern.

Maybe we also want to forbid content on the same line as a try/catch block, but I could also imagine that it might be useful sometimes...

bbrk24 commented 2 days ago

If nothing else, content before the try is definitely useful. Example:

    ast := try parse input catch err
        if err <? AssertionError
            console.error 'DEBUG ASSERTION FAILED'
            console.error err
        else
            console.error makeErrorString err
        return 1