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 3BA433858D33 for ; Wed, 18 Oct 2023 16:29:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3BA433858D33 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 3BA433858D33 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=1697646547; cv=none; b=EeaUmGZCdh8JDbN25QbPMyqlSuNjc6iJr09Lkl/mfDdeLPKWN/EGGpc2tOot//gV6l45gGSV3LG3aSSskWXxS0VnS7R9aqXfHM8ZbQtQ1ICRwxJOmEsNyddj+Sho4xEZDOuxOfwOVfhOU1ZoiumCBQHY8nxoH2K+4v6ERVVsEx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697646547; c=relaxed/simple; bh=J1vonf2dKMSaW5o8PpyeSuZFBC0VH9AaPGVFCUiQo5E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CuvERtpWtUSL8RuhzeRHID+Ejol7DukbbacMbp3pA8xakIN8zxYFJqR6JLl2gZMKtfJExILB1rLqsJr7kIbBWdoyAbLvzGZPUM1JPvRLxL07lr96XXIG8Fugl51Fsz0K4OfQWzHpNMJsQL01HV0BEevq6AiumQfB42FsUoB+HEk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697646545; 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; bh=owFSrc5WRXZcR3Az/zcimFmXnIbH1P9AUdLj95oZ3KM=; b=XYVeGCV98krjYO40X/OiJuVV09ljBczVgw88y24ZfBuILxhxSJTPiYPTC9v1BcRqfz96GC LLNOdRgUl1chM+oXB08yHVpjmClBIo8icUqeDQQlFhUjJEMDBmxV8lO5+k7r8ui0MTQdkn 7Q4DZRmpK36ZiSlAhcJLljwKNsu8GEM= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-249-WO6FpIrxNlSsfUyrBMpk8g-1; Wed, 18 Oct 2023 12:29:04 -0400 X-MC-Unique: WO6FpIrxNlSsfUyrBMpk8g-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-778999c5f2aso31323585a.3 for ; Wed, 18 Oct 2023 09:29:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697646543; x=1698251343; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=owFSrc5WRXZcR3Az/zcimFmXnIbH1P9AUdLj95oZ3KM=; b=k1mqEi8s2ardEXCM5OV/ynjY82AsZb4XNI/9fgpmxI2zvTuJDIAATZYJKlqX0RDr7T mL12zbBRsek21HxjZQwSZHFEo0Q3YMc+Hd0JeZIVuRe+5PxrNfBGeqaZZXLOt6prLNxJ zHyOOpf1JVUH9pnmYyhU2dGET/7VBfQqHx3hs4QApTTw14aSxWshGu0QoPvikLgoKHSv Etoo/1JJetDUYOpUQrvo9kXOl1XtOxONaz6ExB+TdfTFLwjVGpBGi4NNZfQtEfxVeEjM Vi7ASc43KVrEXKNsVDYSrC6WbCMr1ei3F++AZdC+Yi0hLI/bfhrCjSpzt1etnXZOLi/j eWwQ== X-Gm-Message-State: AOJu0Yxs+wtL7zrrNzn92d776VDdOyye9rhVYeuonPvHWuUHP7VwqiTH rYVbZppM3V8SZ+PhG4m1bWABFJK7nTUVDchOliKKPdNxB1mqUfqsQvJCtwHgOaDCKhQAsqiBtXN EaLNmIVUy1kH3lE1QG/VYKtORdYzMhLWxjFsLYYs7AU60L9g7ijeoNPPWc0uxzNilu+ZDxokKBu Y= X-Received: by 2002:a05:620a:389e:b0:774:1d85:54fc with SMTP id qp30-20020a05620a389e00b007741d8554fcmr5062624qkn.74.1697646543212; Wed, 18 Oct 2023 09:29:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKfGtpZAqvMycMj2g0E5p3RqkBITjHBSrOXODkBOu2xiksmxbq+W3gvewFpg2c+1YUUj86Kw== X-Received: by 2002:a05:620a:389e:b0:774:1d85:54fc with SMTP id qp30-20020a05620a389e00b007741d8554fcmr5062605qkn.74.1697646542881; Wed, 18 Oct 2023 09:29:02 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bq43-20020a05620a46ab00b00767177a5bebsm74847qkb.56.2023.10.18.09.29.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 09:29:02 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, nathan@acm.org, Patrick Palka Subject: [PATCH] c++/modules: ICE with lambda initializing local var [PR105322] Date: Wed, 18 Oct 2023 12:28:38 -0400 Message-ID: <20231018162838.3531886-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.398.ga9ecda2788 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.6 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_H4,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: 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(); +} -- 2.42.0.398.ga9ecda2788