Skip to content
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

Enforce user literals parsing #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rykovv
Copy link

@rykovv rykovv commented Jul 5, 2024

Closes #11

Current implementation contains a 🐛 bug. Compilation will not fail for invalid values captured by user literals. Such weakness puts in danger the whole purpose of the safety library. Stronger user literals parsing is needed.

This PR adds strong support for positive decimal and hexadecimal numbers along with negative decimals. The support is provided through creation of three concepts: positive_decimal_integer, negative_decimal_integer, and positive_hex_integer. These concepts match provided values with intended types and dispatch appropriate parsing function. So, additional safety is provided.

@elbeno
Copy link
Contributor

elbeno commented Jul 6, 2024

UDLs will never receive the negative sign? Unary minus is an operator, not part of a literal. https://godbolt.org/z/M1hneGfj7

@rykovv
Copy link
Author

rykovv commented Jul 7, 2024

See the point. My understanding wasn't correct. Hence, think it can be split in following steps:

  1. Remove netagive decimal integer concept
  2. Create additional minus struct but derived from unary_op in dsl/minus.hpp
  3. Specialize for Primitive. Will need to swap min with max in ival and make both negative.
  4. Does specialization for mask make sense?
  5. Add unary minus operator overload

Am I missing something?

@lukevalenty
Copy link
Contributor

See the point. My understanding wasn't correct. Hence, think it can be split in following steps:

  1. Remove netagive decimal integer concept
  2. Create additional minus struct but derived from unary_op in dsl/minus.hpp
  3. Specialize for Primitive. Will need to swap min with max in ival and make both negative.
  4. Does specialization for mask make sense?
  5. Add unary minus operator overload

Am I missing something?

I think you can just remove the negative decimal integer concept and it's fine. The library should already handle unary minus.

requires std::unsigned_integral<T>;
} && (is_0_v<Char0> && is_x_v<Char1> && (is_hex_digit_v<Chars> && ...));
} // namespace safe::detail
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: You have a mix of constexpr bools, bool_constant types, and concepts here. I would make them all concepts and express the subsumption relationships clearly.

Copy link
Author

Choose a reason for hiding this comment

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

Start getting the idea of how to better express concepts. requires would normally be used when we want to handle a value of certain type, like here

template <typename T, char... Chars>
concept positive_decimal_integer = requires { requires std::integral<T>; } &&
(is_decimal_digit_v<Chars> && ...);
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Rather than using a nested concept, just put the concept itself. So:

template <typename T, char... Chars>
concept positive_decimal_integer = std::integral<T> &&
                                   (is_decimal_digit_v<Chars> && ...);

That will enable the compiler to see subsumption, which means easier overload sets.


template <char Char>
constexpr bool is_decimal_digit_v = Char - '0' >= 0 && Char - '0' <= 9;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Why not Char >= '0' and Char <= '9' ? Is the subtraction/integral promotion giving us anything?

Copy link
Author

Choose a reason for hiding this comment

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

Agree. No benefit from subtraction. Like to how the suggestion reads

@rykovv
Copy link
Author

rykovv commented Jul 10, 2024

I think you can just remove the negative decimal integer concept and it's fine. The library should already handle unary minus.

Gotcha. Haven't seen anything in dsl/minus.hpp, but found later in var.

@rykovv
Copy link
Author

rykovv commented Jul 10, 2024

Restructuring the PR.

@rykovv rykovv force-pushed the enforce-literals-parsing branch 2 times, most recently from 50e8eab to 684a8ac Compare July 10, 2024 08:02
@rykovv rykovv closed this Jul 10, 2024
@rykovv rykovv reopened this Jul 10, 2024
@rykovv
Copy link
Author

rykovv commented Jul 10, 2024

@elbeno are there any specific requirements for clang-format? Is the style based on LLVM or Google?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 User defined literals should fail compilation for invalid values
3 participants