hsutter / cppfront

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

[BUG] Bad operator= error message for @value types #1071

Open dutkalex opened 1 month ago

dutkalex commented 1 month ago

Hi folks! I ran into a very confusing bug while experimenting with cppfront. Consider the following type:

dummy: @value type = {
    name_: std::string;
    operator=: (out this, in name: std::string) = { name_ = name; }
}

Cppfront refuses to compile this code snippet and produces the following error message:

reproducer.cpp2: error: in operator=, expected 'name_ = ...' initialization statement (because type scope 
object 'name_' does not have a default initializer)
reproducer.cpp2(2,5): error: see declaration for 'name_' here
reproducer.cpp2: error: an operator= body must start with a series of 'member = value;' initialization statements 
for each of the type-scope objects in the same order they are declared, or the member must have a default 
initializer (in type 'dummy')

However, there is nothing wrong with the operator= here. The problem is actually that a value type must be default constructible, which is not the case in this example. This error message got me very confused at first, and I had to look into the sources to understand what was going on. Maybe the missing piece to get a better diagnostic is to require all data members of @value types to be declared with a default initializer. What do you think of this? I'd be happy to submit a PR to fix this in the definition of the @value metaclass, but I'd rather make sure that my analysis is correct first.

Best regards, Alex

bluetarpmedia commented 1 month ago

I'm definitely in favour of clear, friendly error messages. But instead of requiring all data members to have default initializers, I'd prefer the error to be something like: A value type must be default constructible. Then the user can choose to either add a default constructor or to add data member initializers.