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

Generated C++ code fails to compile if nested namespace contains "slint" #7134

Open
derkalaender opened this issue Dec 17, 2024 · 1 comment
Labels
a:language-c++ C++ API, codegen, CMake build system (mS,mO) bug Something isn't working

Comments

@derkalaender
Copy link

Bug Description

Bug

Using Slint with C++ and CMake allows to specify the namespace in which the generated C++ code should live.

When using nested::namespaces and any of the nested parts (i.e. not the root namespace) equal slint verbatim, the generated code fails to compile as it clashes with the global slint namespace of the library.

Examples

Example namespaces, ❌ means it doesn't compile:

  • ❌ Some nested namespace equals slint verbatim: some::nested::namespace::slint, ui::slint::stuff
  • ✅ The whole namespace equals slint verbatim: slint
  • ✅ The root namespace equals slint verbatim: slint::this::is::fine
  • ✅ Any part of the namespace contains but not equals slint: nested::slintstuff, nested::slintstuff::ui

I believe this is because the generated code tries to access the Slint library via the global slint namespace, but picks up the sorrounding user-defined namespace instead and thus the compiler fails to find the corresponding functions/members of the Slint library.

Proposed fix

This should be easily fixable by making the generated code always access the global slint namespace via ::slint syntax.

Additionally, I suggest disallowing slint as the complete namespace (slint::myui, myui::slint would be fine) as well. Otherwise, it would be possible to export, e.g., a SharedString global, which would always lead to compiler ambiguity with ::slint::SharedString.

Reproducible Code (if applicable)

cmake_minimum_required(VERSION 3.21)
project(example LANGUAGES CXX)

find_package(Slint REQUIRED)

add_executable(example main.cpp)
slint_target_sources(example ../ui/example.slint NAMESPACE this::is::problematic::slint)
target_link_libraries(example PUBLIC Slint::Slint)

Environment Details

  • Slint Version: 1.8.0
  • Platform/OS: N/A
  • Programming Language: C++, gcc
  • Backend/Renderer: N/A

Product Impact

Minor inconvience, should at least be documented

@derkalaender derkalaender added bug Something isn't working need triaging Issue that the owner of the area still need to triage labels Dec 17, 2024
@ogoffart ogoffart added a:language-c++ C++ API, codegen, CMake build system (mS,mO) and removed need triaging Issue that the owner of the area still need to triage labels Dec 17, 2024
@ogoffart
Copy link
Member

Thanks for the detailed bug report.

This should be easily fixable by making the generated code always access the global slint namespace via ::slint syntax.

I agree. This shouldn't be too difficult by searching for all instance of slint:: in cpp.rs and using ::slint:: instead

Additionally, I suggest disallowing slint as the complete namespace

I think we should actually disallow to put extra things in the slint namespace. So i'd also disallow slint::myui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:language-c++ C++ API, codegen, CMake build system (mS,mO) bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants