From patchwork Wed Sep 8 21:28:46 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 64228 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id B096DB6EEC for ; Thu, 9 Sep 2010 07:28:59 +1000 (EST) Received: (qmail 7304 invoked by alias); 8 Sep 2010 21:28:58 -0000 Received: (qmail 7293 invoked by uid 22791); 8 Sep 2010 21:28:57 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, TW_CC, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.44.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 08 Sep 2010 21:28:52 +0000 Received: from wpaz5.hot.corp.google.com (wpaz5.hot.corp.google.com [172.24.198.69]) by smtp-out.google.com with ESMTP id o88LSorF011067 for ; Wed, 8 Sep 2010 14:28:50 -0700 Received: from pzk6 (pzk6.prod.google.com [10.243.19.134]) by wpaz5.hot.corp.google.com with ESMTP id o88LSndm003009 for ; Wed, 8 Sep 2010 14:28:49 -0700 Received: by pzk6 with SMTP id 6so282563pzk.17 for ; Wed, 08 Sep 2010 14:28:49 -0700 (PDT) Received: by 10.142.250.28 with SMTP id x28mr264133wfh.188.1283981328957; Wed, 08 Sep 2010 14:28:48 -0700 (PDT) Received: from coign.google.com (dhcp-172-22-123-203.mtv.corp.google.com [172.22.123.203]) by mx.google.com with ESMTPS id l41sm498826wfa.1.2010.09.08.14.28.47 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 08 Sep 2010 14:28:47 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [gccgo] Do not permit parenthesized types in composite literals Date: Wed, 08 Sep 2010 14:28:46 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-System-Of-Record: true X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org In order to avoid a parsing ambiguity, Go prohibits parenthesized types with composite literals. Otherwise cases like if (S) {} are ambiguous, because the condition of the if statement could be either (S) or (S){}. The language requires that an extra set of parentheses be used if the latter case is intended. For simplicity, the language was changed to simply prohibit a parenthesized type when used with a composite literal. This change implements that in gccgo. Committed to gccgo branch. Ian diff -r 6b9674bf9b5c go/parse.cc --- a/go/parse.cc Wed Sep 08 14:19:56 2010 -0700 +++ b/go/parse.cc Wed Sep 08 14:21:59 2010 -0700 @@ -2513,11 +2513,15 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit, bool* is_type_switch) { + source_location start_loc = this->location(); + bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN); + Expression* ret = this->operand(may_be_sink); // An unknown name followed by a curly brace must be a composite // literal, and the unknown name must be a type. if (may_be_composite_lit + && !is_parenthesized && ret->unknown_expression() != NULL && this->peek_token()->is_op(OPERATOR_LCURLY)) { @@ -2528,11 +2532,16 @@ // We handle composite literals and type casts here, as it is the // easiest way to handle types which are in parentheses, as in - // "((([]int))){1}". + // "((uint))(1)". if (ret->is_type_expression()) { if (this->peek_token()->is_op(OPERATOR_LCURLY)) - ret = this->composite_lit(ret->type(), ret->location()); + { + if (is_parenthesized) + error_at(start_loc, + "cannot parenthesize type in composite literal"); + ret = this->composite_lit(ret->type(), ret->location()); + } else if (this->peek_token()->is_op(OPERATOR_LPAREN)) { source_location loc = this->location();