* friend.c (do_friend): Check for prior declaration of a friend
function of a local class.
* name-lookup.c (lookup_name_innermost_nonclass_level):
No longer static.
* name-lookup.h: Declare it.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@131740
138bc75d-0d04-0410-961f-
82ee72b054a4
+2008-01-22 Jason Merrill <jason@redhat.com>
+
+ PR c++/34912
+ * friend.c (do_friend): Check for prior declaration of a friend
+ function of a local class.
+ * name-lookup.c (lookup_name_innermost_nonclass_level):
+ No longer static.
+ * name-lookup.h: Declare it.
+
2008-01-22 Tom Tromey <tromey@redhat.com>
PR c++/34829:
is instantiated. */
decl = push_template_decl_real (decl, /*is_friend=*/true);
else if (current_function_decl)
- /* This must be a local class, so pushdecl will be ok, and
- insert an unqualified friend into the local scope
- (rather than the containing namespace scope, which the
- next choice will do). */
- decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
+ {
+ /* This must be a local class. 11.5p11:
+
+ If a friend declaration appears in a local class (9.8) 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 function declaration, if there is no prior
+ declaration, the program is ill-formed. */
+ tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
+ if (t)
+ decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
+ else
+ {
+ error ("friend declaration %qD in local class without "
+ "prior declaration", decl);
+ return error_mark_node;
+ }
+ }
else
{
/* We can't use pushdecl, as we might be in a template
static cxx_scope *innermost_nonclass_level (void);
static cxx_binding *binding_for_name (cxx_scope *, tree);
-static tree lookup_name_innermost_nonclass_level (tree);
static tree push_overloaded_decl (tree, int, bool);
static bool lookup_using_namespace (tree, struct scope_binding *, tree,
tree, int);
/* Similar to `lookup_name' but look only in the innermost non-class
binding level. */
-static tree
+tree
lookup_name_innermost_nonclass_level (tree name)
{
struct cp_binding_level *b;
extern tree remove_hidden_names (tree);
extern tree lookup_qualified_name (tree, tree, bool, bool);
extern tree lookup_name_nonclass (tree);
+extern tree lookup_name_innermost_nonclass_level (tree);
extern tree lookup_function_nonclass (tree, tree, bool);
extern void push_local_binding (tree, tree, int);
extern bool pushdecl_class_level (tree);
--- /dev/null
+// PR c++/34912
+
+void foo()
+{
+ struct A
+ {
+ friend void bar(); // { dg-error "without prior declaration" }
+ };
+ bar(); // { dg-error "not declared" }
+}
{
class c
{
- friend void g () { } // { dg-error "local class definition" "" }
+ friend void g () { } // { dg-error "local class" "" }
};
}
void f ()
{
struct A {
- friend void g ();
+ friend void g (); // { dg-error "without prior declaration" }
};
}
void h () {