From patchwork Thu Jan 18 22:39:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 863198 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-471617-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="ZijYIh+r"; 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 3zMzRG4QHPz9s75 for ; Fri, 19 Jan 2018 09:39:37 +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:subject:message-id:mime-version:content-type; q=dns; s= default; b=cByxksXkhmbxAQhhxhLTF0QJirSx+mZeesVBVCn9tMPcQKu6vMesd tU9SJMNRJqv+p1zFAZZdHixdGennqiOeRB39sYRMDzzLYn5gH2Utyn1X7IypsAZE 5fNabw5DeC8wW1C20jczwIyMDy/ADrH/cRmP84B/HqfHDQ1ySNET/Y= 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:subject:message-id:mime-version:content-type; s= default; bh=LqMDd8ncIZucV5j0Wgxo4LgDXmA=; b=ZijYIh+r65wDs+Ha1o9R 6yLzx+TOENPtaOFvtXbUe+3t2R68AEGN370kYKQdUI3CGebA3YZZWYBd0izPjV9u DakTRc7mBLjll7D3JPxsWx5nZFSTuYhIYej2eGlP5Lppi32knbQYEN9OScIO7rfZ 44SuLwDuk2V1hA4DHGEeZNo= Received: (qmail 52209 invoked by alias); 18 Jan 2018 22:39:29 -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 51911 invoked by uid 89); 18 Jan 2018 22:39:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= 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; Thu, 18 Jan 2018 22:39:27 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C8D3013AA6 for ; Thu, 18 Jan 2018 22:39:26 +0000 (UTC) Received: from redhat.com (ovpn-204-33.brq.redhat.com [10.40.204.33]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1C2D55D6B4; Thu, 18 Jan 2018 22:39:25 +0000 (UTC) Date: Thu, 18 Jan 2018 23:39:19 +0100 From: Marek Polacek To: Jason Merrill , GCC Patches Subject: C++ PATCH to fix bogus error with constexpr and empty class (PR c++/81933) Message-ID: <20180118223919.GE3914@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) The problem in this PR is that we get error: constexpr call flows off the end of the function for a valid testcase, but not in C++17 mode. That's because in C++17 we execute: 4321 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo)) 4322 { 4323 tree decl = build_base_field_1 (t, basetype, next_field); 4324 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo); 4325 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node; 4326 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT); 4327 } which puts some additional fields into some classes. This makes a difference for split_nonconstant_init: 751 if (split_nonconstant_init_1 (dest, init)) 752 init = NULL_TREE; where split_nonconstant_init_1 is true so that means that the DECL_INITIAL will be null. Why is split_nonconstant_init_1 true? Because it ends up calling complete_ctor_at_level_p with 6114 return count_type_elements (type, true) == num_elts; but the count_type_elements result differs between c++14/c++17 precisely because of the fields added in build_base_field. But we should only add those fields when initializing aggregates with bases, which is not what's happening in this testcase. With DECL_INITIAL being null, we error here (result is NULL): 1702 result = *ctx->values->get (slot ? slot : res); 1703 if (result == NULL_TREE && !*non_constant_p) 1704 { 1705 if (!ctx->quiet) 1706 error ("% call flows off the end " 1707 "of the function"); 1708 *non_constant_p = true; 1709 } I think the problem is that we're dealing with an empty class here, for which we should just generate an empty ctor, as in few other spots. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-01-18 Marek Polacek PR c++/81933 * constexpr.c (cxx_eval_call_expression): When evaluating a call whose result's type is an empty class, return empty constructor. * g++.dg/cpp1y/constexpr-empty4.C: New test. Marek diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c index 8984613aa41..0ff74b98437 100644 --- gcc/cp/constexpr.c +++ gcc/cp/constexpr.c @@ -1701,7 +1701,15 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, result = void_node; else { - result = *ctx->values->get (slot ? slot : res); + tree retval = slot ? slot : res; + /* If the class is empty, we aren't actually loading anything. */ + if (is_really_empty_class (TREE_TYPE (retval))) + { + result = build_constructor (TREE_TYPE (retval), NULL); + TREE_CONSTANT (result) = true; + } + else + result = *ctx->values->get (retval); if (result == NULL_TREE && !*non_constant_p) { if (!ctx->quiet) diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-empty4.C gcc/testsuite/g++.dg/cpp1y/constexpr-empty4.C index e69de29bb2d..059220f8268 100644 --- gcc/testsuite/g++.dg/cpp1y/constexpr-empty4.C +++ gcc/testsuite/g++.dg/cpp1y/constexpr-empty4.C @@ -0,0 +1,51 @@ +// PR c++/81933 +// { dg-do compile { target c++14 } } + +namespace std { +template struct __decay_and_strip { typedef _Tp __type; }; +template struct enable_if { typedef int type; }; +template struct _Head_base { + constexpr _Head_base(_Head) {} +}; +template struct _Tuple_impl; +template +struct _Tuple_impl<_Idx, _Head, _Tail...> : _Tuple_impl<1, _Tail...>, // { dg-warning "direct base" } + _Head_base<_Head> { + typedef _Tuple_impl<1, _Tail...> _Inherited; + typedef _Head_base<_Head> _Base; + constexpr _Tuple_impl(_Head __head, _Tail... __tail) + : _Inherited(__tail...), _Base(__head) {} + _Tuple_impl(const _Tuple_impl &) = default; + _Tuple_impl(_Tuple_impl &&); +}; +template +struct _Tuple_impl<_Idx, _Head> : _Head_base<_Head> { + typedef _Head_base<_Head> _Base; + constexpr _Tuple_impl(_Head __head) : _Base(__head) {} +}; +template struct _TC { + static constexpr bool _NotSameTuple() { return true; } +}; +template class tuple : _Tuple_impl<0, _Elements...> { + typedef _Tuple_impl<0, _Elements...> _Inherited; + +public: + template ::_NotSameTuple()>::type = false> + constexpr tuple(_UElements... __elements) : _Inherited(__elements...) {} + tuple(const tuple &) = default; +}; +template +constexpr tuple::__type...> + make_tuple(_Elements... __args) { + typedef tuple::__type...> __result_type; + return __result_type(__args...); +} +} +struct any_udt {}; +template constexpr auto flatten(Tuples... tuples) { + auto all = std::make_tuple(tuples...); + auto flat(all); + return flat; +} +constexpr auto fail = flatten(any_udt{}, any_udt{});