-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[ADT] Fix ArrayRef<T>::slice #113048
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
[ADT] Fix ArrayRef<T>::slice #113048
Conversation
@llvm/pr-subscribers-llvm-adt Author: Franklin (FLZ101) ChangesFull diff: https://github.com/llvm/llvm-project/pull/113048.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index ac40ec4a6b2404..69a08fd079043a 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -198,7 +198,10 @@ namespace llvm {
}
/// slice(n) - Chop off the first N elements of the array.
- ArrayRef<T> slice(size_t N) const { return slice(N, size() - N); }
+ ArrayRef<T> slice(size_t N) const {
+ assert(N <= size() && "Invalid specifier");
+ return slice(N, size() - N);
+ }
/// Drop the first \p N elements of the array.
ArrayRef<T> drop_front(size_t N = 1) const {
|
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.
Could you update the PR description and explain what the issue is?
llvm/include/llvm/ADT/ArrayRef.h
Outdated
assert(N <= size() && "Invalid specifier"); | ||
return slice(N, size() - N); |
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.
Instead, we could call into drop_front
which does the same thing. Or make drop_front
call slice.
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 drop_front
is more idiomatic and we could probably mark the single-argument slice as deprecated and eventually remove it. Although that's all out of scope for this PR. cc: @kazutakahirata
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.
Agree.
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've implemented slice(N)
with drop_front(N)
.
|
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.
Looks good but please add the explanation from the comment to the commit description when merging.
Current implementation of `slice(N)` is buggy, since `slice(N, size() - N)` will never fail the assertion `assert(N+M <= size() && "Invalid specifier")` above, even `N > size()`.
Done. |
@kuhar Could you merge this PR? |
No description provided.