From c329bce2408ca4844ffa4d4bd155bf06bc707e75 Mon Sep 17 00:00:00 2001 From: josh11b <15258583+josh11b@users.noreply.github.com> Date: Thu, 23 Oct 2025 09:38:20 -0700 Subject: [PATCH] `PrintChar` from `"io"` library takes a `char` (#6264) As [observed in #toolchain](https://discord.com/channels/655572317891461132/655578254970716160/1428791603786416351), this makes demo code nicer. --------- Co-authored-by: Josh L --- core/io.carbon | 3 +- examples/advent2024/day11_part2.carbon | 10 +-- examples/advent2024/io_utils.carbon | 4 +- .../check/testdata/builtins/print/char.carbon | 77 +++++++++---------- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/core/io.carbon b/core/io.carbon index 00e5fa300519..5cd54ed91ebf 100644 --- a/core/io.carbon +++ b/core/io.carbon @@ -12,7 +12,8 @@ import library "prelude"; // TODO: Support printing other types. // TODO: Consider rewriting using native support once library support exists. fn Print(x: i32) = "print.int"; -fn PrintChar(x: i32) -> i32 = "print.char"; +fn PrintChar(x: char) -> i32 = "print.char"; +// TODO: Return an `Optional(char)` instead of `i32`. fn ReadChar() -> i32 = "read.char"; // TODO: Change this to a global constant once they are fully supported. diff --git a/examples/advent2024/day11_part2.carbon b/examples/advent2024/day11_part2.carbon index e5d13abc9b17..7c7a4f07dc31 100644 --- a/examples/advent2024/day11_part2.carbon +++ b/examples/advent2024/day11_part2.carbon @@ -23,15 +23,15 @@ class Digits { fn Print[self: Self](max_depth: i32) { for (digit: i32 in Core.Range(10)) { - Core.PrintChar(digit + 0x30); - Core.PrintChar(0x3A); + Core.PrintChar(((digit + 0x30) as u8) as char); + Core.PrintChar(':'); for (depth: i32 in Core.Range(max_depth)) { - Core.PrintChar(0x20); + Core.PrintChar(' '); PrintInt64NoNewline(self.count[digit][depth]); } - Core.PrintChar(0x0A); + Core.PrintChar('\n'); } - Core.PrintChar(0x0A); + Core.PrintChar('\n'); } var count: array(array(i64, 75), 10); diff --git a/examples/advent2024/io_utils.carbon b/examples/advent2024/io_utils.carbon index bb16cb32f6e7..7540537fc9d7 100644 --- a/examples/advent2024/io_utils.carbon +++ b/examples/advent2024/io_utils.carbon @@ -72,7 +72,7 @@ fn PrintInt64NoNewline(n_val: i64) { } while (pow10 != 0) { let d: i64 = n / pow10; - Core.PrintChar((d + 0x30) as i32); + Core.PrintChar(((d + 0x30) as u8) as char); n = n % pow10; pow10 = pow10 / 10; } @@ -80,7 +80,7 @@ fn PrintInt64NoNewline(n_val: i64) { fn PrintInt64(n_val: i64) { PrintInt64NoNewline(n_val); - Core.PrintChar(0xA); + Core.PrintChar('\n'); } fn PeekChar() -> i32 { diff --git a/toolchain/check/testdata/builtins/print/char.carbon b/toolchain/check/testdata/builtins/print/char.carbon index e9de27528d3e..404cee77d41c 100644 --- a/toolchain/check/testdata/builtins/print/char.carbon +++ b/toolchain/check/testdata/builtins/print/char.carbon @@ -12,51 +12,47 @@ import Core library "io"; -fn PrintChar(a: i32) -> i32 = "print.char"; +fn PrintChar(a: char) -> i32 = "print.char"; fn Main() { //@dump-sem-ir-begin - PrintChar(1); - Core.PrintChar(2); + PrintChar('1'); + Core.PrintChar('2'); //@dump-sem-ir-end } // CHECK:STDOUT: --- char.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %char: type = class_type @Char [concrete] // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete] // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete] // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] // CHECK:STDOUT: %PrintChar.type.c95: type = fn_type @PrintChar.loc15 [concrete] // CHECK:STDOUT: %PrintChar.843: %PrintChar.type.c95 = struct_value () [concrete] -// CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete] +// CHECK:STDOUT: %.75c: Core.CharLiteral = char_value U+0031 [concrete] // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete] // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete] -// CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete] -// CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete] -// CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340 = struct_value () [symbolic] -// CHECK:STDOUT: %ImplicitAs.impl_witness.204: = impl_witness imports.%ImplicitAs.impl_witness_table.9e9, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584 = struct_value () [concrete] -// CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.204) [concrete] -// CHECK:STDOUT: %.1df: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.abf: = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete] -// CHECK:STDOUT: %bound_method.c11: = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete] -// CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete] +// CHECK:STDOUT: %ImplicitAs.type.3be: type = facet_type <@ImplicitAs, @ImplicitAs(%char)> [concrete] +// CHECK:STDOUT: %ImplicitAs.Convert.type.f57: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%char) [concrete] +// CHECK:STDOUT: %ImplicitAs.impl_witness.892: = impl_witness imports.%ImplicitAs.impl_witness_table.1f2 [concrete] +// CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.3be = facet_value Core.CharLiteral, (%ImplicitAs.impl_witness.892) [concrete] +// CHECK:STDOUT: %.8d6: type = fn_type_with_self_type %ImplicitAs.Convert.type.f57, %ImplicitAs.facet [concrete] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert.type: type = fn_type @Core.CharLiteral.as.ImplicitAs.impl.Convert [concrete] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert: %Core.CharLiteral.as.ImplicitAs.impl.Convert.type = struct_value () [concrete] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert.bound.fd2: = bound_method %.75c, %Core.CharLiteral.as.ImplicitAs.impl.Convert [concrete] +// CHECK:STDOUT: %int_49: %char = int_value 49 [concrete] // CHECK:STDOUT: %PrintChar.type.089: type = fn_type @PrintChar.1 [concrete] // CHECK:STDOUT: %PrintChar.d75: %PrintChar.type.089 = struct_value () [concrete] -// CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.b82: = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete] -// CHECK:STDOUT: %bound_method.8bd: = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete] -// CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete] +// CHECK:STDOUT: %.f8d: Core.CharLiteral = char_value U+0032 [concrete] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert.bound.1f7: = bound_method %.f8d, %Core.CharLiteral.as.ImplicitAs.impl.Convert [concrete] +// CHECK:STDOUT: %int_50: %char = int_value 50 [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { // CHECK:STDOUT: %Core: = namespace file.%Core.import, [concrete] { +// CHECK:STDOUT: .Char = %Core.Char // CHECK:STDOUT: .Int = %Core.Int // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs // CHECK:STDOUT: .PrintChar = %Core.PrintChar @@ -64,36 +60,33 @@ fn Main() { // CHECK:STDOUT: import Core//io // CHECK:STDOUT: import Core//prelude/... // CHECK:STDOUT: } +// CHECK:STDOUT: %Core.Char: type = import_ref Core//prelude/types/char, Char, loaded [concrete = constants.%char] // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic] // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic] -// CHECK:STDOUT: %Core.import_ref.ee7: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340) = import_ref Core//prelude/types/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0)] -// CHECK:STDOUT: %ImplicitAs.impl_witness_table.9e9 = impl_witness_table (%Core.import_ref.ee7), @Core.IntLiteral.as.ImplicitAs.impl [concrete] +// CHECK:STDOUT: %Core.import_ref.543: %Core.CharLiteral.as.ImplicitAs.impl.Convert.type = import_ref Core//prelude/types/char, loc{{\d+_\d+}}, loaded [concrete = constants.%Core.CharLiteral.as.ImplicitAs.impl.Convert] +// CHECK:STDOUT: %ImplicitAs.impl_witness_table.1f2 = impl_witness_table (%Core.import_ref.543), @Core.CharLiteral.as.ImplicitAs.impl [concrete] // CHECK:STDOUT: %Core.PrintChar: %PrintChar.type.089 = import_ref Core//io, PrintChar, loaded [concrete = constants.%PrintChar.d75] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @Main() { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %PrintChar.ref.loc19: %PrintChar.type.c95 = name_ref PrintChar, file.%PrintChar.decl [concrete = constants.%PrintChar.843] -// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8] -// CHECK:STDOUT: %impl.elem0.loc19: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0] -// CHECK:STDOUT: %bound_method.loc19_13.1: = bound_method %int_1, %impl.elem0.loc19 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.abf] -// CHECK:STDOUT: %specific_fn.loc19: = specific_function %impl.elem0.loc19, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc19_13.2: = bound_method %int_1, %specific_fn.loc19 [concrete = constants.%bound_method.c11] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc19: init %i32 = call %bound_method.loc19_13.2(%int_1) [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %.loc19_13.1: %i32 = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc19 [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %.loc19_13.2: %i32 = converted %int_1, %.loc19_13.1 [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %PrintChar.call.loc19: init %i32 = call %PrintChar.ref.loc19(%.loc19_13.2) +// CHECK:STDOUT: %.loc19_13.1: Core.CharLiteral = char_value U+0031 [concrete = constants.%.75c] +// CHECK:STDOUT: %impl.elem0.loc19: %.8d6 = impl_witness_access constants.%ImplicitAs.impl_witness.892, element0 [concrete = constants.%Core.CharLiteral.as.ImplicitAs.impl.Convert] +// CHECK:STDOUT: %bound_method.loc19: = bound_method %.loc19_13.1, %impl.elem0.loc19 [concrete = constants.%Core.CharLiteral.as.ImplicitAs.impl.Convert.bound.fd2] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert.call.loc19: init %char = call %bound_method.loc19(%.loc19_13.1) [concrete = constants.%int_49] +// CHECK:STDOUT: %.loc19_13.2: %char = value_of_initializer %Core.CharLiteral.as.ImplicitAs.impl.Convert.call.loc19 [concrete = constants.%int_49] +// CHECK:STDOUT: %.loc19_13.3: %char = converted %.loc19_13.1, %.loc19_13.2 [concrete = constants.%int_49] +// CHECK:STDOUT: %PrintChar.call.loc19: init %i32 = call %PrintChar.ref.loc19(%.loc19_13.3) // CHECK:STDOUT: %Core.ref: = name_ref Core, imports.%Core [concrete = imports.%Core] // CHECK:STDOUT: %PrintChar.ref.loc20: %PrintChar.type.089 = name_ref PrintChar, imports.%Core.PrintChar [concrete = constants.%PrintChar.d75] -// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc] -// CHECK:STDOUT: %impl.elem0.loc20: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0] -// CHECK:STDOUT: %bound_method.loc20_18.1: = bound_method %int_2, %impl.elem0.loc20 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.b82] -// CHECK:STDOUT: %specific_fn.loc20: = specific_function %impl.elem0.loc20, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc20_18.2: = bound_method %int_2, %specific_fn.loc20 [concrete = constants.%bound_method.8bd] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc20: init %i32 = call %bound_method.loc20_18.2(%int_2) [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %.loc20_18.1: %i32 = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc20 [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %.loc20_18.2: %i32 = converted %int_2, %.loc20_18.1 [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %PrintChar.call.loc20: init %i32 = call %PrintChar.ref.loc20(%.loc20_18.2) +// CHECK:STDOUT: %.loc20_18.1: Core.CharLiteral = char_value U+0032 [concrete = constants.%.f8d] +// CHECK:STDOUT: %impl.elem0.loc20: %.8d6 = impl_witness_access constants.%ImplicitAs.impl_witness.892, element0 [concrete = constants.%Core.CharLiteral.as.ImplicitAs.impl.Convert] +// CHECK:STDOUT: %bound_method.loc20: = bound_method %.loc20_18.1, %impl.elem0.loc20 [concrete = constants.%Core.CharLiteral.as.ImplicitAs.impl.Convert.bound.1f7] +// CHECK:STDOUT: %Core.CharLiteral.as.ImplicitAs.impl.Convert.call.loc20: init %char = call %bound_method.loc20(%.loc20_18.1) [concrete = constants.%int_50] +// CHECK:STDOUT: %.loc20_18.2: %char = value_of_initializer %Core.CharLiteral.as.ImplicitAs.impl.Convert.call.loc20 [concrete = constants.%int_50] +// CHECK:STDOUT: %.loc20_18.3: %char = converted %.loc20_18.1, %.loc20_18.2 [concrete = constants.%int_50] +// CHECK:STDOUT: %PrintChar.call.loc20: init %i32 = call %PrintChar.ref.loc20(%.loc20_18.3) // CHECK:STDOUT: // CHECK:STDOUT: } // CHECK:STDOUT: