Skip to content

Speculative type parsing in expressions does not always suppress parse errors #3062

Open

Description

@trackoor

Bug description

In AssemblyScript, the parser speculatively calls parseType with suppressErrors = true during expression lookahead (such as checking for an arrow function return type after (x) : or type arguments after <), but nested paths inside parseType ignore suppressErrors and emit diagnostics instead of backtracking cleanly.

Inside parseType, tryParseFunctionType does not accept suppressErrors and reports errors when seeing ?, and the union | branch hardcodes suppressErrors = false on recursive parseType calls.

Both functions below are valid TypeScript (tsc parses them cleanly) and should compile without error, just as a ? <i32>c : (a ? b : c) and a < (a >> b) do.

Steps to reproduce

export function test1(a: bool, b: i32, c: i32): i32 {
  return a ? <i32>(c) : (a ? b : c);
}

export function test2(a: i32, b: i32): bool {
  return a < (a | (a >> b));
}

Compiling with asc repro.ts fails with:

ERROR TS1110: Type expected.
 2 │ return a ? <i32>(c) : (a ? b : c);
   │                           ^
ERROR TS1005: ')' expected.
 2 │ return a ? <i32>(c) : (a ? b : c);
   │                          ~
ERROR TS1005: ')' expected.
 6 │ return a < (a | (a >> b));
   │                   ^

Credit: https://ucsbplse.github.io/

AssemblyScript version

v0.28.20

Playground

https://www.assemblyscript.org/editor.html#IyFvcHRpbWl6ZT1zcGVlZCZydW50aW1lPWluY3JlbWVudGFsCmV4cG9ydCBmdW5jdGlvbiB0ZXN0MShhOiBib29sLCBiOiBpMzIsIGM6IGkzMik6IGkzMiB7CiAgcmV0dXJuIGEgPyA8aTMyPihjKSA6IChhID8gYiA6IGMpOwp9CmV4cG9ydCBmdW5jdGlvbiB0ZXN0MihhOiBpMzIsIGI6IGkzMik6IGJvb2wgewogIHJldHVybiBhIDwgKGEgfCAoYSA+PiBiKSk7Cn0KIyFodG1sCg==

Click Run: the module.wat tab shows TS1110: Type expected. and two TS1005: ')' expected. errors for valid code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions