Bug 56037 - Spurious syntax error triggered before ambiguity resolution of type-id
Summary: Spurious syntax error triggered before ambiguity resolution of type-id
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: 4.9.0
Assignee: Paolo Carlini
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-01-18 20:13 UTC by ricilake
Modified: 2013-11-02 09:35 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-01-19 00:00:00


Attachments
Test cases as described in bug (198 bytes, text/plain)
2013-01-18 20:23 UTC, ricilake
Details

Note You need to log in before you can comment on or make changes to this bug.
Description ricilake 2013-01-18 20:13:57 UTC
A spurious syntax error is produced by the following:

  static const int zero = 0;
  std::cout << (std::pair<int, int>(int(zero), int(zero))) << std::endl;

In contrast, 

  static const int zero = 0;
  static const int zero_alias = 0;
  std::cout << (std::pair<int, int>(int(zero), int(zero_alias))) << std::endl;

compiles correctly (assuming that there is an appropriate overload for operator<<).

Section 8.2[2] states that 

The ambiguity arising from the similarity between a function-style cast and a   type-id can occur in different contexts. The ambiguity appears as a choice between a function-style cast expression and a declaration of a type. The resolution is that any construct that could possibly be a type-id in its syntactic context shall be considered a type-id.

In this case, 8.2[2] should not apply, since the syntactic context of the parenthesized expression does not permit the production:

  cast-expression :: ( type-id ) cast-expression

However, in the first code excerpt, where the parenthesized expression would not be a valid type-id (because of the redefinition of the parameter name), the syntax error is apparently being triggered before the ambiguity resolution decides that type-id is not possible. In the second code excerpt, the parenthesize expression could contain a valid type-id, but ambiguity resolution rejects that possibility.

---

Bug report generated as a result of http://stackoverflow.com/questions/14403946/adding-parenthesis-to-a-constructor-call-causes-duplicate-parameter-error-in-xlc/14404900#14404900
Comment 1 ricilake 2013-01-18 20:23:27 UTC
Created attachment 29214 [details]
Test cases as described in bug
Comment 2 Paolo Carlini 2013-07-03 09:49:00 UTC
Reduced:

struct T
{
  T(int, int);
};

int main()
{
  static const int zero = 0;
  (T(int(zero), int(zero)));
}
Comment 3 Paolo Carlini 2013-10-31 17:41:42 UTC
This is closely related to PR29234. In fact, the patch I'm working on, as linked from the latter, already fixes this bug too.
Comment 4 paolo@gcc.gnu.org 2013-11-02 09:34:16 UTC
Author: paolo
Date: Sat Nov  2 09:34:13 2013
New Revision: 204312

URL: http://gcc.gnu.org/viewcvs?rev=204312&root=gcc&view=rev
Log:
/cp
2013-11-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/29234
	PR c++/56037
	* parser.c (cp_parser_cast_expression): If we aren't looking at
	a cast-expression don't call cp_parser_type_id.
	(cp_parser_postfix_expression): Likewise for compound-literal.
	(cp_parser_tokens_start_cast_expression): Adjust.

/testsuite
2013-11-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/29234
	PR c++/56037
	* g++.dg/parse/pr29234.C: New.
	* g++.dg/parse/pr56037.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/parse/pr29234.C
    trunk/gcc/testsuite/g++.dg/parse/pr56037.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Paolo Carlini 2013-11-02 09:35:45 UTC
Fixed for 4.9.0.