@@ -3249,20 +3249,15 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD,
32493249 // Redeclarations or specializations of a function or function template
32503250 // with a declared return type that uses a placeholder type shall also
32513251 // use that placeholder, not a deduced type.
3252- QualType OldDeclaredReturnType =
3253- (Old->getTypeSourceInfo()
3254- ? Old->getTypeSourceInfo()->getType()->castAs<FunctionType>()
3255- : OldType)->getReturnType();
3256- QualType NewDeclaredReturnType =
3257- (New->getTypeSourceInfo()
3258- ? New->getTypeSourceInfo()->getType()->castAs<FunctionType>()
3259- : NewType)->getReturnType();
3252+ QualType OldDeclaredReturnType = Old->getDeclaredReturnType();
3253+ QualType NewDeclaredReturnType = New->getDeclaredReturnType();
32603254 if (!Context.hasSameType(OldDeclaredReturnType, NewDeclaredReturnType) &&
3261- !((NewQType->isDependentType() || OldQType->isDependentType()) &&
3262- New->isLocalExternDecl() )) {
3255+ canFullyTypeCheckRedeclaration(New, Old, NewDeclaredReturnType,
3256+ OldDeclaredReturnType )) {
32633257 QualType ResQT;
32643258 if (NewDeclaredReturnType->isObjCObjectPointerType() &&
32653259 OldDeclaredReturnType->isObjCObjectPointerType())
3260+ // FIXME: This does the wrong thing for a deduced return type.
32663261 ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType);
32673262 if (ResQT.isNull()) {
32683263 if (New->isCXXClassMember() && New->isOutOfLine())
@@ -3427,13 +3422,11 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD,
34273422 if (OldQTypeForComparison == NewQType)
34283423 return MergeCompatibleFunctionDecls(New, Old, S, MergeTypeWithOld);
34293424
3430- if ((NewQType->isDependentType() || OldQType->isDependentType()) &&
3431- New->isLocalExternDecl()) {
3432- // It's OK if we couldn't merge types for a local function declaraton
3433- // if either the old or new type is dependent. We'll merge the types
3434- // when we instantiate the function.
3425+ // If the types are imprecise (due to dependent constructs in friends or
3426+ // local extern declarations), it's OK if they differ. We'll check again
3427+ // during instantiation.
3428+ if (!canFullyTypeCheckRedeclaration(New, Old, NewQType, OldQType))
34353429 return false;
3436- }
34373430
34383431 // Fall through for conflicting redeclarations and redefinitions.
34393432 }
@@ -9336,6 +9329,39 @@ Attr *Sema::getImplicitCodeSegOrSectionAttrForFunction(const FunctionDecl *FD,
93369329 }
93379330 return nullptr;
93389331}
9332+
9333+ /// Determines if we can perform a correct type check for \p D as a
9334+ /// redeclaration of \p PrevDecl. If not, we can generally still perform a
9335+ /// best-effort check.
9336+ ///
9337+ /// \param NewD The new declaration.
9338+ /// \param OldD The old declaration.
9339+ /// \param NewT The portion of the type of the new declaration to check.
9340+ /// \param OldT The portion of the type of the old declaration to check.
9341+ bool Sema::canFullyTypeCheckRedeclaration(ValueDecl *NewD, ValueDecl *OldD,
9342+ QualType NewT, QualType OldT) {
9343+ if (!NewD->getLexicalDeclContext()->isDependentContext())
9344+ return true;
9345+
9346+ // For dependently-typed local extern declarations and friends, we can't
9347+ // perform a correct type check in general until instantiation:
9348+ //
9349+ // int f();
9350+ // template<typename T> void g() { T f(); }
9351+ //
9352+ // (valid if g() is only instantiated with T = int).
9353+ if (NewT->isDependentType() &&
9354+ (NewD->isLocalExternDecl() || NewD->getFriendObjectKind()))
9355+ return false;
9356+
9357+ // Similarly, if the previous declaration was a dependent local extern
9358+ // declaration, we don't really know its type yet.
9359+ if (OldT->isDependentType() && OldD->isLocalExternDecl())
9360+ return false;
9361+
9362+ return true;
9363+ }
9364+
93399365/// Checks if the new declaration declared in dependent context must be
93409366/// put in the same redeclaration chain as the specified declaration.
93419367///
@@ -9346,20 +9372,30 @@ Attr *Sema::getImplicitCodeSegOrSectionAttrForFunction(const FunctionDecl *FD,
93469372/// belongs to.
93479373///
93489374bool Sema::shouldLinkDependentDeclWithPrevious(Decl *D, Decl *PrevDecl) {
9349- // Any declarations should be put into redeclaration chains except for
9350- // friend declaration in a dependent context that names a function in
9351- // namespace scope.
9375+ if (!D->getLexicalDeclContext()->isDependentContext())
9376+ return true;
9377+
9378+ // Don't chain dependent friend function definitions until instantiation, to
9379+ // permit cases like
93529380 //
9353- // This allows to compile code like:
9381+ // void func();
9382+ // template<typename T> class C1 { friend void func() {} };
9383+ // template<typename T> class C2 { friend void func() {} };
93549384 //
9355- // void func();
9356- // template<typename T> class C1 { friend void func() { } };
9357- // template<typename T> class C2 { friend void func() { } };
9385+ // ... which is valid if only one of C1 and C2 is ever instantiated.
93589386 //
9359- // This code snippet is a valid code unless both templates are instantiated.
9360- return !(D->getLexicalDeclContext()->isDependentContext() &&
9361- D->getDeclContext()->isFileContext() &&
9362- D->getFriendObjectKind() != Decl::FOK_None);
9387+ // FIXME: This need only apply to function definitions. For now, we proxy
9388+ // this by checking for a file-scope function. We do not want this to apply
9389+ // to friend declarations nominating member functions, because that gets in
9390+ // the way of access checks.
9391+ if (D->getFriendObjectKind() && D->getDeclContext()->isFileContext())
9392+ return false;
9393+
9394+ auto *VD = dyn_cast<ValueDecl>(D);
9395+ auto *PrevVD = dyn_cast<ValueDecl>(PrevDecl);
9396+ return !VD || !PrevVD ||
9397+ canFullyTypeCheckRedeclaration(VD, PrevVD, VD->getType(),
9398+ PrevVD->getType());
93639399}
93649400
93659401namespace MultiVersioning {
0 commit comments