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 851DD3858D20 for ; Fri, 20 Oct 2023 19:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 851DD3858D20 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 851DD3858D20 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=1697831322; cv=none; b=R+6P9LgI1IKFKoEyNtAQ9tFgobkWHJ9VnHt/VlGTXChYyjxiAXO6jWWoSImxfbqcYvydaWY6xrrbC9IWE5RQdviBpqOT8WFj8z6XEzAqQoUWPY/MVsoRbU3ZG13th1pQ+zu7di+al88o7ygpXY80wEcEAJHVW8pfJhkd9yUovAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697831322; c=relaxed/simple; bh=OD8sMoGdSvj/NQ8bI1FzgMYX07seHS55mYpXsVyYvKI=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=M/A+ZtNq322LX/slY3noQqsMKyT8IdfCzqpKXNK1eUJL6N2PaWG2mTAuxPzzBeZrDgfAW45x1N9FKA4LDHEr26MNi8JlfMzBM4yHDs/3iB6tQJ/ep2PocMdXB3jvsbp43dH8a7yH/dJUuIsqAnwLlZUUKdleTIc7KkOK58S9fpc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697831320; 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=nm0Pyd4UuZ9kxb8iB18rPLw7aYqaLrX9RmrqHmoII2Q=; b=BdqWdenGPGHWbmqJ8qsY2Pj4m/oo3BwiohlI4s3O38qejzJdW8qc93CQXlwxzHeFT1f8o4 KmJZ4IEJeCidU/LV+bU9Buy1V1lnKyMg/I7OgK2NN93dEM9zgA9X2IZW94V+yVv7FUDz5V HUp0yXT/YP+1YckPNgngmAN3llTUkiE= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-340-Vg59P-FMMOSPEQ5YPMd2tA-1; Fri, 20 Oct 2023 15:48:38 -0400 X-MC-Unique: Vg59P-FMMOSPEQ5YPMd2tA-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-41cb900adc7so22296861cf.1 for ; Fri, 20 Oct 2023 12:48:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697831318; x=1698436118; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nm0Pyd4UuZ9kxb8iB18rPLw7aYqaLrX9RmrqHmoII2Q=; b=CXxVSY+IrgFv6gfh6iDLYMlAX7LsvxlY9opgEWuDOplQ/PCNqPhYU0MHppIKEwXv9J MEq22kIZhrtnLmyyCx0+4WClVnxQONCQBtqUkYNB6uJ+QBETcuYuCahuRbPsRNvRbm7T xARylel8P1RiVAxI7687sa9FZ4S1+bwVAh5sRtjHjcmQaTXajcmFX++SnELhQz53SVzr 7s0aihVM0J+OToUza6BPKy2zEnJMjFXoPsZI2wQ/dQxlfKcoR/be2Y+KcUZ9gl2UJgdL ixEYzscK1g0tkJRxJAxAT4dPwtazZO/DM1GBnajDjEXttYIlhenspC28SaytbyXmZ3gf VkiA== X-Gm-Message-State: AOJu0Yzcwl7YfbFwohoZzyMVtLjcI8J1oQVqb2xIOOyWBQLWTiNdGVwx Aa4FsvE2ypFe8rwx86iw10wD33yOUKB23vMDRNBJekTx20hJL93SJQpN847YMaEXAT6lH3Ec+Uk 2p5eOdsyXVTh290rWeQ== X-Received: by 2002:a05:622a:41:b0:415:1804:d9ee with SMTP id y1-20020a05622a004100b004151804d9eemr8703519qtw.16.1697831318282; Fri, 20 Oct 2023 12:48:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEFRSuoaAguBEsnGAFsRPkq04bK6LHbQx2eOc9SpjJmfxOOZi+AnEkyQ1ouo3leOXMSwt8wfg== X-Received: by 2002:a05:622a:41:b0:415:1804:d9ee with SMTP id y1-20020a05622a004100b004151804d9eemr8703496qtw.16.1697831317972; Fri, 20 Oct 2023 12:48:37 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id fg25-20020a05622a581900b00410a9dd3d88sm822397qtb.68.2023.10.20.12.48.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 12:48:37 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 20 Oct 2023 15:48:36 -0400 (EDT) To: Nathan Sidwell cc: Patrick Palka , gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++/modules: ICE with lambda initializing local var [PR105322] In-Reply-To: <3ff7001d-ab30-4244-9f18-e1aae1860757@acm.org> Message-ID: References: <20231018162838.3531886-1-ppalka@redhat.com> <3ff7001d-ab30-4244-9f18-e1aae1860757@acm.org> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP 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 Fri, 20 Oct 2023, Nathan Sidwell wrote: > Thanks for looking at this, but your patch is essentially papering over the > problem. > > It took me a while to figure out, but the clue was that things like > 'decltype(f()).m' worked, but 'decltype(f()){0}' did not. The CONSTRUCTOR > node is the exception to the rule that required an expression node's type to > be streamed after the node's operands. We want the opposite for CTORS. > > I'll commit this once bootstrapped. Very interesting, thanks a lot! It's awesome that both testcases could be fixed simultaneously after all :) > > nathan > > On 10/18/23 12:28, Patrick Palka wrote: > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > > trunk? > > > > -- >8 -- > > > > For a local variable initialized by a lambda: > > > > auto f = []{}; > > > > The corresponding BLOCK_VARS contains the variable declaration first, > > followed by the closure type declaration, consistent with the > > syntactical order. This however means that a use of the closure type > > appears (in the variable type/initializer) before the declaration of the > > type. This ends up causing an ICE when streaming the BLOCK_VARS of f1 > > below because we stream (by value) the CONSTRUCTOR initializer of g1 -- > > which contains components of the closure type -- before we've streamed > > the declaration defining the closure type. The following comment in > > module.cc seems relevant: > > > > /* We want to stream the type of a expression-like nodes /after/ > > we've streamed the operands. The type often contains (bits > > of the) types of the operands, and with things like decltype > > and noexcept in play, we really want to stream the decls > > defining the type before we try and stream the type on its > > own. Otherwise we can find ourselves trying to read in a > > decl, when we're already partially reading in a component of > > its type. And that's bad. */ > > > > This patch narrowly fixes this issue by special casing closure type > > declarations in add_decl_to_level. (A loop is needed since there could > > be multiple variable declarations with an unprocessed initializer in > > light of structured bindings.) > > > > PR c++/105322 > > > > gcc/cp/ChangeLog: > > > > * name-lookup.cc (add_decl_to_level): When adding a closure > > type declaration to a block scope, add it before rather than > > after any variable declarations whose initializer we're still > > processing. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/modules/lambda-5_a.C: New test. > > * g++.dg/modules/lambda-5_b.C: New test. > > --- > > gcc/cp/name-lookup.cc | 19 ++++++++++++++++--- > > gcc/testsuite/g++.dg/modules/lambda-5_a.C | 23 +++++++++++++++++++++++ > > gcc/testsuite/g++.dg/modules/lambda-5_b.C | 10 ++++++++++ > > 3 files changed, 49 insertions(+), 3 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/modules/lambda-5_a.C > > create mode 100644 gcc/testsuite/g++.dg/modules/lambda-5_b.C > > > > diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc > > index a8b9229b29e..bb00baaf9f4 100644 > > --- a/gcc/cp/name-lookup.cc > > +++ b/gcc/cp/name-lookup.cc > > @@ -391,9 +391,22 @@ add_decl_to_level (cp_binding_level *b, tree decl) > > gcc_assert (b->names != decl); > > /* We build up the list in reverse order, and reverse it later if > > - necessary. */ > > - TREE_CHAIN (decl) = b->names; > > - b->names = decl; > > + necessary. If we're adding a lambda closure type to a block > > + scope as part of a local variable initializer, then make sure > > + we declare the type before the variable; modules expects that > > + we see a type declaration before a use of the type. */ > > + tree *prev = &b->names; > > + if (b->kind == sk_block > > + && !processing_template_decl > > + && TREE_CODE (decl) == TYPE_DECL > > + && LAMBDA_TYPE_P (TREE_TYPE (decl))) > > + while (*prev && VAR_P (*prev) > > + && !DECL_EXTERNAL (*prev) > > + && !DECL_INITIALIZED_P (*prev)) > > + prev = &TREE_CHAIN (*prev); > > + > > + TREE_CHAIN (decl) = *prev; > > + *prev = decl; > > /* If appropriate, add decl to separate list of statics. We include > > extern variables because they might turn out to be static later. > > diff --git a/gcc/testsuite/g++.dg/modules/lambda-5_a.C > > b/gcc/testsuite/g++.dg/modules/lambda-5_a.C > > new file mode 100644 > > index 00000000000..6b54c8e3173 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/modules/lambda-5_a.C > > @@ -0,0 +1,23 @@ > > +// PR c++/105322 > > +// { dg-additional-options -fmodules-ts } > > +// { dg-module-cmi pr105322 } > > + > > +export module pr105322; > > + > > +struct A { }; > > + > > +export > > +inline void f1() { > > + A a; > > + auto g1 = [a] { }; // used to ICE here during stream out > > +} > > + > > +export > > +template > > +void f2() { > > + A a; > > + auto g2 = [a] { }; > > +} > > + > > +export > > +inline auto g3 = [a=A{}] { }; > > diff --git a/gcc/testsuite/g++.dg/modules/lambda-5_b.C > > b/gcc/testsuite/g++.dg/modules/lambda-5_b.C > > new file mode 100644 > > index 00000000000..e25a913b726 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/modules/lambda-5_b.C > > @@ -0,0 +1,10 @@ > > +// PR c++/105322 > > +// { dg-additional-options -fmodules-ts } > > + > > +import pr105322; > > + > > +int main() { > > + f1(); > > + f2(); > > + g3(); > > +} > > -- > Nathan Sidwell >