From 9b93f206d89dbf86805b610b417bb874f7f446e8 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 1 Oct 2013 00:28:29 +0000 Subject: [PATCH] Tweak changes in r186464 to avoid a crash. Currently, IR generation can't handle file-scope compound literals with non-constant initializers in C++. Fixes PR17415 (the first crash in the bug). (We should probably change (T){1,2,3} to use the same codepath as T{1,2,3} in C++ eventually, given that the semantics of the latter are actually defined by the standard.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191719 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/Expr.cpp | 3 --- lib/Sema/SemaExpr.cpp | 5 ++++- test/SemaCXX/compound-literal.cpp | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index a3a36da65f..38f5e2f488 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -2654,9 +2654,6 @@ bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const { return Exp->isConstantInitializer(Ctx, false); } case InitListExprClass: { - // FIXME: This doesn't deal with fields with reference types correctly. - // FIXME: This incorrectly allows pointers cast to integers to be assigned - // to bitfields. const InitListExpr *ILE = cast(this); if (ILE->getType()->isArrayType()) { unsigned numInits = ILE->getNumInits(); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 9940e8ba79..239567844d 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -4717,7 +4717,10 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, LiteralExpr = Result.get(); bool isFileScope = getCurFunctionOrMethodDecl() == 0; - if (!getLangOpts().CPlusPlus && isFileScope) { // 6.5.2.5p3 + if (isFileScope && + !LiteralExpr->isTypeDependent() && + !LiteralExpr->isValueDependent() && + !literalType->isDependentType()) { // 6.5.2.5p3 if (CheckForConstantInitializer(LiteralExpr, literalType)) return ExprError(); } diff --git a/test/SemaCXX/compound-literal.cpp b/test/SemaCXX/compound-literal.cpp index 595747e40c..3fc61505df 100644 --- a/test/SemaCXX/compound-literal.cpp +++ b/test/SemaCXX/compound-literal.cpp @@ -76,3 +76,13 @@ namespace brace_initializers { (void)(PrivateDtor){1, 2}; // expected-error {{temporary of type 'brace_initializers::PrivateDtor' has private destructor}} } } + +// This doesn't necessarily need to be an error, but CodeGen can't handle it +// at the moment. +int PR17415 = (int){PR17415}; // expected-error {{initializer element is not a compile-time constant}} + +// Make sure we accept this. (Not sure if we actually should... but we do +// at the moment.) +template struct Value { }; +template +int &check_narrowed(Value); -- 2.40.0