2014-10-03 Jason Merrill <jason@redhat.com>
+ * decl.c (start_decl): Complain about static/thread_local vars
+ in constexpr function.
+ (check_for_uninitialized_const_var): Also uninitialized vars.
+ * parser.c (cp_parser_jump_statement): And gotos.
+ (cp_parser_asm_operand_list): And asm.
+ (cp_parser_try_block): And try.
+ * semantics.c (ensure_literal_type_for_constexpr_object): And
+ non-literal.
+
* semantics.c (constexpr_fn_retval): Ignore declarations in C++14.
(var_in_constexpr_fn): New.
(cxx_eval_constant_expression): Look into DECL_INITIAL.
DECL_THIS_STATIC (decl) = 1;
}
+ if (current_function_decl && VAR_P (decl)
+ && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ {
+ if (DECL_THREAD_LOCAL_P (decl))
+ error ("%qD declared %<thread_local%> in %<constexpr%> function",
+ decl);
+ else if (TREE_STATIC (decl))
+ error ("%qD declared %<static%> in %<constexpr%> function", decl);
+ }
+
if (!processing_template_decl && VAR_P (decl))
start_decl_1 (decl, initialized);
7.1.6 */
if (VAR_P (decl)
&& TREE_CODE (type) != REFERENCE_TYPE
- && CP_TYPE_CONST_P (type)
+ && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
&& !DECL_INITIAL (decl))
{
tree field = default_init_uninitialized_part (type);
if (!field)
return;
- permerror (DECL_SOURCE_LOCATION (decl),
- "uninitialized const %qD", decl);
+ if (CP_TYPE_CONST_P (type))
+ permerror (DECL_SOURCE_LOCATION (decl),
+ "uninitialized const %qD", decl);
+ else
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "uninitialized variable %qD in %<constexpr%> function",
+ decl);
if (CLASS_TYPE_P (type))
{
break;
case RID_GOTO:
+ if (parser->in_function_body
+ && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ error ("%<goto%> in %<constexpr%> function");
+
/* Create the goto-statement. */
if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
{
/* Look for the `asm' keyword. */
cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
+
+ if (parser->in_function_body
+ && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ error ("%<asm%> in %<constexpr%> function");
+
/* See if the next token is `volatile'. */
if (cp_parser_allow_gnu_extensions_p (parser)
&& cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
tree try_block;
cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
+ if (parser->in_function_body
+ && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ error ("%<try%> in %<constexpr%> function");
+
try_block = begin_try_block ();
cp_parser_compound_statement (parser, NULL, true, false);
finish_try_block (try_block);
ensure_literal_type_for_constexpr_object (tree decl)
{
tree type = TREE_TYPE (decl);
- if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
+ if (VAR_P (decl)
+ && (DECL_DECLARED_CONSTEXPR_P (decl)
+ || var_in_constexpr_fn (decl))
&& !processing_template_decl)
{
tree stype = strip_array_types (type);
when we try to initialize the variable. */;
else if (!literal_type_p (type))
{
- error ("the type %qT of constexpr variable %qD is not literal",
- type, decl);
+ if (DECL_DECLARED_CONSTEXPR_P (decl))
+ error ("the type %qT of constexpr variable %qD is not literal",
+ type, decl);
+ else
+ error ("variable %qD of non-literal type %qT in %<constexpr%> "
+ "function", decl, type);
explain_non_literal_class (type);
return NULL;
}
--- /dev/null
+// { dg-do compile { target c++14 } }
+
+struct A { A(); };
+
+constexpr int f(int i) {
+ static int j = i; // { dg-error "static" }
+ thread_local int l = i; // { dg-error "thread_local" }
+ goto foo; // { dg-error "goto" }
+ foo:
+ asm("foo"); // { dg-error "asm" }
+ int k; // { dg-error "uninitialized" }
+ A a; // { dg-error "non-literal" }
+ return i;
+}
+
+// FIXME remove
+// { dg-prune-output "return" }