From 19c72acff6485e4bfa3ced3a94be11885751fd97 Mon Sep 17 00:00:00 2001
From: Michael Gottesman <mgottesman@apple.com>
Date: Mon, 1 Apr 2024 19:25:04 -0700
Subject: [PATCH] [silgen] Emit the location of the original function def if
 SILGen is erroring on a duplicate symbol definition.

This diagnostic is useful around silgen_name where it validates that we do not
have any weird collisions. Sadly, it just points where one of the conflicting
elements is... with this patch, we also emit an error on the other function if
we have a SILLocation.
---
 include/swift/AST/DiagnosticsSIL.def           | 3 +++
 lib/SILGen/SILGen.cpp                          | 2 ++
 test/SILGen/diagnose_duplicate_functions.swift | 4 ++--
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/swift/AST/DiagnosticsSIL.def b/include/swift/AST/DiagnosticsSIL.def
index 73ea8e6913140..90f386946ef8c 100644
--- a/include/swift/AST/DiagnosticsSIL.def
+++ b/include/swift/AST/DiagnosticsSIL.def
@@ -46,6 +46,9 @@ ERROR(bridging_objcbridgeable_broken,none,
 ERROR(sil_function_redefinition,none,
       "multiple definitions of symbol '%0'",
       (StringRef))
+NOTE(sil_function_redefinition_note,none,
+      "other definition here",
+      ())
 
 ERROR(invalid_sil_builtin,none,
       "INTERNAL ERROR: invalid use of builtin: %0",
diff --git a/lib/SILGen/SILGen.cpp b/lib/SILGen/SILGen.cpp
index 12b2417e68b3b..478cae4a0273c 100644
--- a/lib/SILGen/SILGen.cpp
+++ b/lib/SILGen/SILGen.cpp
@@ -813,6 +813,8 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
   if (!f->empty()) {
     diagnose(constant.getAsRegularLocation(), diag::sil_function_redefinition,
              f->getName());
+    if (f->hasLocation())
+      diagnose(f->getLocation(), diag::sil_function_redefinition_note);
     return;
   }
 
diff --git a/test/SILGen/diagnose_duplicate_functions.swift b/test/SILGen/diagnose_duplicate_functions.swift
index 1f70c85d64347..bfa37ae6a2919 100644
--- a/test/SILGen/diagnose_duplicate_functions.swift
+++ b/test/SILGen/diagnose_duplicate_functions.swift
@@ -1,7 +1,7 @@
 // RUN: %target-swift-emit-silgen %s -o /dev/null -verify
 
 @_silgen_name("foo")
-func a(_ x: Int) -> Int {
+func a(_ x: Int) -> Int { // expected-note {{other definition here}}
   return x
 }
 
@@ -11,7 +11,7 @@ func b(_ x: Int) -> Int { // expected-error {{multiple definitions of symbol 'fo
 }
 
 @_cdecl("bar")
-func c(_ x: Int) -> Int {
+func c(_ x: Int) -> Int { // expected-note {{other definition here}}
   return x
 }