From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 7A46D3858D1E for ; Fri, 20 Oct 2023 00:33:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7A46D3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7A46D3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697761989; cv=none; b=Wz/cggBHGigRehNJhi45BZ1b70+gt4/BxJOgHWtYqifIGlCpUDeYiObembd9hkVlPfW1DCnyYpeHbd7FQFxv904RdEwg4LvzrPlNibUHJmb1zs9N+ddDz2r5VLLuphyIZwnbAwjLfNc01Ccl/M98BYRJEh3RV/KGZ2Xs8QqoanA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697761989; c=relaxed/simple; bh=OXV3qLpspZrLQ/N/IrZXkiX545IJZjF4cpv1C1WIhrU=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=sj3wq3AessehGXbbPyaVIRf49BR0Ff3dXu7Q47tPiiQ3v/hC4zP+gsjT3KEe5I4fKTcTHl9XV+YyTqh6SkZI9Bnfz8RnnniZddcdpK1usKOy54ZtVecRyW6E/0aY65MRq8k8r32xynq41YUd5YYrov9MQ0GrTCQvKGWvqOjiz6s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697761987; 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: in-reply-to:in-reply-to:references:references; bh=oyIkukn4fR++C4FQJzvIZpzEu/KcGWjvclQWgkkWins=; b=XGAhpyxI3uUtlCsaZsM/GL6SE9l+oOj7kgwEK2HY3HSP0g0VDTEOY/Li5lcwT+nJeK6NtP qhDpggGKbHXofAlgIxXYWONoYgkn6qRvDCYslrB9ILvYKy/vFsSmyhe7Lj8nyydbEDgAOA HKqzYrrk5TzEy7yGTdVMeuPLaqthq0Y= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84-3OV1Cb8EMRKQePJ4HAH_7g-1; Thu, 19 Oct 2023 20:33:05 -0400 X-MC-Unique: 3OV1Cb8EMRKQePJ4HAH_7g-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-66d12cd3591so2755026d6.3 for ; Thu, 19 Oct 2023 17:33:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697761985; x=1698366785; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Nlkv7T+5IeUTnjEKpCCV8PmQKXJ2pWFhCbRtYpyvo7E=; b=b7nNauIS26p+ETDy36dhz6bTI7YiEhCVwS620txa0Gbbde5huKX9dhTmesWldrrA/f +POdN8BwHjrlSSJ6DRF6kwePLq+zgznwaAs1SGGJaiXWWFv9gDsXDefk00iUjLvFdyle ssSeAau+cGCsA7mhrUkRgOXBIHRAqpqGTCDoOhvJ4cgbgSRnrACi5Bmsm5qB/777z5xp nYVPg+EwnbTPk1LYTiN/4y3G3zKaaY1UC6ohbfckbNMJVMZrkINa5K9xgzSp31ivS9y2 goysON8LOeBJCiPphbBjJ1LMtYk3qn7I9v6kyCEhMYoWe5vQWIiI0/QsgkNvaD44mcFu s7Zg== X-Gm-Message-State: AOJu0Yy0lFZBEKPnGkhOJhqodVoLl4dANUWHUFPeZl0N+YWTD47Y7JHP l1RUY9O2CkTFt6wjxKR06o19c6MfEieKISAx0JbudYWrI3Sty3bJduRlDo0lZ5XA6NIp3mCffAE AZQIlBXhaKxZqDMvKvg== X-Received: by 2002:a05:6214:1d09:b0:66d:44b6:8aa8 with SMTP id e9-20020a0562141d0900b0066d44b68aa8mr496792qvd.47.1697761985146; Thu, 19 Oct 2023 17:33:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnRPfZw0Xcy3pRHz9irqTH8uEJEXzpvdM1bvUyg635K/FeBC5qwKh26OyMqWIudAYR1t0XXg== X-Received: by 2002:a05:6214:1d09:b0:66d:44b6:8aa8 with SMTP id e9-20020a0562141d0900b0066d44b68aa8mr496779qvd.47.1697761984876; Thu, 19 Oct 2023 17:33:04 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id d14-20020a0cfe8e000000b0065896b9fb15sm266002qvs.29.2023.10.19.17.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 17:33:04 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Thu, 19 Oct 2023 20:33:03 -0400 (EDT) To: David Malcolm cc: gcc-patches@gcc.gnu.org, Jason Merrill Subject: Re: [PATCH] c++: print source code in print_instantiation_partial_context_line In-Reply-To: <20231003164812.13294-1-dmalcolm@redhat.com> Message-ID: <97fee3fd-40ec-dc65-9ca1-daf991cb09db@idea> References: <20231003164812.13294-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="8323329-1268258379-1697761984=:986507" X-Spam-Status: No, score=-13.8 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 List-Id: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-1268258379-1697761984=:986507 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT On Tue, 3 Oct 2023, David Malcolm wrote: > 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). This is great! I noticed however that the source code gets printed in a surprising way in some contexts. Consider: template void f(typename T::type); int main() { f(0); } For this testcase we emit: testcase.C: In function ‘int main()’: testcase.C:4:9: error: no matching function for call to ‘f(int)’ 4 | f(0); | ~~~~~~^~~ testcase.C:1:24: note: candidate: ‘template void f(typename T::type)’ 1 | template void f(typename T::type); | ^ testcase.C:1:24: note: template argument deduction/substitution failed: testcase.C: In substitution of ‘template void f(typename T::type) [with T = int]’: testcase.C:4:9: required from here testcase.C:1:24: note: 4 | f(0); testcase.C:1:24: note: | ~~~~~~^~~ testcase.C:1:24: error: ‘int’ is not a class, struct, or union type 1 | template void f(typename T::type); | ^ In particular the source code part following the "required from here" line testcase.C:4:9: required from here testcase.C:1:24: note: 4 | f(0); testcase.C:1:24: note: | ~~~~~~^~~ seems off, I would have expected it be testcase.C:4:9: required from here 4 | f(0); | ~~~~~~^~~ i.e. without the "testcase.C:1:24: note: " prefix. Does this look expected? (I also wonder if we might want to omit printing the source code altogether in this case, since we already printed that same line earlier during the "no matching function" error?) > > > 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 "" } */ > -- > 2.26.3 > > --8323329-1268258379-1697761984=:986507--