Skip to content

[BUG] No support for multi-argument indexing #482

Closed

Description

@JohelEGP

Title: No support for multi-argument indexing.

Description:

An expression like x[y,z]
lowers to cpp2::assert_in_bounds(x, y, z),
but has an arity of 2.

Minimal reproducer (https://cpp2.godbolt.org/z/TPoGe1czc):

t: type = {
  operator[]: (inout this, x: _, y: _) -> i32 = 42;
}
main: () = {
  [[assert: t()[1, 2] == 42]]
}
Commands:
cppfront -clean-cpp1 main.cpp2
clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -I . main.cpp

Expected result:

It doesn't seem trivial to support multi-argument indexing in cpp2::assert_in_bounds.
See https://en.cppreference.com/w/cpp/container/mdspan/operator_at.

  cpp2::Default.expects(t()[1, 2]==42, "");

Actual result and error:

  cpp2::Default.expects(cpp2::assert_in_bounds(t(), 1, 2)==42, "");
Cpp2 lowered to Cpp1.
#include "cpp2util.h"

class t;
  
class t {
  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;
};
auto main() -> int;
  

  [[nodiscard]] auto t::operator[](auto const& x, auto const& y) -> cpp2::i32 { return 42;  }

auto main() -> int{
  cpp2::Default.expects(cpp2::assert_in_bounds(t(), 1, 2)==42, "");
}
Output.
build/_cppfront/main.cpp:20:25: error: no matching function for call to 'assert_in_bounds'
  cpp2::Default.expects(cpp2::assert_in_bounds(t(), 1, 2)==42, "");
                        ^~~~~~~~~~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:399:6: note: candidate function template not viable: requires 2 arguments, but 3 were provided
auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto)
     ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:407:6: note: candidate function template not viable: requires 2 arguments, but 3 were provided
auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto)
     ^

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions