https://gcc.gnu.org/g:6e52c1dbfb77ed09bc1352696384c80a45c70172

commit r16-7536-g6e52c1dbfb77ed09bc1352696384c80a45c70172
Author: David Malcolm <[email protected]>
Date:   Mon Feb 16 21:55:55 2026 -0500

    c++: fix missing file:line:column on "required from here" [PR122001]
    
    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 <[email protected]>

Diff:
---
 gcc/cp/error.cc                                    | 23 ++++++++++++++++------
 .../diagnostic/instantiation-context-pr122001-1.C  | 13 ++++++++++++
 .../diagnostic/instantiation-context-pr122001-2.C  | 13 ++++++++++++
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index e64da242152e..25e438c1784d 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -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
index 000000000000..a1e888a77990
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-1.C
@@ -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
index 000000000000..03edeb12068f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/instantiation-context-pr122001-2.C
@@ -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" }

Reply via email to