Skip to content

feat: 🧑‍💻 Check device literal port numbers at compile time #689

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 7 commits into
base: develop-pros-4
Choose a base branch
from

Conversation

ion098
Copy link
Contributor

@ion098 ion098 commented Jul 16, 2024

Summary:

Checks port numbers for device literals at compile time rather than at runtime. (Thanks to @djava for giving me suggestions on refactoring my mess of templates into actually readable code)

Motivation:

If something can be checked at compile time, it should be checked at compile time.

Test Plan:

  • check it compiles
  • check that port literals work as expected
  • check that out of range port numbers give a compile time error that is readable

Note:

This is an ABI breaking change and therefore should be a minor version bump.

@BennyBot
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@djava
Copy link
Contributor

djava commented Jul 16, 2024

Please fix this MR to only include relevant commits so its reviewable.

@ion098 ion098 force-pushed the feature/comptime-port-check branch from 793eac0 to 9a792f0 Compare July 22, 2024 18:24
@BennyBot
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ion098 ion098 changed the title ✨ Check port numbers at compile time where possible ✨ Check device literal port numbers at compile time Nov 5, 2024
@ion098 ion098 marked this pull request as ready for review November 5, 2024 18:55
@ion098 ion098 marked this pull request as draft November 19, 2024 16:41
@ion098 ion098 marked this pull request as ready for review November 19, 2024 18:11
@ion098 ion098 changed the title ✨ Check device literal port numbers at compile time 🧑‍💻 Check device literal port numbers at compile time Nov 19, 2024
Comment on lines +30 to +31
// from_chars doesn't seem to handle hex/binary/octal prefixes,
// so we handle them ourselves
Copy link
Contributor

Choose a reason for hiding this comment

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

This sucks lmao, good catch though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out it does, I failed to read the docs lol. Fixed in bea9c44.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh thank god

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out I failed to read the docs doubly lol: from_chars does indeed NOT auto detect base womp womp

Copy link
Contributor

Choose a reason for hiding this comment

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

lol? so what, we need to add back in the 0b and 0x detection? that sucks bruh

Comment on lines +34 to +35
// hex literal
base = 16;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does std::from_chars handle both uppercase and lowercase hex literals?

const pros::Distance operator"" _dist(const unsigned long long int d);
template <char... Cs>
const pros::Distance operator"" _dist() {
static_assert(pros::detail::is_valid_port<Cs...>(), "Port is out of range!");
Copy link
Contributor

@djava djava Nov 19, 2024

Choose a reason for hiding this comment

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

Was originally unsure if this is better than requires-clause, but it probably is because it allows giving a message (and benefits of requires-clause, ie SFINAE, arent really useful here).

The message you have is not great though, because a failed from_chars will give the same error. I imagine that this happens if you try to give it a non-integer? I would probably prefer "invalid port number" or something

Copy link
Contributor

Choose a reason for hiding this comment

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

Same goes for other declarations

@@ -2419,7 +2426,12 @@ const pros::Motor operator"" _mtr(const unsigned long long int m);
* }
* \endcode
*/
const pros::Motor operator"" _rmtr(const unsigned long long int m);
template <char... Cs>
const pros::Motor operator"" _rmtr() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if theres a single pros project in the whole world that uses _rmtr

Choose a reason for hiding this comment

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

Comment on lines +373 to +388
/**
* Returns a rotation sesnor that has the same port as the original, but reversed.
*
* This can only be used on a temporary object (such as a device literal).
*
* \b Example
* \code
* using namespace pros::literals;
*
* void opcontrol() {
* pros::Rotation rotation_sensor = -1_rot;
* printf("Reversed: %d \n", rotation_sensor.get_reversed());
* }
* \endcode
*/
Rotation operator-() && { return Rotation(-this->get_port()); }
Copy link
Contributor

Choose a reason for hiding this comment

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

why should this exist? can you not create negatives with the other user-defined literal operator? If you can, then this probably shouldnt exist

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see that its rvalue-this-qualified. still a little weird

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, not for rotation sensors. Both 10_rrot and 10_rotr seem a bit confusing compared to -10_rot (negative port numbers also match how the ctors work)

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is necessary for rotation, its surely necessary for motors too?

Copy link
Contributor

Choose a reason for hiding this comment

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

hopefully its not necessary for either though lol

Copy link
Contributor

Choose a reason for hiding this comment

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

im just confused why this doesnt work with the user-defined literal directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because -10_rot is -(operator""_rot<'1','0'>()), not operator""_rot<'-','1','0'>()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Motors have _rmtr so its not needed for motors, although adding this to motors might be a good idea for consistency

Copy link
Contributor

Choose a reason for hiding this comment

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

oh i see. yeah that sucks lmao. i think this should be added to motors as well if it has to exist.

@Rocky14683
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ion098
Copy link
Contributor Author

ion098 commented Nov 20, 2024

@Rocky14683 can you rerun the pipeline please?

@Rocky14683
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ion098
Copy link
Contributor Author

ion098 commented Nov 20, 2024

@Rocky14683 It should compile now, can you rerun the pipeline please?

@Rocky14683
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ion098 ion098 changed the title 🧑‍💻 Check device literal port numbers at compile time feat: 🧑‍💻 Check device literal port numbers at compile time Feb 3, 2025
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.

5 participants