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

108 lines
6.5 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/int.carbon
// TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/pointer/fail_deref_not_pointer.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/fail_deref_not_pointer.carbon
fn Deref(n: i32) {
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:3: error: cannot dereference operand of non-pointer type `i32` [DerefOfNonPointer]
// CHECK:STDERR:
*n;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:4-5: error: cannot apply `->` operator to non-pointer type `i32` [ArrowOperatorOfNonPointer]
// CHECK:STDERR:
n->foo;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:3: error: cannot dereference operand of non-pointer type `()` [DerefOfNonPointer]
// CHECK:STDERR:
*();
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:5-6: error: cannot apply `->` operator to non-pointer type `()` [ArrowOperatorOfNonPointer]
// CHECK:STDERR:
()->foo;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:3: error: cannot dereference operand of non-pointer type `{}` [DerefOfNonPointer]
// CHECK:STDERR:
*{};
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+2]]:5-6: error: cannot apply `->` operator to non-pointer type `{}` [ArrowOperatorOfNonPointer]
// CHECK:STDERR:
{}->foo;
}
// CHECK:STDOUT: --- fail_deref_not_pointer.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_type @Int [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %pattern_type.d94: type = pattern_type %i32 [concrete]
// CHECK:STDOUT: %n.param_patt: %pattern_type.d94 = value_param_pattern [concrete]
// CHECK:STDOUT: %n.patt: %pattern_type.d94 = wrapper_binding_pattern n, %n.param_patt [concrete]
// CHECK:STDOUT: %Deref.type: type = fn_type @Deref [concrete]
// CHECK:STDOUT: %Deref: %Deref.type = struct_value () [concrete]
// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
// CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .Deref = %Deref.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %Deref.decl: %Deref.type = fn_decl @Deref [concrete = constants.%Deref] {
// CHECK:STDOUT: %n.param_patt: %pattern_type.d94 = value_param_pattern [concrete = constants.%n.param_patt]
// CHECK:STDOUT: %n.patt: %pattern_type.d94 = wrapper_binding_pattern n, %n.param_patt [concrete = constants.%n.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %n.param: %i32 = value_param call_param0
// CHECK:STDOUT: %i32: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %n: %i32 = wrapper_binding n, %n.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Deref(%n.param: %i32) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %n.ref.loc18: %i32 = name_ref n, %n
// CHECK:STDOUT: %.loc18: ref <error> = deref %n.ref.loc18
// CHECK:STDOUT: %n.ref.loc21: %i32 = name_ref n, %n
// CHECK:STDOUT: %.loc21: ref <error> = deref %n.ref.loc21
// CHECK:STDOUT: %foo.ref.loc21: <error> = name_ref foo, <error> [concrete = <error>]
// CHECK:STDOUT: %.loc24_5.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %empty_tuple.loc24: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc24_5.2: %empty_tuple.type = converted %.loc24_5.1, %empty_tuple.loc24 [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc24_3: ref <error> = deref %.loc24_5.2 [concrete = <error>]
// CHECK:STDOUT: %.loc27_4.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %empty_tuple.loc27: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc27_4.2: %empty_tuple.type = converted %.loc27_4.1, %empty_tuple.loc27 [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc27_5: ref <error> = deref %.loc27_4.2 [concrete = <error>]
// CHECK:STDOUT: %foo.ref.loc27: <error> = name_ref foo, <error> [concrete = <error>]
// CHECK:STDOUT: %.loc30_5.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
// CHECK:STDOUT: %empty_struct.loc30: %empty_struct_type = struct_value () [concrete = constants.%empty_struct]
// CHECK:STDOUT: %.loc30_5.2: %empty_struct_type = converted %.loc30_5.1, %empty_struct.loc30 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %.loc30_3: ref <error> = deref %.loc30_5.2 [concrete = <error>]
// CHECK:STDOUT: %.loc33_4.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
// CHECK:STDOUT: %empty_struct.loc33: %empty_struct_type = struct_value () [concrete = constants.%empty_struct]
// CHECK:STDOUT: %.loc33_4.2: %empty_struct_type = converted %.loc33_4.1, %empty_struct.loc33 [concrete = constants.%empty_struct]
// CHECK:STDOUT: %.loc33_5: ref <error> = deref %.loc33_4.2 [concrete = <error>]
// CHECK:STDOUT: %foo.ref.loc33: <error> = name_ref foo, <error> [concrete = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: