From patchwork Sat Jul 1 03:28:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 114941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10805679vqr; Fri, 30 Jun 2023 20:30:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlFRzPPGfA29WsOS415AaDxTrl/q5Hb/cLrs6pRQDPw4QwOI6rcdMwQmYFq6wUnti35oA4e8 X-Received: by 2002:a17:906:f20c:b0:982:c8d0:683f with SMTP id gt12-20020a170906f20c00b00982c8d0683fmr2822772ejb.18.1688182204966; Fri, 30 Jun 2023 20:30:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688182204; cv=none; d=google.com; s=arc-20160816; b=XO7ytlD0Z0Cfoxre5yq3yebQdsbDoQAWuWaQs+y+3EVabuM5mR9ZVe3P4pvMVa9ypC GORX31Zpr/Ilj9oAynIXL5mlfumwjxG7TvMWDmZ5NNTPrLyVh8RuqrhbH2fSkPKOcIaq gF1bB4UN9g+6N9RBWW3U4cjOmw3XYGVLhwd7r8rj8UuFKRoUQAAK1k4Qp20E0t1QE756 evdXhDfZ7RDEIf1xFaf3zrIrkc+lEZYeG6B7Esdd0lXrueLy3JZqT7V6MnLqJvPnoBlE H1jcUY/SR6nz5Bd6Q73jZI03m6Gv5lytcgvwU+8aF07OQvk9AR0w4Z+IM6JTZ6QYUgft htaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=LciYhUEFrOCO7E4n2M1q0X32IdcoB5QF5oW34vNvQj4=; fh=rniJ05hh5TtJEgvFF322qtTpFQcCvp9aWk5OC4T+KQA=; b=aN5uhsGrIeunG+ooTjyncl2AnE5PD9X9+1p26ypAPiQSVEhSen/8UGCzDAD+wRGNoG CRiHR2EbAeplUraqU1JRD3ULU0TzvUUKtJMXpBtWMku9ijugviwODGpHtDpq1NCjxaSU K0PCvLMenqjYddwqe9dpBop1pwHhCYf6bEgcGWL88b42rT5ZCS7Cov7tMwY9J/aNYncB YLZsNcPl/z7uhOhhb4KFbTIrZgjBZgIdQfYV5OvMh3iwk542m6nPiWf0XelmqDwRKfIw TVznNFtRsU7+EnFMsdtkSrsslSvGxb3OApnKgHYiPqQQdbjvgPXU0X6j066nAYq6+jDs dX7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Hz3E4Y2s; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q16-20020a170906361000b009889b296070si8478303ejb.261.2023.06.30.20.30.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:30:04 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=Hz3E4Y2s; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C9E23858D38 for ; Sat, 1 Jul 2023 03:30:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C9E23858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688182203; bh=LciYhUEFrOCO7E4n2M1q0X32IdcoB5QF5oW34vNvQj4=; h=Date:To:Cc:Subject:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Hz3E4Y2sHTdqaBt9y8dFuCSDDhYWN5+vrWicEw7S2ysXq3oD1zu92a+sv2Z5oVRPi K4o5q0TCwau+u/o4+zZ0OH4EfefaLkLS0GJAE8fpZnv/NIfpdah2lK2wlDg4rn4qqH 1gHAx5shEql7ZXSpRJNkSHH4oukGTq5hPT5s2EbA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 852FA3858D33 for ; Sat, 1 Jul 2023 03:28:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 852FA3858D33 Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6b5ef64bca6so2252026a34.3 for ; Fri, 30 Jun 2023 20:28:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688182124; x=1690774124; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LciYhUEFrOCO7E4n2M1q0X32IdcoB5QF5oW34vNvQj4=; b=XEs2keiYGY8KPxG4llTwLA7dvnLDMOEvgcklV13u6ifGKb+zOZQU58fj4tA7Sj8WJV qeRit7QgLAQOdm5Zw3A3R3xEnqioJ+XFd0pNBI+wmcH8FoECCn4C4CeTwgiGRGO1tOfP QKLgXyOs9er44R68K/aAEbXK5gHErMF5xxZk0XARrjiQlpIyRueZQkCQdHd4enakIkZH feNfmAdbeEVvEot93/mXJk0e45edlXu0cW+IDB8FboOtTZLoyJHroiBq+ws2JYjDQy+2 L4uC8PdxT0HBTlTK5kkGTNg1yBD1VI3+hQjn9pcOwidQcOFXaW1zOJBmJ86CEg45OByr oFsw== X-Gm-Message-State: AC+VfDxZhNTYNkG2qRn3Z1LgkdYSg8CcpDpLBAIoJJAgoVIiB12zdPH4 xEwaxjtmC86+zoqZDiNm52TwhV2h0zk= X-Received: by 2002:a9d:6844:0:b0:6b7:43eb:c1a with SMTP id c4-20020a9d6844000000b006b743eb0c1amr4743173oto.36.1688182124468; Fri, 30 Jun 2023 20:28:44 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id me14-20020a17090b17ce00b002630bfd35b0sm7246618pjb.7.2023.06.30.20.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:28:44 -0700 (PDT) Date: Sat, 1 Jul 2023 13:28:34 +1000 To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [PATCH v3 1/3] c++: Track lifetimes in constant evaluation [PR70331,PR96630,PR98675] Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770187343716328171?= X-GMAIL-MSGID: =?utf-8?q?1770187343716328171?= This adds rudimentary lifetime tracking in C++ constexpr contexts, allowing the compiler to report errors with using values after their backing has gone out of scope. We don't yet handle other ways of accessing values outside their lifetime (e.g. following explicit destructor calls). PR c++/96630 PR c++/98675 PR c++/70331 gcc/cp/ChangeLog: * constexpr.cc (constexpr_global_ctx::remove_value): Mark value as outside lifetime. (find_expired_values): New function. (outside_lifetime_error): New function. (cxx_eval_call_expression): Don't cache calls that return references to values outside their lifetime. (cxx_eval_constant_expression): Add checks for out-of-lifetime values. Forget local variables at end of bind expressions, and temporaries after cleanup points. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lifetime1.C: New test. * g++.dg/cpp1y/constexpr-lifetime2.C: New test. * g++.dg/cpp1y/constexpr-lifetime3.C: New test. * g++.dg/cpp1y/constexpr-lifetime4.C: New test. * g++.dg/cpp1y/constexpr-lifetime5.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/constexpr.cc | 112 ++++++++++++++---- .../g++.dg/cpp1y/constexpr-lifetime1.C | 13 ++ .../g++.dg/cpp1y/constexpr-lifetime2.C | 20 ++++ .../g++.dg/cpp1y/constexpr-lifetime3.C | 13 ++ .../g++.dg/cpp1y/constexpr-lifetime4.C | 11 ++ .../g++.dg/cpp1y/constexpr-lifetime5.C | 11 ++ 6 files changed, 160 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index cca0435bafc..bc59b4aab67 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1165,6 +1165,8 @@ public: hash_set *modifiable; /* Number of heap VAR_DECL deallocations. */ unsigned heap_dealloc_count; + /* Values that are not within their lifetime. */ + hash_set outside_lifetime; /* Constructor. */ constexpr_global_ctx () : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr), @@ -1188,7 +1190,12 @@ public: if (!already_in_map && modifiable) modifiable->add (t); } - void remove_value (tree t) { values.remove (t); } + void remove_value (tree t) + { + if (DECL_P (t)) + outside_lifetime.add (t); + values.remove (t); + } }; /* Helper class for constexpr_global_ctx. In some cases we want to avoid @@ -2509,6 +2516,22 @@ cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, return cp_build_addr_expr (obj, complain); } +/* Look for expired values in the expression *TP, called through + cp_walk_tree. DATA is ctx->global->outside_lifetime. */ + +static tree +find_expired_values (tree *tp, int *walk_subtrees, void *data) +{ + hash_set *outside_lifetime = (hash_set *) data; + + if (TYPE_P (*tp)) + *walk_subtrees = 0; + else if (outside_lifetime->contains (*tp)) + return *tp; + + return NULL_TREE; +} + /* Data structure used by replace_decl and replace_decl_r. */ struct replace_decl_data @@ -3160,10 +3183,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, for (tree save_expr : save_exprs) ctx->global->remove_value (save_expr); - /* Remove the parms/result from the values map. Is it worth - bothering to do this when the map itself is only live for - one constexpr evaluation? If so, maybe also clear out - other vars from call, maybe in BIND_EXPR handling? */ + /* Remove the parms/result from the values map. */ ctx->global->remove_value (res); for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) ctx->global->remove_value (parm); @@ -3210,13 +3230,20 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, cacheable = false; } - /* Rewrite all occurrences of the function's RESULT_DECL with the - current object under construction. */ - if (!*non_constant_p && ctx->object - && CLASS_TYPE_P (TREE_TYPE (res)) - && !is_empty_class (TREE_TYPE (res))) - if (replace_decl (&result, res, ctx->object)) - cacheable = false; + /* Also don't cache a call if we return a pointer to an expired + value. */ + if (cacheable && (cp_walk_tree_without_duplicates + (&result, find_expired_values, + &ctx->global->outside_lifetime))) + cacheable = false; + + /* Rewrite all occurrences of the function's RESULT_DECL with the + current object under construction. */ + if (!*non_constant_p && ctx->object + && CLASS_TYPE_P (TREE_TYPE (res)) + && !is_empty_class (TREE_TYPE (res))) + if (replace_decl (&result, res, ctx->object)) + cacheable = false; } else /* Couldn't get a function copy to evaluate. */ @@ -5687,6 +5714,25 @@ cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, return r; } +/* Complain about R, a DECL that is accessed outside its lifetime. */ + +static void +outside_lifetime_error (location_t loc, tree r) +{ + if (DECL_NAME (r) == heap_deleted_identifier) + { + /* Provide a more accurate message for deleted variables. */ + error_at (loc, "use of allocated storage after deallocation " + "in a constant expression"); + inform (DECL_SOURCE_LOCATION (r), "allocated here"); + } + else + { + error_at (loc, "accessing object outside its lifetime"); + inform (DECL_SOURCE_LOCATION (r), "declared here"); + } +} + /* Complain about R, a VAR_DECL, not being usable in a constant expression. FUNDEF_P is true if we're checking a constexpr function body. Shared between potential_constant_expression and @@ -7054,6 +7100,13 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, r = build_constructor (TREE_TYPE (t), NULL); TREE_CONSTANT (r) = true; } + else if (ctx->global->outside_lifetime.contains (t)) + { + if (!ctx->quiet) + outside_lifetime_error (loc, t); + *non_constant_p = true; + break; + } else if (ctx->strict) r = decl_really_constant_value (t, /*unshare_p=*/false); else @@ -7085,7 +7138,7 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, case PARM_DECL: if (lval && !TYPE_REF_P (TREE_TYPE (t))) /* glvalue use. */; - else if (tree v = ctx->global->get_value (r)) + else if (tree v = ctx->global->get_value (t)) r = v; else if (lval) /* Defer in case this is only used for its type. */; @@ -7099,7 +7152,12 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, else { if (!ctx->quiet) - error ("%qE is not a constant expression", t); + { + if (ctx->global->outside_lifetime.contains (t)) + outside_lifetime_error (loc, t); + else + error_at (loc, "%qE is not a constant expression", t); + } *non_constant_p = true; } break; @@ -7328,17 +7386,28 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, auto_vec cleanups; vec *prev_cleanups = ctx->global->cleanups; ctx->global->cleanups = &cleanups; - r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), + + auto_vec save_exprs; + constexpr_ctx new_ctx = *ctx; + new_ctx.save_exprs = &save_exprs; + + r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0), lval, non_constant_p, overflow_p, jump_target); + ctx->global->cleanups = prev_cleanups; unsigned int i; tree cleanup; /* Evaluate the cleanups. */ FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) - cxx_eval_constant_expression (ctx, cleanup, vc_discard, + cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard, non_constant_p, overflow_p); + + /* Forget SAVE_EXPRs and TARGET_EXPRs created by this + full-expression. */ + for (tree save_expr : save_exprs) + ctx->global->remove_value (save_expr); } break; @@ -7855,10 +7924,13 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p, jump_target); case BIND_EXPR: - return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), - lval, - non_constant_p, overflow_p, - jump_target); + r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), + lval, + non_constant_p, overflow_p, + jump_target); + for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) + ctx->global->remove_value (decl); + return r; case PREINCREMENT_EXPR: case POSTINCREMENT_EXPR: diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C new file mode 100644 index 00000000000..43aa7c974c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C @@ -0,0 +1,13 @@ +// PR c++/96630 +// { dg-do compile { target c++14 } } + +struct S { + int x = 0; + constexpr const int& get() const { return x; } +}; + +constexpr const int& test() { + auto local = S{}; // { dg-message "note: declared here" } + return local.get(); +} +constexpr int x = test(); // { dg-error "accessing object outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C new file mode 100644 index 00000000000..22cd919fcda --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C @@ -0,0 +1,20 @@ +// PR c++/98675 +// { dg-do compile { target c++14 } } + +struct S { + int x = 0; + constexpr const int& get() const { return x; } +}; + +constexpr int error() { + const auto& local = S{}.get(); // { dg-message "note: declared here" } + return local; +} +constexpr int x = error(); // { dg-error "accessing object outside its lifetime" } + +constexpr int ok() { + // temporary should only be destroyed after end of full-expression + auto local = S{}.get(); + return local; +} +constexpr int y = ok(); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C new file mode 100644 index 00000000000..6329f8cf6c6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C @@ -0,0 +1,13 @@ +// PR c++/70331 +// { dg-do compile { target c++14 } } + +constexpr int f(int i) { + int *p = &i; + if (i == 0) { + int j = 123; // { dg-message "note: declared here" } + p = &j; + } + return *p; +} + +constexpr int i = f(0); // { dg-error "accessing object outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C new file mode 100644 index 00000000000..181a1201663 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C @@ -0,0 +1,11 @@ +// { dg-do compile { target c++14 } } + +constexpr const double& test() { + const double& local = 3.0; // { dg-message "note: declared here" } + return local; +} + +static_assert(test() == 3.0, ""); // { dg-error "constant|accessing object outside its lifetime" } + +// no deference, shouldn't error +static_assert((test(), true), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C new file mode 100644 index 00000000000..a4bc71d890a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C @@ -0,0 +1,11 @@ +// { dg-do compile { target c++14 } } +// { dg-options "-Wno-return-local-addr" } + +constexpr const int& id(int x) { return x; } + +constexpr bool test() { + const int& y = id(3); + return y == 3; +} + +constexpr bool x = test(); // { dg-error "" } From patchwork Sat Jul 1 03:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 114942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10806018vqr; Fri, 30 Jun 2023 20:31:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpgZ78PdhZgooJ+fgROQJw/KVIvldRHzl5pEEbFErWwvkPrc3uL+gCxuew6mwFS3KFE7r+ X-Received: by 2002:a05:6402:12da:b0:51d:9e0c:1396 with SMTP id k26-20020a05640212da00b0051d9e0c1396mr2620985edx.35.1688182261708; Fri, 30 Jun 2023 20:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688182261; cv=none; d=google.com; s=arc-20160816; b=sIMy24JI/opUoFlkBs+88d432+O6ccSd+c5ACalznydqjBS0i/RKh0DEDtRyqHDuon 1BHidzc5/z6NOFJBXnfp9WaWSCT0iPhm5M5vnDdookDUyIjkvihPilN5Akdsdtgsdmzx LIPQTSmmUiY76r+SYro+4XFCEq7kf7Sdxl75YSjx+F0/IPJfKK+MTn8cwfzOnIl88RnE /1ZP90KuJpMUljktBLE9Skcv6eyUgWql+7mIQRrPxYlC/AL6sab4xFl+bngwQzqwZZXb qwMkFDDgh+Ry4nty3nMB8cyfelvzQ3c2Zriy09kM46ADQ1IJGRvH8SC+6ye9ydDr24uN zlew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; fh=rniJ05hh5TtJEgvFF322qtTpFQcCvp9aWk5OC4T+KQA=; b=KbtHWB4gfXpjIvKopcnbppUDkVRbQ2+TqhkSDFvrgU+vOru6y6+u5kMy5ljsc/6Epd iYH8HjRBaJA9YAKIu39PgZmej5ETxLE9ZAt3AUGsQQwjMuPIoa9508aoeZB+V1XmU873 d6afZ29Z329LfP4LywZtXy8d9JMrFCCfbixN+vYDkK/FC0A+L2a6SiRO9I322CrDEET3 gstBUlsXKhzg1Kvxm9l1/3h61QUOVmO6Y9ANSmezGVCFGF+a28bqH8RZHOmp78/zLz64 K2TOUD1mlgQ54ijgL3v7i81arrDwHCfjbCY8cWTmt26Uz17yDv6Nu3Yu2F73h8ZmMHoP d0xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="dOb/uTr3"; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n21-20020aa7d055000000b0051a6077e2cesi8581626edo.535.2023.06.30.20.31.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:31:01 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b="dOb/uTr3"; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B9283858425 for ; Sat, 1 Jul 2023 03:31:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B9283858425 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688182260; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; h=Date:To:Cc:Subject:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dOb/uTr3QXpFibf/hFaWDqvMs79NVy/94d58Lp4ORpwVwEyAKz4T9sldAn0Tl9/cP /8JbtPA7NIU/wOIcC9joZMnsfddRVUaIe7U+d9DA1iuFcVwWgJUA5jalxaOaDK3WtG heA3ylAd8KOAq4aHV+6EcoqfcWrusXAv9EXLIfzg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id D06013858C50 for ; Sat, 1 Jul 2023 03:30:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D06013858C50 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1b078b34df5so2456963fac.2 for ; Fri, 30 Jun 2023 20:30:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688182211; x=1690774211; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MQbPoQDj5Jsz0Ohf/GwNlhq286NS1+cBG1sXUYyz6k0=; b=iJfmRIv0CyoWoN2/WLNxErlImdzCOjAvwD91EvPSd7njB1vGBP1dUquNsE6hBm8SVA etnuHVHPrinkYeJ7nd/ZiZZc2l/lHr6maOS2cQLuOyDMzGPcRrF82/LRKrV3ZGhAkahW YkRzoN2aAENK1GkRcvFs3tOksJBXleuZxUGY6OvmybY6VPgrKzV84H3za3tcAeKQhUFQ qurOcaFM2FSgJJkeV7t4uXYkk9EyvJtZ+HSWyrc6/dxXHqzQR9LIctpJAhqn4/5ZDwWK f/fFUSwU+N3JLM96ZJYF/bRq6hjcO9F3J3yAxm28Q614dKfJWt4Ea5VINJYRkxWPn0Pi BsLQ== X-Gm-Message-State: ABy/qLZu94dprF+Jg8RiWYBieOOcM4Pmx69LmsnOxAh7d1cVrPAKRU7E INg30Bri0ne7rlhzkBZkBixXAw5lZr4= X-Received: by 2002:a05:6870:96a0:b0:19f:9495:95ff with SMTP id o32-20020a05687096a000b0019f949595ffmr6333063oaq.27.1688182210670; Fri, 30 Jun 2023 20:30:10 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id 18-20020a17090a035200b00262fc689d50sm9025696pjf.4.2023.06.30.20.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:30:10 -0700 (PDT) Date: Sat, 1 Jul 2023 13:30:00 +1000 To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [PATCH v3 2/3] c++: Improve constexpr error for dangling local variables Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770187403078809178?= X-GMAIL-MSGID: =?utf-8?q?1770187403078809178?= Currently, when typeck discovers that a return statement will refer to a local variable it rewrites to return a null pointer. This causes the error messages for using the return value in a constant expression to be unhelpful, especially for reference return values. This patch removes this "optimisation". Relying on this raises a warning by default and causes UB anyway, so there should be no issue in doing so. We also suppress additional warnings from later passes that detect this as a dangling pointer, since we've already indicated this anyway. gcc/cp/ChangeLog: * semantics.cc (finish_return_stmt): Suppress dangling pointer reporting on return statement if already reported. * typeck.cc (check_return_expr): Don't set return expression to zero for dangling addresses. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lifetime5.C: Test reported message is correct. * g++.dg/warn/Wreturn-local-addr-6.C: Remove check for return value optimisation. Signed-off-by: Nathaniel Shead --- gcc/cp/semantics.cc | 5 ++++- gcc/cp/typeck.cc | 5 +++-- gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C | 3 --- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 8fb47fd179e..107407de513 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1260,7 +1260,10 @@ finish_return_stmt (tree expr) r = build_stmt (input_location, RETURN_EXPR, expr); if (no_warning) - suppress_warning (r, OPT_Wreturn_type); + { + suppress_warning (r, OPT_Wreturn_type); + suppress_warning (r, OPT_Wdangling_pointer_); + } r = maybe_cleanup_point_expr_void (r); r = add_stmt (r); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 859b133a18d..47233b3b717 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11273,8 +11273,9 @@ check_return_expr (tree retval, bool *no_warning) else if (!processing_template_decl && maybe_warn_about_returning_address_of_local (retval, loc) && INDIRECT_TYPE_P (valtype)) - retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, - build_zero_cst (TREE_TYPE (retval))); + /* Suppress the Wdangling-pointer warning in the return statement + that would otherwise occur. */ + *no_warning = true; } /* A naive attempt to reduce the number of -Wdangling-reference false diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C index a4bc71d890a..ad3ef579f63 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C @@ -1,11 +1,11 @@ // { dg-do compile { target c++14 } } // { dg-options "-Wno-return-local-addr" } -constexpr const int& id(int x) { return x; } +constexpr const int& id(int x) { return x; } // { dg-message "note: declared here" } constexpr bool test() { const int& y = id(3); return y == 3; } -constexpr bool x = test(); // { dg-error "" } +constexpr bool x = test(); // { dg-error "accessing object outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C index fae8b7e766f..ec8e241d83e 100644 --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C @@ -24,6 +24,3 @@ return_addr_local_as_intref (void) return (const intptr_t&)a; // { dg-warning "\\\[-Wreturn-local-addr]" } */ } - -/* Verify that the return value has been replaced with zero: - { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */ From patchwork Sat Jul 1 03:30:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 114943 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10806302vqr; Fri, 30 Jun 2023 20:31:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ62W1H8jOMY7NYS1ZCkMgow4Injk1hqQEQrAwxm/BlH8UEQU8gertjIAe2uKV9uefZZhzau X-Received: by 2002:a17:907:3f21:b0:98d:abd4:4000 with SMTP id hq33-20020a1709073f2100b0098dabd44000mr6841994ejc.35.1688182309473; Fri, 30 Jun 2023 20:31:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688182309; cv=none; d=google.com; s=arc-20160816; b=g8NxzF6ve0y8w9gkZ3Vp5rHB89uKGgrl3Qsbw9VOxe2IPdlvvDgnkOS2XNymUlHk23 AzE+BfNCDGR85qOniHptvWk3kve3w632Mbh/OkwthWHV3pQaQEFrXTv3Q0JPlsRQl0bI gIm1ouny5VO3aX6XQS7Xh6KydrVgjhbZWof+10LOd9c2guBEdlDe3PO8fz2GEAOlwtXM Qrb7cQ0TAHN8URd50G5xz/RU9jK8GQy15IaYLKqKP8J8NY5rCVgKpwLug/VZpVzEII2b kB2VT3daLCxvPMQkeyzpCAJQd/CL6RTSzLTftmvBiFV3rfe4yn8ACGNzg6aoBSw9kzXH TeSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=gDjt+2WP52RYC+KNuLCDkjmk+bdjVmyLZWiU7stdswI=; fh=rniJ05hh5TtJEgvFF322qtTpFQcCvp9aWk5OC4T+KQA=; b=vTbLjW/RvnS5r38MwH/jhj6Sr1JDMAg0exdxcKVFZ78y7SzpjQvxlAQUZufKNV3Hry mfAbv31hbHY28PELq9IPEe4GisP//IShgmYQeMwcHPmeVAqDFwsgAh8oNeP2Nqw300CE z37Vgr88dTx54EjcwU7b+GiOwnoUAFRCuyuvx+Tnl0/jF1KBtfjFlAv7XEg8cEfu7YB7 x+WLI1oxU6zCT8/4jI1zQI/YzcoRbe/HImBiRQD7gOVGpXm5VVqGOh5P1dsl1Vo9Bgr9 xsWDccLTblGl8VpD1T2jXJQ0Uyzb4feA25MJzqk8xSvzy06kAg65Oddw9VxqW4F5XpJV Oq8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="TrDHdC7/"; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n13-20020a170906378d00b00992f45c9350si1165743ejc.1018.2023.06.30.20.31.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:31:49 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b="TrDHdC7/"; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7456385842A for ; Sat, 1 Jul 2023 03:31:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7456385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688182307; bh=gDjt+2WP52RYC+KNuLCDkjmk+bdjVmyLZWiU7stdswI=; h=Date:To:Cc:Subject:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=TrDHdC7//jo5oSQB1p1BrFuHYF/hnKx3s8zZi5HN5iS2Pv7d5FohKBIOk8UZ6wOQN LISTUmY5oN7+wmNtmVEhC4yEppAUYyTAicblg0M11gtJjNDAAYukHoOGTvzrPyRTf5 7Pyt4H8Py3Eun5rI1hZHb1V+13DRPEHY9CHXJyoU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc2c.google.com (mail-oo1-xc2c.google.com [IPv6:2607:f8b0:4864:20::c2c]) by sourceware.org (Postfix) with ESMTPS id 977873858426 for ; Sat, 1 Jul 2023 03:30:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 977873858426 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-56344354e2cso1804400eaf.1 for ; Fri, 30 Jun 2023 20:30:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688182248; x=1690774248; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gDjt+2WP52RYC+KNuLCDkjmk+bdjVmyLZWiU7stdswI=; b=Zt5ArJDeNmR8sX/rUIc+td8802caEuZShMnNe9budOpMCeiuGbSlujRqTYpky+Y3PE rNuKWNWoTXQWfZhKSa0sHmcnvLM5NigvKkU9QLXAr1LGJTg10+wuOPgUwadLk+YLTZEz 21wc/jViPFlivieThcRbSqNJRN0EDGHCGIhbiz7ZzJwRjSAvzWGKo/SL2NHPpPNd2EH1 n6kLhmkxIifJZqVB2Qusgm7wkyXYvq0xJmAaUdfiDLOigRiXpS+gcLOcZN3wbEftzCVZ J0AiK+GapfWZvGc6qSJF2pcoM5kQf4YgsiPZ39a4N4vxrqyUTAbx5Cg8zxCTJKadYjiz PuXg== X-Gm-Message-State: ABy/qLbCsI9EQ1iWEATpQe72kljWloW4kpJZNM0eeZ6VuJANPRl4shiA F9DMCM1nnVG6WRCsgypLSwcbINdM8I8= X-Received: by 2002:a05:6358:cb1c:b0:134:c739:75f7 with SMTP id gr28-20020a056358cb1c00b00134c73975f7mr4277417rwb.12.1688182247916; Fri, 30 Jun 2023 20:30:47 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id g11-20020a170902740b00b001ae5cc15655sm11388516pll.5.2023.06.30.20.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 20:30:47 -0700 (PDT) Date: Sat, 1 Jul 2023 13:30:37 +1000 To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [PATCH v3 3/3] c++: Improve location information in constant evaluation Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770187453349039620?= X-GMAIL-MSGID: =?utf-8?q?1770187453349039620?= This patch updates 'input_location' during constant evaluation to ensure that errors in subexpressions that lack location information still provide accurate diagnostics. By itself this change causes some small regressions in diagnostic quality for circumstances where errors used 'input_location' but the location of the parent subexpression doesn't make sense, so this patch also includes a couple of other small diagnostic improvements to improve the most egregious cases. gcc/cp/ChangeLog: * constexpr.cc (modifying_const_object_error): Find the source location of the const object's declaration. (cxx_eval_store_expression): Fall back to the location of the target object when evaluating initialiser. (cxx_eval_constant_expression): Update input_location to the location of the currently evaluated expression. libstdc++-v3/ChangeLog: * testsuite/25_algorithms/equal/constexpr_neg.cc: Update diagnostic locations. * testsuite/26_numerics/gcd/105844.cc: Likewise. * testsuite/26_numerics/lcm/105844.cc: Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-48089.C: Update diagnostic locations. * g++.dg/cpp0x/constexpr-70323.C: Likewise. * g++.dg/cpp0x/constexpr-70323a.C: Likewise. * g++.dg/cpp0x/constexpr-delete2.C: Likewise. * g++.dg/cpp0x/constexpr-diag3.C: Likewise. * g++.dg/cpp0x/constexpr-ice20.C: Likewise. * g++.dg/cpp0x/constexpr-recursion.C: Likewise. * g++.dg/cpp0x/overflow1.C: Likewise. * g++.dg/cpp1y/constexpr-89285.C: Likewise. * g++.dg/cpp1y/constexpr-89481.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime1.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime2.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime3.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime4.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime5.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const14.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const16.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const18.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const19.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const21.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const22.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const3.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const4.C: Likewise. * g++.dg/cpp1y/constexpr-tracking-const7.C: Likewise. * g++.dg/cpp1y/constexpr-union5.C: Likewise. * g++.dg/cpp1y/pr68180.C: Likewise. * g++.dg/cpp1z/constexpr-lambda6.C: Likewise. * g++.dg/cpp1z/constexpr-lambda8.C: Likewise. * g++.dg/cpp2a/bit-cast11.C: Likewise. * g++.dg/cpp2a/bit-cast12.C: Likewise. * g++.dg/cpp2a/bit-cast14.C: Likewise. * g++.dg/cpp2a/constexpr-98122.C: Likewise. * g++.dg/cpp2a/constexpr-dynamic17.C: Likewise. * g++.dg/cpp2a/constexpr-init1.C: Likewise. * g++.dg/cpp2a/constexpr-new12.C: Likewise. * g++.dg/cpp2a/constexpr-new3.C: Likewise. * g++.dg/cpp2a/constinit10.C: Likewise. * g++.dg/cpp2a/is-corresponding-member4.C: Likewise. * g++.dg/ext/constexpr-vla2.C: Likewise. * g++.dg/ext/constexpr-vla3.C: Likewise. * g++.dg/ubsan/pr63956.C: Likewise. Signed-off-by: Nathaniel Shead --- gcc/cp/constexpr.cc | 46 ++++++++++++++++++- gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C | 10 ++-- gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C | 8 ++-- gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C | 8 ++-- .../g++.dg/cpp0x/constexpr-delete2.C | 5 +- gcc/testsuite/g++.dg/cpp0x/constexpr-diag3.C | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-ice20.C | 1 + .../g++.dg/cpp0x/constexpr-recursion.C | 6 +-- gcc/testsuite/g++.dg/cpp0x/overflow1.C | 2 +- gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C | 5 +- gcc/testsuite/g++.dg/cpp1y/constexpr-89481.C | 3 +- .../g++.dg/cpp1y/constexpr-lifetime1.C | 1 + .../g++.dg/cpp1y/constexpr-lifetime2.C | 4 +- .../g++.dg/cpp1y/constexpr-lifetime3.C | 4 +- .../g++.dg/cpp1y/constexpr-lifetime4.C | 2 +- .../g++.dg/cpp1y/constexpr-lifetime5.C | 4 +- .../g++.dg/cpp1y/constexpr-tracking-const14.C | 3 +- .../g++.dg/cpp1y/constexpr-tracking-const16.C | 3 +- .../g++.dg/cpp1y/constexpr-tracking-const18.C | 4 +- .../g++.dg/cpp1y/constexpr-tracking-const19.C | 4 +- .../g++.dg/cpp1y/constexpr-tracking-const21.C | 4 +- .../g++.dg/cpp1y/constexpr-tracking-const22.C | 4 +- .../g++.dg/cpp1y/constexpr-tracking-const3.C | 3 +- .../g++.dg/cpp1y/constexpr-tracking-const4.C | 3 +- .../g++.dg/cpp1y/constexpr-tracking-const7.C | 3 +- gcc/testsuite/g++.dg/cpp1y/constexpr-union5.C | 4 +- gcc/testsuite/g++.dg/cpp1y/pr68180.C | 4 +- .../g++.dg/cpp1z/constexpr-lambda6.C | 4 +- .../g++.dg/cpp1z/constexpr-lambda8.C | 5 +- gcc/testsuite/g++.dg/cpp2a/bit-cast11.C | 10 ++-- gcc/testsuite/g++.dg/cpp2a/bit-cast12.C | 10 ++-- gcc/testsuite/g++.dg/cpp2a/bit-cast14.C | 14 +++--- gcc/testsuite/g++.dg/cpp2a/constexpr-98122.C | 4 +- .../g++.dg/cpp2a/constexpr-dynamic17.C | 5 +- gcc/testsuite/g++.dg/cpp2a/constexpr-init1.C | 5 +- gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C | 6 +-- gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C | 10 ++-- gcc/testsuite/g++.dg/cpp2a/constinit10.C | 5 +- .../g++.dg/cpp2a/is-corresponding-member4.C | 4 +- gcc/testsuite/g++.dg/ext/constexpr-vla2.C | 4 +- gcc/testsuite/g++.dg/ext/constexpr-vla3.C | 4 +- gcc/testsuite/g++.dg/ubsan/pr63956.C | 23 +++++----- .../25_algorithms/equal/constexpr_neg.cc | 7 +-- .../testsuite/26_numerics/gcd/105844.cc | 10 ++-- .../testsuite/26_numerics/lcm/105844.cc | 14 +++--- 45 files changed, 170 insertions(+), 124 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index bc59b4aab67..0b8697b4a85 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -2165,7 +2165,33 @@ modifying_const_object_error (tree expr, tree obj) auto_diagnostic_group d; error_at (loc, "modifying a const object %qE is not allowed in " "a constant expression", TREE_OPERAND (expr, 0)); - inform (location_of (obj), "originally declared % here"); + + /* Find the underlying object that was declared as const. */ + location_t decl_loc = UNKNOWN_LOCATION; + for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; ) + switch (TREE_CODE (probe)) + { + case BIT_FIELD_REF: + case COMPONENT_REF: + { + tree elt = TREE_OPERAND (probe, 1); + if (CP_TYPE_CONST_P (TREE_TYPE (elt))) + decl_loc = DECL_SOURCE_LOCATION (elt); + probe = TREE_OPERAND (probe, 0); + } + break; + + case ARRAY_REF: + case REALPART_EXPR: + case IMAGPART_EXPR: + probe = TREE_OPERAND (probe, 0); + break; + + default: + decl_loc = location_of (probe); + break; + } + inform (decl_loc, "originally declared % here"); } /* Return true if FNDECL is a replaceable global allocation function that @@ -6286,6 +6312,21 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, if (TREE_CODE (init) == TARGET_EXPR) if (tree tinit = TARGET_EXPR_INITIAL (init)) init = tinit; + + /* Improve error messages for initialisers when the initialising + expression has no location information by pointing to the decl + that is getting initialised. */ + location_t target_loc = cp_expr_loc_or_input_loc (init); + if (!EXPR_HAS_LOCATION (init)) + { + if (DECL_P (target)) + target_loc = DECL_SOURCE_LOCATION (target); + else if (TREE_CODE (target) == COMPONENT_REF + || TREE_CODE (target) == BIT_FIELD_REF) + target_loc = DECL_SOURCE_LOCATION (TREE_OPERAND (target, 1)); + } + iloc_sentinel sentinel = target_loc; + init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, non_constant_p, overflow_p); /* The hash table might have moved since the get earlier, and the @@ -6986,7 +7027,10 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, return t; } + /* Change the input location to the currently processed expression for + better error messages when a subexpression has no location. */ location_t loc = cp_expr_loc_or_input_loc (t); + iloc_sentinel sentinel (loc); STRIP_ANY_LOCATION_WRAPPER (t); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C index 4574eb83ff7..11630f26ffe 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C @@ -10,11 +10,11 @@ // R() is well-formed because i is initialized before j. struct s { - constexpr s() : v(v) { } + constexpr s() : v(v) { } // { dg-error "accessing uninitialized member" } int v; }; -constexpr s bang; // { dg-error "|" } +constexpr s bang; // { dg-message "in .constexpr. expansion" } struct R { int i,j; @@ -26,14 +26,14 @@ constexpr R r; // { dg-bogus "" } // Ill-formed (no diagnostic required) struct T { int i; - constexpr int f() { return i; } + constexpr int f() { return i; } // { dg-error "accessing uninitialized member" } constexpr T(): i(0) { } - constexpr T(const T& t) : i(f()) { } // { dg-message "" } + constexpr T(const T& t) : i(f()) { } // { dg-message "in .constexpr. expansion" } }; constexpr T t1; // Ill-formed (diagnostic required) -constexpr T t2(t1); // { dg-message "" } +constexpr T t2(t1); // { dg-message "in .constexpr. expansion" } // Well-formed struct U { diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C index 272a225d967..bfb185f2fb5 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323.C @@ -1,10 +1,10 @@ // PR c++/70323 // { dg-do compile { target c++11 } } -constexpr int overflow_if_0 (int i) { return __INT_MAX__ + !i; } -constexpr int overflow_if_1 (int i) { return __INT_MAX__ + i; } +constexpr int overflow_if_0 (int i) { return __INT_MAX__ + !i; } // { dg-error "overflow in constant expression" } +constexpr int overflow_if_1 (int i) { return __INT_MAX__ + i; } // { dg-error "overflow in constant expression" } -constexpr bool i0_0 = overflow_if_0 (0); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr bool i0_0 = overflow_if_0 (0); // { dg-message "in .constexpr. expansion of " } constexpr bool i0_1 = overflow_if_0 (1); constexpr bool i1_0 = overflow_if_1 (0); -constexpr bool i1_1 = overflow_if_1 (1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr bool i1_1 = overflow_if_1 (1); // { dg-message "in .constexpr. expansion of " } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C index 1990ab6be2d..b5ed581e1c8 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-70323a.C @@ -2,10 +2,10 @@ // { dg-do compile { target c++11 } } // { dg-options "-Wall" } -constexpr int overflow_if_0 (int i) { return __INT_MAX__ + !i; } -constexpr int overflow_if_1 (int i) { return __INT_MAX__ + i; } +constexpr int overflow_if_0 (int i) { return __INT_MAX__ + !i; } // { dg-error "overflow in constant expression" } +constexpr int overflow_if_1 (int i) { return __INT_MAX__ + i; } // { dg-error "overflow in constant expression" } -constexpr bool i0_0 = overflow_if_0 (0); // { dg-error "overflow in constant expression|in .constexpr. expansion of" } +constexpr bool i0_0 = overflow_if_0 (0); // { dg-message "in .constexpr. expansion of" } constexpr bool i0_1 = overflow_if_0 (1); constexpr bool i1_0 = overflow_if_1 (0); -constexpr bool i1_1 = overflow_if_1 (1); // { dg-error "overflow in constant expression|in .constexpr. expansion of" } +constexpr bool i1_1 = overflow_if_1 (1); // { dg-message "in .constexpr. expansion of" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-delete2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-delete2.C index 999f9b7851e..f2b6df2509c 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-delete2.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-delete2.C @@ -6,8 +6,9 @@ constexpr int f(int i) { return i; } constexpr int g(A* ap) { return f((delete[] ap, 42)); // { dg-message "" "" { target c++17_down } } + // { dg-error "" "" { target c++2a } .-1 } } A a; -constexpr int i = g(&a); // { dg-error "" } - // { dg-message "in 'constexpr' expansion of" "" { target c++2a } .-1 } +constexpr int i = g(&a); // { dg-error "" "" { target c++17_down } } + // { dg-message "in 'constexpr' expansion of" "" { target c++2a } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-diag3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-diag3.C index 5eedf42ba36..50c676c56cd 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-diag3.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-diag3.C @@ -16,7 +16,7 @@ int main() struct complex // { dg-message "no .constexpr. constructor" "" { target { ! implicit_constexpr } } } { complex(double r, double i) : re(r), im(i) { } - constexpr double real() const { return re; } // { dg-error "not a literal type" "" { target c++11_only } } + constexpr double real() const { return re; } // { dg-error "not a literal type|not usable in a constant expression" "" { target { ! implicit_constexpr } } } double imag() const { return im; } private: diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ice20.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ice20.C index e2d4853a284..43fa9a03c14 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-ice20.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ice20.C @@ -3,5 +3,6 @@ typedef bool (*Function)(int); constexpr bool check(int x, Function p) { return p(x); } // { dg-message "in .constexpr. expansion of" } +// { dg-error "not a constant expression" "" { target *-*-* } .-1 } static_assert(check(2, check), ""); // { dg-error "conversion|constant|in .constexpr. expansion of" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C index 8c4201e1ec2..b00f8794d4c 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion.C @@ -1,6 +1,6 @@ // Test that we catch excessive recursion. // { dg-do compile { target c++11 } } // { dg-options "-fconstexpr-depth=5" } -// { dg-prune-output "in constexpr expansion" } -constexpr int f (int i) { return f (i-1); } // { dg-message "in .constexpr. expansion of " } -constexpr int i = f(42); // { dg-error ".constexpr. evaluation depth|in .constexpr. expansion of " } +// { dg-prune-output "in .constexpr. expansion" } +constexpr int f (int i) { return f (i-1); } // { dg-error ".constexpr. evaluation depth" } +constexpr int i = f(42); diff --git a/gcc/testsuite/g++.dg/cpp0x/overflow1.C b/gcc/testsuite/g++.dg/cpp0x/overflow1.C index b8591b4af41..e295355c88f 100644 --- a/gcc/testsuite/g++.dg/cpp0x/overflow1.C +++ b/gcc/testsuite/g++.dg/cpp0x/overflow1.C @@ -4,7 +4,7 @@ template struct Fib { static const long long value // { dg-error "overflow" } - = Fib::value + Fib::value; + = Fib::value + Fib::value; // { dg-error "overflow" } }; template <> diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C index fe0b8570ca2..d09df091fdb 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C @@ -14,7 +14,8 @@ struct B { } }; struct C : A { - B bar {this}; + B bar {this}; // { dg-error "" "" { target c++14_down } } }; -constexpr C foo {}; // { dg-message "" } +// error path changes in C++17 due to `C` becoming an aggregate +constexpr C foo {}; // { dg-error "" "" { target c++17 } } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-89481.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-89481.C index 8ac4ef0fd36..6f8f6a8038e 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-89481.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-89481.C @@ -6,7 +6,7 @@ foo () { union U { long long a; int b[2]; } u { 5LL }; u.b[1] = 4; // { dg-error "change of the active member of a union from" "" { target c++17_down } } - return u.b[0]; + return u.b[0]; // { dg-error "accessing uninitialized array element" "" { target c++2a } } } constexpr int @@ -19,6 +19,5 @@ bar () static_assert (foo () == 0, ""); // { dg-error "non-constant condition for static assertion" } // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } - // { dg-error "accessing uninitialized array element" "" { target c++2a } .-2 } static_assert (bar () == 4, ""); // { dg-error "non-constant condition for static assertion" "" { target c++17_down } } // { dg-message "in 'constexpr' expansion of" "" { target c++17_down } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C index 43aa7c974c1..f79f1611d5f 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C @@ -11,3 +11,4 @@ constexpr const int& test() { return local.get(); } constexpr int x = test(); // { dg-error "accessing object outside its lifetime" } + diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C index 22cd919fcda..2f5ae8db6d5 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C @@ -8,9 +8,9 @@ struct S { constexpr int error() { const auto& local = S{}.get(); // { dg-message "note: declared here" } - return local; + return local; // { dg-error "accessing object outside its lifetime" } } -constexpr int x = error(); // { dg-error "accessing object outside its lifetime" } +constexpr int x = error(); // { dg-message "in .constexpr. expansion" } constexpr int ok() { // temporary should only be destroyed after end of full-expression diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C index 6329f8cf6c6..53785521d05 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C @@ -7,7 +7,7 @@ constexpr int f(int i) { int j = 123; // { dg-message "note: declared here" } p = &j; } - return *p; + return *p; // { dg-error "accessing object outside its lifetime" } } -constexpr int i = f(0); // { dg-error "accessing object outside its lifetime" } +constexpr int i = f(0); // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C index 181a1201663..4302da1eddc 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C @@ -5,7 +5,7 @@ constexpr const double& test() { return local; } -static_assert(test() == 3.0, ""); // { dg-error "constant|accessing object outside its lifetime" } +static_assert(test() == 3.0, ""); // { dg-error "non-constant condition|accessing object outside its lifetime" } // no deference, shouldn't error static_assert((test(), true), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C index ad3ef579f63..a12920c8fba 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C @@ -5,7 +5,7 @@ constexpr const int& id(int x) { return x; } // { dg-message "note: declared he constexpr bool test() { const int& y = id(3); - return y == 3; + return y == 3; // { dg-error "accessing object outside its lifetime" } } -constexpr bool x = test(); // { dg-error "accessing object outside its lifetime" } +constexpr bool x = test(); // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C index 45c4fcf50be..0c77dd4934d 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C @@ -14,7 +14,7 @@ struct C { struct A { int r; - const C c; + const C c; // { dg-message "originally declared" } constexpr A() : r(11) { r = 14; const_cast(c).n = 42; } // { dg-error "modifying a const object" } }; @@ -34,5 +34,4 @@ struct B { }; constexpr B b(false); // { dg-message "in .constexpr. expansion of" } -// { dg-message "originally declared" "" { target *-*-* } .-1 } static_assert(b.e.d.a.c.n == 2, ""); // { dg-error "non-constant condition" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const16.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const16.C index 5a5b92bc8cc..b5ccf3a4ea5 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const16.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const16.C @@ -7,7 +7,7 @@ constexpr int& impl(const int (&array)[10], int index) { struct A { constexpr int& operator[](int i) { return impl(elems, i); } - const int elems[10]; + const int elems[10]; // { dg-message "originally declared" } }; constexpr bool @@ -19,4 +19,3 @@ f() } constexpr bool b = f(); // { dg-message "in .constexpr. expansion of " } -// { dg-message "originally declared" "" { target *-*-* } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const18.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const18.C index 11a680468c2..278628c121b 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const18.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const18.C @@ -13,11 +13,11 @@ struct array template struct S { using U = array; - const U m; + const U m; // { dg-message "originally declared" } constexpr S(int) : m{} { const_cast(const_cast(m)[0]) = 42; // { dg-error "modifying a const object" } } }; -constexpr S p = { 10 }; // { dg-message "originally declared" } +constexpr S p = { 10 }; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const19.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const19.C index c31222ffcdd..2d18c94537b 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const19.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const19.C @@ -7,7 +7,7 @@ template struct array { constexpr const E &operator[](size_t n) const noexcept { return elems[n]; } - const E elems[N]; + const E elems[N]; // { dg-message "originally declared" } }; template @@ -20,4 +20,4 @@ struct S { } }; -constexpr S p = { 10 }; // { dg-message "originally declared" } +constexpr S p = { 10 }; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const21.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const21.C index 0b16193398e..d3bbcb116a6 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const21.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const21.C @@ -18,11 +18,11 @@ struct array2 { template struct S { using U = array2; - const U m; + const U m; // { dg-message "originally declared" } constexpr S(int) : m{} { const_cast(m.a[0]) = 42; // { dg-error "modifying a const object" } } }; -constexpr S p = { 10 }; // { dg-message "originally declared" } +constexpr S p = { 10 }; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const22.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const22.C index 216cf1607a4..27522f86dbd 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const22.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const22.C @@ -7,11 +7,11 @@ struct X { template struct S { - const X x; + const X x; // { dg-message "originally declared" } constexpr S(int) : x{} { const_cast(x).i = 19; // { dg-error "modifying a const object" } } }; -constexpr S p = { 10 }; // { dg-message "originally declared" } +constexpr S p = { 10 }; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C index 6853775c1e2..fc88dd05eef 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C @@ -7,7 +7,7 @@ struct A { }; struct B { - const A a; + const A a; // { dg-message "originally declared" } constexpr B(bool b) { if (b) const_cast(a).n = 3; // { dg-error "modifying a const object" } @@ -18,5 +18,4 @@ constexpr B b(false); static_assert(b.a.n == 2, ""); constexpr B b2(true); // { dg-message "in .constexpr. expansion of " } -// { dg-message "originally declared" "" { target *-*-* } .-1 } static_assert((b2.a.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C index 8263a7cc505..27fede152c7 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C @@ -2,7 +2,7 @@ // { dg-do compile { target c++14 } } struct A { - const int n; + const int n; // { dg-message "originally declared" } constexpr A() : n(1) { } }; struct B { @@ -13,5 +13,4 @@ struct B { } }; constexpr B b; // { dg-message "in .constexpr. expansion of " } -// { dg-message "originally declared" "" { target *-*-* } .-1 } static_assert((b.a.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C index 922e8ff126f..bea14b05602 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C @@ -3,7 +3,7 @@ struct D { int n; }; -struct C { const D d; }; +struct C { const D d; }; // { dg-message "originally declared" } struct A { C c; @@ -19,5 +19,4 @@ struct B { }; constexpr B b{}; // { dg-message "in .constexpr. expansion of " } -// { dg-message "originally declared" "" { target *-*-* } .-1 } static_assert((b.a.c.d.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-union5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-union5.C index 55fe9fa2f0b..3d76345d564 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-union5.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-union5.C @@ -8,8 +8,8 @@ union U { }; constexpr int foo(U *up) { - up->a++; + up->a++; // { dg-error "accessing uninitialized member" } return {42}; } -extern constexpr U u = {}; // { dg-error "accessing uninitialized member" } +extern constexpr U u = {}; // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp1y/pr68180.C b/gcc/testsuite/g++.dg/cpp1y/pr68180.C index 9e6e5e984f9..8de1ef3936b 100644 --- a/gcc/testsuite/g++.dg/cpp1y/pr68180.C +++ b/gcc/testsuite/g++.dg/cpp1y/pr68180.C @@ -6,11 +6,11 @@ typedef float __attribute__( ( vector_size( 16 ) ) ) float32x4_t; constexpr float32x4_t fill(float x) { float32x4_t v{0}; constexpr auto vs = sizeof(v)/sizeof(v[0]); - for (auto i=0U; i(a); + dynamic_cast(a); // { dg-error "accessing uninitialized member" } } -constexpr D d; // { dg-error "accessing uninitialized member" } -// { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } +constexpr D d; // { dg-message "in 'constexpr' expansion of" } diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-init1.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-init1.C index e56ecfed48a..b4e39b6f928 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-init1.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-init1.C @@ -52,11 +52,10 @@ constexpr int fn5 () { struct S { int a = 9; int b; } s; - return s.b; + return s.b; // { dg-error "accessing uninitialized member" } } -constexpr int b = fn5 (); // { dg-error "accessing uninitialized member" } -// { dg-message "in .constexpr. expansion of" "" { target *-*-* } .-1 } +constexpr int b = fn5 (); // { dg-message "in .constexpr. expansion of" } constexpr int fn6 () diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C index 5a3d06a5fab..832782e1427 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C @@ -17,11 +17,11 @@ struct B : A { constexpr int foo () { - A *a = new B (); + A *a = new B (); // { dg-message "allocated here" } a->a = 4; delete a; - int r = a->foo (); + int r = a->foo (); // { dg-error "constant expression" } return r; } -constexpr auto a = foo (); // { dg-error "constant expression" } +constexpr auto a = foo (); // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C index 70b841208f8..3ba440fec53 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C @@ -45,11 +45,10 @@ constexpr bool f5 () { int *p = new int; // { dg-message "allocated here" } - return *p == 1; + return *p == 1; // { dg-error "the content of uninitialized storage is not usable in a constant expression" } } -constexpr auto v5 = f5 (); // { dg-error "the content of uninitialized storage is not usable in a constant expression" } - // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } +constexpr auto v5 = f5 (); // { dg-message "in 'constexpr' expansion of" } constexpr bool f6 () @@ -57,11 +56,10 @@ f6 () int *p = new int (2); // { dg-message "allocated here" } int *q = p; delete p; - return *q == 2; + return *q == 2; // { dg-error "use of allocated storage after deallocation in a constant expression" } } -constexpr auto v6 = f6 (); // { dg-error "use of allocated storage after deallocation in a constant expression" } - // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } +constexpr auto v6 = f6 (); // { dg-message "in 'constexpr' expansion of" } constexpr int * f7 () diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit10.C b/gcc/testsuite/g++.dg/cpp2a/constinit10.C index b678788541e..d2c49c41f91 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constinit10.C +++ b/gcc/testsuite/g++.dg/cpp2a/constinit10.C @@ -11,15 +11,14 @@ struct S1 struct alignas(64) S2 { constexpr S2 () - : m_tabS1() + : m_tabS1() // { dg-error "used before its definition" } {} S1 m_tabS1[7]; }; constinit S2 objX; // { dg-error ".constinit. variable .objX. does not have a constant initializer" } -// { dg-error "used before its definition" "" { target *-*-* } .-1 } -// // { dg-message "in .constexpr. expansion of" "" { target *-*-* } .-2 } +// { dg-message "in .constexpr. expansion of" "" { target *-*-* } .-1 } constexpr S1::S1 () : m_i(14) diff --git a/gcc/testsuite/g++.dg/cpp2a/is-corresponding-member4.C b/gcc/testsuite/g++.dg/cpp2a/is-corresponding-member4.C index 6b74090306b..241e9976e8c 100644 --- a/gcc/testsuite/g++.dg/cpp2a/is-corresponding-member4.C +++ b/gcc/testsuite/g++.dg/cpp2a/is-corresponding-member4.C @@ -14,8 +14,8 @@ is_corresponding_member (M1 S1::*m1, M2 S2::*m2) noexcept struct A { int a; }; struct B; constexpr int B::*n = nullptr; -constexpr auto a = std::is_corresponding_member (&A::a, n); // { dg-error "invalid use of incomplete type 'struct B'" } -constexpr auto b = std::is_corresponding_member (n, &A::a); // { dg-error "invalid use of incomplete type 'struct B'" } +constexpr auto a = std::is_corresponding_member (&A::a, n); // { dg-message "in .constexpr. expansion of" } +constexpr auto b = std::is_corresponding_member (n, &A::a); // { dg-message "in .constexpr. expansion of" } void foo (int B::*m) diff --git a/gcc/testsuite/g++.dg/ext/constexpr-vla2.C b/gcc/testsuite/g++.dg/ext/constexpr-vla2.C index d4ea7c58c0d..e09a27af3de 100644 --- a/gcc/testsuite/g++.dg/ext/constexpr-vla2.C +++ b/gcc/testsuite/g++.dg/ext/constexpr-vla2.C @@ -4,7 +4,7 @@ constexpr int fn_bad (int n) { - __extension__ int a [n] = { 0 }; + __extension__ int a [n] = { 0 }; // { dg-error "array subscript" } int z = a [0] + (n ? fn_bad (n - 1) : 0); // { dg-message "in .constexpr. expansion of " } return z; } @@ -18,4 +18,4 @@ fn_ok (int n) } constexpr int i1 = fn_ok (3); -constexpr int i2 = fn_bad (3); // { dg-error "array subscript|in .constexpr. expansion of " } +constexpr int i2 = fn_bad (3); // { dg-message "in .constexpr. expansion of " } diff --git a/gcc/testsuite/g++.dg/ext/constexpr-vla3.C b/gcc/testsuite/g++.dg/ext/constexpr-vla3.C index 538b576a825..6f9daa1897f 100644 --- a/gcc/testsuite/g++.dg/ext/constexpr-vla3.C +++ b/gcc/testsuite/g++.dg/ext/constexpr-vla3.C @@ -4,11 +4,11 @@ constexpr int foo (int n) { - __extension__ int a[n] = { 1, 2, 3, 4, 5, 6 }; + __extension__ int a[n] = { 1, 2, 3, 4, 5, 6 }; // { dg-error "array subscript" } int z = 0; for (int i = 0; i <= n; ++i) z += a[i]; return z; } -constexpr int n = foo (3); // { dg-error "array subscript|in .constexpr. expansion of " } +constexpr int n = foo (3); // { dg-message "in .constexpr. expansion of " } diff --git a/gcc/testsuite/g++.dg/ubsan/pr63956.C b/gcc/testsuite/g++.dg/ubsan/pr63956.C index 3a1596e6e2e..6fd0b4f893e 100644 --- a/gcc/testsuite/g++.dg/ubsan/pr63956.C +++ b/gcc/testsuite/g++.dg/ubsan/pr63956.C @@ -56,12 +56,13 @@ fn3 (int a, int b) { if (b != 2) a = a / b; // { dg-error "..7 / 0.. is not a constant expression" } + // { dg-error "overflow in constant expression" "" { target *-*-* } .-1 } return a; } constexpr int k1 = fn3 (8, 4); constexpr int k2 = fn3 (7, 0); // { dg-message "in .constexpr. expansion" } -constexpr int k3 = fn3 (INT_MIN, -1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr int k3 = fn3 (INT_MIN, -1); // { dg-message "in .constexpr. expansion of " } SA (k1 == 2); @@ -100,13 +101,13 @@ constexpr int fn7 (const int *a, int b) { if (b != 3) - return fn6 (*a, b); + return fn6 (*a, b); // { dg-error "null pointer" } return 7; } constexpr int n1 = 7; constexpr int n2 = fn7 (&n1, 5); -constexpr int n3 = fn7 ((const int *) 0, 8); // { dg-error "null pointer|in .constexpr. expansion of " } +constexpr int n3 = fn7 ((const int *) 0, 8); // { dg-message "in .constexpr. expansion of " } constexpr int fn8 (int i) @@ -122,15 +123,15 @@ constexpr int fn9 (int a, int b) { if (b != 0) - return a + b; + return a + b; // { dg-error "overflow in constant expression" } return a; } constexpr int p1 = fn9 (42, 7); -constexpr int p2 = fn9 (__INT_MAX__, 1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr int p2 = fn9 (__INT_MAX__, 1); // { dg-message "in .constexpr. expansion of " } constexpr int p3 = fn9 (__INT_MAX__, -1); constexpr int p4 = fn9 (INT_MIN, 1); -constexpr int p5 = fn9 (INT_MIN, -1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr int p5 = fn9 (INT_MIN, -1); // { dg-message "in .constexpr. expansion of " } SA (p1 == 49); SA (p3 == __INT_MAX__ - 1); @@ -140,13 +141,13 @@ constexpr int fn10 (int a, int b) { if (b != 0) - return a * b; + return a * b; // { dg-error "overflow in constant expression" } return a; } constexpr int q1 = fn10 (10, 10); -constexpr int q2 = fn10 (__INT_MAX__, 2); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } -constexpr int q3 = fn10 (INT_MIN, 2); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr int q2 = fn10 (__INT_MAX__, 2); // { dg-message "in .constexpr. expansion of " } +constexpr int q3 = fn10 (INT_MIN, 2); // { dg-message "in .constexpr. expansion of " } constexpr int q4 = fn10 (-1, -1); SA (q1 == 100); @@ -155,14 +156,14 @@ SA (q4 == 1); constexpr int fn11 (double d) { - int i = d; + int i = d; // { dg-error "overflow in constant expression" } if (i != 0) return i; return i * 2; } constexpr int r1 = fn11 (3.4); -constexpr int r2 = fn11 (__builtin_inf ()); // { dg-error "overflow in constant expression|in .constexpr. expansion of " } +constexpr int r2 = fn11 (__builtin_inf ()); // { dg-message "in .constexpr. expansion of " } constexpr int fn12 (int i) diff --git a/libstdc++-v3/testsuite/25_algorithms/equal/constexpr_neg.cc b/libstdc++-v3/testsuite/25_algorithms/equal/constexpr_neg.cc index 34ca5c4805c..fd89ac0e166 100644 --- a/libstdc++-v3/testsuite/25_algorithms/equal/constexpr_neg.cc +++ b/libstdc++-v3/testsuite/25_algorithms/equal/constexpr_neg.cc @@ -32,7 +32,7 @@ test01() return outa; } -static_assert(test01()); // { dg-error "outside the bounds" } +static_assert(test01()); // { dg-error "non-constant condition" } constexpr bool test02() @@ -44,7 +44,8 @@ test02() return outa; } -static_assert(test02()); // { dg-error "outside the bounds" } +static_assert(test02()); // { dg-error "non-constant condition" } -// { dg-prune-output "non-constant condition" } +// Errors occuring within internals: +// { dg-error "outside the bounds of array" "" { target *-*-* } 0 } // { dg-prune-output "in 'constexpr'" } diff --git a/libstdc++-v3/testsuite/26_numerics/gcd/105844.cc b/libstdc++-v3/testsuite/26_numerics/gcd/105844.cc index 5b6fea7b560..bc9b29bc39d 100644 --- a/libstdc++-v3/testsuite/26_numerics/gcd/105844.cc +++ b/libstdc++-v3/testsuite/26_numerics/gcd/105844.cc @@ -13,9 +13,11 @@ static_assert( std::gcd(LLONG_MIN, 2ull) == 2 ); static_assert( std::gcd(2ull, LLONG_MIN) == 2 ); // But |INT_MIN| cannot be represented in common_type i.e. int. -constexpr int a = std::gcd(INT_MIN, 1); // { dg-error "overflow" } -constexpr int b = std::gcd(1, INT_MIN); // { dg-error "overflow" } +constexpr int a = std::gcd(INT_MIN, 1); // { dg-error "in .constexpr." } +constexpr int b = std::gcd(1, INT_MIN); // { dg-error "in .constexpr." } // And |LLONG_MIN| cannot be represented in long. -constexpr long long c = std::gcd(LLONG_MIN, 1); // { dg-error "overflow" } -constexpr long long d = std::gcd(1, LLONG_MIN); // { dg-error "overflow" } +constexpr long long c = std::gcd(LLONG_MIN, 1); // { dg-error "in .constexpr." } +constexpr long long d = std::gcd(1, LLONG_MIN); // { dg-error "in .constexpr." } + +// { dg-error "overflow" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc index d0e032e03e0..d853974f77e 100644 --- a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc +++ b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc @@ -9,14 +9,16 @@ static_assert( std::lcm(INT_MIN, 1u) == INT_MAX+1u ); static_assert( std::lcm(1u, INT_MIN) == INT_MAX+1u ); // But |INT_MIN| cannot be represented in common_type i.e. int. -constexpr int a = std::lcm(INT_MIN, 1); // { dg-error "overflow" } -constexpr int b = std::lcm(1, INT_MIN); // { dg-error "overflow" } +constexpr int a = std::lcm(INT_MIN, 1); // { dg-error "in .constexpr." } +constexpr int b = std::lcm(1, INT_MIN); // { dg-error "in .constexpr." } // And the LCM of 50000 and 49999 cannot be represented in int. -constexpr int c = std::lcm(50000, 49999); // { dg-error "overflow" } -constexpr int d = std::lcm(49999, 50000); // { dg-error "overflow" } +constexpr int c = std::lcm(50000, 49999); // { dg-error "in .constexpr." } +constexpr int d = std::lcm(49999, 50000); // { dg-error "in .constexpr." } // Similarly for unsigned, but the diagnostic is a failed assertion instead. -constexpr int e = std::lcm(500000u, 499999); // { dg-error "in 'constexpr'" } -constexpr int f = std::lcm(499999u, 500000); // { dg-error "in 'constexpr'" } +constexpr int e = std::lcm(500000u, 499999); // { dg-error "in .constexpr." } +constexpr int f = std::lcm(499999u, 500000); // { dg-error "in .constexpr." } + +// { dg-error "overflow" "" { target *-*-* } 0 } // { dg-error "unreachable" "" { target *-*-* } 0 }