public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly
@ 2021-05-03 14:41 vopl at bk dot ru
  2021-05-03 15:14 ` [Bug c++/100396] " ppalka at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: vopl at bk dot ru @ 2021-05-03 14:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100396
           Summary: [11.1 regression] The template function overload is
                    not selected correctly
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vopl at bk dot ru
  Target Milestone: ---

Regression in 11.1.0



$ cat src.cpp && echo EOFFFFFFF
/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args>
constexpr bool valid = (0 < sizeof(F{}(Args{}...)));

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args>
void ovr(F f, Args... args)
requires valid<F, Args...> // external checker - fail, wrong Args formed
//requires (0 < sizeof(F{}(Args{}...))) // inline checker - ok (are wrong Args
compensated by SFINAE?)
{
    return ovr<F, Args...>(int{}, f, args...); // a second ovr is expected to
be called, but the compiler tries to call this ovr again. Collision between
'int' and 'F' is ignored, extra Args appended from 'f'
}

template<class F, class... Args> void ovr(int, F, Args...){}

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
void use()
{
    ovr([]{return 'c';});
}

EOFFFFFFF



$ g++-11.1.0 -std=c++20 -c src.cpp 
src.cpp: In instantiation of 'constexpr const bool valid<use()::<lambda()>,
use()::<lambda()> >':
src.cpp:8:10:   required by substitution of 'template<class F, class ... Args>
void ovr(F, Args ...) requires  valid<F, Args ...> [with F = use()::<lambda()>;
Args = {use()::<lambda()>}]'
src.cpp:11:27:   required from 'void ovr(F, Args ...) requires  valid<F, Args
...> [with F = use()::<lambda()>; Args = {}]'
src.cpp:19:8:   required from here
src.cpp:3:39: error: no match for call to '(use()::<lambda()>)
(use()::<lambda()>)'
    3 | constexpr bool valid = (0 < sizeof(F{}(Args{}...)));
      |                                   ~~~~^~~~~~~~~~~~
src.cpp:3:39: note: candidate: 'char (*)()' (conversion)
src.cpp:3:39: note:   candidate expects 1 argument, 2 provided
src.cpp:19:9: note: candidate: 'use()::<lambda()>'
   19 |     ovr([]{return 'c';});
      |         ^
src.cpp:19:9: note:   candidate expects 0 arguments, 1 provided



$ g++-11.1.0 -v
Using built-in specs.
COLLECT_GCC=g++-11.1.0
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/11.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-11.1.0/work/gcc-11.1.0/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.1.0
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.1.0
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.1.0/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.1.0/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/g++-v11
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.1.0/python
--enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --disable-nls --enable-checking=release
--with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 11.1.0 p1'
--disable-esp --enable-libstdcxx-time --with-build-config=bootstrap-lto
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64
--disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp
--disable-libada --enable-systemtap --disable-valgrind-annotations
--enable-vtable-verify --with-zstd --enable-lto --with-isl
--disable-isl-version-check --enable-default-pie --disable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.0 (Gentoo 11.1.0 p1)

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

* [Bug c++/100396] [11.1 regression] The template function overload is not selected correctly
  2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
@ 2021-05-03 15:14 ` ppalka at gcc dot gnu.org
  2021-05-03 16:07 ` vopl at bk dot ru
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-05-03 15:14 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Thanks for the bug report.  It looks like we started rejecting this after
r11-2774.

The testcase is invalid, I think.  You need to declare 'valid' as a concept
instead of a constexpr variable so that substitution failure during constraint
checking of the first overload of 'ovr' is a SFINAE error.

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

* [Bug c++/100396] [11.1 regression] The template function overload is not selected correctly
  2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
  2021-05-03 15:14 ` [Bug c++/100396] " ppalka at gcc dot gnu.org
@ 2021-05-03 16:07 ` vopl at bk dot ru
  2021-05-03 17:59 ` ppalka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: vopl at bk dot ru @ 2021-05-03 16:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from vopl at bk dot ru ---
Please, try this, also failed

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args> struct Checker
{
    using Some = decltype(F{}(Args{}...));
};

template<class F, class... Args> concept valid =
    requires { typename Checker<F, Args...>::Some; };

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args> auto ovr(F f, Args... args)
requires valid<F, Args...>
{
    return ovr<F, Args...>(int{}, f, args...); // a second ovr is expected to
be called, but the compiler tries to call this ovr again. Collision between
'int' and 'F' is ignored, extra Args provided to call
}

template<class F, class... Args> auto ovr(int, F f, Args... args)
{
    return f(args...);
}

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
void use()
{
    ovr([]{});
}


$ g++-11.1.0 -std=c++20 -c src.cpp 
src.cpp: In instantiation of 'struct Checker<use()::<lambda()>,
use()::<lambda()> >':
src.cpp:8:25:   required by substitution of 'template<class F, class ... Args>
auto ovr(F, Args ...) requires  valid<F, Args ...> [with F = use()::<lambda()>;
Args = {use()::<lambda()>}]'
src.cpp:14:27:   required from 'auto ovr(F, Args ...) requires  valid<F, Args
...> [with F = use()::<lambda()>; Args = {}]'
src.cpp:25:8:   required from here
src.cpp:4:30: error: no match for call to '(use()::<lambda()>)
(use()::<lambda()>)'
    4 |     using Some = decltype(F{}(Args{}...));
      |                           ~~~^~~~~~~~~~~
src.cpp:4:30: note: candidate: 'void (*)()' (conversion)
src.cpp:4:30: note:   candidate expects 1 argument, 2 provided
src.cpp:25:9: note: candidate: 'use()::<lambda()>'
   25 |     ovr([]{});
      |         ^
src.cpp:25:9: note:   candidate expects 0 arguments, 1 provided

----------
IMHO, the problem is not caused by concepts, but caused by corrupted
overloading selection mechanic. Please, take a look at the collision between
argument 'int{}' and first template parameter specified as 'F'. Next, no SFINAE
expected in this case due to correct overloading, but overloading failed and
SFINAE happens. Thanks.

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

* [Bug c++/100396] [11.1 regression] The template function overload is not selected correctly
  2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
  2021-05-03 15:14 ` [Bug c++/100396] " ppalka at gcc dot gnu.org
  2021-05-03 16:07 ` vopl at bk dot ru
@ 2021-05-03 17:59 ` ppalka at gcc dot gnu.org
  2021-05-04 10:08 ` vopl at bk dot ru
  2021-05-04 14:29 ` [Bug c++/100396] [11 " ppalka at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-05-03 17:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to vopl from comment #2)
> Please, try this, also failed
> 
> /////////0/////////1/////////2/////////3/////////4/////////5/////////6///////
> //7
> template<class F, class... Args> struct Checker
> {
>     using Some = decltype(F{}(Args{}...));
> };
> 
> template<class F, class... Args> concept valid =
>     requires { typename Checker<F, Args...>::Some; };
> 
> /////////0/////////1/////////2/////////3/////////4/////////5/////////6///////
> //7
> template<class F, class... Args> auto ovr(F f, Args... args)
> requires valid<F, Args...>
> {
>     return ovr<F, Args...>(int{}, f, args...); // a second ovr is expected
> to be called, but the compiler tries to call this ovr again. Collision
> between 'int' and 'F' is ignored, extra Args provided to call
> }
> 
> template<class F, class... Args> auto ovr(int, F f, Args... args)
> {
>     return f(args...);
> }
> 
> /////////0/////////1/////////2/////////3/////////4/////////5/////////6///////
> //7
> void use()
> {
>     ovr([]{});
> }
> 
> 
> $ g++-11.1.0 -std=c++20 -c src.cpp 
> src.cpp: In instantiation of 'struct Checker<use()::<lambda()>,
> use()::<lambda()> >':
> src.cpp:8:25:   required by substitution of 'template<class F, class ...
> Args> auto ovr(F, Args ...) requires  valid<F, Args ...> [with F =
> use()::<lambda()>; Args = {use()::<lambda()>}]'
> src.cpp:14:27:   required from 'auto ovr(F, Args ...) requires  valid<F,
> Args ...> [with F = use()::<lambda()>; Args = {}]'
> src.cpp:25:8:   required from here
> src.cpp:4:30: error: no match for call to '(use()::<lambda()>)
> (use()::<lambda()>)'
>     4 |     using Some = decltype(F{}(Args{}...));
>       |                           ~~~^~~~~~~~~~~
> src.cpp:4:30: note: candidate: 'void (*)()' (conversion)
> src.cpp:4:30: note:   candidate expects 1 argument, 2 provided
> src.cpp:25:9: note: candidate: 'use()::<lambda()>'
>    25 |     ovr([]{});
>       |         ^
> src.cpp:25:9: note:   candidate expects 0 arguments, 1 provided
> 
> ----------
> IMHO, the problem is not caused by concepts, but caused by corrupted
> overloading selection mechanic. Please, take a look at the collision between
> argument 'int{}' and first template parameter specified as 'F'. Next, no
> SFINAE expected in this case due to correct overloading, but overloading
> failed and SFINAE happens. Thanks.

Although the collision between 'int{}' and the template argument for 'F' does
mean that the first overload ultimately isn't viable, the compiler as per
CWG2369 first needs to check the overload's constraints (using the
provided/deduced template arguments) _before_ checking whether 'int' is
convertible to 'F', and it's during this constraint checking we hit the hard
error.  So as far as I can tell, the compiler is behaving as specified.  See
also PR99599 and its related PRs.

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

* [Bug c++/100396] [11.1 regression] The template function overload is not selected correctly
  2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
                   ` (2 preceding siblings ...)
  2021-05-03 17:59 ` ppalka at gcc dot gnu.org
@ 2021-05-04 10:08 ` vopl at bk dot ru
  2021-05-04 14:29 ` [Bug c++/100396] [11 " ppalka at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: vopl at bk dot ru @ 2021-05-04 10:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from vopl at bk dot ru ---
I understand what you're saying. But, apparently, there is still corruption in
the calculus of the substituted Args, please look at this code (thanks to      
 johny5):

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args> struct Checker
{
    using Some = decltype(F{}(Args{}...));
};

template<class F, class... Args> concept valid =
    requires { typename Checker<F, Args...>::Some; };

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
template<class F, class... Args> auto ovr(F f, Args... args)
requires valid<F, Args...>
{
    return ovr<F, Args...>(int{}, f, args...);
}

template<class F, class... Args> auto ovr(int, F f, Args... args)
{
    return f(args...);
}

/////////0/////////1/////////2/////////3/////////4/////////5/////////6/////////7
void use()
{
    ovr([](double){}, 2.);
}
-----------------------

<source>: In instantiation of 'struct Checker<use()::<lambda(double)>, double,
double>':
<source>:8:25:   required by substitution of 'template<class F, class ... Args>
auto ovr(F, Args ...) requires  valid<F, Args ...> [with F =
use()::<lambda(double)>; Args = {double, double}]'
<source>:14:27:   required from 'auto ovr(F, Args ...) requires  valid<F, Args
...> [with F = use()::<lambda(double)>; Args = {double}]'
<source>:25:8:   required from here
<source>:4:30: error: no match for call to '(use()::<lambda(double)>) (double,
double)'
    4 |     using Some = decltype(F{}(Args{}...));
      |                           ~~~^~~~~~~~~~~
<source>:4:30: note: candidate: 'void (*)(double)' (conversion)
<source>:4:30: note:   candidate expects 2 arguments, 3 provided
<source>:25:9: note: candidate: 'use()::<lambda(double)>'
   25 |     ovr([](double){}, 2.);
      |         ^
<source>:25:9: note:   candidate expects 1 argument, 2 provided


Take attention at 'Args' - it was {double} initially, but becomes {double,
double} for second ovr call

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

* [Bug c++/100396] [11 regression] The template function overload is not selected correctly
  2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
                   ` (3 preceding siblings ...)
  2021-05-04 10:08 ` vopl at bk dot ru
@ 2021-05-04 14:29 ` ppalka at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-05-04 14:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to vopl from comment #4)
> Take attention at 'Args' - it was {double} initially, but becomes {double,
> double} for second ovr call.

That seems correct to me.  The second ovr call has three function arguments,
and two explicit template arguments.  The first double in Args={double,double}
comes from the second explicit template argument.  The second double is deduced
from the third function argument.

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

end of thread, other threads:[~2021-05-04 14:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-03 14:41 [Bug c++/100396] New: [11.1 regression] The template function overload is not selected correctly vopl at bk dot ru
2021-05-03 15:14 ` [Bug c++/100396] " ppalka at gcc dot gnu.org
2021-05-03 16:07 ` vopl at bk dot ru
2021-05-03 17:59 ` ppalka at gcc dot gnu.org
2021-05-04 10:08 ` vopl at bk dot ru
2021-05-04 14:29 ` [Bug c++/100396] [11 " ppalka 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).