From: Fariborz Jahanian Date: Tue, 12 Apr 2011 23:39:33 +0000 (+0000) Subject: Redeclaration of 'self' should be flagged in X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b8f17abf30a9aab6382c84a758fd6679a8b5583a;p=clang Redeclaration of 'self' should be flagged in objective-c instead of crashing in IRgen. // rdar://9154582. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129412 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 1dc3282c75..1bdcb88445 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -3797,6 +3797,8 @@ def warn_ivar_use_hidden : Warning< "local declaration of %0 hides instance variable">; def error_ivar_use_in_class_method : Error< "instance variable %0 accessed in class method">; +def error_implicit_ivar_access : Error< + "instance variable %0 cannot be accessed because 'self' has been redeclared">; def error_private_ivar_access : Error<"instance variable %0 is private">, AccessControl; def error_protected_ivar_access : Error<"instance variable %0 is protected">, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index be1d666c3a..96b2e56b41 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1915,6 +1915,17 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, return ExprError(); MarkDeclarationReferenced(Loc, IV); + Expr *base = SelfExpr.take(); + base = base->IgnoreParenImpCasts(); + if (const DeclRefExpr *DE = dyn_cast(base)) { + const NamedDecl *ND = DE->getDecl(); + if (!isa(ND)) { + Diag(Loc, diag::error_implicit_ivar_access) + << IV->getDeclName(); + Diag(ND->getLocation(), diag::note_declared_at); + return ExprError(); + } + } return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), Loc, SelfExpr.take(), true, true)); diff --git a/test/SemaObjC/self-declared-in-block.m b/test/SemaObjC/self-declared-in-block.m new file mode 100644 index 0000000000..c82089999c --- /dev/null +++ b/test/SemaObjC/self-declared-in-block.m @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin10 -fblocks -fobjc-nonfragile-abi -verify %s +// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -triple x86_64-apple-darwin10 -fblocks -fobjc-nonfragile-abi -verify %s +// rdar://9154582 + +@interface Blocky @end + +@implementation Blocky { + int _a; +} +- (void)doAThing { + ^{ + char self; // expected-note {{declared here}} + _a; // expected-error {{instance variable '_a' cannot be accessed because 'self' has been redeclared}} + }(); +} + +@end +