[Bug c++/98919] New: auto declaration of multiple variables allows inconsistent types

2021-02-01 Thread stephan.oostveen--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98919

Bug ID: 98919
   Summary: auto declaration of multiple variables allows
inconsistent types
   Product: gcc
   Version: 10.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: stephan.oostv...@nextlevel-electronics.com
  Target Milestone: ---

Created attachment 50111
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50111&action=edit
Preprocessed file

gcc version: 10.2.1 20201125 (Red Hat 10.2.1-9) (GCC)
System version: Fedora 33 x86_64
Compile command: gcc -v -save-temps -std=c++17 -O3 -Wall -Wextra -Wpedantic -o
test test.cpp

auto allows to declare two different types in a single statement when used in a
templated context.

See also https://godbolt.org/z/brWYjP as an example.

Compiler output:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-offload-targets=nvptx-none
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20201125 (Red Hat 10.2.1-9) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++17' '-O3' '-Wall' '-Wextra'
'-Wpedantic' '-o' 'test' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/10/cc1plus -E -quiet -v -D_GNU_SOURCE
test.cpp -mtune=generic -march=x86-64 -std=c++17 -Wall -Wextra -Wpedantic -O3
-fpch-preprocess -o test.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/10/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/10/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10

/usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward
 /usr/lib/gcc/x86_64-redhat-linux/10/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++17' '-O3' '-Wall' '-Wextra'
'-Wpedantic' '-o' 'test' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/10/cc1plus -fpreprocessed test.ii -quiet
-dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -O3 -Wall -Wextra
-Wpedantic -std=c++17 -version -o test.s
GNU C++17 (GCC) version 10.2.1 20201125 (Red Hat 10.2.1-9)
(x86_64-redhat-linux)
compiled by GNU C version 10.2.1 20201125 (Red Hat 10.2.1-9), GMP
version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version
isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 10.2.1 20201125 (Red Hat 10.2.1-9)
(x86_64-redhat-linux)
compiled by GNU C version 10.2.1 20201125 (Red Hat 10.2.1-9), GMP
version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version
isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9e47728dd41983b7aeec830bfccf6d2e
test.cpp: In instantiation of ‘constexpr void test() [with long unsigned int N
= 42]’:
test.cpp:15:14:   required from here
test.cpp:11:10: warning: unused variable ‘aIt’ [-Wunused-variable]
   11 | auto aIt = begin(a), bIt = begin(b);
  |  ^~~
test.cpp:11:26: warning: unused variable ‘bIt’ [-Wunused-variable]
   11 | auto aIt = begin(a), bIt = begin(b);
  |  ^~~
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++17' '-O3' '-Wall' '-Wextra'
'-Wpedantic' '-o' 'test' '-mtune=generic' '-march=x86-64'
 as -v --64 -o test.o test.s
GNU assembler version 2.35 (x86_64-redhat-linux) using BFD version version
2.35-18.fc33
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/10/:/usr/libexec/gcc/x86_64-redhat-linux/10/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/10/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/10/:/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/10/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps'

[Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression

2022-01-21 Thread stephan.oostveen--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160

Bug ID: 104160
   Summary: Wrongly allowed: Function argumentin constant
expression
   Product: gcc
   Version: 11.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: stephan.oostv...@nextlevel-electronics.com
  Target Milestone: ---

Created attachment 52256
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52256&action=edit
Preprocessed file

The code found below compiles using several versions of GCC (since 7.1), while
clang is refusing it since version 5.0. I believe that clang is right in this
case, I have tested using the following compiler flags for both compilers:
-Wall -Wextra -Wpedantic -O2 -std=c++17 

Attached you will find the .ii file generated by the following command, the
output is found below:
g++ -v -save-temps -Wall -Wextra -Wpedantic -O2 -std=c++17 main.cpp -o main

The code:
// External C++ includes
#include 
class Base {
   public:

static constexpr bool isError() noexcept { return false; }
static constexpr bool isArray() noexcept { return false; }
static constexpr bool isObject() noexcept { return false; }
};

class ParseError : public Base {
   public:
static constexpr bool isError() noexcept { return true; }
};
class Object : public Base {
   public:
static constexpr bool isObject() noexcept { return true; }
};
class Array : public Base {
   public:
static constexpr bool isArray() noexcept { return true; }
};

using Value = std::variant;

template 
constexpr int buggyImpl(JSONVariant& JSONValue) noexcept {
if constexpr (JSONValue.isArray()) {
return 42;
} else if constexpr (JSONValue.isError()) {
return 0;
} else {
return 5;
}
}

template 
constexpr int correctImpl(JSONVariant& /*unused*/) noexcept {
if constexpr (JSONVariant::isArray()) {
return 42;
} else if constexpr (JSONVariant::isError()) {
return 0;
} else {
return 5;
}
}

int main() {
constexpr Value jsonVariant{Array{}};
static_assert(std::visit([](auto& v) noexcept { return buggyImpl(v); },
jsonVariant) == 42);
static_assert(std::visit([](auto& v) noexcept {return correctImpl(v);},
jsonVariant) == 42);
return 0;
}

The output:
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-Wpedantic' '-O2'
'-std=c++17' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1plus -E -quiet -v -D_GNU_SOURCE
main.cpp -mtune=generic -march=x86-64 -std=c++17 -Wall -Wextra -Wpedantic -O2
-fpch-preprocess -o main.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11

/usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/backward
 /usr/lib/gcc/x86_64-redhat-linux/11/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-Wpedantic' '-O2'
'-std=c++17' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1plus -fpreprocessed main.ii -quiet
-dumpbase main.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -O2 -Wall
-Wextra -Wpedantic -std=c++17 -version -o main.s
GNU C++17 (GCC) version 11.2.1 20210728 (Red Hat 11.2.1-1)
(x86_64-redhat-linux)
compiled by GNU C version 11.2.1 20210728 (Red Hat 11.2.1-1), GMP
version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.