From patchwork Tue Feb 20 09:54:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 86021 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D1E743858D35 for ; Tue, 20 Feb 2024 09:54:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 622C13858D28 for ; Tue, 20 Feb 2024 09:54:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 622C13858D28 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 622C13858D28 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=1708422867; cv=none; b=OWCJaiZuwx64rowpWUlPYLukoTalkkWjf6tFJBiXAQ45bo3ITe+B8n66XgJTUOd5a3Jv3r9VTr14v1HR0fvmQOTaRAGSldLxryOD5r6UyC56qVfj5C7E4rsR6PJCyHCNLMdB9eTSUCjdZlSPGM+PcSpZY9C1vaFp3KAWMv1mD9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708422867; c=relaxed/simple; bh=2KsTsK9+IR3i4ypHy4a1oeV0xU6HvAX+k/ZmLDmY4Yw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=sIIrQUPFb0e+PvAq/jA6Mngf2GJv3wbE0tqJ5OyFs9El/EE8bpSTjAVu5DECZoe/NVxIZU/Ee/wPxCWMAPLwdH10pknZC1HrERZj+CG7mxgWJCF9gvN/m/DA9mJZiTCumkTHsGWQZ+JJroeS0itiETYTxB0cDNW80IKw2rkNYwQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708422865; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=JPMf3OW/xBObQUdpGMu8mQphVt+kqfv0V/ror+AnhE4=; b=UKL5wEuNqSGu1IZtezcm6uVc8PgQqSBs+9RSOa3r6paWxDS+wc5fA066n9N8sF2cwjrTHu zYFIS+ENXkU0UpjVd2fw30ki0RPceffu8KH0jg0MW9ZBAAaR3fkb6BYy19WBt6rQOxG0v6 horx1Je5q+z4TcHviXV5WMzpkR3/XC0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-r1mSOTKEMyGUVi0T1KRnpg-1; Tue, 20 Feb 2024 04:54:22 -0500 X-MC-Unique: r1mSOTKEMyGUVi0T1KRnpg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4CA1B185A788 for ; Tue, 20 Feb 2024 09:54:22 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0FAA82166AEF; Tue, 20 Feb 2024 09:54:21 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41K9sJZ8436054 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 20 Feb 2024 10:54:20 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41K9sJcE436053; Tue, 20 Feb 2024 10:54:19 +0100 Date: Tue, 20 Feb 2024 10:54:19 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Patrick Palka Subject: [PATCH] c++: Fix explicit instantiation of const variable templates after earlier implicit instantation [PR113976] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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_H2, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! Already previously instantiated const variable templates had cp_apply_type_quals_to_decl called when they were instantiated, but if they need runtime initialization, their TREE_READONLY flag has been subsequently cleared. Explicit variable template instantiation calls grokdeclarator which calls cp_apply_type_quals_to_decl on them again, setting TREE_READONLY flag again, but nothing clears it afterwards, so we emit such instantiations into rodata sections and segfault when the dynamic initialization attempts to initialize them. The following patch fixes that by not calling cp_apply_type_quals_to_decl on already instantiated variable declarations. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-19 Jakub Jelinek Patrick Palka PR c++/113976 * decl.cc (grokdeclarator): Don't call cp_apply_type_quals_to_decl on DECL_TEMPLATE_INSTANTIATED VAR_DECLs. * g++.dg/cpp1y/var-templ87.C: New test. Jakub --- gcc/cp/decl.cc.jj 2024-02-15 09:51:34.460065992 +0100 +++ gcc/cp/decl.cc 2024-02-19 19:18:09.839188137 +0100 @@ -15263,7 +15263,12 @@ grokdeclarator (const cp_declarator *dec /* Record constancy and volatility on the DECL itself . There's no need to do this when processing a template; we'll do this for the instantiated declaration based on the type of DECL. */ - if (!processing_template_decl) + if (!processing_template_decl + /* Don't do it for instantiated variable templates either, + cp_apply_type_quals_to_decl should have been called on it + already and might have been overridden in cp_finish_decl + if initializer needs runtime initialization. */ + && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl))) cp_apply_type_quals_to_decl (type_quals, decl); return decl; --- gcc/testsuite/g++.dg/cpp1y/var-templ87.C.jj 2024-02-19 19:21:49.668129195 +0100 +++ gcc/testsuite/g++.dg/cpp1y/var-templ87.C 2024-02-19 19:21:42.218232862 +0100 @@ -0,0 +1,43 @@ +// PR c++/113976 +// { dg-do run { target c++14 } } + +int +foo () +{ + return 42; +} + +template +const int a = foo (); +const int *b = &a <0>; +template +const int c = foo (); +template const int c <0>; +template +const int d = foo (); +const int *e = &d <0>; +template const int d <0>; +template +const int f = foo (); +template const int f <0>; +const int *g = &f <0>; +struct S { int a, b; }; +template +const S h = { 42, foo () }; +const S *i = &h <0>; +template +const S j = { 42, foo () }; +template const S j <0>; +template +const S k = { 42, foo () }; +const S *l = &k <0>; +template const S k <0>; +template +const S m = { 42, foo () }; +template const S m <0>; +const S *n = &m <0>; + +int +main () +{ +}