hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.23k stars 224 forks source link

[BUG] semicolons allowed in parameter list #1097

Closed gregmarr closed 2 weeks ago

gregmarr commented 3 weeks ago

Describe the bug Cppfront allows a semicolon at the end of each parameter, before the comma.

To Reproduce Steps to reproduce the behavior:

  1. Sample code

Found from https://github.com/isidorostsa/RayTrayCpp2/blob/main/src/inc/camera.h2#L32, but much simpler:

func: (a: double;, b: uint;, c: double;) = {}
  1. Command lines including which C++ compiler you are using

https://cpp2.godbolt.org

  1. Expected result - what you expected to happen

Error for having a semicolon in the parameter list.

  1. Actual result/error

Success.

Additional context I tried digging into the grammar to see where this semicolon is being allowed, but I couldn't find it.

Matching `(a: double;, b: uint;, c: double;)`
//G parameter-declaration-list:
//G     '(' parameter-declaration-seq? ','? ')' 
'(' <a: double;, b: uint;, c: double;> <> ')'

//G parameter-declaration-seq:
//G     parameter-declaration 
<a: double;>
<b: uint;>
<c: double;>
//G     parameter-declaration-seq ',' parameter-declaration
<a: double;, b: uint;> ',' <c: double;>
<a: double;> ',' <b: uint;>

//G parameter-declaration:
//G     this-specifier? parameter-direction? declaration 
<> <> <a: double;>
<> <> <b: uint;>
<> <> <c: double;>

//G declaration:
//G     access-specifier? identifier '...'? unnamed-declaration
<> <a> <> <: double;>
<> <b> <> <: uint;>
<> <c> <> <: double;>
//G     access-specifier? identifier alias
No '==' for alias, so these won't match.

//G unnamed-declaration:
//G     ':' meta-functions? template-parameters? function-type requires-clause? '=' statement
No '=', doesn't match.
//G     ':' meta-functions? template-parameters? function-type statement
No '(' for function-type, doesn't match.
//G     ':' meta-functions? template-parameters? type-id? requires-clause? '=' statement
No '=', doesn't match.
//G     ':' meta-functions? template-parameters? type-id
':' <> <> <double;>
':' <> <> <uint;>
//G     ':' meta-functions? template-parameters? 'final'? 'type' requires-clause? '=' statement
No '=', doesn't match.
//G     ':' 'namespace' '=' statement
No '=', doesn't match.

//G type-id:
//G     type-qualifier-seq? qualified-id
No '::' for qualified-id, doesn't match.
//G     type-qualifier-seq? unqualified-id
<> <double;>
<> <uint;>

//G unqualified-id:
//G     identifier
Shouldn't include ';'.
//G     keyword
Not a keyword.
//G     template-id
No '<' for template-id, doesn't match.
//GTODO     operator-function-id
//G     ...
Is this an actual grammar thing that might be picking up `double;` and `uint;`?
sookach commented 3 weeks ago

Thanks for the interesting bug and detailed report. I found the offending code, and the problem lies in the fact that parameter declarations are parsed as regular declarations (which allow a semicolon at the end). I can get a patch out pretty soon.