This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fix PR c++/14622: Type mismatch in explicit template instantiation not detected


Hi all.

No error is reported today for the following invalid snippet:

=== cut here ===
template<class T>
class A
{
  static T a;
};

template<class T>
T A<T>::a;

struct B {};

template B A<int>::a; // Type mismatch
=== cut here ===

The problem is that we don't check that the type of A::a in the explicit 
instantiation is the correct one.

The attached patch fixes this by doing this check in do_decl_instantiation and 
reporting an error if it fails.

I have had to modify the testsuite because there is a testcase for which this 
error is now triggered.

This has been successfully regtested on i686-pc-linux-gnu. Is it OK?

Thanks in advance,
Simon

:ADDPATCH c++:
2007-02-04  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/14622
	* pt.c (do_decl_instantiation): Detect type mismatches in explicit
	instantiations for variables.
Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c	(revision 121576)
+++ gcc/cp/pt.c	(working copy)
@@ -11727,6 +11727,12 @@ do_decl_instantiation (tree decl, tree s
 	  error ("no matching template for %qD found", decl);
 	  return;
 	}
+      if (TREE_TYPE (result) != TREE_TYPE (decl))
+	{
+	  error ("explicit instantiation %q#D does not match declaration",
+		 decl);
+	  return;
+	}
     }
   else if (TREE_CODE (decl) != FUNCTION_DECL)
     {
2007-02-04  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/14622
	* g++.dg/template/instantiate9.C: New test.
	* g++.old-deja/g++.pt/instantiate12.C: Fixed type mismatches in explicit
	instantiations.
Index: gcc/testsuite/g++.old-deja/g++.pt/instantiate12.C
===================================================================
--- gcc/testsuite/g++.old-deja/g++.pt/instantiate12.C	(revision 121576)
+++ gcc/testsuite/g++.old-deja/g++.pt/instantiate12.C	(working copy)
@@ -56,6 +56,6 @@ int main ()
 // const-ness should allow the compiler to elide references to the
 // actual variables.
 template const bool X<int>::cflag;
-template const bool X<int>::flag;
+template bool X<int>::flag;
 template const bool X<float>::cflag;
-template const bool X<float>::flag;
+template bool X<float>::flag;
/* PR c++/14622. The invalid explicit instantiation was not reported. */
/* { dg-do "compile" } */
template<class T>
class A
{
  static T a;
};

template<class T>
T A<T>::a;

struct B {};

template B A<int>::a; /* { dg-error "does not match declaration" } */
template float A<float>::a;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]