Support for mapping str to std::string_view in interop. (#6079)

We already did the opposite direction; this enables use of `str` in
overload resolution.

Fixes #6062

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
This commit is contained in:
Richard Smith
2025-09-16 23:40:33 +00:00
committed by GitHub
co-authored by Dana Jansens Carbon Infra Bot
parent 9d84391f11
commit 730935691a
5 changed files with 127 additions and 88 deletions
@@ -10,8 +10,6 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/stdlib/string_view.carbon
// TODO: Tests marked as `fail_todo_5891_` to fixed as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
// --- string_view.h
namespace std {
@@ -38,7 +36,7 @@ namespace std {
auto Consume(std::string_view sv) -> void;
auto Produce() -> std::string_view;
// --- fail_todo_5891_import_multiple.carbon
// --- import_multiple.carbon
library "[[@TEST_NAME]]";
@@ -46,13 +44,6 @@ import Cpp library "string_view.h";
//@dump-sem-ir-begin
fn F() {
// CHECK:STDERR: fail_todo_5891_import_multiple.carbon:[[@LINE+7]]:15: error: call argument of type `str` is not supported [CppCallArgTypeNotSupported]
// CHECK:STDERR: Cpp.Consume("hello");
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_todo_5891_import_multiple.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
// CHECK:STDERR: Cpp.Consume("hello");
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
Cpp.Consume("hello");
}
//@dump-sem-ir-end
@@ -63,7 +54,7 @@ fn G() -> str {
}
//@dump-sem-ir-end
// CHECK:STDOUT: --- fail_todo_5891_import_multiple.carbon
// CHECK:STDOUT: --- import_multiple.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
@@ -75,17 +66,19 @@ fn G() -> str {
// CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
// CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
// CHECK:STDOUT: %ptr.3e8: type = ptr_type %u8 [concrete]
// CHECK:STDOUT: %.fd2: type = cpp_overload_set_type @Produce [concrete]
// CHECK:STDOUT: %.fd2: type = cpp_overload_set_type @Consume__carbon_thunk [concrete]
// CHECK:STDOUT: %empty_struct.c28: %.fd2 = struct_value () [concrete]
// CHECK:STDOUT: %str.3b1: %ptr.3e8 = string_literal "hello" [concrete]
// CHECK:STDOUT: %int_5: %u64 = int_value 5 [concrete]
// CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str.3b1, %int_5) [concrete]
// CHECK:STDOUT: %pattern_type.461: type = pattern_type %str.ee0 [concrete]
// CHECK:STDOUT: %ptr.85f: type = ptr_type %str.ee0 [concrete]
// CHECK:STDOUT: %Consume__carbon_thunk.type: type = fn_type @Consume__carbon_thunk [concrete]
// CHECK:STDOUT: %Consume__carbon_thunk: %Consume__carbon_thunk.type = struct_value () [concrete]
// CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
// CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
// CHECK:STDOUT: %.a47: type = cpp_overload_set_type @Produce__carbon_thunk [concrete]
// CHECK:STDOUT: %.a47: type = cpp_overload_set_type @G [concrete]
// CHECK:STDOUT: %empty_struct.ab9: %.a47 = struct_value () [concrete]
// CHECK:STDOUT: %ptr.85f: type = ptr_type %str.ee0 [concrete]
// CHECK:STDOUT: %Produce__carbon_thunk.type: type = fn_type @Produce__carbon_thunk [concrete]
// CHECK:STDOUT: %Produce__carbon_thunk: %Produce__carbon_thunk.type = struct_value () [concrete]
// CHECK:STDOUT: }
@@ -96,8 +89,13 @@ fn G() -> str {
// CHECK:STDOUT: .Produce = %.5d1
// CHECK:STDOUT: import Cpp//...
// CHECK:STDOUT: }
// CHECK:STDOUT: %.f17: %.fd2 = cpp_overload_set_value @Produce [concrete = constants.%empty_struct.c28]
// CHECK:STDOUT: %.5d1: %.a47 = cpp_overload_set_value @Produce__carbon_thunk [concrete = constants.%empty_struct.ab9]
// CHECK:STDOUT: %.f17: %.fd2 = cpp_overload_set_value @Consume__carbon_thunk [concrete = constants.%empty_struct.c28]
// CHECK:STDOUT: %Consume__carbon_thunk.decl: %Consume__carbon_thunk.type = fn_decl @Consume__carbon_thunk [concrete = constants.%Consume__carbon_thunk] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.5d1: %.a47 = cpp_overload_set_value @G [concrete = constants.%empty_struct.ab9]
// CHECK:STDOUT: %Produce__carbon_thunk.decl: %Produce__carbon_thunk.type = fn_decl @Produce__carbon_thunk [concrete = constants.%Produce__carbon_thunk] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
@@ -123,6 +121,9 @@ fn G() -> str {
// CHECK:STDOUT: %str: %ptr.3e8 = string_literal "hello" [concrete = constants.%str.3b1]
// CHECK:STDOUT: %int_5: %u64 = int_value 5 [concrete = constants.%int_5]
// CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str, %int_5) [concrete = constants.%String.val]
// CHECK:STDOUT: %.loc8: ref %str.ee0 = value_as_ref %String.val
// CHECK:STDOUT: %addr: %ptr.85f = addr_of %.loc8
// CHECK:STDOUT: %Consume__carbon_thunk.call: init %empty_tuple.type = call imports.%Consume__carbon_thunk.decl(%addr)
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -130,10 +131,10 @@ fn G() -> str {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Produce.ref: %.a47 = name_ref Produce, imports.%.5d1 [concrete = constants.%empty_struct.ab9]
// CHECK:STDOUT: %.loc20: ref %str.ee0 = splice_block %return {}
// CHECK:STDOUT: %addr: %ptr.85f = addr_of %.loc20
// CHECK:STDOUT: %.loc13: ref %str.ee0 = splice_block %return {}
// CHECK:STDOUT: %addr: %ptr.85f = addr_of %.loc13
// CHECK:STDOUT: %Produce__carbon_thunk.call: init %empty_tuple.type = call imports.%Produce__carbon_thunk.decl(%addr)
// CHECK:STDOUT: %.loc21: init %str.ee0 = in_place_init %Produce__carbon_thunk.call, %.loc20
// CHECK:STDOUT: return %.loc21 to %return
// CHECK:STDOUT: %.loc14: init %str.ee0 = in_place_init %Produce__carbon_thunk.call, %.loc13
// CHECK:STDOUT: return %.loc14 to %return
// CHECK:STDOUT: }
// CHECK:STDOUT: