Skip to content

[libc++][test] Make narrowing in nasty_char_traits::to_char_type more explicit #138375

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

frederick-vs-ja
Copy link
Contributor

Previously, the cast was allowed due to P0960R3, which made narrowing implicitly done in the parenthesized aggregate initialization. It seems that MSVC doesn't like such an implicit manner and emits warning C4242.

This patch makes the narrowing more explicit. Follows up #135338.

@frederick-vs-ja frederick-vs-ja added libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. test-suite labels May 3, 2025
@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner May 3, 2025 03:11
@llvmbot
Copy link
Member

llvmbot commented May 3, 2025

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

Changes

Previously, the cast was allowed due to P0960R3, which made narrowing implicitly done in the parenthesized aggregate initialization. It seems that MSVC doesn't like such an implicit manner and emits warning C4242.

This patch makes the narrowing more explicit. Follows up #135338.


Full diff: https://github.com/llvm/llvm-project/pull/138375.diff

1 Files Affected:

  • (modified) libcxx/test/support/nasty_string.h (+1-1)
diff --git a/libcxx/test/support/nasty_string.h b/libcxx/test/support/nasty_string.h
index c2968f52e3a00..6b17bbc3f2232 100644
--- a/libcxx/test/support/nasty_string.h
+++ b/libcxx/test/support/nasty_string.h
@@ -70,7 +70,7 @@ struct nasty_char_traits {
 
   static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? ~eof() : c; }
 
-  static constexpr char_type to_char_type(int_type c) noexcept { return char_type(c); }
+  static constexpr char_type to_char_type(int_type c) noexcept { return {static_cast<char>(c)}; }
 
   static constexpr int_type to_int_type(char_type c) noexcept { return int_type(c.c); }
 

@philnik777
Copy link
Contributor

How is this more explicit? It replaces one cast with a different one.

@frederick-vs-ja
Copy link
Contributor Author

How is this more explicit? It replaces one cast with a different one.

It seems intended that parenthesized (direct-non-list) aggregate initialization behaves like initialization via constructors, and MSVC considers narrowing in ctor-init-list to be "not explicit enough".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. test-suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants