]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fix missing file:line:column on "required from here" [PR122001]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 17 Feb 2026 02:55:55 +0000 (21:55 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 17 Feb 2026 02:55:55 +0000 (21:55 -0500)
PR diagnostics/122001 notes that in GCC 15 we emit:

<source>: In instantiation of 'void foo(T) [with T = std::nullptr_t]':
<source>:7:8:   required from here
    7 |     foo(nullptr);
      |     ~~~^~~~~~~~~
<source>:3:7: error: invalid operands of types 'int' and 'std::nullptr_t' to binary 'operator+'
    3 |     1 + t;
      |     ~~^~~

whereas in GCC 16 we emit:

<source>: In instantiation of 'void foo(T) [with T = std::nullptr_t]':
required from here
<source>:7:8:
    7 |     foo(nullptr);
      |     ~~~^~~~~~~~~
<source>:3:7: error: invalid operands of types 'int' and 'std::nullptr_t' to binary 'operator+'
    3 |     1 + t;
      |     ~~^~~

where the "required from line" has lost its file:line:column prefix,
with the latter appearing on a follow line.

The root cause is that in r15-5995-g339246fb9ef4ca I changed
cp/error.cc's print_instantiation_partial_context_line from using
print_location to using auto_context_line, so that the location
information would be better integrated with nested diagnostics,
and enabled this by default in r16-3092-gd3fe5a560f0bcc.

text_sink::build_indent_prefix is returning the empty string for such
lines, rather than an indented bullet point.

This patch tweaks things so that such lines print the location before
the message at the top nesting level, and on a separate line at nested
nesting levels, and always the latter with
-fno-diagnostics-show-nesting.

gcc/cp/ChangeLog:
PR diagnostics/122001
* error.cc (auto_context_line::auto_context_line): Initialize
m_nesting_level and m_location_printed.  Update the condition
for printing location in ctor to also do it at top-level
nesting level.  Record into m_location_printed if we did
print the location.
(auto_context_line::~auto_context_line): Don't call print_location
if we already printed it in the ctor.
(auto_context_line::m_nesting_level): New field.
(auto_context_line::m_location_printed): New field.

gcc/testsuite/ChangeLog:
PR diagnostics/122001
* g++.dg/diagnostic/instantiation-context-pr122001-1.C: New test.
* g++.dg/diagnostic/instantiation-context-pr122001-2.C: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/cp/error.cc
gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-2.C [new file with mode: 0644]

index e64da242152ec521291de0f5d99d603323d334fa..25e438c1784da9ac6634c81b3ddfee3d552e75e2 100644 (file)
@@ -4091,13 +4091,18 @@ public:
                     bool show_locus = false)
   : m_text_output (text_output),
     m_loc (loc),
-    m_show_locus (show_locus)
+    m_show_locus (show_locus),
+    m_nesting_level (text_output.get_context ().get_diagnostic_nesting_level ()),
+    m_location_printed (false)
   {
     char *indent = m_text_output.build_indent_prefix (true);
     pp_verbatim (m_text_output.get_printer (), indent);
     free (indent);
-    if (!m_text_output.show_nesting_p ())
-      print_location (m_text_output, m_loc);
+    if (m_nesting_level == 0 || !m_text_output.show_nesting_p ())
+      {
+       print_location (m_text_output, m_loc);
+       m_location_printed = true;
+      }
   }
   ~auto_context_line ()
   {
@@ -4107,9 +4112,13 @@ public:
        if (m_text_output.show_locations_in_nesting_p ())
          {
            char *indent = m_text_output.build_indent_prefix (false);
-           pp_verbatim (pp, indent);
-           print_location (m_text_output, m_loc);
-           pp_newline (pp);
+           if (!m_location_printed)
+             {
+               pp_verbatim (pp, indent);
+               print_location (m_text_output, m_loc);
+               pp_newline (pp);
+               m_location_printed = true;
+             }
 
            char *saved_prefix = pp_take_prefix (pp);
            pp_set_prefix (pp, indent);
@@ -4137,6 +4146,8 @@ private:
   diagnostics::text_sink &m_text_output;
   location_t m_loc;
   bool m_show_locus;
+  int m_nesting_level;
+  bool m_location_printed;
 };
 
 /* Helper function of print_instantiation_partial_context() that
diff --git a/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-1.C b/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-1.C
new file mode 100644 (file)
index 0000000..a1e888a
--- /dev/null
@@ -0,0 +1,13 @@
+// { dg-additional-options "-fdiagnostics-show-nesting" }
+
+template <typename T>
+void foo(T t) {
+  1 + t; // { dg-error "pointer-arith" }
+}
+
+int main() {
+  foo((void *)0);
+}
+
+// Verify that we print the file/line/column before the "required from here"
+// { dg-regexp ".*instantiation-context-pr122001-1.C:9:6:   required from here" }
diff --git a/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-2.C b/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-2.C
new file mode 100644 (file)
index 0000000..03edeb1
--- /dev/null
@@ -0,0 +1,13 @@
+// { dg-additional-options "-fno-diagnostics-show-nesting" }
+
+template <typename T>
+void foo(T t) {
+  1 + t; // { dg-error "pointer-arith" }
+}
+
+int main() {
+  foo((void *)0);
+}
+
+// Verify that we print the file/line/column before the "required from here"
+// { dg-regexp ".*instantiation-context-pr122001-2.C:9:6:   required from here" }