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

Implement Range for OpStack #338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cyberbono3
Copy link

Closes #322

@jan-ferdinand
Copy link
Member

Thanks for taking this on! Unfortunately, there is a logic error in the implementation. The documentation of field stack on OpStack reads:

/// The underlying, actual stack. When manually accessing, be aware of reversed indexing:
/// while `op_stack[0]` is the top of the stack, `op_stack.stack[0]` is the lowest element in
/// the stack.

Consequently, accessing op_stack[0..2] (equivalent to op_stack.index(0..2) or op_stack.index_mut(0..2) depending on context and determined by the compiler) should give op_stack.stack[op_stack.stack.len() - 2..op_stack.stack.len()].reverse() or similar. Note that this probably (a) doesn't compile and (b) is likely to contain off-by-one errors.

Copy link
Member

Choose a reason for hiding this comment

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

This file is only generated due to a bug in proptest. It has purposefully not been commited.

@@ -4,6 +4,7 @@ use std::fmt::Result as FmtResult;
use std::num::TryFromIntError;
use std::ops::Index;
use std::ops::IndexMut;
use std::ops::{Range, RangeInclusive};
Copy link
Member

Choose a reason for hiding this comment

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

In this repository, we use an import granularity of item in order to trivialize git merging of imports.

if range.end <= self.stack.len() && range.start < range.end {
&self.stack[range]
} else {
panic!("a range is out of bounds")
Copy link
Member

@jan-ferdinand jan-ferdinand Nov 7, 2024

Choose a reason for hiding this comment

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

I think's it's cleaner to just use the underlying panics, should there be any:

  • By not implementing these checks yourself, you cannot introduce logic bugs in them. Concretely, the range check in the impl Index<RangeInclusive<usize>> for OpStack looks suspiciously similar to the one in impl Index<Range<usize>> for OpStack.
  • The panic's message will correctly reflect the kind of access violation. For example, if the range is invalid, the current implementation will display an error regarding range bounds, whereas rust's panic message reads “slice index starts at x but ends at y.”
  • By dropping the manual bounds-check here people will see the accustomed message “index out of bounds: the len is x but the index is y.”
  • The stacktrace will point to the relevant method in either case.

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.

Implement Index<Range<…>> for OpStack
2 participants