diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-11-17 05:17:33 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-11-17 05:17:33 +0000 |
commit | 03c5705a99a96a471b2868898ee9688a6721e02a (patch) | |
tree | 2d8c60e17f7c37c80d3e44db50196188a2a148ff | |
parent | 145e2ea10fb6240265c3fac8137a5f2b01b1a426 (diff) |
Require the object type of a member access expression ("." or "->") to
be complete.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89042 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 4 | ||||
-rw-r--r-- | lib/Sema/SemaExprCXX.cpp | 9 | ||||
-rw-r--r-- | lib/Sema/SemaTemplate.cpp | 12 | ||||
-rw-r--r-- | test/SemaTemplate/class-template-spec.cpp | 2 | ||||
-rw-r--r-- | test/SemaTemplate/member-function-template.cpp | 13 |
5 files changed, 31 insertions, 9 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e1fe4dd4b6..fd544827b1 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -401,7 +401,9 @@ def note_ambig_member_ref_scope : Note< "lookup from the current scope refers here">; def err_qualified_member_nonclass : Error< "qualified member access refers to a member in %0">; - +def err_incomplete_member_access : Error< + "member access into incomplete type %0">; + // C++ class members def err_storageclass_invalid_for_member : Error< "storage class specified for a member declaration">; diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index fdce0e6c2d..dd3fd2c5c1 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -2141,12 +2141,19 @@ Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, return move(Base); } + // The object type must be complete (or dependent). + if (!BaseType->isDependentType() && + RequireCompleteType(OpLoc, BaseType, + PDiag(diag::err_incomplete_member_access))) + return ExprError(); + // C++ [basic.lookup.classref]p2: // If the id-expression in a class member access (5.2.5) is an - // unqualified-id, and the type of the object expres- sion is of a class + // unqualified-id, and the type of the object expression is of a class // type C (or of pointer to a class type C), the unqualified-id is looked // up in the scope of class C. [...] ObjectType = BaseType.getAsOpaquePtr(); + return move(Base); } diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 317d133954..a799ddb24b 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -130,12 +130,17 @@ TemplateNameKind Sema::isTemplateName(Scope *S, QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); LookupCtx = computeDeclContext(ObjectType); isDependent = ObjectType->isDependentType(); + assert((isDependent || !ObjectType->isIncompleteType()) && + "Caller should have completed object type"); } else if (SS.isSet()) { // This nested-name-specifier occurs after another nested-name-specifier, // so long into the context associated with the prior nested-name-specifier. - LookupCtx = computeDeclContext(SS, EnteringContext); isDependent = isDependentScopeSpecifier(SS); + + // The declaration context must be complete. + if (LookupCtx && RequireCompleteDeclContext(SS)) + return TNK_Non_template; } LookupResult Found(*this, TName, SourceLocation(), LookupOrdinaryName); @@ -145,11 +150,6 @@ TemplateNameKind Sema::isTemplateName(Scope *S, // computed, which is either the type of the base of a member access // expression or the declaration context associated with a prior // nested-name-specifier. - - // The declaration context must be complete. - if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(SS)) - return TNK_Non_template; - LookupQualifiedName(Found, LookupCtx); if (ObjectTypePtr && Found.empty()) { diff --git a/test/SemaTemplate/class-template-spec.cpp b/test/SemaTemplate/class-template-spec.cpp index 5bc9a6cd67..2808404b1d 100644 --- a/test/SemaTemplate/class-template-spec.cpp +++ b/test/SemaTemplate/class-template-spec.cpp @@ -19,7 +19,7 @@ int test_specs(A<float, float> *a1, A<float, int> *a2) { int test_incomplete_specs(A<double, double> *a1, A<double> *a2) { - (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}} + (void)a1->x; // expected-error{{member access into incomplete type}} (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}} } diff --git a/test/SemaTemplate/member-function-template.cpp b/test/SemaTemplate/member-function-template.cpp index 756b510e97..1d46d31e35 100644 --- a/test/SemaTemplate/member-function-template.cpp +++ b/test/SemaTemplate/member-function-template.cpp @@ -60,3 +60,16 @@ struct Functor { void test_Functor(Functor f) { f(1); } + +// Instantiation on -> +template<typename T> +struct X1 { + template<typename U> U& get(); +}; + +template<typename T> struct X2; // expected-note{{here}} + +void test_incomplete_access(X1<int> *x1, X2<int> *x2) { + float &fr = x1->get<float>(); + (void)x2->get<float>(); // expected-error{{implicit instantiation of undefined template}} +} |