Files
carbon-lang/toolchain/check/testdata/facet/runtime_value.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

193 lines
7.0 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/convert.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/facet/runtime_value.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/runtime_value.carbon
// --- facet_value_copy_from_reference.carbon
library "[[@TEST_NAME]]";
interface I {}
class C {
var i: I;
}
fn F(c: C) {
// Member access produces a reference, which is copied into the value
// binding.
//@dump-sem-ir-begin
let unused a: I = c.i;
//@dump-sem-ir-end
}
// --- fail_todo_facet_copy_narrowing_from_reference.carbon
library "[[@TEST_NAME]]";
interface I {}
interface J {}
class C {
var ij: I & J;
}
fn F(c: C) {
// Member access produces a reference, which is copied into the value
// binding.
//
// TODO: This copy should work, but the narrowing prevents it currently.
//
//@dump-sem-ir-begin
// CHECK:STDERR: fail_todo_facet_copy_narrowing_from_reference.carbon:[[@LINE+2]]:21-24: error: semantics TODO: `conversion of runtime facet value` [SemanticsTodo]
// CHECK:STDERR:
let unused a: I = c.ij;
//@dump-sem-ir-end
}
// --- fail_compound_member_lookup_in_runtime_facet_without_value.carbon
library "[[@TEST_NAME]]";
interface Z {
let X: type;
}
// CHECK:STDERR: fail_compound_member_lookup_in_runtime_facet_without_value.carbon:[[@LINE+2]]:15-21: error: semantics TODO: `associated value lookup on runtime facet value` [SemanticsTodo]
// CHECK:STDERR:
fn F(T: Z, v: T.(Z.X));
// --- fail_member_lookup_in_runtime_facet_without_value.carbon
library "[[@TEST_NAME]]";
interface Z {
let X: type;
}
// TODO: Improve this diagnostic. The problem is that T is a runtime facet
// _and_ its facet type doesn't specific a value for `.X`. Or make this work
// by producing an ImplWitnessAccess that functions against a runtime facet.
//
// CHECK:STDERR: fail_member_lookup_in_runtime_facet_without_value.carbon:[[@LINE+2]]:15-17: error: cannot access member of interface `Z` in type `Z` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR:
fn F(T: Z, v: T.X);
// --- fail_todo_compound_member_lookup_in_runtime_facet_with_value.carbon
library "[[@TEST_NAME]]";
interface Z {
let X: type;
}
// TODO: We should be able to get the value of `.X` from the `FacetType` of
// `T`.
//
// CHECK:STDERR: fail_todo_compound_member_lookup_in_runtime_facet_with_value.carbon:[[@LINE+2]]:29-35: error: semantics TODO: `conversion of runtime facet value` [SemanticsTodo]
// CHECK:STDERR:
fn F(T: Z where .X = (), v: T.(Z.X));
// --- fail_todo_member_lookup_in_runtime_facet_with_value.carbon
library "[[@TEST_NAME]]";
interface Z {
let X: type;
}
// TODO: We should be able to get the value of `.X` from the `FacetType` of
// `T`.
//
// CHECK:STDERR: fail_todo_member_lookup_in_runtime_facet_with_value.carbon:[[@LINE+2]]:29-31: error: cannot access member of interface `Z` in type `Z where .(Z.X) = ()` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR:
fn F(T: Z where .X = (), v: T.X);
// --- fail_todo_member_lookup_in_type_of_runtime_facet_with_value.carbon
library "[[@TEST_NAME]]";
interface Z {
fn G();
}
fn F(T: Z) {
// CHECK:STDERR: fail_todo_member_lookup_in_type_of_runtime_facet_with_value.carbon:[[@LINE+2]]:3-5: error: cannot access member of interface `Z` in type `Z` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR:
T.G();
}
// --- fail_member_access_runtime_facet.carbon
library "[[@TEST_NAME]]";
interface Z {
fn G();
}
fn F(T: Z) {
// CHECK:STDERR: fail_member_access_runtime_facet.carbon:[[@LINE+2]]:3-5: error: cannot access member of interface `Z` in type `Z` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR:
T.G();
}
// --- fail_member_access_runtime_type.carbon
library "[[@TEST_NAME]]";
fn F(T: type) {
// CHECK:STDERR: fail_member_access_runtime_type.carbon:[[@LINE+2]]:3-5: error: type `type` does not support qualified expressions [QualifiedExprUnsupported]
// CHECK:STDERR:
T.G();
}
// CHECK:STDOUT: --- facet_value_copy_from_reference.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: %pattern_type.6cb: type = pattern_type %I.type [concrete]
// CHECK:STDOUT: %C.elem: type = unbound_element_type %C, %I.type [concrete]
// CHECK:STDOUT: %a.patt: %pattern_type.6cb = value_binding_pattern a [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%c.param: %C) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %c.ref: %C = name_ref c, %c
// CHECK:STDOUT: %i.ref: %C.elem = name_ref i, @C.%field_decl [concrete = @C.%field_decl]
// CHECK:STDOUT: %.loc13_22.1: ref %I.type = class_element_access %c.ref, element0
// CHECK:STDOUT: %.loc13_22.2: %I.type = acquire_value %.loc13_22.1
// CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
// CHECK:STDOUT: %a: %I.type = wrapper_binding a, %.loc13_22.2
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.6cb = value_binding_pattern a [concrete = constants.%a.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_todo_facet_copy_narrowing_from_reference.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: %facet_type: type = facet_type <@I & @J> [concrete]
// CHECK:STDOUT: %C.elem: type = unbound_element_type %C, %facet_type [concrete]
// CHECK:STDOUT: %pattern_type.6cb: type = pattern_type %I.type [concrete]
// CHECK:STDOUT: %a.patt: %pattern_type.6cb = value_binding_pattern a [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%c.param: %C) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %c.ref: %C = name_ref c, %c
// CHECK:STDOUT: %ij.ref: %C.elem = name_ref ij, @C.%field_decl [concrete = @C.%field_decl]
// CHECK:STDOUT: %.loc19_22.1: ref %facet_type = class_element_access %c.ref, element0
// CHECK:STDOUT: %.loc19_22.2: %facet_type = acquire_value %.loc19_22.1
// CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
// CHECK:STDOUT: %a: %I.type = wrapper_binding a, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.6cb = value_binding_pattern a [concrete = constants.%a.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: