decl.c (start_decl): Complain about static/thread_local vars in constexpr function.

* 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.

From-SVN: r215863
This commit is contained in:
Jason Merrill 2014-10-03 12:48:24 -04:00 committed by Jason Merrill
parent e01a49c11b
commit b2cb98ed66
5 changed files with 66 additions and 6 deletions

View File

@ -1,5 +1,14 @@
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.

View File

@ -4767,6 +4767,16 @@ start_decl (const cp_declarator *declarator,
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);
@ -5135,15 +5145,20 @@ check_for_uninitialized_const_var (tree decl)
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))
{

View File

@ -10915,6 +10915,10 @@ cp_parser_jump_statement (cp_parser* parser)
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))
{
@ -16484,6 +16488,11 @@ cp_parser_asm_definition (cp_parser* parser)
/* 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))
@ -21441,6 +21450,10 @@ cp_parser_try_block (cp_parser* parser)
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);

View File

@ -7537,7 +7537,9 @@ tree
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);
@ -7546,8 +7548,12 @@ ensure_literal_type_for_constexpr_object (tree decl)
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;
}

View File

@ -0,0 +1,17 @@
// { 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" }