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

Add [[nodiscard]] to some types/functions when >= C23 or C++17 #287

Open
4 tasks
kainino0x opened this issue Apr 25, 2024 · 3 comments
Open
4 tasks

Add [[nodiscard]] to some types/functions when >= C23 or C++17 #287

kainino0x opened this issue Apr 25, 2024 · 3 comments
Labels
non-breaking Does not require a breaking change (that would block V1.0)

Comments

@kainino0x
Copy link
Collaborator

kainino0x commented Apr 25, 2024

C23 and C++17 support [[nodiscard]] on types and functions. As a tangent of #115 (comment) we decided we would like to use this. I think we would check the language version and turn this on if supported. Then it would produce warnings on discards if compiling with a supported language version.

  • WGPUStatus: Tentatively yes
  • Other returned enum types? Or just the functions they're returned from?
  • WGPUSurfaceTexture? Or just GetCurrentTexture?
  • All of the handles (like WGPUTexture)? Or only put it on create methods and any other functions that return owning refs (because they will leak)?
@kainino0x
Copy link
Collaborator Author

  • WGPUStatus: Tentatively yes

I'm less sure about this, what would applications do with this except ASSERT on every single one (which is really onerous)?

Anyway since this only affects warnings I'm going to drop this to non-breaking.

@kainino0x kainino0x added the non-breaking Does not require a breaking change (that would block V1.0) label Jun 6, 2024
@eliemichel
Copy link
Collaborator

eliemichel commented Jul 4, 2024

FWIW I've added some time ago a couple of [[nodiscard]] attributes in my webgpu.hpp generator in the case of callback handles (unique_ptr to the callback pointer + its closure data that must not be freed before the callback gets invoked). IDK if there is a similar case in one of the WGPUFuture flavors, but might be worth considering it.

Also, I had to deal with a pretty boring MSVC issue when trying to detect the version of C++ (see the #define workaround, also note how C++20 enables custom nodiscard warning messages).

Other question: should we leave a #define-based way to disable these [[nodiscard]] (like defining WEBGPU_ALLOW_DISCARD before #include <webgpu/webgpu.h> for instance)? Or is this too much of a hack? I do not really care personnally though, let's not invent problems that nobody has.

@kainino0x
Copy link
Collaborator Author

Probably we would do something like we have for attributes:

#if !defined(WGPU_NODISCARD)
#define WGPU_NODISCARD ..........
#endif

So if you #define WGPU_NODISCARD before including webgpu.h then it won't do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
non-breaking Does not require a breaking change (that would block V1.0)
Projects
None yet
Development

No branches or pull requests

2 participants