Skip to content

[BUG] std::array usage #564

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

Closed
jmafc opened this issue Aug 1, 2023 · 6 comments
Closed

[BUG] std::array usage #564

jmafc opened this issue Aug 1, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@jmafc
Copy link

jmafc commented Aug 1, 2023

Describe the bug
cppfront -p allows both operator[] and at() on a std::array, but these result in errors in g++.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code
main: () = {
    arr: std::array<std::string, 5>;
    arr[0] = "some";
    arr.at(1) = "thing";
}
  1. Command lines including which C++ compiler you are using

cppfront compiler v0.2.1 Build 8724:0846
g++ (Debian 12.2.0-14) 12.2.0

$ cppfront test.cpp2 -p
test.cpp2... ok (all Cpp2, passes safety checks)

$ c++ -std=c++20 -I ~/src/include test.cpp
  1. Expected result

Although it's unclear whether std::array is properly supported, I was hoping that at least the .at() syntax would be accepted (and cppfront did accept both, as shown above) and subsequently compiled by g++.

  1. Actual result/error

The compilation step results in the following:

In file included from test.cpp:7:
/home/jma/src/include/cpp2util.h: In instantiation of ‘decltype(auto) cpp2::assert_in_bounds(auto:56&&, auto:57&&) requires !((is_integral_v<typename std::remove_cvref<decltype(cpp2::assert_in_bounds::arg)>::type>) && requires{std::ssize(cpp2::assert_in_bounds::x);(cpp2::assert_in_bounds::x)[cpp2::assert_in_bounds::arg];std::begin(cpp2::assert_in_bounds::x) + 2;}) [with auto:56 = deferred_init<std::array<std::__cxx11::basic_string<char>, 5> >&; auto:57 = int]’:
test.cpp2:3:27:   required from here
/home/jma/src/include/cpp2util.h:411:28: error: no match for ‘operator[]’ (operand types are ‘cpp2::deferred_init<std::array<std::__cxx11::basic_string<char>, 5> >’ and ‘int’)
  411 |     return CPP2_FORWARD(x) [ CPP2_FORWARD(arg) ];

If the arr[0] line is commented out, g++ outputs the following:

In file included from test.cpp:7:
test.cpp2: In instantiation of ‘main()::<lambda(auto:89&&, auto:90&& ...)> [with auto:89 = cpp2::deferred_init<std::array<std::__cxx11::basic_string<char>, 5> >; auto:90 = {int}]’:
test.cpp2:4:5:   required from here
/home/jma/src/include/cpp2util.h:718:24: error: ‘at’ was not declared in this scope
  718 |         return FUNCNAME(CPP2_FORWARD(obj), CPP2_FORWARD(params)...); \
test.cpp2:4:5: note: in expansion of macro ‘CPP2_UFCS’
    4 |     arr.at(1) = "thing";
      |     ^~~~~~~~~

Additional context
I have been alpha-testing cppfront with some "beginners" C++-11 programs. It would be very helpful if there was a simple list of keywords/features with an indication of support status.

@jmafc jmafc added the bug Something isn't working label Aug 1, 2023
@JohelEGP
Copy link
Contributor

JohelEGP commented Aug 1, 2023

You're missing the initializer, see https://cpp2.godbolt.org/z/48vhscfKj or https://cpp2.godbolt.org/z/WbGdec3bz:

-    arr: std::array<std::string, 5>;
+    arr: std::array<std::string, 5> = ();
     arr: std::array<std::string, 5>;
+    arr = ();

@JohelEGP
Copy link
Contributor

JohelEGP commented Aug 1, 2023

You should've gotten an error like https://cpp2.godbolt.org/z/YraaP1oTK:

main.cpp2(3,9): error: local variable arr is used before it was initialized
  ==> program violates initialization safety guarantee - see previous errors

So there's definitely a bug in Cppfront.

@jmafc
Copy link
Author

jmafc commented Aug 1, 2023

Ok, yes, I just discovered that when I tried to call a cpp1 function to add the string. Then it did tell me that I was trying to use the array before initializing it.

@jmafc
Copy link
Author

jmafc commented Aug 2, 2023

One other thing that I've noticed (although this may be unrelated to the bug described above), is that if cppfront is invoked without the -p, the generated .cpp does not include the line

#define CPP2_USE_MODULES         Yes

and as a result, g++ then fails with the message that std::array ... has initializer but incomplete type.

@JohelEGP
Copy link
Contributor

JohelEGP commented Aug 2, 2023

Yeah.
-pure-cpp2 implies import std;.
So without -p, "cpp2util.h" happens to not #include <array>, so you need that.

@hsutter
Copy link
Owner

hsutter commented Aug 12, 2023

Thanks. Good points... I'm working on a commit to give a better diagnostic for the lack of first use, and to always emit a deferred constructions as list initialization (which I'd already been moving towards in recent commits... sorry, vector<size_t>::vector(size_t,size_t)).

So this example:

arr: std::array<std::string, 5>;
arr[0] = "some";
arr.at(1) = "thing";

will get the nicer error error: local variable arr is used before it was initialized.

And this fixed example:

arr: std::array<std::string, 5>;
arr = ("a","b","c","d","e");  // now fixed
arr[0] = "some";
arr.at(1) = "thing";

will now work.

zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
…sutter#564

Also fixed a build break that slipped in a couple of hours ago for GCC and Clang (from inadvertently using an incomplete type, which MSVC didn't diagnose and I hadn't re-run the GCC and Clang builds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants