Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5613 - Generated C++ code issue with keyword "delete" #6441

Merged
merged 20 commits into from
Oct 8, 2024

Conversation

Qubi0-0
Copy link
Contributor

@Qubi0-0 Qubi0-0 commented Oct 3, 2024

  • lazy_static crate to slint-compiler added.
    I added lazy static hashset of all cpp_keywords. Now every ident is checked for potential collidations. If they happen, then _ is added.

Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR.

Some feedback:

  • lazy_static is kind of being phased out and being replaced by std::sync::OnceLock. See https://github.com/rust-lang-nursery/lazy-static.rs?tab=readme-ov-file#standard-library
    You can put the CPP_KEYWORDS OnceLock directly in the is_cpp_keyword function
  • This adds a cpp_keywords.json, but this is not usefull, right?
  • keywrods needs to be checked after normalizing for -. For example there can be an identifier static-assert
  • regarding the test, the files in tests/syntax are not being generated into C++. Only the ones in tests/cases
    An example of test that can be extended with C++ keywords: https://github.com/slint-ui/slint/blob/master/tests/cases/types/structs_keyword.slint
  • as the CI points out, this currently break if you have a property of function that is a keyword (example, do -> invoke_do) looks like the ident function should be called on the whole name rather than on part of the identifier.

Thanks a lot for your patch. Do you think you can do the changes?

@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 4, 2024

Hi.
I will gladly try to fix the Code! I will get on that today.

@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 4, 2024

I see there's still some issues. I'm not sure what should be done to resolve (as the CI points out, this currently break if you have a property of function that is a keyword (example, do -> invoke_do) looks like the ident function should be called on the whole name rather than on part of the identifier.). Could you elaborate on that please?
Also i have a question about testing. Is there a way to do this locally? I don't want to use your resources to much 😅 (unless it's irrelevant)

@ogoffart
Copy link
Member

ogoffart commented Oct 4, 2024

Basically, the problem is that we use the output of the ident function and concatenate.
For example there:

name: format!("global_{}", ident(&glob.name)),

"{0}if constexpr(std::is_same_v<T, {1}>) {{ return *m_globals.global_{1}.get(); }}",

name: format!("invoke_{prop_ident}"),

name: format!("on_{}", ident(&p.name)),

name: format!("set_{}", &prop_ident),

(and maybe other places)

In these cases, we should either have a different function to escape the - without replacing the keyword (eg: escape), or pass the whole production to the indent function.

@@ -12,6 +12,10 @@ match := Rectangle {
property<bool> test: move.loop == "mod";
}

export struct Name {
delete: image
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also try to add more stuff such as

struct struct {
   delete: image;
}

export global const {
   in-out property<struct> return;
}

Copy link
Contributor Author

@Qubi0-0 Qubi0-0 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some in new commits. Although in case of for example

 export global const{ 
    in-out property<unsigned> atomic_commit;
    in-out property<namespace> return;  
 }

there is

running 1 test
test test_cpp_types_structs_keyword ... FAILED

failures:

---- test_cpp_types_structs_keyword stdout ----
/tmp/.tmpn7hGBG.cpp: In member function ‘const T& char_::global() const’:
/tmp/.tmpn7hGBG.cpp:293:41: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
 293 |     else if constexpr(std::is_same_v<T, const>) { return *m_globals.global_const.get(); }
     |                                         ^~~~~
thread 'test_cpp_types_structs_keyword' panicked at /home/qubi/Projects/slint/target/debug/build/test-driver-cpp-815659352fb91da7/out/test_functions.rs:3100:20:
called `Result::unwrap()` on an `Err` value: "C++ Compilation error (see stdout)"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'm not sure if this should be addressed or not.

@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 4, 2024

A scenario with concatenated names is resolved with a new function that does what the previous Ident did.
However I see new issues with tests/cases/types/structs_keyword.slint. Im not sure how to address that.

Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the test was old and relied on the old syntax that generated the last component. But now there are more components added in the test, so the original match component is no longer generated.
The solution is to keep the match component the last component in the test. (so move the new struct and component before it)

internal/compiler/generator/cpp.rs Outdated Show resolved Hide resolved
@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 8, 2024

I changed tests/cases/types/structs_keyword.slint. Now it passes the tests.
I see that the last failing test is about formatting. I know of cargo fmt, although it makes a whole long list of all hashtset entries being put one for each line. What should be done here?

@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 8, 2024

Thanks for all the help from the beginning. It`s my first opensource PR, so it means a lot to me :D

@Qubi0-0
Copy link
Contributor Author

Qubi0-0 commented Oct 8, 2024

Seems everything is in order now :D

@ogoffart ogoffart merged commit f6184ef into slint-ui:master Oct 8, 2024
36 checks passed
@ogoffart
Copy link
Member

ogoffart commented Oct 8, 2024

Thanks a lot for this PR and fixing all the issues with it!

NigelBreslaw pushed a commit that referenced this pull request Oct 15, 2024
Fixes #5613

ChangeLog: C++: generated code adds `_` to the end of identifier that would otherwise be keywords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants