Bug 21514

Summary: [DR 488] templates and anonymous enum
Product: gcc Reporter: papadopo
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: caolanm, christian.joensson, fang, gcc-bugs, mmarcus, snoonan
Priority: P2    
Version: 4.0.0   
Target Milestone: 4.0.2   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2005-05-11 14:14:28
Bug Depends on:    
Bug Blocks: 21701    
Attachments: test case

Description papadopo 2005-05-11 14:12:12 UTC
Hi,

The attached code emits a mysterious error:

$ g++ -c foo.cc
foo.cc: In function 'int main()':
foo.cc:13: error: '<anonymous enum>' is/uses anonymous type
foo.cc:13: error:   trying to instantiate 'template<class T> void
operator<<(MyClass&, const T&)'
$ 

The compiler apparently fails to see that the anonymous enum can be cast to an
integer.
Comment 1 Andrew Pinski 2005-05-11 14:14:28 UTC
There is some discussion on the list about this.  Right now there is a DR report about this and about is 
this really invalid code or not.
Comment 2 Andrew Pinski 2005-05-11 14:15:22 UTC
Suspending as there is still questions is this really invalid code or not.
Comment 3 papadopo 2005-05-11 14:18:31 UTC
Created attachment 8860 [details]
test case
Comment 4 papadopo 2005-05-11 14:21:19 UTC
Whether the code is valid or not, please improve the error message :-)
Comment 5 Andrew Pinski 2005-05-11 14:25:06 UTC
I don't see any how to improve the error message really since it says what the problem is.
Comment 6 papadopo 2005-05-11 14:38:23 UTC
I think the first line could be made clearer:
'<anonymous enum>' is/uses anonymous type
I'm reading it again and again, and it doesn't make sense to me.

Then the second line refers to an operator<< most probably defined in some
remote header file. This is distracting. I would suggest not printing this
second line at all.
Comment 7 papadopo 2005-05-18 09:33:04 UTC
I'm not sure what a DR is, probably a C++ standard defect report. After asking
on fr.comp.lang.c++ I was referred to Core Issue 278:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#278

Whether the compiler should compile the code or not, the error message really
needs to be improved. My understanding of the situation is that :

1.a) The compiler first attempts to apply the shift operator to integer "i".
However the right-side argument is an anonymous enum. The compiler will not
implictly cast the anonymous enum to a size_t to be used as the right-hand
argument for operator<<.

1.b) Note that an *explicit* cast does work, so I'm not sure how this whole
issue is related to Core Issue 278 and external linkage. Just change from:
    i<<HSize;
to:
    i<<int(HSize);
and the program while compile and link.

2) The compiler then attemps to use operator<<(MyClass& b, const T& t). Instead
of reporting an error on the first argument (type 'MyClass' expected, type 'int'
actually used), it reports an obscure error on the second argument. This is
totally non-intuitive for humans.

Therefore:

* Concerning 1.a) and 1.b) I'm not convinced this is related to Core Issue 278
and external linkage since the explicit cast works. Anyway, if you feel Core
Issue 278 or some paragraph of the standard forbid the implicit cast, I agree
this bug report should be kept open until the comitee resolves the problem.

* In the meanwhile, the error message really needs to be improved, taking into
account point 2).
Comment 8 Gabriel Dos Reis 2005-05-18 14:04:23 UTC
Subject: Re:  templates and anonymous enum

"papadopo at shfj dot cea dot fr" <gcc-bugzilla@gcc.gnu.org> writes:

[...]

| Therefore:
| 
| * Concerning 1.a) and 1.b) I'm not convinced this is related to Core Issue 278
| and external linkage since the explicit cast works. Anyway, if you feel Core
| Issue 278 or some paragraph of the standard forbid the implicit cast, I agree
| this bug report should be kept open until the comitee resolves the problem.

The way it is related is that the CWG has introduced the concent of
linkage for type -- which did not exist in ARM and existed only for
function type in C++98/03 and the unnamed enum no longer has an
external linkage.  As I said on fr.comp.lang.c++, I brought the issue
to the C++ committee and I think the issue should not be closed.

The answer I gave you was much more detailed and accurate that what
you report here :-) -- in particular, the ocmpiler is attempting to do
overload resolution and it is in the phase of constructing the
overload set that the error appears.  But, the whole thing is confused
:-)

-- Gaby
Comment 9 papadopo 2005-05-18 14:56:10 UTC
I guess what I meant is: whatever the reason for this error message and whatever
the eventual decision taken about it, it would be nice to have an error message
that really means something to end-users.

For example, as an end-user, I still can't understand the difference between the
explicit cast and the implicit (non-)cast, why it works in the first case and
not in the latter. The error message shoudl really point at what's wrong in the
source code, not at what's wrong in the C++ standard.
Comment 10 Paolo Carlini 2005-05-18 15:00:52 UTC
> .......... The error message shoudl really point at what's wrong in the
> source code, not at what's wrong in the C++ standard.

But unfortunately if the compiler is conforming, and the latter is wrong,
the former is "correct" as-is and the error message too ;)
Comment 11 Gabriel Dos Reis 2005-05-18 15:07:05 UTC
Subject: Re:  [DR 278] templates and anonymous enum

"papadopo at shfj dot cea dot fr" <gcc-bugzilla@gcc.gnu.org> writes:

| I guess what I meant is: whatever the reason for this error message
| and whatever the eventual decision taken about it, it would be nice
| to have an error message that really means something to end-users.

100% agreed.

-- Gaby
Comment 12 papadopo 2005-05-18 15:13:18 UTC
OK, then what I really meant is that error message doesn't mean anything in
plain English, thus it doesn't mean anything to end-users. So it needs to be
improved. I understand it's not easy - after all no one here is able to explain
what really is behind this bug. I'm not a native speaker, I'm just a rather
skilled end-user; I can nevertheless guarantee that end-users don't understand
this error message. We spent a few hours trying and finding what needs to be
changed in some 3rd party library we're using, because the compiler was pointing
to a totally unrelated template definition of operator<< in our own sources.
Comment 13 Gabriel Dos Reis 2005-05-18 17:08:00 UTC
Subject: Re:  [DR 278] templates and anonymous enum

"pcarlini at suse dot de" <gcc-bugzilla@gcc.gnu.org> writes:

| > .......... The error message shoudl really point at what's wrong in the
| > source code, not at what's wrong in the C++ standard.
| 
| But unfortunately if the compiler is conforming, and the latter is wrong,
| the former is "correct" as-is and the error message too ;)

I think his point is that the diagnostic message is not helpful.  And
I agree with him.

-- Gaby
Comment 14 Andrew Pinski 2005-05-21 18:01:35 UTC
*** Bug 21701 has been marked as a duplicate of this bug. ***
Comment 15 Eric Botcazou 2005-05-31 16:55:33 UTC
Not specific to sparc-sun-solaris2.8.
Comment 16 Andrew Pinski 2005-07-15 19:48:23 UTC
*** Bug 22505 has been marked as a duplicate of this bug. ***
Comment 17 Mark Mitchell 2005-09-15 21:31:33 UTC
Why is this marked as DR 278?  I think it's DR 488.
Comment 18 CVS Commits 2005-09-16 15:42:00 UTC
Subject: Bug 21514

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2005-09-16 15:41:45

Modified files:
	gcc/cp         : ChangeLog pt.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.dg/template: crash19.C local4.C 

Log message:
	PR c++/21514
	* pt.c (check_instantiated_args): Treat uses of anonymous types as
	causing type-deduction failure.
	
	PR c++/21514
	* g++.dg/template/crash19.C: Remove dg-error marker.
	* g++.dg/template/local4.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4895&r2=1.4896
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.1037&r2=1.1038
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.6070&r2=1.6071
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/crash19.C.diff?cvsroot=gcc&r1=1.3&r2=1.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/local4.C.diff?cvsroot=gcc&r1=1.3&r2=1.4

Comment 20 Mark Mitchell 2005-09-16 15:47:31 UTC
Fixed in 4.0.2.
Comment 21 Andrew Pinski 2005-09-16 16:01:50 UTC
*** Bug 21701 has been marked as a duplicate of this bug. ***
Comment 22 Jason Merrill 2009-11-04 20:40:46 UTC
*** Bug 20589 has been marked as a duplicate of this bug. ***
Comment 23 David Fang 2011-11-18 01:05:04 UTC
/me wishes this was backported to *-apple-darwin8 gcc (Xcode 2.5)