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 CBDBB3858CDB for ; Mon, 26 Feb 2024 17:10:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBDBB3858CDB 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 CBDBB3858CDB 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=1708967435; cv=none; b=VCuBH1rE+UWZmK+BbY4fupxoL4BI/p96VbM4Qe1nuK1C3whCmbEvR05IfP8yggoqGewJ52MNYZ2DEp1ZMLzTTlC4XeSQ6iC3o2IuTA4lEnHJJ1TqDD36qLK/lJMQNuYO+nQcaJ+Kllg84jKk2ezQ1+6sYvVNLM8LxhuWoE6C3vQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708967435; c=relaxed/simple; bh=MaAuTp8mZXXfv6x2BqR+pGpHjYFfrsdU2PRxFYgk1L4=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=HxbQTGC7K+I/WERsyoWtoD6BmCGkextx54VPab9WUXF4XLDtbZpgJBCZxUHrx8FXWPhHy5NtMTYLUrBruS2dpnwOcLAOqfMHRPW816+2EBWGlPbq1vGpOgn3HgApSn8IWskzU5dgcxKuIyIXL3A9WDqKVHOg+K85U3ncLb2sSu4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708967431; 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=y9lilR1vO2nH8tdon8fGHYAXZiw41td5uiMXn3OeEf8=; b=HfE3fm08oxERbIIFLLXDxa7U7gHk/xeLpNeNjWiAr7bzqEQLR1HvF0hFq8V4ivDCkUM9v2 UkdMAYT6ut4jM0FtoWT14ZpSifhWaKiSxpQPtmlqYMVdcE2GKTxIycRDOla9JejtdRWKar O44QwlLpCDofpqOxDm7+dgU15ChnXSo= 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-400-A1ExuJMZOsen3SpM0FlG2w-1; Mon, 26 Feb 2024 12:10:29 -0500 X-MC-Unique: A1ExuJMZOsen3SpM0FlG2w-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-42e81ab63c6so12832281cf.3 for ; Mon, 26 Feb 2024 09:10:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708967429; x=1709572229; 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=y9lilR1vO2nH8tdon8fGHYAXZiw41td5uiMXn3OeEf8=; b=Z3F3ToO0BlWnmmLBhczNEm+H2ysuAuxqeO05LRwrk6uwOXIl7uSfSycsr4fc4LwQBn gMxoieG6AoVWdPgWP+CIk3jNMfKCpp6BBG9ZBuF/0/geurVACANR2U8nSrCY2/lvpV3K 0/op+LXvWyylKcDMRv36nSAXVxp3ZCv+wRNBIjkoLldThUI/cScLBhASSr989eKpJL6x +sv9H1tLM1DPEgqpxEfRCtmC76EVcZlNNWuyLVv4Z8mmHf0NzgrXde8jN1kmnz6SKMm+ F3D58/MyDfnSwXrE6NqaR1sdR5NY9aNwt54JE6nLiq7Rkg1cIqVpACFCjnbXRNisre0k tw/w== X-Forwarded-Encrypted: i=1; AJvYcCVppfW4zYOeI8OT6rUqJA1J7EkiHJgzPbr6qoMv8ZAAgHlfpio+t2gCNckcJXpUjbw1cx7eQ/gQ1+/oYftZW8dA+qmJNrxirA== X-Gm-Message-State: AOJu0YwsRnI+tjZ2xAd/gBxbhFKiwt9JluZkuf0JyaArtC0/5zMDJKM/ rEpoavtrbPa65Pr160v8gE0j9RMTJh0H2l3m4FE1c1s7W62FQAD/z1mI/L4UXmrAu7ewpMNQ5Rr 4agk9VJLzsqry2KA8KlJom1kqCske/UqhfsmvzxoYlzjB59qOK9oA1mg= X-Received: by 2002:a05:622a:1788:b0:42e:57b7:b239 with SMTP id s8-20020a05622a178800b0042e57b7b239mr10159360qtk.20.1708967428975; Mon, 26 Feb 2024 09:10:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlQGD5qsqnGyj8PQ+R+rlZZ6kNu3ANR9i2fhF6VrTrtCUWaj0vaKc5MfB/tqj+gF6cCSxglA== X-Received: by 2002:a05:622a:1788:b0:42e:57b7:b239 with SMTP id s8-20020a05622a178800b0042e57b7b239mr10159344qtk.20.1708967428706; Mon, 26 Feb 2024 09:10:28 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id df24-20020a05622a0ed800b0042e5da69d93sm2661343qtb.1.2024.02.26.09.10.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 09:10:28 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 26 Feb 2024 12:10:27 -0500 (EST) To: Jakub Jelinek cc: Jason Merrill , gcc-patches@gcc.gnu.org, Patrick Palka Subject: Re: [PATCH] c++: Fix explicit instantiation of const variable templates after earlier implicit instantation [PR113976] In-Reply-To: Message-ID: <5f8065e9-65c2-64a1-3a8d-5437c334e059@idea> References: 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=-7.7 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_H4,RCVD_IN_MSPIKE_WL,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 List-Id: On Tue, 20 Feb 2024, Jakub Jelinek wrote: > 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. LGTM, this seems like the safest approach for backporting. Note we can't check DECL_EXPLICIT_INSTANTIATION at this point because that doesn't get set until later from do_decl_instantiation. > > 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. > > --- 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 () > +{ > +} > > Jakub > >