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: 203522 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp293648dyc; Tue, 20 Feb 2024 01:55:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVn9XaJqSlp/mN5APjqktkFFKsf7LHo9yBF/IqbYAznOlRwqFfL24tX3BigdE2uu7rQRybO8UZOPFg54d1Jd56wlJl3cQ== X-Google-Smtp-Source: AGHT+IFQ6cgfgwSuqRh1t3Au62JyiRmBuCXfy2x3nyXCJpYS+D1gNJBwTE8tSWMTg1CuASHfs/h1 X-Received: by 2002:a05:622a:18a7:b0:42c:78fd:e4fa with SMTP id v39-20020a05622a18a700b0042c78fde4famr25563741qtc.32.1708422907935; Tue, 20 Feb 2024 01:55:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708422907; cv=pass; d=google.com; s=arc-20160816; b=H926dKVUWmxwTlmXme7+ZwzUhqPtQ1U0ww90NWI7gnv/5FSyGHaLN/LUgEYLSSdrbz ltvdsNBAMJNtG1OrL/bmRgQd5dOd4pYcdCQenmtAAcy5DIjfqCk8xn3RjvqaRETz2r8K Ary2mLa0sC416WF/JcaUCsZ7dV9TPIl53Esaq9qZKRMibM1zkqIWVUSPaHRP93zbhDZD xKUAm9e34PazS4fAfwjGL2NY09ZUtr+0Na3wFrBv3PyvanG514WaDDH6SUiFwCHvTuzN pfdYlk+Yi9MsFmyn5gbdjcN0vYaInizEBE9iKEnsnBoDpEPiran8xMeDiKV1GZ2HgdIk 6lwg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=JPMf3OW/xBObQUdpGMu8mQphVt+kqfv0V/ror+AnhE4=; fh=lVm2NyZN/MZSVbe6vWFZEpN3aGq/JctUZ2qgijG+iGg=; b=DwpBbyOjHd9nbfxcQKvQkx1J8S9zAxAJsRmZQfqUiBB6yX7XLlrB2gEqsdknWwaEtm /Z2yeX1IOj75L9XegztGEceAwHyvNoR5EsLNnPTplQ+09VstperGRqo2SdRIgxXvctch 5gAk5174Dq3NWf0vhdvctMaabN1oF8lyLCTpQ5vUAEsJ5MgywFA+uO+pDbHCI3VOzZ6A n/aX/fcrfABRkIPoLiNNnbKXGce2e3ckO4XoRyG9pa6kZJzFHIAE0DrBRRDnbAVZdF0g Ud9M7ren2r3xL7Nt2QRkqI09+VP+b9t1tYRKbWgAC1JrjepX23fTQAmPStcMmeeRgtu+ RCWg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UKL5wEuN; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r12-20020ac8520c000000b0042c79898854si7743965qtn.730.2024.02.20.01.55.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 01:55:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UKL5wEuN; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9370B3858D35 for ; Tue, 20 Feb 2024 09:55:07 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791411259191132621 X-GMAIL-MSGID: 1791411259191132621 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 () +{ +}