Use C++20 consteval to verify translation strings #30379

issue maflcko opened this issue on July 3, 2024
  1. maflcko commented at 8:42 AM on July 3, 2024: member

    It is possible to write _(str_ptr), which is wrong, see #30267 (review).

    I am not familiar with the translation build system integration, but it would be good to effectively change the signature of _ from auto _(const char*) to consteval auto _(const char*).

    My understanding is that this would catch this error at compile time, saving review effort and follow-up fixup changes.

    Assumed compiler error message:

    error: the value of 'reason' is not usable in a constant expression
          |   _(reason);
          |     ^~~~~~
    
  2. maflcko commented at 10:40 AM on July 3, 2024: member
    diff --git a/src/util/translation.h b/src/util/translation.h
    index d33fd2d0a0..7d82264a43 100644
    --- a/src/util/translation.h
    +++ b/src/util/translation.h
    @@ -71,7 +71,7 @@ const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
      * Translation function.
      * If no translation function is set, simply return the input.
      */
    -inline bilingual_str _(const char* psz)
    +inline consteval bilingual_str _(const char* psz)
     {
         return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
     }
    

    does not work:

    ./util/translation.h:74:32: error: consteval function never produces a constant expression [-Winvalid-constexpr]
       74 | inline consteval bilingual_str _(const char* psz)
          |                                ^
    ./util/translation.h:76:31: note: non-constexpr function 'operator bool' cannot be used in a constant expression
       76 |     return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
          |                               ^
    

    So I guess this needs a clang-tidy plugin.

  3. maflcko commented at 10:47 AM on July 3, 2024: member

    nvm, I'll open a pull request soon.

  4. maflcko closed this on Jul 3, 2024

  5. ryanofsky commented at 7:05 PM on July 3, 2024: contributor

    So I guess this needs a clang-tidy plugin.

    Clang-tidy plugin is not actually required because #30383 can catch the errors, IIUC.

  6. bitcoin locked this on Jul 3, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 00:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me