hsutter / cppfront

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

[BUG] Cppfront ICE when parsing invalid syntax in an unnamed function expression #1006

Open bluetarpmedia opened 4 months ago

bluetarpmedia commented 4 months ago

Describe the bug Cppfront crashes with an ICE when given some invalid Cpp2 code.

To Reproduce Run cppfront on this code. The unnamed function / lambda expression has a syntax error.

main: () -> int = {

    v: std::vector<int> = ();
    it:= std::find_if(v.begin(), v.end(), :() return true);   // <-- Syntax error

    return it != v.end();
}

Cppfront reports:

cppfront: source/parse.h:8403: std::unique_ptr<cpp2::declaration_node> cpp2::parser::unnamed_declaration(cpp2::source_position, bool, bool, bool, bool, bool, std::unique_ptr<cpp2::unqualified_id_node>, cpp2::accessibility, bool, cpp2::statement_node*): Assertion `n->initializer && "ICE: should have already validated that there's a valid expression-statement here"' failed.

Repro on Godbolt

RungeCC commented 1 week ago

Reduced to:

f:()  return 0
: () -> int = {}

Notice the following code does not trigger ICE but reports an error at first line:

f:() return 0

g = 1;

The situation seems to be that cppfront tried to continue to parse after encountering an error "missing `;' after return" and then ran into an ICE:

https://github.com/hsutter/cppfront/blob/a76e23b74f91ccec68336ddd5f84edb5b5216a7e/source/parse.h#L7058-L7062

Maybe we should just give up parsing at that point and raise an error? Ping @hsutter.