Files
carbon-lang/toolchain/check/testdata/builtins/char_literal/convert_char.carbon
T
Chandler Carruth 2c9739799b Render diagnostics through the terminal library (#7683)
Diagnostic rendering was part of the data model: `Loc::FormatLocation`
and
`Loc::FormatSnippet` wrote plain text to a stream, so no capability,
color, or
terminal width could reach them.

A diagnostic is now one message with labels, contexts, and location
information
attached, and `Diagnostics::Renderer` draws it into a
`Terminal::Buffer`. The
design is in `toolchain/docs/diagnostics_rendering.md` (#7668),
authoring in
`toolchain/docs/diagnostics.md`.

Before:

```
example.carbon:6:3: error: 1 argument passed to function expecting 0 arguments
  Run0(1);
  ^~~~~~~
example.carbon:3:1: note: calling function declared here
fn Run0() {}
^~~~~~~~~~~
```

After:

```
error: 1 argument passed to function expecting 0 arguments
       ╭─┤ example.carbon:6:3
       ┆
     3 │ fn Run0() {}
       · ─────┬─────
       ·      ╰──┤ calling function declared here
       ┆
->   6 │   Run0(1);
       ·   ───────
       ╰────────────
```

`--no-diagnostic-snippets` gives a compact form, `example.carbon:6:3-9`.
File
tests use it; the rendering tests under `toolchain/diagnostics/testdata`
ask for
snippets.

C++ interop diagnostics are data now rather than text Clang drew:
`SemIR::ClangSourceLocId` names a `clang::SourceRange`, and a Clang
location is
marked as the point it names.

`StreamConsumer` moves to a `:renderer` target that `:emitter` does not
depend
on, keeping `common/terminal` out of `consumer.h`. Capabilities come
from the
file behind the error stream, so tests get plain ASCII.

`renderer_fuzzer` draws diagnostics built from fuzzer bytes with every
capability combination, since a shape the renderer doesn't expect must
never be
why the compiler dies while reporting a problem.

TODOs mark the follow-ups: line weights and crossing lines, wrapping
that keeps
a quoted type whole, and ordering diagnostics that share a byte offset.

Also: `IncorrectExtension`'s hint moves into its message, since its note
had no
location of its own, and `strip_noise` in `check_diagnostics.py` blanks
multi-line `#define`s instead of deleting them.

Assisted-by: Claude Code
2026-10-15 06:08:57 +00:00

165 lines
9.2 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/char_literal/convert_char.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/char_literal/convert_char.carbon
// --- builtin.carbon
library "[[@TEST_NAME]]";
fn MakeCharLiteral() -> type = "char_literal.make_type";
alias CharLiteral = MakeCharLiteral();
fn MakeIntLiteral() -> type = "int_literal.make_type";
alias IntLiteral = MakeIntLiteral();
fn UInt(n: IntLiteral) -> type = "int.make_type_unsigned";
fn ToChar(c: CharLiteral) -> UInt(8) = "char_literal.convert_char";
// --- fail_bad_decl.carbon
library "[[@TEST_NAME]]";
import library "builtin";
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+2]]:1-51: error: invalid signature for builtin function "char_literal.convert_char" [InvalidBuiltinSignature]
// CHECK:STDERR:
fn ToCharBarResultU16(c: CharLiteral) -> UInt(16) = "char_literal.convert_char";
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+2]]:1-49: error: invalid signature for builtin function "char_literal.convert_char" [InvalidBuiltinSignature]
// CHECK:STDERR:
fn ToCharBadResultU9(c: CharLiteral) -> UInt(9) = "char_literal.convert_char";
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+2]]:1-31: error: invalid signature for builtin function "char_literal.convert_char" [InvalidBuiltinSignature]
// CHECK:STDERR:
fn ToCharNoParam() -> UInt(8) = "char_literal.convert_char";
// --- fail_runtime_call.carbon
library "[[@TEST_NAME]]";
import library "builtin";
let c: CharLiteral = 'a';
// CHECK:STDERR: fail_runtime_call.carbon:[[@LINE+4]]:18-26: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: imported from: fail_runtime_call.carbon:[[@LINE-4]]:1 [InImport]
// CHECK:STDERR: builtin.carbon:10:1-38: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR:
let d: UInt(8) = ToChar(c);
// --- narrow.carbon
library "[[@TEST_NAME]]";
import library "builtin";
//@dump-sem-ir-begin
let a: UInt(8) = ToChar('\0');
let b: UInt(8) = ToChar('b');
let c: UInt(8) = ToChar('\u{7F}');
//@dump-sem-ir-end
// --- fail_size_small.carbon
library "[[@TEST_NAME]]";
import library "builtin";
// CHECK:STDERR: fail_size_small.carbon:[[@LINE+2]]:18-33: error: character value U+0080 too large for type `<builtin u8>` [CharTooLargeForType]
// CHECK:STDERR:
let c: UInt(8) = ToChar('\u{80}');
// --- fail_size_multi_byte.carbon
library "[[@TEST_NAME]]";
import library "builtin";
// CHECK:STDERR: fail_size_multi_byte.carbon:[[@LINE+2]]:18-35: error: character value U+1E15 too large for type `<builtin u8>` [CharTooLargeForType]
// CHECK:STDERR:
let c: UInt(8) = ToChar('\u{1E15}');
// CHECK:STDOUT: --- narrow.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %UInt.type: type = fn_type @UInt [concrete]
// CHECK:STDOUT: %UInt: %UInt.type = struct_value () [concrete]
// CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
// CHECK:STDOUT: %u8.builtin: type = int_type unsigned, %int_8 [concrete]
// CHECK:STDOUT: %pattern_type.456: type = pattern_type %u8.builtin [concrete]
// CHECK:STDOUT: %a.patt: %pattern_type.456 = value_binding_pattern a [concrete]
// CHECK:STDOUT: %ToChar.type: type = fn_type @ToChar [concrete]
// CHECK:STDOUT: %ToChar: %ToChar.type = struct_value () [concrete]
// CHECK:STDOUT: %.dc9: Core.CharLiteral = char_value U+0000 [concrete]
// CHECK:STDOUT: %int_0: %u8.builtin = int_value 0 [concrete]
// CHECK:STDOUT: %b.patt: %pattern_type.456 = value_binding_pattern b [concrete]
// CHECK:STDOUT: %.711: Core.CharLiteral = char_value U+0062 [concrete]
// CHECK:STDOUT: %int_98: %u8.builtin = int_value 98 [concrete]
// CHECK:STDOUT: %c.patt.7eb: %pattern_type.456 = value_binding_pattern c [concrete]
// CHECK:STDOUT: %.e28: Core.CharLiteral = char_value U+007F [concrete]
// CHECK:STDOUT: %int_127: %u8.builtin = int_value 127 [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Main.UInt: %UInt.type = import_ref Main//builtin, UInt, loaded [concrete = constants.%UInt]
// CHECK:STDOUT: %Main.ToChar: %ToChar.type = import_ref Main//builtin, ToChar, loaded [concrete = constants.%ToChar]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %.loc6_29.1: %u8.builtin = value_of_initializer @__global_init.%ToChar.call.loc6 [concrete = constants.%int_0]
// CHECK:STDOUT: %.loc6_29.2: %u8.builtin = converted @__global_init.%ToChar.call.loc6, %.loc6_29.1 [concrete = constants.%int_0]
// CHECK:STDOUT: %.loc6_14.1: type = splice_block %.loc6_14.3 [concrete = constants.%u8.builtin] {
// CHECK:STDOUT: %UInt.ref.loc6: %UInt.type = name_ref UInt, imports.%Main.UInt [concrete = constants.%UInt]
// CHECK:STDOUT: %int_8.loc6: Core.IntLiteral = int_value 8 [concrete = constants.%int_8]
// CHECK:STDOUT: %UInt.call.loc6: init type = call %UInt.ref.loc6(%int_8.loc6) [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc6_14.2: type = value_of_initializer %UInt.call.loc6 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc6_14.3: type = converted %UInt.call.loc6, %.loc6_14.2 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a: %u8.builtin = wrapper_binding a, %.loc6_29.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.456 = value_binding_pattern a [concrete = constants.%a.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc7_28.1: %u8.builtin = value_of_initializer @__global_init.%ToChar.call.loc7 [concrete = constants.%int_98]
// CHECK:STDOUT: %.loc7_28.2: %u8.builtin = converted @__global_init.%ToChar.call.loc7, %.loc7_28.1 [concrete = constants.%int_98]
// CHECK:STDOUT: %.loc7_14.1: type = splice_block %.loc7_14.3 [concrete = constants.%u8.builtin] {
// CHECK:STDOUT: %UInt.ref.loc7: %UInt.type = name_ref UInt, imports.%Main.UInt [concrete = constants.%UInt]
// CHECK:STDOUT: %int_8.loc7: Core.IntLiteral = int_value 8 [concrete = constants.%int_8]
// CHECK:STDOUT: %UInt.call.loc7: init type = call %UInt.ref.loc7(%int_8.loc7) [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc7_14.2: type = value_of_initializer %UInt.call.loc7 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc7_14.3: type = converted %UInt.call.loc7, %.loc7_14.2 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: }
// CHECK:STDOUT: %b: %u8.builtin = wrapper_binding b, %.loc7_28.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %b.patt: %pattern_type.456 = value_binding_pattern b [concrete = constants.%b.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc8_33.1: %u8.builtin = value_of_initializer @__global_init.%ToChar.call.loc8 [concrete = constants.%int_127]
// CHECK:STDOUT: %.loc8_33.2: %u8.builtin = converted @__global_init.%ToChar.call.loc8, %.loc8_33.1 [concrete = constants.%int_127]
// CHECK:STDOUT: %.loc8_14.1: type = splice_block %.loc8_14.3 [concrete = constants.%u8.builtin] {
// CHECK:STDOUT: %UInt.ref.loc8: %UInt.type = name_ref UInt, imports.%Main.UInt [concrete = constants.%UInt]
// CHECK:STDOUT: %int_8.loc8: Core.IntLiteral = int_value 8 [concrete = constants.%int_8]
// CHECK:STDOUT: %UInt.call.loc8: init type = call %UInt.ref.loc8(%int_8.loc8) [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc8_14.2: type = value_of_initializer %UInt.call.loc8 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: %.loc8_14.3: type = converted %UInt.call.loc8, %.loc8_14.2 [concrete = constants.%u8.builtin]
// CHECK:STDOUT: }
// CHECK:STDOUT: %c: %u8.builtin = wrapper_binding c, %.loc8_33.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %c.patt: %pattern_type.456 = value_binding_pattern c [concrete = constants.%c.patt.7eb]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %ToChar.ref.loc6: %ToChar.type = name_ref ToChar, imports.%Main.ToChar [concrete = constants.%ToChar]
// CHECK:STDOUT: %.loc6: Core.CharLiteral = char_value U+0000 [concrete = constants.%.dc9]
// CHECK:STDOUT: %ToChar.call.loc6: init %u8.builtin = call %ToChar.ref.loc6(%.loc6) [concrete = constants.%int_0]
// CHECK:STDOUT: %ToChar.ref.loc7: %ToChar.type = name_ref ToChar, imports.%Main.ToChar [concrete = constants.%ToChar]
// CHECK:STDOUT: %.loc7: Core.CharLiteral = char_value U+0062 [concrete = constants.%.711]
// CHECK:STDOUT: %ToChar.call.loc7: init %u8.builtin = call %ToChar.ref.loc7(%.loc7) [concrete = constants.%int_98]
// CHECK:STDOUT: %ToChar.ref.loc8: %ToChar.type = name_ref ToChar, imports.%Main.ToChar [concrete = constants.%ToChar]
// CHECK:STDOUT: %.loc8: Core.CharLiteral = char_value U+007F [concrete = constants.%.e28]
// CHECK:STDOUT: %ToChar.call.loc8: init %u8.builtin = call %ToChar.ref.loc8(%.loc8) [concrete = constants.%int_127]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: