From 3fb5afbb70da8cd74e697b1b2ed329b851f9ef20 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 13 Mar 2014 00:28:45 +0000 Subject: [PATCH] PR18275: If a member function of a class template is declared with a const-qualified parameter type and the defined with a non-const-qualified parameter type, the parameter is not const inside its body. Ensure that the type we use when instantiating the body is the right one. Patch by suyog sarda! This is still rather unsatisfactory; it seems like it might be better to instantiate at least the function parameters, and maybe the complete function declaration, when we instantiate the definition for such a member function (instead of reusing the declaration from inside the instantiated class definition). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203741 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaTemplateInstantiateDecl.cpp | 11 +++++++++ test/SemaTemplate/dependent-type-identity.cpp | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 0219b6fc76..4f78ae7636 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2991,6 +2991,14 @@ static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, // Simple case: not a parameter pack. assert(FParamIdx < Function->getNumParams()); ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); + // If the parameter's type is not dependent, update it to match the type + // in the pattern. They can differ in top-level cv-qualifiers, and we want + // the pattern's type here. If the type is dependent, they can't differ, + // per core issue 1668. + // FIXME: Updating the type to work around this is at best fragile. + if (!PatternDecl->getType()->isDependentType()) + FunctionParam->setType(PatternParam->getType()); + FunctionParam->setDeclName(PatternParam->getDeclName()); Scope.InstantiatedLocal(PatternParam, FunctionParam); ++FParamIdx; @@ -3005,6 +3013,9 @@ static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, "should only be called when all template arguments are known"); for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); + if (!PatternDecl->getType()->isDependentType()) + FunctionParam->setType(PatternParam->getType()); + FunctionParam->setDeclName(PatternParam->getDeclName()); Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); ++FParamIdx; diff --git a/test/SemaTemplate/dependent-type-identity.cpp b/test/SemaTemplate/dependent-type-identity.cpp index 731013c863..a796834ce0 100644 --- a/test/SemaTemplate/dependent-type-identity.cpp +++ b/test/SemaTemplate/dependent-type-identity.cpp @@ -98,3 +98,27 @@ namespace PR7460 { template T TemplateClass2::member[TemplateClass2::SIZE]; } + +namespace PR18275 { + template struct A { + void f(const int); + void g(int); + void h(const T); + void i(T); + }; + + template + void A::f(int x) { x = 0; } + + template + void A::g(const int x) { x = 0; } // expected-error {{not assignable}} + + template + void A::h(T) {} // FIXME: Should reject this. Type is different from prior decl if T is an array type. + + template + void A::i(const T) {} // FIXME: Should reject this. Type is different from prior decl if T is an array type. + + template struct A; + template struct A; +} -- 2.40.0