Bug 80227 - [6/7 Regression] SFINAE ambiguity with a pointer to array argument
Summary: [6/7 Regression] SFINAE ambiguity with a pointer to array argument
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P2 normal
Target Milestone: 7.4
Assignee: Jason Merrill
URL:
Keywords: accepts-invalid, rejects-valid
: 68280 80169 82893 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-03-27 21:30 UTC by Martin Sebor
Modified: 2018-05-10 18:53 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.5.4
Known to fail: 4.6.3, 4.7.4, 4.8.4, 4.9.4, 5.3.0, 6.3.0, 7.0
Last reconfirmed: 2017-08-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-03-27 21:30:48 UTC
Prior to version 4.6, GCC accepted the well-formed program below.  Since r166453 GCC rejects it because it fails to eliminate the second overload of the foo template from the overload list.  The second overload isn't viable because substituting int for T results in the second argument to the function having an invalid type (pointer to an array of excessive size).

$ cat y.C && gcc -S -Wall -Wextra -Wpedantic y.C
template <class T>
int foo (T);

template <class T, class U = T [sizeof (T) - 5]>
int foo (T, U* = 0);

int i = foo (123);
y.C:7:17: error: call of overloaded ‘foo(int)’ is ambiguous
 int i = foo (123);
                 ^
y.C:2:5: note: candidate: int foo(T) [with T = int]
 int foo (T);
     ^~~
y.C:5:5: note: candidate: int foo(T, U*) [with T = int; U = int [-1]]
 int foo (T, U* = 0);
     ^~~


Conversely, while prior to r166453 GCC would reject the following invalid program

template <class T>
int foo (T, int (*)[sizeof (T) - 5] = 0);

int i = foo (123);

with

t.C:4:17: error: no matching function for call to ‘foo(int)’
t.C:2:40: note: candidate is: template<class T> int foo(T, int (*)[(sizeof (T) - 5)])

GCC 4.6 and later accept it.
Comment 1 Eric Gallager 2017-08-17 16:20:03 UTC
Redoing lost comments:

https://gcc.gnu.org/ml/gcc-bugs/2017-08/msg01610.html

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2017-08-14
                 CC|                            |egallager at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
Confirmed that gcc rejects the former and accepts the latter, although the
former should probably still get a warning anyways even if it's not supposed to
error, just for being confusing (in my
biased-against-c++-things-like-overloading opinion)

https://gcc.gnu.org/ml/gcc-bugs/2017-08/msg01626.html

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Idiomatic C++ code should not warn.

Clang and ICC accept the former and reject the latter. VC++ rejects both, which
is odd.
Comment 2 Jakub Jelinek 2017-10-10 13:28:26 UTC
GCC 5 branch is being closed
Comment 3 Jason Merrill 2018-03-16 18:56:35 UTC
Author: jason
Date: Fri Mar 16 18:56:01 2018
New Revision: 258604

URL: https://gcc.gnu.org/viewcvs?rev=258604&root=gcc&view=rev
Log:
	PR c++/80227 - SFINAE and negative array size.

	* decl.c (compute_array_index_type): Use
	build_converted_constant_expr and valid_constant_size_p.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/fntmpdefarg8.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/g++.dg/gomp/pr47963.C
    trunk/gcc/testsuite/g++.dg/init/new37.C
    trunk/gcc/testsuite/g++.dg/init/new44.C
    trunk/gcc/testsuite/g++.dg/ubsan/pr81530.C
Comment 4 Jason Merrill 2018-03-23 14:40:24 UTC
Author: jason
Date: Fri Mar 23 14:39:47 2018
New Revision: 258811

URL: https://gcc.gnu.org/viewcvs?rev=258811&root=gcc&view=rev
Log:
	PR c++/80227 - SFINAE and negative array size.

	* decl.c (compute_array_index_type): Convert to signed for negative
	check.

Added:
    branches/gcc-7-branch/gcc/testsuite/g++.dg/cpp0x/fntmpdefarg8.C
Modified:
    branches/gcc-7-branch/gcc/cp/ChangeLog
    branches/gcc-7-branch/gcc/cp/decl.c
    branches/gcc-7-branch/gcc/testsuite/g++.dg/init/new44.C
Comment 5 Jason Merrill 2018-03-23 14:52:07 UTC
Fixed for 7.4/8.
Comment 6 Jason Merrill 2018-05-10 18:48:02 UTC
*** Bug 82893 has been marked as a duplicate of this bug. ***
Comment 7 Jason Merrill 2018-05-10 18:49:22 UTC
*** Bug 68280 has been marked as a duplicate of this bug. ***
Comment 8 Jason Merrill 2018-05-10 18:53:46 UTC
*** Bug 80169 has been marked as a duplicate of this bug. ***