]> granicus.if.org Git - clang/commitdiff
When determining whether to try evaluating the initializer of a variable, check
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 28 Oct 2012 06:18:02 +0000 (06:18 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 28 Oct 2012 06:18:02 +0000 (06:18 +0000)
whether the initializer is value-dependent rather than whether we are in a
dependent context. This allows us to detect some errors sooner, and fixes a
crash-on-invalid if a dependent type leaks out to a non-dependent context in
error recovery.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@166898 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/constant-expression-cxx11.cpp
test/SemaCXX/cxx11-crashes.cpp

index 357619031066883d9275f3662bef403c5154c750..6eef427731bc5c15d0d921bbf2d22b3dd4ea2bb4 100644 (file)
@@ -7201,7 +7201,7 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
   Expr *Init = var->getInit();
   bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal();
 
-  if (!var->getDeclContext()->isDependentContext() && Init) {
+  if (Init && !Init->isValueDependent()) {
     if (IsGlobal && !var->isConstexpr() &&
         getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor,
                                             var->getLocation())
index 9ce5b857d99b736849078a4ab81b1e957b8ea3d8..9a9746ee29b069ad5dea8045d4f6adc6ff3f8b0b 100644 (file)
@@ -521,12 +521,19 @@ namespace DependentValues {
 
 struct I { int n; typedef I V[10]; };
 I::V x, y;
-template<bool B> struct S {
+int g(); // expected-note {{here}}
+template<bool B, typename T> struct S : T {
   int k;
   void f() {
     I::V &cells = B ? x : y;
     I &i = cells[k];
     switch (i.n) {}
+
+    constexpr int n = g(); // \
+    // expected-error {{must be initialized by a constant expression}} \
+    // expected-note {{non-constexpr function 'g'}}
+
+    constexpr int m = this->g(); // ok, could be constexpr
   }
 };
 
index 66b182bf7be0c40d66b9a20c34fcb23152aecad9..bd51af1da2fa5b71bd0edc6d956e92e95013c790 100644 (file)
@@ -62,3 +62,15 @@ void foobar()
 }
 
 }
+
+namespace b6981007 {
+  struct S {}; // expected-note 3{{candidate}}
+  void f() {
+    S s(1, 2, 3); // expected-error {{no matching}}
+    for (auto x : s) {
+      // We used to attempt to evaluate the initializer of this variable,
+      // and crash because it has an undeduced type.
+      const int &n(x);
+    }
+  }
+}