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.129.124]) by sourceware.org (Postfix) with ESMTPS id 297BF3858C54 for ; Tue, 14 Nov 2023 15:58:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 297BF3858C54 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 297BF3858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699977523; cv=none; b=UJHZghjzEzFmFPpzH6Kr0M8CDkrJzTu8Exr3K/fzJXnAiM1nnjvCUd0qWMgYNMsc7WO/Uy3BFHfs+7Z9IUJMrYNAjtho96ZF6bxRSV2dO77dUevPf2Gw9GaxEXTLXK93thNrSO+1yaPa3K1OxofAnUKwSH8EW/8zeXR2d0Wo/kk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699977523; c=relaxed/simple; bh=2eK0B2YQE2cczAQps3lJ5KUB3O8FE3xMODCmvgqordI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=o7CaZ4u1nWDXSYDI4aU2RwF8we9xl+bFBraHerZ0yXPdwBmMgkOpQuSuIN6ImlKehaCdqKAelVENMdC4HFW3fT8Cp6CQx4hH4Hd9YqfYmCN5lDLd8VB5ALl78ivDuKzPIaM4Bp8JbVOwSNhXPXtA7lEuQzT98yD9UueBouo2hVk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699977521; 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: in-reply-to:in-reply-to:references:references; bh=/aOIxc9oMU5t+3C3zjXXkVLQ3JfzLd8BEj6QgUledaQ=; b=Kk44KSM88R+1zazq5WZUeICYLbmcacYjZesFcnBqZwuHDn3AU754ls39eGIr0RKGJSgZPC 82jKnFpuYm4Q88HOxmL9xlrn67NiuB6+DMn9dCppR3LhucIY6OKDZS75JXK1bMONZrgC9y gqqNwbkXuqTxwgHnjHLhY6xQeQgyGtE= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-263-6KaJbahyOgOv_-X5qYPVFQ-1; Tue, 14 Nov 2023 10:58:39 -0500 X-MC-Unique: 6KaJbahyOgOv_-X5qYPVFQ-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-67389986b6dso58834846d6.3 for ; Tue, 14 Nov 2023 07:58:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699977518; x=1700582318; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/aOIxc9oMU5t+3C3zjXXkVLQ3JfzLd8BEj6QgUledaQ=; b=GWYVWg7HogoDaeao/vXvGVak9b/SiJA0qqgwvwEhBjgsR7WXvhKBpO+LcpYQLhRJfY z2+rajPAVaOaMup90GJwHahC/36DYrvl+xhKjTTPYBWoCrXY1t9etWXz2wXVrrcxXtMh 6NOwPpjS67rN1fgNi5C12dLttpJbrUAFf62TsgF3mOA+gDWBTutTDKIiZaVY3pPpzBZu sd4d6PNYVN+y/8qxwVc9QdcSKNt0M40wPpfiK2M3gSb1CvmPW4kJpe+f4DLFZuWjJPNE P5ADomNNrLDIO8RyIKKUtpaOXAlm9yXY7vyAcSRObckczLBdEr0PjK6WZ3/C3gRBxefi fDYA== X-Gm-Message-State: AOJu0YwI+VRQF8F16cBPuB5mwExg+kPBBLtwIV0ssN5Tt9qN3pr8AE0M KTxMydQl+Dlqczjb06jbTOpJl8+Mnsn7gV1BSH16XJtKZBI9Oisa6MEzpU5oNc1l7VDpC4SKFIR BtnBGek4tj1dY/bhRZvbEh5np+A== X-Received: by 2002:a05:6214:1810:b0:66d:4569:9941 with SMTP id o16-20020a056214181000b0066d45699941mr2824792qvw.45.1699977518599; Tue, 14 Nov 2023 07:58:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHTyqVnc+Fp+ou9lRKrWzStKTeWzak2TFY/j2HkcX5eeqIPYYhOsyJAmKL1FrTzpLMgneQMVw== X-Received: by 2002:a05:6214:1810:b0:66d:4569:9941 with SMTP id o16-20020a056214181000b0066d45699941mr2824779qvw.45.1699977518346; Tue, 14 Nov 2023 07:58:38 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id d3-20020a0cc683000000b00670bd5a3720sm3012243qvj.97.2023.11.14.07.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 07:58:38 -0800 (PST) Date: Tue, 14 Nov 2023 10:58:36 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH v2] c++: fix parsing with auto(x) [PR112410] Message-ID: References: <20231109195835.429291-1-polacek@redhat.com> <1c680274-040d-40d7-b998-b100084c0ba5@redhat.com> <5b947ae5-b4bb-4bf2-91b5-6dcace7b537d@redhat.com> MIME-Version: 1.0 In-Reply-To: <5b947ae5-b4bb-4bf2-91b5-6dcace7b537d@redhat.com> User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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_H4,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 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. :/ Marek