-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Clarify BTree range_search
comments
#81312
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea | |
K: Borrow<Q>, | ||
R: RangeBounds<Q>, | ||
{ | ||
// WARNING: Inlining these variables would be unsound (#81138) | ||
// It might be unsound to inline these variables if this logic changes (#81138). | ||
// We assume the bounds reported by `range` remain the same, but | ||
// an adversarial implementation could change between calls | ||
let (start, end) = (range.start_bound(), range.end_bound()); | ||
|
@@ -114,6 +114,8 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea | |
loop { | ||
let (lower_edge_idx, lower_child_bound) = self.find_lower_bound_index(lower_bound); | ||
let (upper_edge_idx, upper_child_bound) = self.find_upper_bound_index(upper_bound); | ||
// SAFETY: This panic is used for safety, so external impls can't be called here. The | ||
// comparison is done with integers for that reason. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm quite confused here. If with "external impls" you mean the The comparison is done with integers because those are what we end up with, what we need to identify positions in the data structure. For me, the question is: why do we compare and panic at all? Because if we didn't, and if the I thought "SAFETY:" was for documenting why calling something marked unsafe is in fact safe, and here it's kind of the opposite. So I would add API documentation about the promise here and elsewhere (not sure if I could or should squeeze that into this PR), and change this comment to something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out I'm pretty wrong, after noticing there is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We can call it, but the result can't be used for a safety check. The user impl could return anything, which makes it useless.
Yes, but it's really because the
I debated putting it here. However, it does document that the panic allows using unsafe code later with user impls involved.
This would be okay with me, but it should say something about the panic providing safety. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Again, I don't understand why you write that. We have called it, we needed to call it, it's the purpose of the function we're discussing. Yes, the user impl could return anything, but that doesn't make it useless. We need to know what it says in case it's not trying to pull our leg.
The purpose of this function is to translate a pair of bounds (let's say a pair of keys) to (a node and) a pair of integers, edge indices, indices into various arrays. To translate from the problem domain to the solution domain, in terms of the btree data structure.
In my view, the fact that later code is calling unsafe code under some assumption about the result is not an unsafety problem in this function and not the only reason to have the panic. Even if there was no unsafe code outside, without
Actually, come to think of it, that's not even true. AFAIK the panic used to provide safety (to code outside the function) in the old code, but no longer after #81094. The line below the main panic ( All the panics in this function are only there to fulfill an additional service this function delivers, diagnosing user error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree. All I'm saying is that we can't make a safety decision based on it. For example, you can't decide if a range can be passed to
You wrote the new code, so whatever you prefer is good with me. The purpose of this comment was to document what part of the implementation of this function makes it safe, since that wasn't obvious when we talked about the function before your PR.
I didn't realize this. In this case, I see no reason to have this comment. I'll remove it and fix the other one as we discussed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also didn't realize this, so it was a fruitful discussion. |
||
if lower_edge_idx > upper_edge_idx { | ||
panic!("Ord is ill-defined in BTreeMap range") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's good to tone down the warning, but I don't know what you mean with "if this logic changes". What logic? It might be bad to inline these variables without any other change, because successfully passing the match statement might fool you to believe the start and end actually used were fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the code in the rest of the method. This might be better:
The rest of the comment already explains why inlining can cause a safety issue.