From patchwork Wed Dec 13 22:21:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 848264 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-469157-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="h4eVoRAg"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yxrlm1pQdz9s7m for ; Thu, 14 Dec 2017 09:22:08 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=Ut1qjZQUbAAby0xypZIn8ugE3duQm pt4G9w1j4MLWuwyNz/tY+3P8CO5e9qRc0xq0sj8TNpDp07EdKqLmvJaxbX3VeNSA vawW3d2G6OSXVL5CFir4kD44qSFUmDvq8Z560yuuNoQlfPCa157Mb8/xy4U+HeZe OYgqGioq4aoLJA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=1CUCb4oTCxcw2D1BTbxD298Rzl4=; b=h4e VoRAg8q3WvKAfTHUcmOiykJZHueBEd2c72g2Z8kn4PxKL/sY81YQcUbInDoaQF9u 3JYEEv50rQ/wqrSZun5STmh06RxuFDVyQ5g+hpdmDAFKhP2Qm5jcO9ghzK+zE6o2 Pq2Usk3+D2O1z3Oj1glQubnkQmoQJGuC6vIRzqbw= Received: (qmail 18844 invoked by alias); 13 Dec 2017 22:22:02 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 18827 invoked by uid 89); 13 Dec 2017 22:22:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=cam X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Dec 2017 22:22:00 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3357D37E74; Wed, 13 Dec 2017 22:21:59 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-34.ams2.redhat.com [10.36.116.34]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C3DC3784DD; Wed, 13 Dec 2017 22:21:58 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id vBDMLtAa011011; Wed, 13 Dec 2017 23:21:55 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id vBDMLs8h011010; Wed, 13 Dec 2017 23:21:54 +0100 Date: Wed, 13 Dec 2017 23:21:54 +0100 From: Jakub Jelinek To: Jason Merrill , Nathan Sidwell Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix ICE with label difference as template non-type argument (PR c++/79650) Message-ID: <20171213222154.GN2353@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes Hi! reduced_constant_expression_p uses initializer_constant_valid_p to determine what is a valid constant expression. That accepts several cases which aren't compile time INTEGER_CST, just something that the assembler can finalize into a constant, e.g. difference of labels, difference of STRING_CSTs, something plus ADDR_EXPR of a static var etc. But for template non-type arguments my understanding is we really need an INTEGER_CST, we cam hardly instantiate on something that only during assembly will become a constant. The following patch attempts to diagnose it. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Or do you have better suggestions for the diagnostics wording? 2017-12-13 Jakub Jelinek PR c++/79650 * pt.c (convert_nontype_argument): Diagnose reduced_constant_expression_p expressions that aren't INTEGER_CST. * g++.dg/template/pr79650.C: New test. Jakub --- gcc/cp/pt.c.jj 2017-12-13 15:52:51.000000000 +0100 +++ gcc/cp/pt.c 2017-12-13 19:21:35.861825357 +0100 @@ -6577,7 +6577,19 @@ convert_nontype_argument (tree type, tre return NULL_TREE; /* else cxx_constant_value complained but gave us a real constant, so go ahead. */ - gcc_assert (TREE_CODE (expr) == INTEGER_CST); + if (TREE_CODE (expr) != INTEGER_CST) + { + /* Some assemble time constant expressions like + (intptr_t)&&lab1 - (intptr_t)&&lab2 or + 4 + (intptr_t)&&var satisfy reduced_constant_expression_p + as we can emit them into .rodata initializers of + variables, yet they can't fold into an INTEGER_CST at + compile time. Refuse them here. */ + gcc_checking_assert (reduced_constant_expression_p (expr)); + error_at (loc, "template argument %qE for type %qT not " + "a constant integer", expr, type); + return NULL_TREE; + } } else return NULL_TREE; --- gcc/testsuite/g++.dg/template/pr79650.C.jj 2017-12-13 19:29:04.549196268 +0100 +++ gcc/testsuite/g++.dg/template/pr79650.C 2017-12-13 19:34:15.202298913 +0100 @@ -0,0 +1,20 @@ +// PR c++/79650 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +typedef __INTPTR_TYPE__ intptr_t; +template struct A {}; + +void +foo () +{ + static int a, b; +lab1: +lab2: + A<(intptr_t)&&lab1 - (__INTPTR_TYPE__)&&lab2> c; // { dg-error "not a constant integer" } + A<(intptr_t)&&lab1 - (__INTPTR_TYPE__)&&lab1> d; + A<(intptr_t)&a - (intptr_t)&b> e; // { dg-error "is not a constant expression" } + A<(intptr_t)&a - (intptr_t)&a> f; + A<(intptr_t)sizeof(a) + (intptr_t)&a> g; // { dg-error "not a constant integer" } + A<(intptr_t)&a> h; // { dg-error "conversion from pointer type" } +}