X Tutup
Skip to content

src: constrain MaybeStackBuffer::ToString and ToStringView to standard char types#62507

Open
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/constrain-maybstackbuffer-tostring-char-traits
Open

src: constrain MaybeStackBuffer::ToString and ToStringView to standard char types#62507
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/constrain-maybstackbuffer-tostring-char-traits

Conversation

@om-ghante
Copy link
Copy Markdown

@om-ghante om-ghante commented Mar 30, 2026

On newer libc++ (shipped with macOS Xcode 16+), std::char_traits<T> for T not
equal to char, wchar_t, char8_t, char16_t, or char32_t is deprecated and
will be removed in a future release.

When the MaybeStackBuffer template is instantiated with unsigned char or uint8_t
(e.g., in test/cctest/test_util.cc), the compiler instantiates the declarations
for all member functions. The ToString() and ToStringView() methods historically returned
std::basic_string<T> and std::basic_string_view<T>, which triggered this
deprecation warning (-Wdeprecated-declarations) during class instantiation because
the return type references std::char_traits<unsigned char>.

Changes

This PR resolves the warning by:

  1. Extracting the list of standard character types into a reusable C++20 concept
    standard_char_type.
  2. Converting ToString() and ToStringView() from normal member functions into
    member function templates with a constrained default template parameter:
    template <standard_char_type U = T>.

By making these member function templates, the C++ compiler defers instantiating
the return type (std::basic_string<U>) until the function is actually called. Since
these methods are never actually called for unsigned char or uint8_t variants
in the Node.js codebase, the deprecated std::char_traits<unsigned char> is never
evaluated or instantiated, completely bypassing the warning.

Refs: #62506

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Mar 30, 2026
@om-ghante om-ghante force-pushed the fix/constrain-maybstackbuffer-tostring-char-traits branch from ecb3efb to 7c9594c Compare March 30, 2026 13:48
std::is_same_v<T, char8_t> || std::is_same_v<T, char16_t> ||
std::is_same_v<T, char32_t>) {
return {out(), length()};
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These could be condensed into a single concept couldn't it? Just to avoid the duplication.

@targos
Copy link
Copy Markdown
Member

targos commented Mar 30, 2026

Have you tried your changes? I applied the patch and still see a deprecation warning.

@om-ghante om-ghante force-pushed the fix/constrain-maybstackbuffer-tostring-char-traits branch from 7c9594c to b42db3d Compare March 30, 2026 15:23
…d char types

On newer libc++ (shipped with macOS Xcode 16+), std::char_traits<T> for
T not equal to char, wchar_t, char8_t, char16_t, or char32_t is
deprecated and will be removed in a future release. When
MaybeStackBuffer is instantiated with unsigned char or uint8_t (e.g. in
test/cctest/test_util.cc), the ToString() and ToStringView() methods
trigger this deprecation warning because they reference
std::basic_string<unsigned char> and std::basic_string_view<unsigned
char>, even though these methods are never actually called for those
types.

Add C++20 requires clauses to constrain ToString() and ToStringView()
to only be available for standard character types, preventing the
deprecated std::char_traits<unsigned char> from being instantiated.

This is consistent with the existing use of C++20 concepts elsewhere
in the same file (e.g. the is_callable concept).
@om-ghante om-ghante force-pushed the fix/constrain-maybstackbuffer-tostring-char-traits branch from b42db3d to 8eb1cad Compare March 30, 2026 15:27
@om-ghante
Copy link
Copy Markdown
Author

om-ghante commented Mar 30, 2026

Have you tried your changes? I applied the patch and still see a deprecation warning.

Yes, I have checked my updated changes and the deprecation warning is now gone.

The issue with the first patch was that requires only stops the function from being callable, but the return type std::basic_string is still evaluated during class instantiation, which triggers the warning. By changing ToString() and ToStringView() to member function templates (template <standard_char_type U = T>), the compiler defers instantiating the return type (std::basic_string<U>) until the method is actually called. Since these methods are never used for MaybeStackBuffer<unsigned char>, the deprecation warning is completely bypassed.

@targos
Copy link
Copy Markdown
Member

targos commented Mar 30, 2026

Thanks. I confirm there is no warning anymore with the latest push. I defer to @nodejs/cpp-reviewers for the code review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup