Bug 61857 - An init-capturing lambda is parsed incorrectly when used in a braced-init-list
Summary: An init-capturing lambda is parsed incorrectly when used in a braced-init-list
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, rejects-valid
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2014-07-20 12:27 UTC by Ville Voutilainen
Modified: 2022-03-11 00:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-07-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ville Voutilainen 2014-07-20 12:27:30 UTC
struct A { 
    A(int val){} 
};

int main()
{ 
    A a{ [x=123]{ return x; }() }; 
} 

init-capture2.cpp: In function ‘int main()’:
init-capture2.cpp:7:11: error: ‘x’ was not declared in this scope
     A a{ [x=123]{ return x; }() }; 

Clang recently fixed this bug, for clang it was caused by the parser
getting confused with the lambda and designated initializers, perhaps
gcc has a similar bug. With parentheses the code works:

struct A { 
    A(int val){} 
};

int main()
{ 
    A a( [x=123]{ return x; }() ); 
}
Comment 1 Paolo Carlini 2014-09-22 12:16:31 UTC
In our case the issue is slightly different, the problem is in cp_parser_initializer_list, here:

	  cp_parser_parse_tentatively (parser);
	  cp_lexer_consume_token (parser->lexer);
	  designator = cp_parser_constant_expression (parser, true, &non_const);
	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
	  cp_parser_require (parser, CPP_EQ, RT_EQ);
	  if (!cp_parser_parse_definitely (parser))
	    designator = NULL_TREE;
	  else if (non_const)
	    require_potential_rvalue_constant_expression (designator);

where cp_parser_constant_expression is tentatively called. I think it can be solved by looking forward for the '=', similarly to other GNU extensions, for example compound literals.
Comment 2 Paolo Carlini 2014-09-23 12:02:21 UTC
Mine.
Comment 3 paolo@gcc.gnu.org 2014-09-23 18:08:31 UTC
Author: paolo
Date: Tue Sep 23 18:07:59 2014
New Revision: 215528

URL: https://gcc.gnu.org/viewcvs?rev=215528&root=gcc&view=rev
Log:
/cp
2014-09-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/61857
	* parser.c (cp_parser_skip_to_closing_square_bracket,
	cp_parser_array_designator_p): New.
	(cp_parser_initializer_list): Use the latter.

/testsuite
2014-09-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/61857
	* g++.dg/cpp1y/lambda-init10.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1y/lambda-init10.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Paolo Carlini 2014-09-23 18:09:08 UTC
Fixed.