slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.82k stars 554 forks source link

Generated C++ code issue with keyword "delete" #5613

Open abalogstromer opened 1 month ago

abalogstromer commented 1 month ago

I found an issue where if you expose a struct from slint to C++, depending on the fields name, it could come in conflict with C++ specific keywords (e.g. delete). Here is an example:

export struct AStruct {
    delete: image
}

generates the following:

class AStruct {
    public:
    slint::Image delete;
    friend inline auto operator== (const class AStruct &a, const class AStruct &b) -> bool = default;
};

Since delete is a keyword used in C++, the build failed with the following: error: expected unqualified-id before ‘delete’

As a workaround, I am using a different name of the variable. I think the generated code should be valid regardless of the namings used in slint code.

ogoffart commented 1 month ago

thanks for filling an issue.

It is true that using a C++ keyword as a name of a field of an exported struct (or the name of an exported component/struct) will result as an error.

(In rust we workaround that with r# raw identifier, but that's not possible in C++) The solution would probably be to somehow "mangle" such names, eg: rename it to delete_ or something like that. Or just forbid it in the compiler by throwing a slint compiler error when using such name and integrating with C++

abalogstromer commented 1 month ago

I had this issue again, but with default this time. Probably the best fit for C++ would be to prefix/suffix the generated part with, as you mentioned, an underscore _ or anything that would fit for a generated code.

ogoffart commented 1 month ago

This should be fixable in cpp.rs

One easy way to solve this is to change the ident function to rename the identifier matching C++ keywords to something else. That's generic and is applied to every identifier, also struct name, component name and such.