]> granicus.if.org Git - clang/commitdiff
Improve r146813 (for PR11595) to give an appropriate diagnostic.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 19 Dec 2011 22:01:37 +0000 (22:01 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 19 Dec 2011 22:01:37 +0000 (22:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146915 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/SemaCXX/constant-expression-cxx11.cpp

index ff556c3094427ac41081ca44e8c11dcb5504010b..630136ea7f862465f8615c3140ee0a6af9417929 100644 (file)
@@ -2059,9 +2059,7 @@ public:
         return false;
       BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
     } else if (E->getBase()->isRValue()) {
-      if (!E->getBase()->getType()->isRecordType() ||
-          !E->getBase()->getType()->isLiteralType())
-        return false;
+      assert(E->getBase()->getType()->isRecordType());
       if (!EvaluateTemporary(E->getBase(), Result, this->Info))
         return false;
       BaseTy = E->getBase()->getType();
@@ -2242,7 +2240,7 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
 bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
     const MaterializeTemporaryExpr *E) {
   if (E->GetTemporaryExpr()->isRValue()) {
-    if (E->getType()->isRecordType() && E->getType()->isLiteralType())
+    if (E->getType()->isRecordType())
       return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
 
     Result.set(E, Info.CurrentCall);
@@ -2770,8 +2768,15 @@ public:
 
 /// Evaluate an expression of record type as a temporary.
 static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
-  assert(E->isRValue() && E->getType()->isRecordType() &&
-         E->getType()->isLiteralType());
+  assert(E->isRValue() && E->getType()->isRecordType());
+  if (!E->getType()->isLiteralType()) {
+    if (Info.getLangOpts().CPlusPlus0x)
+      Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral)
+        << E->getType();
+    else
+      Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
+    return false;
+  }
   return TemporaryExprEvaluator(Info, Result).Visit(E);
 }
 
index 94da73fcf6a853f7aa51434fe76491591e65c828..e4ada1ed832d275362016cc59ff1126184a6201f 100644 (file)
@@ -915,6 +915,11 @@ static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
 
 namespace PR11595 {
   struct A { constexpr bool operator==(int x) { return true; } };
-  struct B { B(); ~B(); A& x; };
-  static_assert(B().x == 3, "");  // expected-error {{constant expression}}
+  struct B { B(); A& x; };
+  static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
+
+  constexpr bool f(int k) {
+    return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
+  }
+  constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
 }