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 runtime checks for builder position #436

Merged
merged 12 commits into from
Sep 4, 2023
Merged

Add runtime checks for builder position #436

merged 12 commits into from
Sep 4, 2023

Conversation

EricLBuehler
Copy link
Contributor

@EricLBuehler EricLBuehler commented Aug 30, 2023

I added an assert_eq to check that the builder has a position set in every build_* method, which improves safety.

Description

I added an enum to keep track of the state, which was added to the Builder struct. The state on construction is NotSet, and the position_* methods are used to set the state to Set. I factored out the raw assert_eq into a macro to improve code readability.

Related Issue

This resolves the problem discussed in #425.

How This Has Been Tested

I ran the tests locally on my WSL2 Ubuntu 22.04, as well as the Github Actions on my fork, all of which passed.

Checklist

@EricLBuehler
Copy link
Contributor Author

EricLBuehler commented Aug 31, 2023

Note that this PR fixes a safety bug with Inkwell as described in #425.
@TheDan64, I would appreciate your thoughts on my solution.
Edit: @TheDan64, I noticed #425 is marked as for milestone 0.3.0, so perhaps this should be marked similarly?

@EricLBuehler EricLBuehler changed the title Runtime check for builder position Add runtime checks for builder position Aug 31, 2023
@TheDan64 TheDan64 self-requested a review September 3, 2023 19:41
Cargo.toml Outdated Show resolved Hide resolved
src/builder.rs Outdated Show resolved Hide resolved
@TheDan64 TheDan64 added this to the 0.3.0 milestone Sep 4, 2023
src/builder.rs Outdated
#[derive(Error, Debug, PartialEq, Eq)]
pub enum BuilderError {
#[error("Builder position is not set")]
PositonError,
Copy link
Owner

Choose a reason for hiding this comment

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

typo: Position

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe UnsetPosition would be a better name? It's sorta redundant to have Error in the variant name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. Changing that now - and I will also run typos over Inkwell. Perhaps adding typos can be added to CI?

Copy link
Owner

Choose a reason for hiding this comment

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

I didn't know about typos before - could you open a ticket please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will open it now.

Copy link
Owner

@TheDan64 TheDan64 left a comment

Choose a reason for hiding this comment

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

This looks great; thank you for all of the great work and cleanup!

@EricLBuehler
Copy link
Contributor Author

Glad to help!

@TheDan64 TheDan64 merged commit 0cfc956 into TheDan64:master Sep 4, 2023
14 checks passed
@EricLBuehler EricLBuehler mentioned this pull request Sep 4, 2023
Comment on lines +69 to +70
#[error("GEP error: does not point to a struct or index is out of bounds")]
GEPError,

Choose a reason for hiding this comment

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

Does this solve #213?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. The GEPError I added just replaces an Err<'static str> that was returned before #436 was merged. Could a BuilderError or something similar could be returned for #214?

Comment on lines +69 to +70
#[error("GEP error: does not point to a struct or index is out of bounds")]
GEPError,

Choose a reason for hiding this comment

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

This is called GEPError, in my head it goes like "any GEP error goes into this variant", but there are actually 2 other GEP error variants.

I'd say this one should have a different name or be broken down into more variants.

WDYT? Is this worth an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the GEPError on my fork. Turns out, GEPPointee and GEPIndex are all that were needed.

Copy link
Contributor Author

@EricLBuehler EricLBuehler Sep 4, 2023

Choose a reason for hiding this comment

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

Should I reopen another PR?
Edit: I will bundle these changes in my next PR regarding typos.

Copy link

@marcospb19 marcospb19 Sep 4, 2023

Choose a reason for hiding this comment

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

Oh nice, I'd say yes! But we can wait for TheDan to be 100% sure.
Edit: I just saw your edit. 😆

This was referenced Sep 4, 2023
@TheDan64 TheDan64 modified the milestones: 0.5.0, 0.3.0 Feb 21, 2024
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.

3 participants