public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation
@ 2021-03-08 10:27 gcc.mexon at spamgourmet dot com
  2021-03-08 11:00 ` [Bug c++/99460] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gcc.mexon at spamgourmet dot com @ 2021-03-08 10:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99460

            Bug ID: 99460
           Summary: [C++20] Template with complex non-type argument
                    re-uses different specialisation
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc.mexon at spamgourmet dot com
  Target Milestone: ---

See below example of a Builder class.  It is templated with an array of two
booleans, and includes member functions that return an instance of a different
specialisation of the same template.  However, the different specialisations
appear to get mixed up.

The intended behaviour is that both usages in main() result in a Builder with
both booleans set.  In fact the result is that the second usage only has one
boolean set.  However, removing the first usage resolves the problem,
indicating that the problem is caused by unrelated specialisations interfering
with each other.

I checked this with gcc 9.3.0 and gcc 10.2.0.  I also tried compiling from git,
version 11.0.1 change a18ebd6c439.  Command line was "g++ -Wall --std=c++2a
builder.cpp".

More discussion and examples [on
StackOverflow](https://stackoverflow.com/questions/66520012/c-20-stdarray-as-non-type-template-argument-reshuffles-elements/66520217#66520217).

It seems likely that this is a known problem, in which case this bug report can
be closed as a duplicate.  But I would like a tracking bug to attach to the
StackOverflow question.

----

#include <array>
#include <cassert>

using Flags = std::array<bool, 2>;

template<Flags flags = Flags{}>
class Builder
{
public:
    Builder() {
    }

    auto SetFirst() {
        constexpr auto new_flags = SetFieldFlag<0>();
        Builder<new_flags> new_builder;
        return new_builder;
    }

    auto SetSecond() {
        constexpr auto new_flags = SetFieldFlag<1>();
        Builder<new_flags> new_builder;
        return new_builder;
    }

    Flags GetFlags() const {
        return flags;
    }

private:
    template<int field>
    static constexpr auto SetFieldFlag() {
        auto new_flags = flags;
        std::get<field>(new_flags) = true;
        return new_flags;
    }
};

int main()
{
    auto flags1 = Builder().SetFirst().SetSecond().GetFlags();
    assert(flags1[0]);
    assert(flags1[1]);

    auto flags2 = Builder().SetSecond().SetFirst().GetFlags();
    assert(flags2[0]);
    assert(flags2[1]);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99460] [C++20] Template with complex non-type argument re-uses different specialisation
  2021-03-08 10:27 [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation gcc.mexon at spamgourmet dot com
@ 2021-03-08 11:00 ` redi at gcc dot gnu.org
  2021-03-08 13:05 ` gcc.mexon at spamgourmet dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-08 11:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99460

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-03-08
           Keywords|                            |wrong-code

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99460] [C++20] Template with complex non-type argument re-uses different specialisation
  2021-03-08 10:27 [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation gcc.mexon at spamgourmet dot com
  2021-03-08 11:00 ` [Bug c++/99460] " redi at gcc dot gnu.org
@ 2021-03-08 13:05 ` gcc.mexon at spamgourmet dot com
  2021-03-08 22:09 ` gcc.mexon at spamgourmet dot com
  2021-08-23 21:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: gcc.mexon at spamgourmet dot com @ 2021-03-08 13:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99460

--- Comment #1 from mexon <gcc.mexon at spamgourmet dot com> ---
Here's a transcript showing the platform (Ubuntu 20.10 in a virtual machine)
and output of g++ -v -save-temps:

$ vagrant init ubuntu/groovy64
$ vagrant up
$ ssh 127.0.0.1

vagrant@ubuntu-groovy:~$ sudo apt install build-essential bison flex
libgmp3-dev libmpc-dev libmpfr-dev texinfo
vagrant@ubuntu-groovy:~$ wget
ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-10.2.0/gcc-10.2.0.tar.gz
vagrant@ubuntu-groovy:~$ tar xzf gcc-10.2.0.tar.gz 
vagrant@ubuntu-groovy:~$ cd gcc-10.2.0
vagrant@ubuntu-groovy:~/gcc-10.2.0$ ./contrib/download_prerequisites
vagrant@ubuntu-groovy:~/gcc-10.2.0$ cd ..
vagrant@ubuntu-groovy:~$ mkdir build-gcc
vagrant@ubuntu-groovy:~$ cd build-gcc
vagrant@ubuntu-groovy:~/build-gcc$ 
vagrant@ubuntu-groovy:~/build-gcc$ export PREFIX=$HOME/install-gcc
vagrant@ubuntu-groovy:~/build-gcc$ ../gcc-10.2.0/configure --prefix="$PREFIX"
--disable-nls --enable-languages=c,c++ --disable-multilib
vagrant@ubuntu-groovy:~/build-gcc$ make -j8
vagrant@ubuntu-groovy:~/build-gcc$ make install
vagrant@ubuntu-groovy:~/build-gcc$ cd ..
vagrant@ubuntu-groovy:~$ export PATH=$PREFIX/bin:$PATH
vagrant@ubuntu-groovy:~$ g++ -v -save-temps -Wall --std=c++2a
builder_simplified.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-10.2.0/configure --prefix=/home/vagrant/install-gcc
--disable-nls --enable-languages=c,c++ --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++2a' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus -E
-quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE builder_simplified.cpp
-mtune=generic -march=x86-64 -std=c++2a -Wall -fpch-preprocess -o
builder_simplified.ii
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/x86_64-pc-linux-gnu

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/backward
 /home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include
 /usr/local/include
 /home/vagrant/install-gcc/include
 /home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++2a' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus
-fpreprocessed builder_simplified.ii -quiet -dumpbase builder_simplified.cpp
-mtune=generic -march=x86-64 -auxbase builder_simplified -Wall -std=c++2a
-version -o builder_simplified.s
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.1.0, MPFR version
3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.1.0, MPFR version
3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 431465f3c3fef2337a2db16592d4b8cd
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++2a' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 as -v --64 -o builder_simplified.o builder_simplified.s
GNU assembler version 2.35.1 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.35.1
COMPILER_PATH=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/:/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/:/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib64/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++2a' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/collect2
-plugin
/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/liblto_plugin.so
-plugin-opt=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
-plugin-opt=-fresolution=builder_simplified.res
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 /lib/x86_64-linux-gnu/crt1.o
/lib/x86_64-linux-gnu/crti.o
/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtbegin.o
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib64
-L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu
-L/usr/lib/../lib64
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../..
builder_simplified.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtend.o
/lib/x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++2a' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
vagrant@ubuntu-groovy:~$ ./a.out
a.out: builder_simplified.cpp:46: int main(): Assertion `flags2[1]' failed.
Aborted (core dumped)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99460] [C++20] Template with complex non-type argument re-uses different specialisation
  2021-03-08 10:27 [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation gcc.mexon at spamgourmet dot com
  2021-03-08 11:00 ` [Bug c++/99460] " redi at gcc dot gnu.org
  2021-03-08 13:05 ` gcc.mexon at spamgourmet dot com
@ 2021-03-08 22:09 ` gcc.mexon at spamgourmet dot com
  2021-08-23 21:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: gcc.mexon at spamgourmet dot com @ 2021-03-08 22:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99460

--- Comment #2 from mexon <gcc.mexon at spamgourmet dot com> ---
For completeness, output from change a18ebd6c439:

vagrant@ubuntu-groovy:~$ g++ -v -save-temps -Wall --std=c++2a
builder_simplified.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-git/configure --prefix=/home/vagrant/install-gcc
--disable-nls --enable-languages=c,c++ --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.1 20210307 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++20' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/cc1plus -E
-quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE builder_simplified.cpp
-mtune=generic -march=x86-64 -std=c++20 -Wall -fpch-preprocess -o
a-builder_simplified.ii
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../include/c++/11.0.1

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../include/c++/11.0.1/x86_64-pc-linux-gnu

/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../include/c++/11.0.1/backward
 /home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/include
 /usr/local/include
 /home/vagrant/install-gcc/include
 /home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++20' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/cc1plus
-fpreprocessed a-builder_simplified.ii -quiet -dumpdir a- -dumpbase
builder_simplified.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -Wall
-std=c++20 -version -o a-builder_simplified.s
GNU C++20 (GCC) version 11.0.1 20210307 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 11.0.1 20210307 (experimental), GMP version
6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++20 (GCC) version 11.0.1 20210307 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 11.0.1 20210307 (experimental), GMP version
6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 95262a810cbe258ee31ada9b2568b963
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++20' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 as -v --64 -o a-builder_simplified.o a-builder_simplified.s
GNU assembler version 2.35.1 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.35.1
COMPILER_PATH=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/:/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/:/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib64/:/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++20' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'
 /home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/collect2
-plugin
/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/liblto_plugin.so
-plugin-opt=/home/vagrant/install-gcc/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/lto-wrapper
-plugin-opt=-fresolution=a.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o
/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/crtbegin.o
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../../../lib64
-L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu
-L/usr/lib/../lib64
-L/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/../../..
a-builder_simplified.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/home/vagrant/install-gcc/lib/gcc/x86_64-pc-linux-gnu/11.0.1/crtend.o
/lib/x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c++20' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'
vagrant@ubuntu-groovy:~$ ./a.out
a.out: builder_simplified.cpp:46: int main(): Assertion `flags2[1]' failed.
Aborted (core dumped)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99460] [C++20] Template with complex non-type argument re-uses different specialisation
  2021-03-08 10:27 [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation gcc.mexon at spamgourmet dot com
                   ` (2 preceding siblings ...)
  2021-03-08 22:09 ` gcc.mexon at spamgourmet dot com
@ 2021-08-23 21:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-23 21:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99460

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=94511

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This seems similar to PR 94511.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-23 21:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-08 10:27 [Bug c++/99460] New: [C++20] Template with complex non-type argument re-uses different specialisation gcc.mexon at spamgourmet dot com
2021-03-08 11:00 ` [Bug c++/99460] " redi at gcc dot gnu.org
2021-03-08 13:05 ` gcc.mexon at spamgourmet dot com
2021-03-08 22:09 ` gcc.mexon at spamgourmet dot com
2021-08-23 21:02 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).