-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
t: type = { | ||
operator[]: (inout this) -> i32 = 1; | ||
operator[]: (inout this, x: _) -> i32 = 2; | ||
operator[]: (inout this, x: _, y: _) -> i32 = 3; | ||
} | ||
main: () = { | ||
[[assert: t()[] == 1]] | ||
[[assert: t()[1] == 2]] | ||
[[assert: t()[1, 2] == 3]] | ||
} |
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions
42
regression-tests/test-results/pure2-bugfix-for-empty-index.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
#define CPP2_USE_MODULES Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-bugfix-for-empty-index.cpp2" | ||
class t; | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-bugfix-for-empty-index.cpp2" | ||
class t { | ||
public: [[nodiscard]] auto operator[]() -> cpp2::i32; | ||
public: [[nodiscard]] auto operator[](auto const& x) -> cpp2::i32; | ||
public: [[nodiscard]] auto operator[](auto const& x, auto const& y) -> cpp2::i32; | ||
|
||
public: t() = default; | ||
public: t(t const&) = delete; /* No 'that' constructor, suppress copy */ | ||
public: auto operator=(t const&) -> void = delete; | ||
#line 5 "pure2-bugfix-for-empty-index.cpp2" | ||
}; | ||
auto main() -> int; | ||
|
||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
|
||
#line 2 "pure2-bugfix-for-empty-index.cpp2" | ||
[[nodiscard]] auto t::operator[]() -> cpp2::i32 { return 1; } | ||
[[nodiscard]] auto t::operator[](auto const& x) -> cpp2::i32 { return 2; } | ||
[[nodiscard]] auto t::operator[](auto const& x, auto const& y) -> cpp2::i32 { return 3; } | ||
|
||
auto main() -> int{ | ||
cpp2::Default.expects(t()[]==1, ""); | ||
cpp2::Default.expects(cpp2::assert_in_bounds(t(), 1)==2, ""); | ||
cpp2::Default.expects(t()[1, 2]==3, ""); | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-bugfix-for-empty-index.cpp2.output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pure2-bugfix-for-empty-index.cpp2... ok (all Cpp2, passes safety checks) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4084,7 +4084,7 @@ class parser | |
//G postfix-expression: | ||
//G primary-expression | ||
//G postfix-expression postfix-operator [Note: without whitespace before the operator] | ||
//G postfix-expression '[' expression-list ']' | ||
//G postfix-expression '[' expression-list? ']' | ||
//G postfix-expression '(' expression-list? ')' | ||
//G postfix-expression '.' id-expression | ||
//G | ||
|
@@ -4161,13 +4161,9 @@ class parser | |
if (term.op->type() == lexeme::LeftBracket) | ||
{ | ||
term.expr_list = expression_list(term.op); | ||
if ( | ||
!term.expr_list | ||
|| term.expr_list->expressions.empty() | ||
) | ||
if (!term.expr_list) | ||
{ | ||
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 commentThe 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 |
||
error("[ is not followed by a valid expression list"); | ||
return {}; | ||
} | ||
if (curr().type() != lexeme::RightBracket) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.