hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 223 forks source link

[BUG] Wrong generation of construction for @struct and clang-15 #1146

Closed MaxSagebaum closed 2 days ago

MaxSagebaum commented 4 days ago

Describe the bug While using clang-15 a structure created with @struct can not be initialized with values while using clang-15.

This does not seem to be a problem in gcc-10 and upwards.

To Reproduce On compiler explorer: https://cpp2.godbolt.org/z/Me86GzYE8

match_return: @struct <Iter> type = {
    matched: bool = false;
    pos: Iter = ();
}

main: () = {
    c: char = 'c';
    v := match_return<*char>(false, c&);
    _ = v;
}

Error:

/opt/compiler-explorer/clang-15.0.0/bin/clang++ --gcc-toolchain=/opt/compiler-explorer/gcc-12.2.0  -I/app -I/app -std=c++20 -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -Werror=unused-value -Werror=unused-parameter -stdlib=libc++ -Wno-read-modules-implicitly -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /app/build/main.cpp
warning: unknown warning option '-Wno-read-modules-implicitly'; did you mean '-Wno-module-conflict'? [-Wunknown-warning-option]
main.cpp2:8:13: error: no matching constructor for initialization of 'match_return<char *>'
    auto v {match_return<char*>(false, &c)}; 
            ^                   ~~~~~~~~~
main.cpp2:1:31: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
template<typename Iter> class match_return {
                              ^
main.cpp2:1:31: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
main.cpp2:1:31: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 2 were provided
1 warning and 1 error generated.

Variants

MaxSagebaum commented 4 days ago

This could be fixed by generating the appropriate constructor. That is a constructor with as many arguments as member in the order of the members.

hsutter commented 2 days ago

Thanks! I ran into this in cppfront's own code, notably in the parse.h types, where I had to do the same thing -- write out the constructor. It also happened only in newer/stricter compilers. For aggregates, my untested suspicion is that the problem might go away on its own once the rule change for () aggregate initialization is implemented, but there's no reason not to fix it in the meantime anyway...

hsutter commented 2 days ago

Oh wait, I forgot to re-run regression, still have bugs to fix...

hsutter commented 2 days ago

Now actually fixed...