From: Richard Smith Date: Fri, 9 Mar 2018 02:00:01 +0000 (+0000) Subject: PR36645: Go looking for an appropriate array bound when constant-evaluating a X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=078193eb3eb2eb10caa7cc7a8a64e665714c2f81;p=clang PR36645: Go looking for an appropriate array bound when constant-evaluating a name of an array object. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327099 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index c3e41658c2..851a03aa2e 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -63,14 +63,22 @@ namespace { static QualType getType(APValue::LValueBase B) { if (!B) return QualType(); - if (const ValueDecl *D = B.dyn_cast()) + if (const ValueDecl *D = B.dyn_cast()) { // FIXME: It's unclear where we're supposed to take the type from, and - // this actually matters for arrays of unknown bound. Using the type of - // the most recent declaration isn't clearly correct in general. Eg: + // this actually matters for arrays of unknown bound. Eg: // // extern int arr[]; void f() { extern int arr[3]; }; // constexpr int *p = &arr[1]; // valid? - return cast(D->getMostRecentDecl())->getType(); + // + // For now, we take the array bound from the most recent declaration. + for (auto *Redecl = cast(D->getMostRecentDecl()); Redecl; + Redecl = cast_or_null(Redecl->getPreviousDecl())) { + QualType T = Redecl->getType(); + if (!T->isIncompleteArrayType()) + return T; + } + return D->getType(); + } const Expr *Base = B.get(); diff --git a/test/SemaCXX/constant-expression-cxx11.cpp b/test/SemaCXX/constant-expression-cxx11.cpp index 51dd6199e6..fe4c54e7e6 100644 --- a/test/SemaCXX/constant-expression-cxx11.cpp +++ b/test/SemaCXX/constant-expression-cxx11.cpp @@ -629,6 +629,10 @@ namespace ArrayOfUnknownBound { extern const int carr[]; // expected-note {{here}} constexpr int n = carr[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}} + + constexpr int local_extern[] = {1, 2, 3}; + void f() { extern const int local_extern[]; } + static_assert(local_extern[1] == 2, ""); } namespace DependentValues {