-
Notifications
You must be signed in to change notification settings - Fork 259
fix(cpp1): support empty and multi-argument indexing #483
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
Conversation
I'll have to enhance https://github.com/modern-cmake/cppfront for this. |
See the generated test passing at https://cpp2.godbolt.org/z/PfoWWqznG in C++23. |
One question would be how the range checker can be extended to multiple dimensions. I do not know the standard proposal for this therefore I ask the question: How does the c++ standard defines the size operator for multi dimensional arrays? Can we use this mechanism to check the range of other dimensions? |
@@ -3065,6 +3066,7 @@ class cppfront | |||
if ( | |||
flag_safe_subscripts | |||
&& i->op->type() == lexeme::LeftBracket | |||
&& std::ssize(i->expr_list->expressions) == 1 |
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.
Maybe add a TODO, that this needs to be extended for multiple dimensions.
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 that perhaps the correct thing to do
is to only avoid cpp2::assert_in_bounds
when there are no index arguments,
and add a multi-dimensional overload for cpp2::assert_in_bounds
that doesn't assert.
Then someone versed in std::mdspan
can just add an overload for it that asserts.
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.
Thanks! I think it's fine to do only single-dimensional bounds checks for the time being -- it will be quite a while before multidimensional is actually usable/used in the field.
There's none. |
Ok thanks for applying my remarks.
I had a look at cpp2::assert_in_bounds. Out of my had, one option would be:
I have not tested this. The increment of dim in the expansion is kind of hacky. Here we would assume that there is a Do you want to pursue such an implementation or should we leave it. I you want I can try and implement it. |
That doesn't work for It could work for a container of containers Without actual use-cases, it's hard to argue in favor of an overload that asserts. |
{ | ||
error("subscript expression [ ] must not be empty (if you were trying to name a C-style array type, use 'std::array' instead)"); | ||
next(); |
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.
We should still have an error here though, should we? Like the one for (
at line 4181 below.
Signed-off-by: Herb Sutter <herb.sutter@gmail.com>
* fix(cpp1): support empty and multi-argument indexing * Make error message for `[` arg case symmetric with `(` arg case Signed-off-by: Herb Sutter <herb.sutter@gmail.com> --------- Signed-off-by: Herb Sutter <herb.sutter@gmail.com> Co-authored-by: Herb Sutter <herb.sutter@gmail.com>
Resolves #482.
Partially addresses #387 (
operator[]
).The added test requires C++23.
Testing summary.
Acknowledgements.