From: Simon Martin <simartin@users.sourceforge.net>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] Fix PR c++/30917: ICE with friend in local class (to a function)
Date: Thu, 08 Mar 2007 06:58:00 -0000 [thread overview]
Message-ID: <200703080754.45342.simartin@users.sourceforge.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 1486 bytes --]
Hi all.
The following testcase currently triggers an ICE in 4.1, 4.2 and 4.3:
=== cut here ===
class QGList; // L1
unsigned count() {
class QGListIterator {
friend class QGList; // L2
QGListIterator( const QGList & );
};
}
=== cut here ===
If I read the standard properly, the friend declaration in L2 introduces a
local class QGList, which must not be taken into account during name lookup,
it is therefore marked as "hidden" (front-end term).
When QGList is looked up in QGListIterator's constructor, the local QGList
is found, and an assertion fails in 'name_lookup_real', hence the ICE. This
assertion ensures that only namespace-scope bindings are hidden; this
constraint is not verified in our case: we have a hidden and non
namespace-scope binding (the local QGList).
The attached patch fixes this by ensuring that if the binding is hidden, we
are processing a local class. Moreover, the lookup will fail because there is
no suitable declaration for QGList: the local one coming from the friend
declaration is not a candidate, nor is the one at L1 (only a declaration in
the innermost enclosing nonclass scope - that is 'count' - is to be found). In
other words, this testcase is invalid, and the compiler will output an error
(however, please note that icc accepts this snippet, so my interpretation
might be wrong...).
I have successfully regtested this on i686-pc-linux-gnu. Is it OK for
mainline? For 4.2?
Best regards,
Simon
:ADDPATCH c++:
[-- Attachment #2: CL_30917 --]
[-- Type: text/plain, Size: 200 bytes --]
2007-03-07 Simon Martin <simartin@users.sourceforge.net>
PR c++/30917
* name-lookup.c (lookup_name_real): Non namespace-scope bindings can be
hidden due to friend declarations in local classes.
[-- Attachment #3: pr30917.patch --]
[-- Type: text/plain, Size: 1037 bytes --]
Index: gcc/cp/name-lookup.c
===================================================================
--- gcc/cp/name-lookup.c (revision 122641)
+++ gcc/cp/name-lookup.c (working copy)
@@ -4010,10 +4010,24 @@ lookup_name_real (tree name, int prefer_
if (binding)
{
- /* Only namespace-scope bindings can be hidden. */
- gcc_assert (!hidden_name_p (binding));
- val = binding;
- break;
+ if (hidden_name_p (binding))
+ {
+ /* A non namespace-scope binding can only be hidden if we
+ are in a local class, due to friend declarations. */
+ gcc_assert (current_class_type &&
+ LOCAL_CLASS_P (current_class_type));
+
+ /* This binding comes from a friend declaration in the local
+ class. The standard (11.4.8) states that the lookup can
+ only succeed if there is a non-hidden declaration in the
+ current scope, which is not the case here. */
+ POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
+ }
+ else
+ {
+ val = binding;
+ break;
+ }
}
}
[-- Attachment #4: CL_30917_testsuite --]
[-- Type: text/plain, Size: 113 bytes --]
2007-03-07 Simon Martin <simartin@users.sourceforge.net>
PR c++/30917
* g++.dg/lookup/friend11.C: New test.
[-- Attachment #5: friend11.C --]
[-- Type: text/plain, Size: 480 bytes --]
/* PR c++/30917 */
/* This used to ICE */
/* { dg-do "compile" } */
// This is invalid: QGList must only be looked up in count.
class QGList;
unsigned count() {
class QGListIterator {
friend class QGList;
QGListIterator( const QGList & ); /* { dg-error "expected|with no type" } */
};
return 0;
}
// This is valid.
unsigned count2() {
class QGList2;
class QGListIterator2 {
friend class QGList2;
QGListIterator2( const QGList2 & );
};
return 0;
}
next reply other threads:[~2007-03-08 6:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-08 6:58 Simon Martin [this message]
2007-07-28 14:54 ` Simon Martin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200703080754.45342.simartin@users.sourceforge.net \
--to=simartin@users.sourceforge.net \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).