[llvm] [Support] Fix format_object streaming ambiguity in Objective-C++ mode (PR #215826)
Anthony Latsis via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 08:27:33 PDT 2026
https://github.com/AnthonyLatsis created https://github.com/llvm/llvm-project/pull/215826
Upstreams https://github.com/swiftlang/llvm-project/pull/13625.
Streaming a format_object writes it through a temporary lambda. Swift's C++ interoperability enables block pointer conversions, which makes `raw_ostream::operator<<(const void *)` a viable candidate alongside `operator<<(function_ref<size_t(char *, size_t)>)`, so overload resolution is ambiguous. This breaks the Swift compiler because it contains Swift code that interoperates with C++ code that instantiates this template.
Bind the lambda to an explicit function_ref before streaming so the intended overload is selected unambiguously in every language mode.
Assisted-by: Claude Code
>From 622dd8b1f94fc9e3365421abf61db9b12b7f95e2 Mon Sep 17 00:00:00 2001
From: Anthony Latsis <alatsis at apple.com>
Date: Wed, 22 Jul 2026 18:38:25 +0100
Subject: [PATCH] [Support] Fix format_object streaming ambiguity in
Objective-C++ mode
Streaming a format_object writes it through a temporary lambda. Swift's
C++ interoperability enables block pointer conversions, which makes
`raw_ostream::operator<<(const void *)` a viable candidate alongside
`operator<<(function_ref<size_t(char *, size_t)>)`, so
overload resolution is ambiguous. This breaks the Swift compiler because
it contains Swift code that interoperates with C++ code that
instantiates this template.
Bind the lambda to an explicit function_ref before streaming so the
intended overload is selected unambiguously in every language mode.
Assisted-by: Claude Code
---
llvm/include/llvm/Support/Format.h | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Support/Format.h b/llvm/include/llvm/Support/Format.h
index 9f8bd7eddaba5..ae9f08f1f3ac4 100644
--- a/llvm/include/llvm/Support/Format.h
+++ b/llvm/include/llvm/Support/Format.h
@@ -76,8 +76,16 @@ template <typename... Ts> class format_object {
template <typename... Ts>
raw_ostream &operator<<(raw_ostream &OS, format_object<Ts...> Fmt) {
- OS <<
- [&Fmt](char *Buf, size_t Size) -> int { return Fmt.snprint(Buf, Size); };
+ // Stream through an explicitly-typed function_ref. Passing the lambda
+ // directly is ambiguous when block pointer conversions are enabled due to a
+ // competing raw_ostream::operator<<(const void *) candidate. This ambiguity
+ // affects the Swift compiler because it contains Swift code that
+ // interoperates with C++ code that instantiates this template, and Swift's
+ // C++ interoperability enables block pointer conversions.
+ auto Print = [&Fmt](char *Buf, size_t Size) -> int {
+ return Fmt.snprint(Buf, Size);
+ };
+ OS << function_ref<int(char *, size_t)>(Print);
return OS;
}
More information about the llvm-commits
mailing list