cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Bad ``operator=`` error message for ``@value type``s

Open dutkalex opened this issue 1 year ago • 4 comments
trafficstars

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

dutkalex avatar May 15 '24 23:05 dutkalex