cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Initializer as a statement: generated code does not compile

Open h0nzZik opened this issue 3 years ago • 0 comments

Describe the bug The fact that initializers of unnamed-declarations are statements makes it possible to write code whose C++ image does not compile.

unnamed-declaration:
  : id-expression-opt = statement

To Reproduce Steps to reproduce the behavior:

  1. This Sample code is accepted by cppfront
// weird2.cpp
#include <functional>
main: () -> int = {
  x: std::function<int()> = return 0;
}

This works and generates weird2.cpp:

$ cppfront weird2.cpp2

but clang 12.0.1 reject the generated file

$ clang++ -std=c++2b -I include/ weird2.cpp
weird2.cpp2:4:28: error: expected expression
  std::function<int()> x { return 0;  }; 

I would expect the sample code to be rejected by cppfront.

Comment Personally, I could imagine that the grammar would instead say:

unnamed-declaration:
  : id-expression-opt = expression ;

It feel weird that initializer is a statement, particularly when the generating code is treating it as if it were an expression: e.g., here, and here.

Edit: another problem I see with this approach is that it is not clear how should semantics of an initializer be described in a language specification.

h0nzZik avatar Nov 22 '22 15:11 h0nzZik