From patchwork Fri Jan 21 20:18:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 50336 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D86E0385AC31 for ; Fri, 21 Jan 2022 20:21:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D86E0385AC31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642796481; bh=H1p/wK5cESp1as/qXX0aBoJWawOrEELnxugruOfv85E=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fbCdMWiKODoKcBDcnebApUGTRAOvDCasxG70Sb3w8uEuJWw5aEiBM6ew/kysaQ2zA pVLd+hUkhsZgMRd/GsqzayN/zN8n9zEa6IYmPvyX29pHi3Zb3YdkHzlUbQsO8DZ6RJ yNE2pXIJDJ7xnAOhrJ7BoSzdQaN/T7vUTZ/Bv1i0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 168E6385AC1B for ; Fri, 21 Jan 2022 20:18:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 168E6385AC1B Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-572-_VFsoaYdNnq9ThfPHwu9qg-1; Fri, 21 Jan 2022 15:18:09 -0500 X-MC-Unique: _VFsoaYdNnq9ThfPHwu9qg-1 Received: by mail-qk1-f199.google.com with SMTP id u12-20020a05620a0c4c00b00475a9324977so7514481qki.13 for ; Fri, 21 Jan 2022 12:18:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=H1p/wK5cESp1as/qXX0aBoJWawOrEELnxugruOfv85E=; b=b9YV00z67uFVg6l4lj1AZtahzIS5QV+8bCFyE+KV8xMEYohp9apAfkmElD/EJJtcQC QOFXH+37nhBF541Fc8fSWMfKbj/3JySDH8Ea0OfDF9SiAvWCBNT+y+fQ2uG4A/5aUmKu 2Yzh3/4mp0LmW01cQHDoDWr7qVevh2YU3/B5pPRHHwFecKvS4b+wC/vZn/lwG0TQGWYr JEvx7PtmfPwzIlg9Og08MdlnBohvgOcvU1u4HBvfid0J53tds7UmNG2KrGRLWDgvieaJ q8TVsUZlFSRLDaQ2CGWi2F5s+WxWbY2s3kEZv3spNqOFod9I0uP2cnQ8XhP3UoqpWxOL DlTw== X-Gm-Message-State: AOAM533b46a2zB0JB9VWhfTxSINPStraQsU21/l5ZuYSuljrTY23eLQS 8zKV4N8PtX3THIGHVCnU48Q7fzKaRgM6GOiFZxaB91ta7wNKkC9fNo1P2uPbVWX1AUmqMjn4kyB r/hPmqH+JAiLpPXm/Xnl5NtaE9wouhxxKD1Nnz/qTM0CDO9e1s5R4pJ7tusb+t7dzhA== X-Received: by 2002:ac8:5713:: with SMTP id 19mr4783611qtw.642.1642796288195; Fri, 21 Jan 2022 12:18:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJzAgFleRczRUS+qa3l9OUjD7xWj9OrdTHO3ewqw9UyPXYl3bAmOC+iIktuG+ldnUcYRvh8Zhw== X-Received: by 2002:ac8:5713:: with SMTP id 19mr4783589qtw.642.1642796287745; Fri, 21 Jan 2022 12:18:07 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id k1sm3685049qko.84.2022.01.21.12.18.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jan 2022 12:18:07 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: explain failing static_assert Date: Fri, 21 Jan 2022 15:18:05 -0500 Message-Id: <20220121201805.563013-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" While looking at another bug I wanted the compiler to tell me what the two unequal values were. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * semantics.cc (find_failing_clause): Return expr if not decomposable. (finish_static_assert): Show constant values in failing comparison. gcc/testsuite/ChangeLog: * g++.dg/template/explicit-args6.C: Add expected message. --- gcc/cp/semantics.cc | 33 +++++++++++-------- .../g++.dg/template/explicit-args6.C | 2 ++ 2 files changed, 22 insertions(+), 13 deletions(-) base-commit: c163647ffbc9a20c8feb6e079dbecccfe016c82e prerequisite-patch-id: 6e305598074684612bebec8a36ecf37de96519f8 diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 8e22944c2dc..640b2d75471 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11062,9 +11062,10 @@ find_failing_clause_r (tree expr) static tree find_failing_clause (tree expr) { - if (TREE_CODE (expr) != TRUTH_ANDIF_EXPR) - return NULL_TREE; - return find_failing_clause_r (expr); + if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) + if (tree e = find_failing_clause_r (expr)) + expr = e; + return expr; } /* Build a STATIC_ASSERT for a static assertion with the condition @@ -11134,9 +11135,9 @@ finish_static_assert (tree condition, tree message, location_t location, tree bad = find_failing_clause (orig_condition); /* If not, or its location is unusable, fall back to the previous location. */ - location_t cloc = location; - if (cp_expr_location (bad) != UNKNOWN_LOCATION) - cloc = cp_expr_location (bad); + location_t cloc = cp_expr_loc_or_loc (bad, location); + /* Nobody wants to see the artificial (bool) cast. */ + bad = tree_strip_nop_conversions (bad); /* Report the error. */ if (len == 0) @@ -11144,16 +11145,22 @@ finish_static_assert (tree condition, tree message, location_t location, else error_at (cloc, "static assertion failed: %s", TREE_STRING_POINTER (message)); - if (show_expr_p) - inform (cloc, "%qE evaluates to false", - /* Nobody wants to see the artificial (bool) cast. */ - (bad ? tree_strip_nop_conversions (bad) : orig_condition)); /* Actually explain the failure if this is a concept check or a requires-expression. */ - if (concept_check_p (orig_condition) - || TREE_CODE (orig_condition) == REQUIRES_EXPR) - diagnose_constraints (location, orig_condition, NULL_TREE); + if (concept_check_p (bad) + || TREE_CODE (bad) == REQUIRES_EXPR) + diagnose_constraints (location, bad, NULL_TREE); + else if (COMPARISON_CLASS_P (bad) + && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) + { + tree op0 = fold_non_dependent_expr (TREE_OPERAND (bad, 0)); + tree op1 = fold_non_dependent_expr (TREE_OPERAND (bad, 1)); + tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); + inform (cloc, "the comparison reduces to %qE", cond); + } + else if (show_expr_p) + inform (cloc, "%qE evaluates to false", bad); } else if (condition && condition != error_mark_node) { diff --git a/gcc/testsuite/g++.dg/template/explicit-args6.C b/gcc/testsuite/g++.dg/template/explicit-args6.C index d853564e3be..311a7678923 100644 --- a/gcc/testsuite/g++.dg/template/explicit-args6.C +++ b/gcc/testsuite/g++.dg/template/explicit-args6.C @@ -20,6 +20,8 @@ constexpr unsigned frob() { static_assert(N == 1, "user-friendly diagnostic"); // { dg-error "user-friendly" } + // dg-message { "-1 == 1" "" { target *-*-* } .-1 } + // narrowing check, reject negative values return unsigned{N}; // { dg-prune-output "narrowing" } } // { dg-prune-output "flows off the end" }