Files
carbon-lang/toolchain/check/testdata/interop/cpp/operators/deref.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

231 lines
13 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
//
// EXTRA-ARGS: --clang-arg=-std=c++20
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/operators/deref.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/operators/deref.carbon
// --- const_deref.carbon
library "[[@TEST_NAME]]";
import Cpp inline '''c++
class ConstDeref {
public:
auto operator*() const -> int&;
};
''';
fn TestDerefPass(var int_ptr: Cpp.ConstDeref) -> ref i32 {
//@dump-sem-ir-begin
return int_ptr.(Core.CppUnsafeDeref.Op)();
//@dump-sem-ir-end
}
// --- mutable_deref.carbon
library "[[@TEST_NAME]]";
import Cpp inline '''c++
class MutableDeref {
public:
auto operator*() -> int&;
};
''';
fn TestDerefPass(var int_ptr: Cpp.MutableDeref) -> ref i32 {
//@dump-sem-ir-begin
return int_ptr.(Core.CppUnsafeDeref.Op)();
//@dump-sem-ir-end
}
// --- fail_todo_multi_deref.carbon
library "[[@TEST_NAME]]";
import Cpp inline '''c++
class MultiDeref {
public:
auto operator*() & -> int&;
auto operator*() const& -> const int&;
auto operator*() && -> int&&;
auto operator*() const&& -> int const&&;
};
''';
fn TestDerefPass(var int_ptr: Cpp.MultiDeref) -> ref i32 {
// CHECK:STDERR: fail_todo_multi_deref.carbon:[[@LINE+2]]:10-41: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
// CHECK:STDERR:
return int_ptr.(Core.CppUnsafeDeref.Op)();
}
// --- fail_todo_maybe_deref.carbon
library "[[@TEST_NAME]]";
import Cpp inline '''c++
template<class T, class U>
concept SameImpl = __is_same(T, U);
template<class T, class U>
concept SameAs = SameImpl<T, U> && SameImpl<U, T>;
template<class T>
class MaybeDeref {
public:
auto operator*() const -> int&
requires SameAs<T, int>;
auto operator*() const -> const T&;
};
''';
fn TestDerefPass(var int_ptr: Cpp.MaybeDeref(i32)) -> ref i32 {
// CHECK:STDERR: fail_todo_maybe_deref.carbon:[[@LINE+2]]:10-41: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
// CHECK:STDERR:
return int_ptr.(Core.CppUnsafeDeref.Op)();
}
// --- fail_not_a_ptr.carbon
library "[[@TEST_NAME]]";
import Cpp inline '''c++
class NotAPtr {};
''';
fn TestDerefFail(not_ptr: Cpp.NotAPtr) {
// CHECK:STDERR: fail_not_a_ptr.carbon:[[@LINE+2]]:3-34: error: cannot access member of interface `Core.CppUnsafeDeref` in type `Cpp.NotAPtr` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR:
not_ptr.(Core.CppUnsafeDeref.Op)();
}
// CHECK:STDOUT: --- const_deref.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %ConstDeref: type = class_type @ConstDeref [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_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: %CppUnsafeDeref.type: type = facet_type <@CppUnsafeDeref> [concrete]
// CHECK:STDOUT: %Self.d49: %CppUnsafeDeref.type = symbolic_binding Self, 0 [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.fde: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%Self.d49) [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.ac2: %CppUnsafeDeref.WithSelf.Op.type.fde = struct_value () [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.assoc_type: type = assoc_entity_type @CppUnsafeDeref [concrete]
// CHECK:STDOUT: %assoc1: %CppUnsafeDeref.assoc_type = assoc_entity element1, imports.%Core.import_ref.953 [concrete]
// CHECK:STDOUT: %ConstDeref.cpp_operator.type: type = fn_type @ConstDeref.cpp_operator [concrete]
// CHECK:STDOUT: %ConstDeref.cpp_operator: %ConstDeref.cpp_operator.type = struct_value () [concrete]
// CHECK:STDOUT: %operator_Star__carbon_thunk.type: type = fn_type @operator_Star__carbon_thunk [concrete]
// CHECK:STDOUT: %operator_Star__carbon_thunk: %operator_Star__carbon_thunk.type = struct_value () [concrete]
// CHECK:STDOUT: %ConstDeref.Op.type: type = fn_type @ConstDeref.Op [concrete]
// CHECK:STDOUT: %ConstDeref.Op: %ConstDeref.Op.type = struct_value () [concrete]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %custom_witness.f45: <witness> = custom_witness (%i32, %ConstDeref.Op), @CppUnsafeDeref [concrete]
// CHECK:STDOUT: %CppUnsafeDeref.facet.35f: %CppUnsafeDeref.type = facet_value %ConstDeref, (%custom_witness.f45) [concrete]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.736: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%CppUnsafeDeref.facet.35f) [concrete]
// CHECK:STDOUT: %.364: type = fn_type_with_self_type %CppUnsafeDeref.WithSelf.Op.type.736, %CppUnsafeDeref.facet.35f [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .CppUnsafeDeref = %Core.CppUnsafeDeref
// CHECK:STDOUT: .Copy = %Core.Copy
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.CppUnsafeDeref: type = import_ref Core//prelude/operators/deref, CppUnsafeDeref, loaded [concrete = constants.%CppUnsafeDeref.type]
// CHECK:STDOUT: %Core.import_ref.002: %CppUnsafeDeref.assoc_type = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [concrete = constants.%assoc1]
// CHECK:STDOUT: %Core.import_ref.953: @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op.type (%CppUnsafeDeref.WithSelf.Op.type.fde) = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [symbolic = @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op (constants.%CppUnsafeDeref.WithSelf.Op.ac2)]
// CHECK:STDOUT: %ConstDeref.cpp_operator.decl: %ConstDeref.cpp_operator.type = fn_decl @ConstDeref.cpp_operator [concrete = constants.%ConstDeref.cpp_operator] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %operator_Star__carbon_thunk.decl: %operator_Star__carbon_thunk.type = fn_decl @operator_Star__carbon_thunk [concrete = constants.%operator_Star__carbon_thunk] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/copy, Copy, loaded [concrete = constants.%Copy.type]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @TestDerefPass(%int_ptr.param: ref %ConstDeref) -> ref %i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %int_ptr.ref: ref %ConstDeref = name_ref int_ptr, %int_ptr
// CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
// CHECK:STDOUT: %CppUnsafeDeref.ref: type = name_ref CppUnsafeDeref, imports.%Core.CppUnsafeDeref [concrete = constants.%CppUnsafeDeref.type]
// CHECK:STDOUT: %Op.ref.loc13_38: %CppUnsafeDeref.assoc_type = name_ref Op, imports.%Core.import_ref.002 [concrete = constants.%assoc1]
// CHECK:STDOUT: %impl_witness_assoc_constant: type = impl_witness_assoc_constant constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %impl.elem1: %.364 = impl_witness_access constants.%custom_witness.f45, element1 [concrete = constants.%ConstDeref.Op]
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_ptr.ref, %impl.elem1
// CHECK:STDOUT: %.loc13: %ConstDeref = acquire_value %int_ptr.ref
// CHECK:STDOUT: %Op.ref.loc13_43: %ConstDeref.cpp_operator.type = name_ref Op, imports.%ConstDeref.cpp_operator.decl [concrete = constants.%ConstDeref.cpp_operator]
// CHECK:STDOUT: %ConstDeref.cpp_operator.bound: <bound method> = bound_method %.loc13, %Op.ref.loc13_43
// CHECK:STDOUT: %operator_Star__carbon_thunk.call: ref %i32 = call imports.%operator_Star__carbon_thunk.decl(%.loc13)
// CHECK:STDOUT: return %operator_Star__carbon_thunk.call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- mutable_deref.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %MutableDeref: type = class_type @MutableDeref [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_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: %CppUnsafeDeref.type: type = facet_type <@CppUnsafeDeref> [concrete]
// CHECK:STDOUT: %Self: %CppUnsafeDeref.type = symbolic_binding Self, 0 [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.fde: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%Self) [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.ac2: %CppUnsafeDeref.WithSelf.Op.type.fde = struct_value () [symbolic]
// CHECK:STDOUT: %CppUnsafeDeref.assoc_type: type = assoc_entity_type @CppUnsafeDeref [concrete]
// CHECK:STDOUT: %assoc1: %CppUnsafeDeref.assoc_type = assoc_entity element1, imports.%Core.import_ref.953 [concrete]
// CHECK:STDOUT: %MutableDeref.cpp_operator.type: type = fn_type @MutableDeref.cpp_operator [concrete]
// CHECK:STDOUT: %MutableDeref.cpp_operator: %MutableDeref.cpp_operator.type = struct_value () [concrete]
// CHECK:STDOUT: %custom_witness.4fc: <witness> = custom_witness (%i32, %MutableDeref.cpp_operator), @CppUnsafeDeref [concrete]
// CHECK:STDOUT: %CppUnsafeDeref.facet.64f: %CppUnsafeDeref.type = facet_value %MutableDeref, (%custom_witness.4fc) [concrete]
// CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.bc9: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%CppUnsafeDeref.facet.64f) [concrete]
// CHECK:STDOUT: %.c8b: type = fn_type_with_self_type %CppUnsafeDeref.WithSelf.Op.type.bc9, %CppUnsafeDeref.facet.64f [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .CppUnsafeDeref = %Core.CppUnsafeDeref
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.CppUnsafeDeref: type = import_ref Core//prelude/operators/deref, CppUnsafeDeref, loaded [concrete = constants.%CppUnsafeDeref.type]
// CHECK:STDOUT: %Core.import_ref.002: %CppUnsafeDeref.assoc_type = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [concrete = constants.%assoc1]
// CHECK:STDOUT: %Core.import_ref.953: @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op.type (%CppUnsafeDeref.WithSelf.Op.type.fde) = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [symbolic = @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op (constants.%CppUnsafeDeref.WithSelf.Op.ac2)]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @TestDerefPass(%int_ptr.param: ref %MutableDeref) -> ref %i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %int_ptr.ref: ref %MutableDeref = name_ref int_ptr, %int_ptr
// CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
// CHECK:STDOUT: %CppUnsafeDeref.ref: type = name_ref CppUnsafeDeref, imports.%Core.CppUnsafeDeref [concrete = constants.%CppUnsafeDeref.type]
// CHECK:STDOUT: %Op.ref: %CppUnsafeDeref.assoc_type = name_ref Op, imports.%Core.import_ref.002 [concrete = constants.%assoc1]
// CHECK:STDOUT: %impl_witness_assoc_constant: type = impl_witness_assoc_constant constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %impl.elem1: %.c8b = impl_witness_access constants.%custom_witness.4fc, element1 [concrete = constants.%MutableDeref.cpp_operator]
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_ptr.ref, %impl.elem1
// CHECK:STDOUT: %.loc13: %MutableDeref = acquire_value %int_ptr.ref
// CHECK:STDOUT: %MutableDeref.cpp_operator.call: ref %i32 = call %bound_method(%.loc13)
// CHECK:STDOUT: return %MutableDeref.cpp_operator.call
// CHECK:STDOUT: }
// CHECK:STDOUT: