Perform overload resolution immediately in C++ operator lookup. (#6416)

Don't attempt to defer overload resolution by creating a
`CppOverloadSet`; this was incorrect as we weren't saving the complete
clang::OverloadCandidateSet, resulting in template candidates not being
found. Moreover, saving the overload candidate set would be expensive,
as the representation is surprisingly large, and is unnecessary since
we're about to build a call.

In passing, improve the diagnostics for overload resolution failure to
use Clang's operator overload resolution messages rather than its call
overload resolution messages.

This fixes calls to templated operator overloads, which is the final
piece needed for us to successfully compile an iostream-based "Hello
world" program.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2025-11-24 23:01:28 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 167b45ca35
commit 054dfca685
8 changed files with 339 additions and 133 deletions
+43 -20
View File
@@ -2,31 +2,54 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import Cpp inline "#include <cstdio>";
fn Run() {
// TODO: Requires class with virtual bases (`basic_ostream`).
// Cpp.std.cout << "Hello world!\n";
// TODO: Requires variadic function.
// Cpp.printf("Hello world!\n");
// TODO: Requires nullable pointer.
// Cpp.puts("Hello world!\n");
// TODO: Requires nullable void pointer.
// Cpp.write(1, "Hello world!\n", 13);
// TODO: Requires Core.String API.
// let message: str = "Hello world!\n\n";
// for (c: char in message) {
// Cpp.putchar((c as u8) as i32);
// }
import Cpp library "<cstdio>";
import Cpp library "<iostream>";
import Cpp library "<string_view>";
import Cpp library "<unistd.h>";
// Demonstrate passing `char`s to a C++ function.
fn HelloPutchar() {
let message: array(char, 13) =
('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n');
for (c: char in message) {
// TODO: u8 should probably have an implicit cast to i32.
Cpp.putchar((c as u8) as i32);
}
// TODO: Use a loop over a string literal instead. Requires IndexWith support
// for str.
//
// let message: str = "Hello world!\n";
// for (c: char in message) {
// Cpp.putchar((c as u8) as i32);
// }
}
// Demonstrate passing a null-terminated string to a C++ function.
fn HelloStdio() {
// TODO: Requires mapping from Optional(const char*) into C++.
// TODO: There should be a better way to interact with functions that expect a
// null-terminated string.
// Cpp.puts(Cpp.std.data("Hello world!\n\0"));
// TODO: Requires variadic function support.
// Cpp.printf("Hello world!\n\0");
}
// Demonstrate passing a string as a void pointer to a C++ function.
fn HelloWrite() {
// TODO: Requires mapping from Optional(const char*) into a C++ const void*.
// let s: str = "Hello world!\n";
// Cpp.write(1, Cpp.std.data(s), Cpp.std.size(s));
}
fn HelloIostreams() {
Cpp.std.cout << "Hello world!\n";
}
fn Run() {
HelloPutchar();
HelloStdio();
HelloWrite();
HelloIostreams();
}