public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: David Malcolm <dmalcolm@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: print source code in print_instantiation_partial_context_line
Date: Tue, 3 Oct 2023 17:10:52 -0400	[thread overview]
Message-ID: <bbc27ffb-5ea2-4e94-8f45-bfc530a20ee4@redhat.com> (raw)
In-Reply-To: <20231003164812.13294-1-dmalcolm@redhat.com>

On 10/3/23 12:48, 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<bar>]’:
> ../../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<bar>]’:
> ../../src/demo-1.C:15:32:   required from here
>     15 |   return std::make_unique<bar> ();
>        |          ~~~~~~~~~~~~~~~~~~~~~~^~
> ../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?

OK, thanks.  Now that you mention it, that's long been a small annoyance 
that never quite reached the point that it occurred to me to fix it.

Jason

> 
> 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 <dmalcolm@redhat.com>
> ---
>   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 <typename T, typename U> struct is_same { static constexpr bool value = false; };
>   template <typename T> struct is_same<T, T> { static constexpr bool value = true; };
>   
> +/* { dg-begin-multiline-output "" }
> +  f(0, 1.3);
> +  ~^~~~~~~~
> +   { dg-end-multiline-output "" } */
> +
>   template <typename T, typename U>
>   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 <typename Foo>
> +struct my_pointer
> +{
> +  my_pointer (Foo *ptr) // { dg-message " initializing argument 1" }
> +  : m_ptr (ptr)
> +  {}
> +
> +  Foo *m_ptr;
> +};
> +
> +template <typename Foo>
> +void test (Foo val)
> +{
> +  my_pointer<Foo> ptr (val); // { dg-error "invalid conversion from 'int' to 'int\\*'" }
> +}
> +
> +void usage ()
> +{
> +  test<int> (42); // { dg-message " required from here" }
> +  /* { dg-begin-multiline-output "" }
> +   test<int> (42);
> +   ~~~~~~~~~~^~~~
> +     { dg-end-multiline-output "" } */
> +}
> +
> +  /* { dg-begin-multiline-output "" }
> +   my_pointer (Foo *ptr)
> +               ~~~~~^~~
> +     { dg-end-multiline-output "" } */
> +  /* { dg-begin-multiline-output "" }
> +   my_pointer<Foo> ptr (val);
> +                        ^~~
> +                        |
> +                        int
> +     { dg-end-multiline-output "" } */


  reply	other threads:[~2023-10-03 21:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 16:48 David Malcolm
2023-10-03 21:10 ` Jason Merrill [this message]
2023-10-20  0:33 ` Patrick Palka
2024-04-09 19:41   ` Patrick Palka
2024-04-24 19:17     ` Patrick Palka
2024-04-24 19:28       ` Patrick Palka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bbc27ffb-5ea2-4e94-8f45-bfc530a20ee4@redhat.com \
    --to=jason@redhat.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).