public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression
@ 2022-01-21 11:46 stephan.oostveen@nextlevel-electronics.com
2022-01-21 21:05 ` [Bug c++/104160] " pinskia at gcc dot gnu.org
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: stephan.oostveen@nextlevel-electronics.com @ 2022-01-21 11:46 UTC (permalink / raw)
To: 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.oostveen@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 <variant>
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<ParseError, Object, Array>;
template <typename JSONVariant>
constexpr int buggyImpl(JSONVariant& JSONValue) noexcept {
if constexpr (JSONValue.isArray()) {
return 42;
} else if constexpr (JSONValue.isError()) {
return 0;
} else {
return 5;
}
}
template <typename JSONVariant>
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.2.1, isl version
isl-0.18-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
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.2.1, isl version
isl-0.18-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ee1a2f208dd48c6a5d24bdcd31126e66
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-Wpedantic' '-O2'
'-std=c++17' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v --64 -o main.o main.s
GNU assembler version 2.35.2 (x86_64-redhat-linux) using BFD version version
2.35.2-6.fc34
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-Wpedantic' '-O2'
'-std=c++17' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
'-dumpdir' 'main.'
/usr/libexec/gcc/x86_64-redhat-linux/11/collect2 -plugin
/usr/libexec/gcc/x86_64-redhat-linux/11/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
-plugin-opt=-fresolution=main.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 --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -o main
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/11/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/11
-L/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../.. main.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-redhat-linux/11/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-Wpedantic' '-O2'
'-std=c++17' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
'-dumpdir' 'main.'
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
@ 2022-01-21 21:05 ` pinskia at gcc dot gnu.org
2022-01-21 21:06 ` pinskia at gcc dot gnu.org
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-21 21:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
struct Array {
static constexpr bool isArray() noexcept { return true; }
};
template <typename JSONVariant>
constexpr void buggyImpl(const JSONVariant& JSONValue) noexcept {
constexpr bool t = JSONValue.isArray();
}
int main() {
buggyImpl(Array{});
return 0;
}
There might be a dup of this too.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
2022-01-21 21:05 ` [Bug c++/104160] " pinskia at gcc dot gnu.org
@ 2022-01-21 21:06 ` pinskia at gcc dot gnu.org
2022-01-21 21:14 ` pinskia at gcc dot gnu.org
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-21 21:06 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Status|UNCONFIRMED |NEW
Last reconfirmed| |2022-01-21
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
2022-01-21 21:05 ` [Bug c++/104160] " pinskia at gcc dot gnu.org
2022-01-21 21:06 ` pinskia at gcc dot gnu.org
@ 2022-01-21 21:14 ` pinskia at gcc dot gnu.org
2022-01-21 21:15 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-21 21:14 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, MSVC accepts this code just like GCC.
ICC does too.
Only clang rejects it.
If Value was not marked as a constexpr, then GCC rejects it too.
I do have wonder if clang is right to reject this or not.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
` (2 preceding siblings ...)
2022-01-21 21:14 ` pinskia at gcc dot gnu.org
@ 2022-01-21 21:15 ` pinskia at gcc dot gnu.org
2022-06-22 9:36 ` csaba_22 at yahoo dot co.uk
2022-12-01 20:17 ` pinskia at gcc dot gnu.org
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-21 21:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The error message from clang is:
<source>:28:19: note: function parameter 'JSONValue' with unknown value cannot
be used in a constant expression
if constexpr (JSONValue.isArray()) {
^
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
` (3 preceding siblings ...)
2022-01-21 21:15 ` pinskia at gcc dot gnu.org
@ 2022-06-22 9:36 ` csaba_22 at yahoo dot co.uk
2022-12-01 20:17 ` pinskia at gcc dot gnu.org
5 siblings, 0 replies; 7+ messages in thread
From: csaba_22 at yahoo dot co.uk @ 2022-06-22 9:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
Csaba Ráduly <csaba_22 at yahoo dot co.uk> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |csaba_22 at yahoo dot co.uk
--- Comment #5 from Csaba Ráduly <csaba_22 at yahoo dot co.uk> ---
Here's my simple example:
https://godbolt.org/z/9aMevabbq
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/104160] Wrongly allowed: Function argumentin constant expression
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
` (4 preceding siblings ...)
2022-06-22 9:36 ` csaba_22 at yahoo dot co.uk
@ 2022-12-01 20:17 ` pinskia at gcc dot gnu.org
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-01 20:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104160
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |DUPLICATE
Status|NEW |RESOLVED
--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 79083.
*** This bug has been marked as a duplicate of bug 79083 ***
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-12-01 20:17 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 11:46 [Bug c++/104160] New: Wrongly allowed: Function argumentin constant expression stephan.oostveen@nextlevel-electronics.com
2022-01-21 21:05 ` [Bug c++/104160] " pinskia at gcc dot gnu.org
2022-01-21 21:06 ` pinskia at gcc dot gnu.org
2022-01-21 21:14 ` pinskia at gcc dot gnu.org
2022-01-21 21:15 ` pinskia at gcc dot gnu.org
2022-06-22 9:36 ` csaba_22 at yahoo dot co.uk
2022-12-01 20:17 ` 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).