Skip to content

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 2 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions regression-tests/pure2-bugfix-for-empty-index.cpp2
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]]
}
42 changes: 42 additions & 0 deletions regression-tests/test-results/pure2-bugfix-for-empty-index.cpp
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, "");
}

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)

2 changes: 2 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,6 +3029,7 @@ class cppfront
if (
flag_safe_subscripts
&& i->op->type() == lexeme::LeftBracket
&& std::ssize(i->expr_list->expressions) == 1
)
{
suffix.emplace_back( ")", i->op->position() );
Expand Down Expand Up @@ -3065,6 +3066,7 @@ class cppfront
if (
flag_safe_subscripts
&& i->op->type() == lexeme::LeftBracket
&& std::ssize(i->expr_list->expressions) == 1
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Owner

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.

)
{
prefix.emplace_back( "cpp2::assert_in_bounds(", i->op->position() );
Expand Down
10 changes: 3 additions & 7 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Copy link
Owner

@hsutter hsutter Jul 23, 2023

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.

error("[ is not followed by a valid expression list");
return {};
}
if (curr().type() != lexeme::RightBracket)
Expand Down