From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id ABDEE3858CD1 for ; Tue, 14 Nov 2023 22:27:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ABDEE3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ABDEE3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700000828; cv=none; b=eSp2WmZFloXLoUCCsIfKsjKgSMQOWtoFxaE6BKyXuMO9HOYZ8uAL//4LNjPESi5zANzYMuWbkDydA4Ez4GJbNRJ4PnoZVcBAdXTRyGc0JzGwTc7s6XF0hah7VrTcJIxmkX/04qq9hDowSNeQ02f/7LnuwFlzLEdk5Q8m7CsqJnc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700000828; c=relaxed/simple; bh=CEPqn7I6+IBnjD16ScbKo4c5p5tYlz65ZartpghJJps=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=dczcDDhC9BGjcbhBhJrOKYCAACeS4EhYfwSzob+idX72Zy4hlOCcAmmMbJBhx23uNahSAENpXxl+7klgX2ruvhgH4gJa2s0lXl6NoMx+yXWVpOdZC6PkwSxIUSpgVuYDxe+/K9zZ+o6CrJj0fIdWg6q7d1kS5AXCVIGwOlX/yd4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700000827; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=arqo2IJrShVqXTyb404R/6seuNMagAfy/Y2r6LB29/w=; b=Pu3l3Sv5hgJGbBAmWnP2D8cf7jXLfOF1lT+WfrLEWe0ntSCQ84+8hVI6Tw2R9E2sFsrqOj yzddAF8P2sipXbBTrEFdN31b2h/L3gjaUDhxOMcdusQzp6OpmyhFvU+TQncu44+Z8UceCn P7MN7rilfUHxRBtbuptapYh9evJonUE= Received: from mail-vs1-f70.google.com (mail-vs1-f70.google.com [209.85.217.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-yYJqbRGbNn6O_YAVePSEMg-1; Tue, 14 Nov 2023 17:27:05 -0500 X-MC-Unique: yYJqbRGbNn6O_YAVePSEMg-1 Received: by mail-vs1-f70.google.com with SMTP id ada2fe7eead31-45d94427e98so1347807137.1 for ; Tue, 14 Nov 2023 14:27:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700000825; x=1700605625; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=arqo2IJrShVqXTyb404R/6seuNMagAfy/Y2r6LB29/w=; b=fWaY4+i68Dzd+dKYSB4BvUrSQKD3zOKaubwYEtoa0zwbh52jJx1HWtcJt6Ee/fB4mJ SO+j6OFoPcbXKw5R2r67wCTVVa4eKBP7dlFJM6wU9eirYn1NW5+g7Kp3Uir13q6YSOyK SMFtlQEpww7AfttCCpyc33dC1yrFF3HxrDZ0WWVZBd5Hs4psjVX8xlWO84WZrz93Itvz gcFDh2HK1AYItsiEsFoHNHUK1lkATz8UTQwYDSwdLfq2AVtwTPpt0CV8Pw7S/zCzfv9I VuGMYCBTiewTN6BmM7v5dPGmm7f7rpkOrS5Gjp279t5vn+GMpbm23agCYeqLyDdubGuZ dYrg== X-Gm-Message-State: AOJu0Yxtr5/RVDIPosMOiBksxQ7P9Vkh0yYbc5QKd3JUqfppVeLZVue+ hTKmL9OP5MkS+Mpan0j/hZ6EUnwjI/sTOqF10YY6P1PEU0p/kon7BWG0ie2tSLMoyDxxMr4nKVs BbGSQUGNkyCj7svuafw== X-Received: by 2002:a05:6102:23f5:b0:462:5f45:2fbb with SMTP id p21-20020a05610223f500b004625f452fbbmr1782249vsc.22.1700000825121; Tue, 14 Nov 2023 14:27:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHufI0ypJYxV9mclvOnRn7N+pv8H6JWTzuf5N2238TtfcRIjTZJL5v5nj81m31PEb2NQIZJpQ== X-Received: by 2002:a05:6102:23f5:b0:462:5f45:2fbb with SMTP id p21-20020a05610223f500b004625f452fbbmr1782237vsc.22.1700000824780; Tue, 14 Nov 2023 14:27:04 -0800 (PST) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j5-20020ac84f85000000b00405502aaf76sm3060724qtw.57.2023.11.14.14.27.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Nov 2023 14:27:04 -0800 (PST) Message-ID: Date: Tue, 14 Nov 2023 17:27:03 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] c++: fix parsing with auto(x) [PR112410] To: Marek Polacek Cc: GCC Patches References: <20231109195835.429291-1-polacek@redhat.com> <1c680274-040d-40d7-b998-b100084c0ba5@redhat.com> <5b947ae5-b4bb-4bf2-91b5-6dcace7b537d@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 11/14/23 10:58, Marek Polacek wrote: > On Mon, Nov 13, 2023 at 09:26:41PM -0500, Jason Merrill wrote: >> On 11/10/23 20:13, Marek Polacek wrote: >>> On Thu, Nov 09, 2023 at 07:07:03PM -0500, Jason Merrill wrote: >>>> On 11/9/23 14:58, Marek Polacek wrote: >>>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>>>> >>>>> -- >8 -- >>>>> Here we are wrongly parsing >>>>> >>>>> int y(auto(42)); >>>>> >>>>> which uses the C++23 cast-to-prvalue feature, and initializes y to 42. >>>>> However, we were treating the auto as an implicit template parameter. >>>>> >>>>> Fixing the auto{42} case is easy, but when auto is followed by a (, >>>>> I found the fix to be much more involved. For instance, we cannot >>>>> use cp_parser_expression, because that can give hard errors. It's >>>>> also necessary to disambiguate 'auto(i)' as 'auto i', not a cast. >>>>> auto(), auto(int), auto(f)(int), auto(*), auto(i[]), auto(...), etc. >>>>> are all function declarations. We have to look at more than one >>>>> token to decide. >>>> >>>> Yeah, this is a most vexing parse problem. The code is synthesizing >>>> template parameters before we've resolved whether the auto is a >>>> decl-specifier or not. >>>> >>>>> In this fix, I'm (ab)using cp_parser_declarator, with member_p=false >>>>> so that it doesn't commit. But it handles even more complicated >>>>> cases as >>>>> >>>>> int fn (auto (*const **&f)(int) -> char); >>>> >>>> But it doesn't seem to handle the extremely vexing >>>> >>>> struct A { >>>> A(int,int); >>>> }; >>>> >>>> int main() >>>> { >>>> int a; >>>> A b(auto(a), 42); >>>> } >>> >>> Argh. This test should indeed be accepted and is currently rejected, >>> but it's a different problem: 'b' is at block scope and you can't >>> have a template there. But when I put it into a namespace scope, >>> it shows that my patch doesn't work correctly. I've added auto-fncast14.C >>> for the latter and opened c++/112482 for the block-scope problem. >>>> I think we need to stop synthesizing immediately when we see RID_AUTO, and >>>> instead go back after we successfully parse a declaration and synthesize for >>>> any autos we saw along the way. :/ >>> >>> That seems very complicated :(. I had a different idea though; how >>> about the following patch? The idea is that if we see that parsing >>> the parameter-declaration-list didn't work, we undo what synthesize_ >>> did, and let cp_parser_initializer parse "(auto(42))", which should >>> succeed. I checked that after cp_finish_decl y is initialized to 42. >> >> Nice, that's much simpler. Do you also still need the changes to >> cp_parser_simple_type_specifier? > > I do, otherwise we parse > > int f (auto{42}); > > just as if it had been > > int f (auto); > > because the {42} is consumed in the cp_parser_simple_type_specifier/RID_AUTO > loop. :/ It isn't consumed there, that loop is just scanning forward to see if there's a ->. The { is still the next token when we expect it to be a closing ) in cp_parser_direct_declarator: > /* Parse the parameter-declaration-clause. */ > params > = cp_parser_parameter_declaration_clause (parser, flags); > const location_t parens_end > = cp_lexer_peek_token (parser->lexer)->location; > > /* Consume the `)'. */ > parens.require_close (parser); Maybe we want to abort_fully_implicit_template here rather than in cp_parser_parameter_declaration_clause? Jason