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

150 lines
4.7 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/none.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/impl_self_as.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_self_as.carbon
// --- match.carbon
library "[[@TEST_NAME]]";
interface I1 {}
interface I2 {}
interface J1(T1: type) {}
interface J2(T2: type) {}
// `impl Self as` should match `impl as`, so these should not trigger impl
// declaration without definition diagnostics.
class C1 {
impl Self as I1;
impl as I1 {}
impl as I2;
impl Self as I2 {}
impl forall [U: type] Self as J1(U);
impl forall [U: type] as J1(U) {}
impl forall [V: type] as J2(V);
impl forall [V: type] Self as J2(V) {}
}
class C2(W: type) {
impl Self as I1;
impl as I1 {}
impl as I2;
impl Self as I2 {}
impl forall [X: type] Self as J1(X);
impl forall [X: type] as J1(X) {}
impl forall [Y: type] as J2(Y);
impl forall [Y: type] Self as J2(Y) {}
}
// --- fail_no_match.carbon
library "[[@TEST_NAME]]";
interface I3 {}
interface I4 {}
interface I5 {}
interface I6 {}
interface J3(T3: type) {}
interface J4(T4: type) {}
interface J5(T5: type) {}
interface J6(T6: type) {}
// `impl C as` should not match `impl Self as` or `impl as`.
class C3 {
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-16: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl C3 as I3;
impl as I3 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-16: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl C3 as I4;
impl Self as I4 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-13: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl as I5;
impl C3 as I5 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-18: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl Self as I6;
impl C3 as I6 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-38: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [Z3: type] C3 as J3(Z3);
impl forall [Z3: type] as J3(Z3) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-38: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [Z4: type] C3 as J4(Z4);
impl forall [Z4: type] Self as J4(Z4) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-35: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [Z5: type] as J5(Z5);
impl forall [Z5: type] C3 as J5(Z5) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-40: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [Z6: type] Self as J6(Z6);
impl forall [Z6: type] C3 as J6(Z6) {}
}
class C4(A: type) {
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-19: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl C4(A) as I3;
impl as I3 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-19: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl C4(A) as I4;
impl Self as I4 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-13: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl as I5;
impl C4(A) as I5 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-18: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl Self as I6;
impl C4(A) as I6 {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-41: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [B3: type] C4(A) as J3(B3);
impl forall [B3: type] as J3(B3) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-41: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [B4: type] C4(A) as J4(B4);
impl forall [B4: type] Self as J4(B4) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-35: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [B5: type] as J5(B5);
impl forall [B5: type] C4(A) as J5(B5) {}
// CHECK:STDERR: fail_no_match.carbon:[[@LINE+2]]:3-40: error: impl declared but not defined [ImplMissingDefinition]
// CHECK:STDERR:
impl forall [B6: type] Self as J6(B6);
impl forall [B6: type] C4(A) as J6(B6) {}
}