From 6086d6eef28e101086a174756da4c635ef846770 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 16 Sep 2025 18:04:24 -0700 Subject: [PATCH] Make `str` copyable. (#6083) --- core/prelude/types/string.carbon | 4 ++++ toolchain/lower/testdata/primitives/string.carbon | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/prelude/types/string.carbon b/core/prelude/types/string.carbon index 984d0e34bd4a..2f70a2372249 100644 --- a/core/prelude/types/string.carbon +++ b/core/prelude/types/string.carbon @@ -12,6 +12,10 @@ import library "prelude/types/uint"; class String { fn Size[self: Self]() -> u64 { return self.size; } + impl as Copy { + fn Op[self: Self]() -> Self { return {.ptr = self.ptr, .size = self.size}; } + } + // TODO: This should be an array iterator. private var ptr: Char*; // TODO: This should be a word-sized integer. diff --git a/toolchain/lower/testdata/primitives/string.carbon b/toolchain/lower/testdata/primitives/string.carbon index 977a8e17e3a3..0037d91f86d7 100644 --- a/toolchain/lower/testdata/primitives/string.carbon +++ b/toolchain/lower/testdata/primitives/string.carbon @@ -31,6 +31,10 @@ fn I() { F(v); } +fn Copy(s: str) -> str { + return s; +} + // CHECK:STDOUT: ; ModuleID = 'string.carbon' // CHECK:STDOUT: source_filename = "string.carbon" // CHECK:STDOUT: @@ -59,6 +63,14 @@ fn I() { // CHECK:STDOUT: ret void, !dbg !14 // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: define void @_CCopy.Main(ptr sret({ ptr, i64 }) %return, ptr %s) !dbg !15 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @"_COp.String.Core:Copy.Core"(ptr %return, ptr %s), !dbg !16 +// CHECK:STDOUT: ret void, !dbg !17 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: declare void @"_COp.String.Core:Copy.Core"(ptr sret({ ptr, i64 }), ptr) +// CHECK:STDOUT: // CHECK:STDOUT: !llvm.module.flags = !{!0, !1} // CHECK:STDOUT: !llvm.dbg.cu = !{!2} // CHECK:STDOUT: @@ -77,3 +89,6 @@ fn I() { // CHECK:STDOUT: !12 = distinct !DISubprogram(name: "I", linkageName: "_CI.Main", scope: null, file: !3, line: 28, type: !5, spFlags: DISPFlagDefinition, unit: !2) // CHECK:STDOUT: !13 = !DILocation(line: 31, column: 3, scope: !12) // CHECK:STDOUT: !14 = !DILocation(line: 28, column: 1, scope: !12) +// CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Copy", linkageName: "_CCopy.Main", scope: null, file: !3, line: 34, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !16 = !DILocation(line: 35, column: 10, scope: !15) +// CHECK:STDOUT: !17 = !DILocation(line: 35, column: 3, scope: !15)