From: David Majnemer Date: Wed, 26 Aug 2015 05:13:19 +0000 (+0000) Subject: [Sema] Don't assume CallExpr::getDirectCallee will succeed X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5de2927c0b2d09215ddd753d6e308bf7dbe3d841;p=clang [Sema] Don't assume CallExpr::getDirectCallee will succeed We tried to provide a very nice diagnostic when diagnosing an assignment to a const int & produced by a function call. However, we cannot always determine what function was called. This fixes PR24568. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@246014 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f2c0fb82ee..5297d7e26a 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9316,7 +9316,7 @@ static void DiagnoseConstAssignment(Sema &S, const Expr *E, if (const CallExpr *CE = dyn_cast(E)) { // Function calls const FunctionDecl *FD = CE->getDirectCallee(); - if (!IsTypeModifiable(FD->getReturnType(), IsDereference)) { + if (FD && !IsTypeModifiable(FD->getReturnType(), IsDereference)) { if (!DiagnosticEmitted) { S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange << ConstFunction << FD; diff --git a/test/SemaCXX/err_typecheck_assign_const.cpp b/test/SemaCXX/err_typecheck_assign_const.cpp index 376b6e6491..7e28125651 100644 --- a/test/SemaCXX/err_typecheck_assign_const.cpp +++ b/test/SemaCXX/err_typecheck_assign_const.cpp @@ -122,3 +122,10 @@ void test12(H h) { h.a = 1; // expected-error {{cannot assign to non-static data member 'a' with const-qualified type 'const int'}} h.b = 2; // expected-error {{cannot assign to non-static data member 'b' with const-qualified type 'const int &'}} } + +void test() { + typedef const int &Func(); + + Func &bar(); + bar()() = 0; // expected-error {{read-only variable is not assignable}} +}