Bug 30917 - [4.1 Regression] ICE with friend in local class (to a function)
Summary: [4.1 Regression] ICE with friend in local class (to a function)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P1 normal
Target Milestone: 4.1.3
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code, monitored
Depends on:
Blocks:
 
Reported: 2007-02-21 18:24 UTC by Andrew Pinski
Modified: 2007-08-30 16:33 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.1 4.0.4 4.3.0
Known to fail: 4.1.2
Last reconfirmed: 2007-02-22 09:35:53


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2007-02-21 18:24:14 UTC
Testcase:
 unsigned count() {
  class QGListIterator {
 friend class QGList;
  QGListIterator( const QGList & );
};
}
--------
I think this is valid code.
Comment 1 Andrew Pinski 2007-02-21 18:24:43 UTC
The ICE:
t2.cc: In function 'unsigned int count()':
t2.cc:4: internal compiler error: in lookup_name_real, at cp/name-lookup.c:4014
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 2 Andrew Pinski 2007-02-22 06:52:52 UTC
I take back this being valid code, to make it valid add "class QGList;" before the function.  You still end up with an ICE so this is still a bug.
This worked with "4.1.0 20051026" so it is at least a 4.3.0 regression.
Comment 3 Richard Biener 2007-02-22 09:35:53 UTC
This is a regression on the 4.1 branch.  For reference, the valid testcase is

class QGList;
unsigned count()
{
  class QGListIterator {
    friend class QGList;
    QGListIterator( const QGList & );
  };
}
Comment 4 patchapp@dberlin.org 2007-03-08 06:55:13 UTC
Subject: Bug number PR c++/30917

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-03/msg00463.html
Comment 5 Mark Mitchell 2007-07-08 22:40:56 UTC
Simon --

It took me a while to decide that this patch was OK -- but it is. :-)  I will suggest a few changes to make it more obviously correct.

First, minor coding style nit: drop the else after the return that you've introduced.

Second, we need some chapter-and-verse in here.  Please avoid references like "11.4.8" as the numbers tend to move around as the standard changes.  (For example, I don't think 11.4.8 is the right reference in either the published standard or the current WP.) 

For the comment on the gcc_asert, I would say:

/* A non namespace-scope binding can only be hidden if we
   are in a local class, due to friend declarations.  In particular,
   consider:

     void f() { 
      struct A { 
        friend struct B; 
        void g() { B* b; } // error: B is hidden
      } 
      struct B {};
     }

   The standard says that "B" is a local class in "f" (but not nested within
   "A") -- but that name lookup for "B" does not find this declaration until
   it is declared directly with "f".

   In particular:

   [class.friend]

   If a friend declaration appears in a local class and the name specified
   is an unqualified name, a prior declaration is looked up without considering 
   scopes that are outside the innermost enclosing non-class scope....
   For a friend class declaration, if there is no prior declaration, the class
   that is specified belongs to the innermost enclosing non-class scope, but 
   if it is subsequently referenced, its name is not found by name lookup until
   a matching declaration is provided in the innermost enclosing nonclass 
   scope.
*/

OK with that change.

Thanks,

-- Mark
Comment 6 Simon Martin 2007-07-28 10:48:40 UTC
Subject: Bug 30917

Author: simartin
Date: Sat Jul 28 10:48:30 2007
New Revision: 127016

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=127016
Log:
gcc/cp/

2007-07-28  Simon Martin  <simartin@users.sourceforge.net>
	    Mark Mitchell  <mark@codesourcery.com>

	PR c++/30917
	* name-lookup.c (lookup_name_real): Non namespace-scope bindings can be
	hidden due to friend declarations in local classes.

gcc/testsuite/

2007-07-28  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/30917
	* g++.dg/lookup/friend11.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/lookup/friend11.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/name-lookup.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Simon Martin 2007-07-28 20:12:54 UTC
Subject: Bug 30917

Author: simartin
Date: Sat Jul 28 20:12:42 2007
New Revision: 127023

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=127023
Log:
gcc/cp/

2007-07-28  Simon Martin  <simartin@users.sourceforge.net>
            Mark Mitchell  <mark@codesourcery.com>

        PR c++/30917
        * name-lookup.c (lookup_name_real): Non namespace-scope bindings can be
        hidden due to friend declarations in local classes.

gcc/testsuite/

2007-07-28  Simon Martin  <simartin@users.sourceforge.net>

        PR c++/30917
        * g++.dg/lookup/friend11.C: New test.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/lookup/friend11.C
Modified:
    branches/gcc-4_2-branch/gcc/cp/ChangeLog
    branches/gcc-4_2-branch/gcc/cp/name-lookup.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 8 Simon Martin 2007-07-28 20:14:21 UTC
Fixed in 4.2 and 4.3.
Comment 9 Jason Merrill 2007-08-30 16:33:15 UTC
Fixed by Simon Martin.