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 B87CE3858D33 for ; Wed, 18 Oct 2023 16:34:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B87CE3858D33 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 B87CE3858D33 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=1697646895; cv=none; b=BYRzciAJsy74kf0Rp3+t5cax8WdpIwVxs9whE8B3mkdmjdUVs/kuiXjuJ/QLQwS7TbuPPzxkT1/hKR0M+vqbF6BT8hEIeTlOUqJV6uDWmRxFEKvTSqz+wNzIzPUu9JJY6/oVrQLhRITjf5pZatOQESuNPgztRDNyg1JKYccuEBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697646895; c=relaxed/simple; bh=LTJDQtsffRwcLwQW9rMZIaF7JhsoerLiS4x8kS2TyUw=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=OxJj9iyTY6B6A+cXouh7PU7F5owjbjIKX3ZtjxNjH+4U4wa6am8FN+e9eMyCnIcZ2oyW89JuqHcUBZ9wHPtJzCSJ339H3js+xddQsHvozv/rmFy8Mmfb6LJqhbRgOzRJ7w8vFG8w029pvVxXL889imaCoj4/7FSu7zsul5Dj1xk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697646893; 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=+S+BMOzBtUl5U/rMjDguQaDUoP8m36LVyrYSZaQY3SU=; b=d92Qs7n4aaw4xlbSYug87XkwdsTfRRxZPwIpfaitz+Y1iTE2OjM3QekTCWj6tEb+GF/qNC Nh9kqCe93H10AmX1aBw9tt7Xq1gU7t3gnHI1QJ+qv8RIkJMDfiPeUxCz/43vfjcvMPBX3E ol5ftEZAoJDDDkNnYzQdsob8A0ctONw= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-130-wi0AiuEFM9-rVUY9wSL-fw-1; Wed, 18 Oct 2023 12:34:52 -0400 X-MC-Unique: wi0AiuEFM9-rVUY9wSL-fw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-41b83b3fd18so22629271cf.3 for ; Wed, 18 Oct 2023 09:34:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697646892; x=1698251692; 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=+S+BMOzBtUl5U/rMjDguQaDUoP8m36LVyrYSZaQY3SU=; b=NpGrwmQAt5vFxAMupm8lgQcn7yVUaKc4TBh3mJlq5vRMWdjPypo+Qk2WJN7Hxsd+ko GiMapEYu7p4RajmfDaMZLXaYTEfzlpHtjEPFDAm6yznrM+bhF1c/1eAIkyAI9z2U5yLZ QJ+chx7uvKt/PMV6LWX2joDYBXQsv0ciz4IPEhM8YgOReuR8DamscOP7qDramz5O5ElY yFo2MMr8Qm6czpAdEBTHYe+b90aUMWYyphJEMTcWr5zgxHo+Ch1U5WYqRos4vvM/Lh8U s19APh9WBLjBMiAHq15CmT2qPMSLpLhF2wKt9iMWj7KavmxWaKc7nhHX3+X0H5B9InTA 0KxA== X-Gm-Message-State: AOJu0Yzn82UiHlyI7COnFsYtYAtRqEJ2+71lodVdjAycnl7tlauqJGDy XAi1Y/fIt88Yqn3NmNqZpkbbg7sAO65YT7R3HJsq5N2c7D8iuHLz+Tpt7ePLdtOdBgtgLLRG+us U+haFH47vdfsx7Gs2ZA== X-Received: by 2002:ac8:5a42:0:b0:417:9962:4de9 with SMTP id o2-20020ac85a42000000b0041799624de9mr6857792qta.1.1697646891792; Wed, 18 Oct 2023 09:34:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfHi901gd2hVkY3OfuFM1zxwiMoW7zHqTQt285S8HBeCoEhyVR6dxtrDEj4u/mFkSjBjupZw== X-Received: by 2002:ac8:5a42:0:b0:417:9962:4de9 with SMTP id o2-20020ac85a42000000b0041799624de9mr6857775qta.1.1697646891514; Wed, 18 Oct 2023 09:34:51 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id o8-20020ac841c8000000b0041cc2852f62sm79054qtm.5.2023.10.18.09.34.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 09:34:51 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 18 Oct 2023 12:34:50 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com, nathan@acm.org Subject: Re: [PATCH] c++/modules: ICE with lambda initializing local var [PR105322] In-Reply-To: <20231018162838.3531886-1-ppalka@redhat.com> Message-ID: References: <20231018162838.3531886-1-ppalka@redhat.com> 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 Wed, 18 Oct 2023, Patrick Palka wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? Note that this doesn't fix the other testcase in the PR, which doesn't use any lambdas and which ICEs in the same way: export module pr105322; auto f() { struct A { int m; }; return A{}; } export inline void g() { auto r = decltype(f()){0}; } Here when streaming the CONSTRUCTOR initializer of r, we end up streaming components of f()::A before ever streaming the declaration/definition of f()::A. I suspect a separate fix might be needed for this testcase? The narrow fix for the lambda testcase still seems useful nonetheless. > > -- >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(); > +} > -- > 2.42.0.398.ga9ecda2788 > >