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

Add harnesses for safety of primitive conversions #233

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

Conversation

ShoyuVanilla
Copy link

@ShoyuVanilla ShoyuVanilla commented Dec 21, 2024

Towards #220

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@ShoyuVanilla ShoyuVanilla requested a review from a team as a code owner December 21, 2024 19:08
@ShoyuVanilla ShoyuVanilla force-pushed the challenge-14 branch 4 times, most recently from 3d9c94a to 140887c Compare December 21, 2024 19:38
Copy link

@carolynzech carolynzech left a comment

Choose a reason for hiding this comment

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

Thanks!

library/core/src/convert/num.rs Outdated Show resolved Hide resolved
library/core/src/convert/num.rs Outdated Show resolved Hide resolved
library/core/src/convert/num.rs Outdated Show resolved Hide resolved
@ShoyuVanilla ShoyuVanilla force-pushed the challenge-14 branch 4 times, most recently from 0bf5fd7 to 5ebcf38 Compare January 3, 2025 23:53
library/core/src/convert/num.rs Outdated Show resolved Hide resolved
Comment on lines +1192 to +1197
generate_float_to_int_harness!(f16 => u8, check_u8_from_f16_unchecked);
generate_float_to_int_harness!(f16 => u16, check_u16_from_f16_unchecked);
generate_float_to_int_harness!(f16 => u32, check_u32_from_f16_unchecked);
generate_float_to_int_harness!(f16 => u64, check_u64_from_f16_unchecked);
generate_float_to_int_harness!(f16 => u128, check_u128_from_f16_unchecked);
generate_float_to_int_harness!(f16 => usize, check_usize_from_f16_unchecked);
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be possible to use a macro to generate all those generators? As is, it's very hard to tell whether any combinations have possibly been forgotten.

Copy link
Author

@ShoyuVanilla ShoyuVanilla Jan 8, 2025

Choose a reason for hiding this comment

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

I wish I could do so, but I think that might require extra proc macro crate to generate all those check_a_from_b_... identifiers as MBEs cannot generate identifiers.

There might be some possible alternatives;

  • Make a huge, single macro that requires all identifiers like some kind of a table. This would still be verbose, but might prevent mistakes like forgotting something
  • Use same identifier for same kind of harness, but segregate them into different namespaces, like verify_from_f16::to_u32::check_float_to_int_unchecked. But this way, we will have multiple harnesses with same names

Do you have some recommendations?

Copy link

Choose a reason for hiding this comment

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

Indeed, you cannot generate identifiers with regular macros. I think your second option sounds reasonable to me. It is similar to what we've done here.

I think you might be able to do generate the namespace for the from type, and the harness using the to type.

Copy link
Author

Choose a reason for hiding this comment

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

I'll try it. Thanks!

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.

4 participants