From patchwork Tue Oct 3 16:48:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 148006 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2216731vqb; Tue, 3 Oct 2023 09:49:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHm2QG1Js/t57tqOZfs402KDknwQ+GG2dlgYsYs8PMJ72titwrmnlmGY3dsn/XF+glGB1bO X-Received: by 2002:a17:906:301a:b0:9a5:d972:af50 with SMTP id 26-20020a170906301a00b009a5d972af50mr15116298ejz.19.1696351747115; Tue, 03 Oct 2023 09:49:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696351747; cv=none; d=google.com; s=arc-20160816; b=JVCq2ESV3RgYe7c88q20qtOoEBpM5CdU/jA2uyqeiLzvQXzcPpflGNyAHIQkno9ip+ cokca+I2tz6oRJRVx22yQoSY9bZLuc2q9/3Gmy3l9ngutNoFcVvWk+Snyfdfci1SUCqK /DRnaEjD7zcn/CNzzyKPOTtHl9vQxUOKNjr7Na2wpdNHe4WcTuMiH2anlQxcxJQ3lgTd gd+wrsn9RKpR1LeP5T7heO7TTP2UJfJcQeLDZokjvqpIheKNXCfseU17gY3rHNjzvnED 1S6URUBikKPkVbLRs6903tAePU5vDmsuyMD7AS050O/KjSUxE0Egfq0RkDMjz0gmyqKI kYNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=vCsWSv6quCg3DwoKzbhER4uJREwZdXfgvRzTEHNKhUQ=; fh=MxIJpr9e5YAra7NAbNCprLQxFB1Du0HUseC6WRGVOis=; b=wuksPl76q2HUwUzpvxCxsHXirz5TWR1gas+vZDmGdUYYky+KKnJvK/JUuQO3rPqAD6 MoPdRDl7Ih0Bjg8ieWM8NPOyj7fIjj4Awpr5rSP01LAM3t5GqNSfmY0NvCT4t2FBm4Ht LjZuNegnbhU1fWRZFoV3ciqKpyOcvtcpILjad7/lC4F4vrTbw7Q20/iQXagND7O8QEyN xF7akBRUtjKPXgme2Pp9QLapfOh6lr6wjOkiN12aEBdhH7CKw5aSDVlJZqH00dGWhP6M mGQy7GToa/i3EHA7GEzd5LmdNHEbEu+lTLiDhtacjoHXGSTZR9QblkLltsTEwcZYN11t xAnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FgCkdqM3; 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ci7-20020a170906c34700b009a1bf605ebesi806730ejb.847.2023.10.03.09.49.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 09:49:07 -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=@redhat.com header.s=mimecast20190719 header.b=FgCkdqM3; 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F0CEF3858D20 for ; Tue, 3 Oct 2023 16:48:51 +0000 (GMT) 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 610E93858D33 for ; Tue, 3 Oct 2023 16:48:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 610E93858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696351699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vCsWSv6quCg3DwoKzbhER4uJREwZdXfgvRzTEHNKhUQ=; b=FgCkdqM3oFC3owR+OpXTJuFcoLxDfVeQs3o8jvb+Oli6jbXOkg5ryhoydyTCfX7cxOHlUT cWEqCsZKrSkPegJyznWPUYJ1SXuBDppKFy0IYzX70rD0U3OGb2OYFcMINPi7XsIc59tCP/ Gkxisuo6zhrQLHPIA0P9YXptMLRzlKg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-248-6JOE18vbOXiGmNDI6suwFA-1; Tue, 03 Oct 2023 12:48:16 -0400 X-MC-Unique: 6JOE18vbOXiGmNDI6suwFA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0028B828BC7 for ; Tue, 3 Oct 2023 16:48:16 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.18.119]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3413140E962; Tue, 3 Oct 2023 16:48:15 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org, Jason Merrill Cc: David Malcolm Subject: [PATCH] c++: print source code in print_instantiation_partial_context_line Date: Tue, 3 Oct 2023 12:48:12 -0400 Message-Id: <20231003164812.13294-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778753729898657886 X-GMAIL-MSGID: 1778753729898657886 As mentioned in my Cauldron talk, this patch adds a call to diagnostic_show_locus to the "required from here" messages in print_instantiation_partial_context_line, so that e.g., rather than the rather mystifying: In file included from ../x86_64-pc-linux-gnu/libstdc++-v3/include/memory:78, from ../../src/demo-1.C:1: ../x86_64-pc-linux-gnu/libstdc++-v3/include/bits/unique_ptr.h: In instantiation of ‘std::__detail::__unique_ptr_t<_Tp> std::make_unique(_Args&& ...) [with _Tp = bar; _Args = {}; __detail::__unique_ptr_t<_Tp> = __detail::__unique_ptr_t]’: ../../src/demo-1.C:15:32: required from here ../x86_64-pc-linux-gnu/libstdc++-v3/include/bits/unique_ptr.h:1066:30: error: no matching function for call to ‘bar::bar()’ 1066 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../src/demo-1.C:10:3: note: candidate: ‘bar::bar(int)’ 10 | bar (int); | ^~~ ../../src/demo-1.C:10:3: note: candidate expects 1 argument, 0 provided ../../src/demo-1.C:7:7: note: candidate: ‘constexpr bar::bar(const bar&)’ 7 | class bar : public foo | ^~~ ../../src/demo-1.C:7:7: note: candidate expects 1 argument, 0 provided ../../src/demo-1.C:7:7: note: candidate: ‘constexpr bar::bar(bar&&)’ ../../src/demo-1.C:7:7: note: candidate expects 1 argument, 0 provided we emit: In file included from ../x86_64-pc-linux-gnu/libstdc++-v3/include/memory:78, from ../../src/demo-1.C:1: ../x86_64-pc-linux-gnu/libstdc++-v3/include/bits/unique_ptr.h: In instantiation of ‘std::__detail::__unique_ptr_t<_Tp> std::make_unique(_Args&& ...) [with _Tp = bar; _Args = {}; __detail::__unique_ptr_t<_Tp> = __detail::__unique_ptr_t]’: ../../src/demo-1.C:15:32: required from here 15 | return std::make_unique (); | ~~~~~~~~~~~~~~~~~~~~~~^~ ../x86_64-pc-linux-gnu/libstdc++-v3/include/bits/unique_ptr.h:1066:30: error: no matching function for call to ‘bar::bar()’ 1066 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../src/demo-1.C:10:3: note: candidate: ‘bar::bar(int)’ 10 | bar (int); | ^~~ ../../src/demo-1.C:10:3: note: candidate expects 1 argument, 0 provided ../../src/demo-1.C:7:7: note: candidate: ‘constexpr bar::bar(const bar&)’ 7 | class bar : public foo | ^~~ ../../src/demo-1.C:7:7: note: candidate expects 1 argument, 0 provided ../../src/demo-1.C:7:7: note: candidate: ‘constexpr bar::bar(bar&&)’ ../../src/demo-1.C:7:7: note: candidate expects 1 argument, 0 provided which shows the code that's leading to the error (the bad call to std::make_unique). Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. OK for trunk? gcc/cp/ChangeLog: * error.cc (print_instantiation_partial_context_line): Call diagnostic_show_locus. gcc/testsuite/ChangeLog: * g++.dg/diagnostic/static_assert3.C: Add directives for additional source printing. * g++.dg/template/error60.C: New test. Signed-off-by: David Malcolm --- gcc/cp/error.cc | 2 + .../g++.dg/diagnostic/static_assert3.C | 7 +++- gcc/testsuite/g++.dg/template/error60.C | 37 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/error60.C diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index ef96e140f24..767478cf5fd 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -3774,6 +3774,8 @@ print_instantiation_partial_context_line (diagnostic_context *context, ? _("recursively required from here\n") : _("required from here\n")); } + gcc_rich_location rich_loc (loc); + diagnostic_show_locus (context, &rich_loc, DK_NOTE); } /* Same as print_instantiation_full_context but less verbose. */ diff --git a/gcc/testsuite/g++.dg/diagnostic/static_assert3.C b/gcc/testsuite/g++.dg/diagnostic/static_assert3.C index 5d363884508..4ec53f17120 100644 --- a/gcc/testsuite/g++.dg/diagnostic/static_assert3.C +++ b/gcc/testsuite/g++.dg/diagnostic/static_assert3.C @@ -5,6 +5,11 @@ template struct is_same { static constexpr bool value = false; }; template struct is_same { static constexpr bool value = true; }; +/* { dg-begin-multiline-output "" } + f(0, 1.3); + ~^~~~~~~~ + { dg-end-multiline-output "" } */ + template void f(T, U) { @@ -32,5 +37,5 @@ void f(T, U) void g() { - f(0, 1.3); + f(0, 1.3); // { dg-message " required from here" } } diff --git a/gcc/testsuite/g++.dg/template/error60.C b/gcc/testsuite/g++.dg/template/error60.C new file mode 100644 index 00000000000..8c2139b207c --- /dev/null +++ b/gcc/testsuite/g++.dg/template/error60.C @@ -0,0 +1,37 @@ +// { dg-options "-fdiagnostics-show-caret" } + +template +struct my_pointer +{ + my_pointer (Foo *ptr) // { dg-message " initializing argument 1" } + : m_ptr (ptr) + {} + + Foo *m_ptr; +}; + +template +void test (Foo val) +{ + my_pointer ptr (val); // { dg-error "invalid conversion from 'int' to 'int\\*'" } +} + +void usage () +{ + test (42); // { dg-message " required from here" } + /* { dg-begin-multiline-output "" } + test (42); + ~~~~~~~~~~^~~~ + { dg-end-multiline-output "" } */ +} + + /* { dg-begin-multiline-output "" } + my_pointer (Foo *ptr) + ~~~~~^~~ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + my_pointer ptr (val); + ^~~ + | + int + { dg-end-multiline-output "" } */