RFR: 8314258: checked_cast doesn't properly check some cases
Afshin Zafari
azafari at openjdk.org
Thu Feb 5 11:56:24 UTC 2026
On Thu, 5 Feb 2026 00:03:20 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:
> Please review this addition of the `integer_cast` utility.
>
> Details in first comment, to avoid email spamming from automatic messages.
>
> Testing: mach5 tier1-5 with `checked_cast` for integral types changed to call
> the new `integer_cast`. Addition of `integer_cast` includes gtests.
Thank you Kim for this great work.
Couple of questions:
To test some corner cases I used the templates in the following test:
```C++
TEST(TestIntegerCast, additional_tests) {
// int32_t a = integer_cast<int32_t>(-1); // compile error
// int32_t b = integer_cast<int32_t>((int32_t)-1); // compile error
// int32_t c = integer_cast<int32_t>(1 << 31); // compile error
EXPECT_TRUE(is_integer_convertible<int32_t>(-1));
EXPECT_FALSE(is_integer_convertible<int32_t>(0xFFFFFFFF));
EXPECT_FALSE(is_integer_convertible<int32_t>(0x8000000000000000));
EXPECT_TRUE(is_integer_convertible<int32_t>(1 << 31));
EXPECT_FALSE(is_integer_convertible<int32_t>(1L << 63));
EXPECT_TRUE(is_integer_convertible<int32_t>((int32_t)-1));
EXPECT_TRUE(is_integer_convertible<int32_t>((int32_t)0xFFFFFFFF));
EXPECT_TRUE(is_integer_convertible<int32_t>((int32_t)0x8000000000000000));
EXPECT_TRUE(is_integer_convertible<int32_t>((int32_t)(1 << 31)));
}
I expected that all tests to be `EXPECT_TRUE` but they had to be the opposite to let the Test pass. How can we describe this?
Could we have such a unit test that shows how to _correctly_ use the templates and some comments on why some use-cases are wrong?
How it should be interpreted that even when `is_integer_convertible(x)` returns `true` but `integer_cast(x)` fails in compilation?
-------------
PR Review: https://git.openjdk.org/jdk/pull/29582#pullrequestreview-3756578151
More information about the hotspot-dev
mailing list