diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 6fec22f833de438ffa0291013ea0b7f8dbc17671..cb78daa5d5537d0eec6e8a2ff75a7851c5ded468 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2008-01-15  Douglas Gregor  <doug.gregor@gmail.com>
+
+	PR c++/34399
+	* friend.c (do_friend): Don't query TYPE_BEING_DEFINED unless we
+	know we have a class type.
+
 2008-01-15  Douglas Gregor  <doug.gregor@gmail.com>
 
 	PR c++/34751
diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c
index 95f98c45397304e1dab727ca0bf41a68da1f6923..094501b109a3762ea5bb61beaf68a6f47663fd92 100644
--- a/gcc/cp/friend.c
+++ b/gcc/cp/friend.c
@@ -468,7 +468,7 @@ do_friend (tree ctype, tree declarator, tree decl,
 	 the process of being defined.  */
       if (class_template_depth
 	  || COMPLETE_TYPE_P (ctype)
-	  || TYPE_BEING_DEFINED (ctype))
+	  || (CLASS_TYPE_P (ctype) && TYPE_BEING_DEFINED (ctype)))
 	{
 	  if (DECL_TEMPLATE_INFO (decl))
 	    /* DECL is a template specialization.  No need to
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c6184f91da7e058fe815394c8e6faa4ed87ff15c..c9d0e6a6923dc8f6f37a02f672e580dff3428b02 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2008-01-15  Douglas Gregor  <doug.gregor@gmail.com>
+
+	PR c++/34399
+	* g++.dg/cpp0x/vt-34399.C: New.
+	* g++.dg/template/friend50.C: New.
+
 2008-01-15  Douglas Gregor  <doug.gregor@gmail.com>
 
 	PR c++/34751
diff --git a/gcc/testsuite/g++.dg/cpp0x/vt-34399.C b/gcc/testsuite/g++.dg/cpp0x/vt-34399.C
new file mode 100644
index 0000000000000000000000000000000000000000..542fae586ca8a3336952b8b493d09a0a7524e94c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/vt-34399.C
@@ -0,0 +1,10 @@
+// { dg-options "-std=c++0x" }
+template<int...> struct A
+{
+  void foo();
+};
+
+struct B
+{
+  template<int N> friend void A<N>::A::foo(); // { dg-error "declared as friend" }
+};
diff --git a/gcc/testsuite/g++.dg/template/friend50.C b/gcc/testsuite/g++.dg/template/friend50.C
new file mode 100644
index 0000000000000000000000000000000000000000..93c429ab161c0812c7a807ca3cba7d9a40dadaf8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/friend50.C
@@ -0,0 +1,9 @@
+// PR c++/34399
+template<int> struct X
+{
+  void foo();
+};
+
+struct Y {
+  template<long N> friend void X<N>::X::foo(); // { dg-error "declared as friend" }
+};