diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index f68f278500defb87ac5836d657ef6359841bcfaa..3ff6c91434c1b0174415071db9c637af3c2df25f 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,13 @@
+2006-01-24  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
+
+	PR c++/25552
+	* parser.c (cp_parser_unqualified_id): Check that destructor name
+	and scope match.
+	* call.c (check_dtor_name): Do not expect a BIT_NOT_EXPR.
+	Adjust comment.  Return early if possible.
+	Use same_type_p to compare types.
+	* typeck.c (lookup_destructor): Adjust call to check_dtor_name.
+
 2006-01-24  Mark Mitchell  <mark@codesourcery.com>
 
 	* semantics.c: Remove outdated comment.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 0c23caa20a3ea413ac62ba9f394d7347a0562c27..79001763016f6a8f65c9602364bb177a04f4e7a5 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -198,15 +198,12 @@ typedef void (*diagnostic_fn_t) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
 static tree build_temp (tree, tree, int, diagnostic_fn_t *);
 static void check_constructor_callable (tree, tree);
 
-/* Returns nonzero iff the destructor name specified in NAME
-   (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
-   forms...  */
+/* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
+   NAME can take many forms...  */
 
 bool
 check_dtor_name (tree basetype, tree name)
 {
-  name = TREE_OPERAND (name, 0);
-
   /* Just accept something we've already complained about.  */
   if (name == error_mark_node)
     return true;
@@ -220,7 +217,7 @@ check_dtor_name (tree basetype, tree name)
       if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
 	  || (TREE_CODE (basetype) == ENUMERAL_TYPE
 	      && name == TYPE_IDENTIFIER (basetype)))
-	name = basetype;
+	return true;
       else
 	name = get_type_value (name);
     }
@@ -237,9 +234,9 @@ check_dtor_name (tree basetype, tree name)
       return false;
     }
 
-  if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
-    return true;
-  return false;
+  if (!name)
+    return false;
+  return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
 }
 
 /* We want the address of a function or method.  We avoid creating a
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 4c09b2fde9bf6e1a71f30595ae1b4823964279e2..3ba92852a77ba24233f01ee7b5a16ba87292eca0 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -3434,6 +3434,15 @@ cp_parser_unqualified_id (cp_parser* parser,
 	else if (type_decl == error_mark_node)
 	  return error_mark_node;
 
+	/* Check that destructor name and scope match.  */
+	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
+	  {
+	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
+	      error ("declaration of %<~%T%> as member of %qT",
+		     type_decl, scope);
+	    return error_mark_node;
+	  }
+
 	/* [class.dtor]
 
 	   A typedef-name that names a class shall not be used as the
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 49deccc0ad3e1cd1e98b62be7a91906750e774e4..5eb4b644426e96baa1b54c30377a025c5bdf7adf 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1828,7 +1828,7 @@ lookup_destructor (tree object, tree scope, tree dtor_name)
   tree dtor_type = TREE_OPERAND (dtor_name, 0);
   tree expr;
 
-  if (scope && !check_dtor_name (scope, dtor_name))
+  if (scope && !check_dtor_name (scope, dtor_type))
     {
       error ("qualified type %qT does not match destructor name ~%qT",
 	     scope, dtor_type);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index aa0ed47e6352a24b1f35d3ed0c2f253db3bf79fc..3434543725a4c42adedf2da17393ff44f90da84f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-01-24  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
+
+	PR c++/25552
+	* g++.dg/parse/dtor8.C: New test.
+
 2006-01-24  Hans-Peter Nilsson  <hp@axis.com>
 
 	PR testsuite/25891
diff --git a/gcc/testsuite/g++.dg/parse/dtor8.C b/gcc/testsuite/g++.dg/parse/dtor8.C
new file mode 100644
index 0000000000000000000000000000000000000000..2fe0a974bb97e3d48c8b3f7a159cc5b633704013
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/dtor8.C
@@ -0,0 +1,8 @@
+// { dg-do compile }
+
+struct A {};
+
+struct B
+{
+  friend A::~B();  // { dg-error "as member of" }
+};