Skip to content

Commit

Permalink
Fix is_narrowing_v - closes hsutter#1257
Browse files Browse the repository at this point in the history
The current implementation of `is_narrowing_v` does not handle type conversions correctly.
For example, `cpp2::impl::is_narrowing_v<std::size_t, int>` returns `false`,
indicating that it mistakenly perceives the conversion from a signed `int` to an unsigned `size_t`
as potentially non-narrowing.

Closes hsutter#1257
  • Loading branch information
filipsajdak committed Aug 23, 2024
1 parent 065a993 commit 83563ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
18 changes: 4 additions & 14 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ concept valid_custom_is_operator = predicate_member_fun<X, F, &F::op_is>
|| brace_initializable_to<X, argument_of_op_is_t<F>>
);

template <typename T>
concept arithmetic = std::is_arithmetic_v<std::remove_cvref_t<T>>;

//-----------------------------------------------------------------------
//
// General helpers
Expand Down Expand Up @@ -1736,20 +1739,7 @@ inline constexpr auto is( X const& x, bool (*value)(X const&) ) -> bool {
// If it's confusing, we can switch this to <From, To>

template< typename To, typename From >
inline constexpr auto is_narrowing_v =
// [dcl.init.list] 7.1
(std::is_floating_point_v<From> && std::is_integral_v<To>) ||
// [dcl.init.list] 7.2
(std::is_floating_point_v<From> && std::is_floating_point_v<To> && sizeof(From) > sizeof(To)) ||
// [dcl.init.list] 7.3
(std::is_integral_v<From> && std::is_floating_point_v<To>) ||
(std::is_enum_v<From> && std::is_floating_point_v<To>) ||
// [dcl.init.list] 7.4
(std::is_integral_v<From> && std::is_integral_v<To> && sizeof(From) > sizeof(To)) ||
(std::is_enum_v<From> && std::is_integral_v<To> && sizeof(From) > sizeof(To)) ||
// [dcl.init.list] 7.5
(std::is_pointer_v<From> && std::is_same_v<To, bool>)
;
concept is_narrowing_v = arithmetic<To> && arithmetic<From> && !brace_initializable_to<From,To>;

template< typename To, typename From >
inline constexpr auto is_unsafe_pointer_conversion_v =
Expand Down
8 changes: 4 additions & 4 deletions source/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -3661,9 +3661,9 @@ parse_context_branch_reset_state::parse_context_branch_reset_state(){}
#line 1993 "reflect.h2"
[[nodiscard]] auto parse_context::grab_n(cpp2::impl::in<int> n, cpp2::impl::out<std::string> r) & -> bool
{
if (cpp2::impl::cmp_less_eq(pos + cpp2::impl::as_<size_t>(n),regex.size())) {
r.construct(regex.substr(pos, cpp2::impl::as_<size_t>(n)));
pos += (cpp2::impl::as_<size_t>(n)) - 1;
if (cpp2::impl::cmp_less_eq(pos + cpp2::unsafe_narrow<size_t>(n),regex.size())) {
r.construct(regex.substr(pos, cpp2::unsafe_narrow<size_t>(n)));
pos += (cpp2::unsafe_narrow<size_t>(n)) - 1;
return true;
}
else {
Expand Down Expand Up @@ -3844,7 +3844,7 @@ parse_context_branch_reset_state::parse_context_branch_reset_state(){}

#line 2178 "reflect.h2"
auto generation_function_context::remove_tabs(cpp2::impl::in<int> c) & -> void{
tabs = tabs.substr(0, (cpp2::impl::as_<size_t>(c)) * 2);
tabs = tabs.substr(0, (cpp2::unsafe_narrow<size_t>(c)) * 2);
}

generation_function_context::generation_function_context(auto const& code_, auto const& tabs_)
Expand Down
8 changes: 4 additions & 4 deletions source/reflect.h2
Original file line number Diff line number Diff line change
Expand Up @@ -1992,9 +1992,9 @@ parse_context: type =

grab_n: (inout this, in n: int, out r: std::string) -> bool =
{
if pos + n as size_t <= regex..size() {
r = regex..substr(pos, n as size_t);
pos += (n as size_t) - 1;
if pos + cpp2::unsafe_narrow<size_t>(n) <= regex..size() {
r = regex..substr(pos, cpp2::unsafe_narrow<size_t>(n));
pos += (cpp2::unsafe_narrow<size_t>(n)) - 1;
return true;
}
else {
Expand Down Expand Up @@ -2176,7 +2176,7 @@ generation_function_context: @struct type = {
}

remove_tabs: (inout this, c: int) = {
tabs = tabs..substr(0, (c as size_t) * 2);
tabs = tabs..substr(0, (cpp2::unsafe_narrow<size_t>(c)) * 2);
}
}

Expand Down

0 comments on commit 83563ac

Please sign in to comment.