Breaking Changes in Cadence parser? 🤔

This one would work:

  const check = await fcl.send([
    sdk.script`pub fun main():Int { return 2 }`
  ]);
  const result = await fcl.decode(check);

This one will throw:

  const check = await fcl.send([
    sdk.script`pub fun main():Int{ return 2 }`
  ]);
  const result = await fcl.decode(check);

The only difference is the abscence of white space after Int type. As soon as you add it - it will work as well. Tried to run in Playground - both versions work just fine…

level=warning msg="\x1b[1;31mERR\x1b[0m \x1b[2m[e5b19e]\x1b[0m Execution failed:\nParsing failed:\nunexpected token: got decimal integer, expected ',' or '}'\n:\n unexpected token: got decimal integer, expected ',' or '}'\n"

I’ve checked different versions of emulators via Docker:

  • 0.4.0 - Works just fine without space after Int type
  • 0.5.0 and higher - Throws error

@MaxStarka Thank you for reporting this!

In Cadence v0.5.0 we’ve enabled the new parser, and it looks like the behaviour between the old and new one is different here.

The example above actually shows an ambiguity: Is the return type annotation : Int{ the type Int and the open curly brace the beginning of the body of the function, or is it the start of a restricted type Int{...}?

I think given that the former is more common, we should probably parse it as that.
I’ve opened a bug report in the Cadence repository and plan to fix it next week: https://github.com/onflow/cadence/issues/258

1 Like