Bug 11858 - Name lookup error ignored when instantiated from expression within sizeof() in template function parameter
Summary: Name lookup error ignored when instantiated from expression within sizeof() i...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: 24666
  Show dependency treegraph
 
Reported: 2003-08-08 22:12 UTC by Eelis
Modified: 2022-12-07 02:10 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 3.3, 4.5.3, 4.8.3, 4.9.3, 5.3.0, 6.3.0, 7.0
Last reconfirmed: 2022-12-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eelis 2003-08-08 22:12:53 UTC
GCC accepts the following invalid code:

template <typename T> struct S { static typename T::x f (); };
template <class T> int f (int [sizeof(T::f())]);
int const i = f<S<int> >(0);

Comeau (4.3.3 beta) does the detect the error, and says:

line 1: error: name followed by "::" must be a class or namespace name
template <typename T> struct S { static typename T::x f (); };
                                                   ^
detected during:
 instantiation of class "S<T> [with T=int]" at line 2
 instantiation of "f" based on template argument <S<int>> at line 3

I'm using GCC version 3.4 20030723.
Comment 1 Andrew Pinski 2003-08-08 22:28:16 UTC
I can confirm this on the mainline (20030808).
Maybe related to bug 11814.
Comment 2 Wolfgang Bangerth 2003-08-09 15:52:40 UTC
I'm not where exactly is the bug here: T::x doesn't exist if T=int, so this is a substitution 
failure. This is not an error, the function is simply not available for name lookup. If this 
were the correct interpretation, then we should get an error that there is no suitable 
function T::f in the sizeof expression. But there's a bug anyway. 
 
By the way, this is rejected: 
--------------------------------------------- 
template <int> struct X {}; 
template <typename T> struct S { static typename T::x f (); }; 
template <class T> int f (X<sizeof(T::f())> *); 
int const i = f<S<int> >(0); 
---------------------------------------------- 
 
So it has something to do with the fact that gcc simply ignores the size expression if 
an argument to a function is an array. 
 
W, 
Comment 3 Andrew Pinski 2005-12-18 20:14:52 UTC
This looks like a case where array decays to a pointer too early problem (PR 24666).
Comment 4 Kurt Stutsman 2012-10-10 19:39:12 UTC
Still seeing this as of version 4.6.1.
Comment 5 Jonathan Wakely 2012-10-10 20:30:13 UTC
and 4.8
Comment 6 Patrick Palka 2016-01-19 00:19:49 UTC
Author: ppalka
Date: Tue Jan 19 00:19:16 2016
New Revision: 232547

URL: https://gcc.gnu.org/viewcvs?rev=232547&root=gcc&view=rev
Log:
Fix the remaining PR c++/24666 blockers (arrays decay to pointers too early)

gcc/cp/ChangeLog:

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* decl.c (grokdeclarator): Don't decay array parameter type to
	a pointer type if it's dependent.
	(grokparms): Invoke strip_top_quals instead of directly invoking
	cp_build_qualified_type.
	* pt.c (decay_dependent_array_parm_type): New static function.
	(type_unification_real): Call decay_dependent_array_parm_type
	to decay a dependent array parameter type to its corresponding
	pointer type before unification.
	(more_specialized_fn): Likewise.
	(get_bindings): Likewise.
	* tree.c (cp_build_qualified_type): Trivial typofix in
	documentation.

gcc/testsuite/ChangeLog:

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* g++.dg/template/pr11858.C: New test.
	* g++.dg/template/pr24663.C: New test.
	* g++.dg/template/unify12.C: New test.
	* g++.dg/template/unify13.C: New test.
	* g++.dg/template/unify14.C: New test.
	* g++.dg/template/unify15.C: New test.
	* g++.dg/template/unify16.C: New test.
	* g++.dg/template/unify17.C: New test.


Added:
    trunk/gcc/testsuite/g++.dg/template/pr11858.C
    trunk/gcc/testsuite/g++.dg/template/pr24663.C
    trunk/gcc/testsuite/g++.dg/template/unify12.C
    trunk/gcc/testsuite/g++.dg/template/unify13.C
    trunk/gcc/testsuite/g++.dg/template/unify14.C
    trunk/gcc/testsuite/g++.dg/template/unify15.C
    trunk/gcc/testsuite/g++.dg/template/unify16.C
    trunk/gcc/testsuite/g++.dg/template/unify17.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Patrick Palka 2016-01-19 16:09:33 UTC
Fixed for GCC 6.
Comment 8 Patrick Palka 2016-01-24 17:45:53 UTC
Author: ppalka
Date: Sun Jan 24 17:45:21 2016
New Revision: 232778

URL: https://gcc.gnu.org/viewcvs?rev=232778&root=gcc&view=rev
Log:
Revert "Fix the remaining PR c++/24666 blockers"

gcc/cp/ChangeLog:

	Revert:
	2016-01-18  Patrick Palka  <ppalka@gcc.gnu.org>

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* decl.c (grokdeclarator): Don't decay array parameter type to
	a pointer type if it's dependent.
	(grokparms): Invoke strip_top_quals instead of directly invoking
	cp_build_qualified_type.
	* pt.c (decay_dependent_array_parm_type): New static function.
	(type_unification_real): Call decay_dependent_array_parm_type
	to decay a dependent array parameter type to its corresponding
	pointer type before unification.
	(more_specialized_fn): Likewise.
	(get_bindings): Likewise.
	* tree.c (cp_build_qualified_type): Trivial typofix in
	documentation.

gcc/testsuite/ChangeLog:

	Revert:
	2016-01-18  Patrick Palka  <ppalka@gcc.gnu.org>

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* g++.dg/template/pr11858.C: New test.
	* g++.dg/template/pr24663.C: New test.
	* g++.dg/template/unify12.C: New test.
	* g++.dg/template/unify13.C: New test.
	* g++.dg/template/unify14.C: New test.
	* g++.dg/template/unify15.C: New test.
	* g++.dg/template/unify16.C: New test.
	* g++.dg/template/unify17.C: New test.


Removed:
    trunk/gcc/testsuite/g++.dg/template/pr11858.C
    trunk/gcc/testsuite/g++.dg/template/pr24663.C
    trunk/gcc/testsuite/g++.dg/template/unify12.C
    trunk/gcc/testsuite/g++.dg/template/unify13.C
    trunk/gcc/testsuite/g++.dg/template/unify14.C
    trunk/gcc/testsuite/g++.dg/template/unify15.C
    trunk/gcc/testsuite/g++.dg/template/unify16.C
    trunk/gcc/testsuite/g++.dg/template/unify17.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Patrick Palka 2016-01-24 17:48:27 UTC
Patch reverted due to numerous issues mentioned in the following thread:

https://gcc.gnu.org/ml/gcc-patches/2016-01/msg01463.html
Comment 10 Martin Sebor 2017-01-11 20:04:58 UTC
The invalid test case is still accepted by current trunk of GCC 7.0.