public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104691] New: SFINAE does not disable static_assert
@ 2022-02-25 15:45 github at quantconsulting dot com
  2022-02-25 15:53 ` [Bug c++/104691] " github at quantconsulting dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: github at quantconsulting dot com @ 2022-02-25 15:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104691
           Summary: SFINAE does not disable static_assert
           Product: gcc
           Version: og10 (devel/omp/gcc-10)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: github at quantconsulting dot com
  Target Milestone: ---

Created attachment 52516
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52516&action=edit
File generated by -save-temps

Description: "Substitution failure is not an error" (SFINAE) correctly disables
a second instantiation of a templated function and compilation continues to
completion -- unless the implementation to be disabled by SFINAE contains a
failing static_assert, in which case compilation reports the static_assert
failure as an error and halts.

Expected behavior:
Even when the failing static_assert is present, compilation should continue to
completion because there is substitution failure -- which is *earlier* in that
function instantiation, no less -- and thus the static_assert failure should be
ignored.

gcc -v:
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
10.3.0-1ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-10
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-10-S4I5Pr/gcc-10-10.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-S4I5Pr/gcc-10-10.3.0/debian/tmp-gcn/usr,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Ubuntu 10.3.0-1ubuntu1~20.04) 

Command line that triggers the bug:
g++ -save-temps -o test3 test3.cc

Error message:
test3.cc: In function ‘std::enable_if_t<std::is_class<typename
TContainer::allocator_type>::value, TContainer> MakeFilled(const typename
std::remove_reference<decltype (* TContainer{}.begin())>::type&)’:
test3.cc:23:17: error: static assertion failed: Substitution failure so this
should never be checked!
   23 |   static_assert(false, "Substitution failure so this should never be
checked!");
      |                 ^~~~~

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
@ 2022-02-25 15:53 ` github at quantconsulting dot com
  2022-02-25 17:01 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: github at quantconsulting dot com @ 2022-02-25 15:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Leengit <github at quantconsulting dot com> ---
Closely related bug: although not demonstrated in the supplied code, note that
compilation should complete successfully even if the static_assert fails to
compile because its first argument is not `constexpr` given these template
arguments.  Whether because of the substitution error for the first argument of
the static_assert or the substitution error earlier in the instantiation -- one
or both of these should cause the failure to be `constexpr` to be ignored.

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
  2022-02-25 15:53 ` [Bug c++/104691] " github at quantconsulting dot com
@ 2022-02-25 17:01 ` pinskia at gcc dot gnu.org
  2022-02-25 17:36 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-25 17:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The unincluded source:
#include <array>
#include <iostream>
#include <type_traits>

template <typename TContainer>
std::enable_if_t<!TContainer{}.empty(), TContainer>
MakeFilled(const typename
std::remove_reference<decltype(*TContainer{}.begin())>::type & value)
{
  TContainer result{};
  std::fill(result.begin(), result.end(), value);
  return result;
}

template <typename TContainer>
std::enable_if_t<std::is_class<typename TContainer::allocator_type>::value,
TContainer>
MakeFilled(const typename
std::remove_reference<decltype(*TContainer{}.begin())>::type & value)
{
  static_assert(false, "Substitution failure so this should never be
checked!");
}


int
main()
{
  using ArrayWithPositiveSize = std::array<int, 4>;
  ArrayWithPositiveSize a = MakeFilled<ArrayWithPositiveSize>(8);
  std::cout << "a[" << a.size() - 1 << "] = " << a[a.size() - 1] << std::endl;
}
---- CUT ----
clang also causes the assert to happen. I have not looked further into why
though.

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
  2022-02-25 15:53 ` [Bug c++/104691] " github at quantconsulting dot com
  2022-02-25 17:01 ` pinskia at gcc dot gnu.org
@ 2022-02-25 17:36 ` redi at gcc dot gnu.org
  2022-02-25 17:38 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-25 17:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Not a bug, you need to make your static_assert condition type dependent or
value dependent, static_assert(false) will always fire.

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
                   ` (2 preceding siblings ...)
  2022-02-25 17:36 ` redi at gcc dot gnu.org
@ 2022-02-25 17:38 ` redi at gcc dot gnu.org
  2022-02-25 17:45 ` redi at gcc dot gnu.org
  2023-02-18 21:19 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-25 17:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:

template<typename T>
typename T::foo
f(T) { }

template<typename T>
typename T::bar
f(T) { static_assert(false, ""); }

struct X { using foo = void; };

int main()
{
  X x;
  f(x);
}


All compilers reject this code.

Something as simple as static_assert(sizeof(T) != 0, ""); can be used to make
it dependent, and then it will only fail when the template is instantiated.

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
                   ` (3 preceding siblings ...)
  2022-02-25 17:38 ` redi at gcc dot gnu.org
@ 2022-02-25 17:45 ` redi at gcc dot gnu.org
  2023-02-18 21:19 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-25 17:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
N.B. this has nothing to do with SFINAE. The static_assert fails when the
template is first parsed *not* when instantiated. You can verify this easily:

template<typename T>
typename T::bar
f(T) { static_assert(false, ""); }

There is no SFINAE here, because there is no call and no substitution.

The relevant rule in the standard is:

The program is ill-formed, no diagnostic required, if:
— no valid specialization can be generated for a template or a substatement of
a constexpr if statement (8.5.2) within a template and the template is not
instantiated, or [...]

No valid specialization can ever be generated for that template. Every possible
set of template arguments will result in a failing static assert.

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

* [Bug c++/104691] SFINAE does not disable static_assert
  2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
                   ` (4 preceding siblings ...)
  2022-02-25 17:45 ` redi at gcc dot gnu.org
@ 2023-02-18 21:19 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-02-18 21:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:9944ca17c0766623bce260684edc614def7ea761

commit r13-6133-g9944ca17c0766623bce260684edc614def7ea761
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Feb 10 16:16:45 2023 -0800

    c++: static_assert (false) in template [DR2518]

    For a long time, people have expected to be able to write
    static_assert (false) in a template and only have it diagnosed if the
    template is instantiated, but we (and other implementations) gave an error
    about the uninstantiated template because the standard says that if no
valid
    instantiation of the template is possible, the program is ill-formed, no
    diagnostic required, and we try to diagnose IFNDR things when feasible.

    At the meeting last week we were looking at CWG2518, which wanted to
specify
    that an implementation must not accept a program containing a failing
#error
    or static_assert.  We also looked at P2593, which proposed allowing
    static_assert in an uninstantiated template.  We ended up combining these
    two in order to avoid requiring implementations to reject programs with
    static_assert (false) in uninstantiated templates.

    The committee accepted this as a DR, so I'm making the change to all
    standard modes.  This behavior was also conformant previously, since no
    diagnostic was required in this case.

    We continue to diagnose non-constant or otherwise ill-formed conditions, so
    no changes to existing tests were needed.

            DR 2518
            PR c++/52809
            PR c++/53638
            PR c++/87389
            PR c++/89741
            PR c++/92099
            PR c++/104041
            PR c++/104691

    gcc/cp/ChangeLog:

            * semantics.cc (finish_static_assert): Don't diagnose in
            template context.

    gcc/testsuite/ChangeLog:

            * g++.dg/DRs/dr2518.C: New test.

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

end of thread, other threads:[~2023-02-18 21:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-25 15:45 [Bug c++/104691] New: SFINAE does not disable static_assert github at quantconsulting dot com
2022-02-25 15:53 ` [Bug c++/104691] " github at quantconsulting dot com
2022-02-25 17:01 ` pinskia at gcc dot gnu.org
2022-02-25 17:36 ` redi at gcc dot gnu.org
2022-02-25 17:38 ` redi at gcc dot gnu.org
2022-02-25 17:45 ` redi at gcc dot gnu.org
2023-02-18 21:19 ` cvs-commit 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).