Files
carbon-lang/toolchain/check/testdata/class/self/fail_self.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

159 lines
8.4 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/class/self/fail_self.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/self/fail_self.carbon
// --- fail_self_implicit_list.carbon
library "[[@TEST_NAME]]";
// `self` is not permitted in the implicit parameter list, regardless of how
// its type is spelled (explicit `Self`, an arbitrary type, or omitted).
class Class {
// CHECK:STDERR: fail_self_implicit_list.carbon:[[@LINE+2]]:8-17: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
fn F[self: Self]();
// CHECK:STDERR: fail_self_implicit_list.carbon:[[@LINE+2]]:8-18: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
fn G[self: Class]();
// CHECK:STDERR: fail_self_implicit_list.carbon:[[@LINE+2]]:8-11: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
fn H[self]();
// CHECK:STDERR: fail_self_implicit_list.carbon:[[@LINE+2]]:8-21: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
fn I[ref self: Self]();
// CHECK:STDERR: fail_self_implicit_list.carbon:[[@LINE+2]]:8-15: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
fn J[ref self]();
}
// --- fail_self_outside_class.carbon
library "[[@TEST_NAME]]";
// `self` outside any class scope has no `Self` to default to.
// CHECK:STDERR: fail_self_outside_class.carbon:[[@LINE+2]]:6-9: error: name `Self` not found [NameNotFound]
// CHECK:STDERR:
fn F(self);
// `ref self` is similarly invalid at file scope.
// CHECK:STDERR: fail_self_outside_class.carbon:[[@LINE+2]]:6-13: error: name `Self` not found [NameNotFound]
// CHECK:STDERR:
fn G(ref self);
// --- fail_self_repeated.carbon
library "[[@TEST_NAME]]";
class Class {
// Repeating `self` within a single parameter list is rejected.
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:14-17: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn F(self, self);
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:20-23: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-17: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn G(self: Self, self);
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:14-17: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn H(self, self: Self);
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:18-21: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-15: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn I(ref self, self);
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:24-27: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-21: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn J(ref self: Self, self: Self);
// `self` in both lists, even just once each, is rejected: the implicit
// occurrence is wrong, and the explicit one becomes a duplicate.
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+5]]:8-11: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:14-17: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn K[self](self);
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+5]]:8-17: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:20-23: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-17: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn L[self: Self](self);
// Duplicates within the implicit list and across both lists.
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+7]]:8-11: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+5]]:14-17: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:14-17: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn M[self, self]();
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+13]]:8-11: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+11]]:14-17: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+9]]:14-17: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+8]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+6]]:20-23: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+5]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+3]]:26-29: error: duplicate name `self` being declared in the same scope [NameDeclDuplicate]
// CHECK:STDERR: fail_self_repeated.carbon:[[@LINE+2]]:8-11: note: name is previously declared here [NameDeclPrevious]
// CHECK:STDERR:
fn N[self, self](self, self);
}
// --- fail_var_self.carbon
library "[[@TEST_NAME]]";
class Class {
fn F();
}
fn Class.F() {
// `var self` is only valid as the receiver of a method, not as a local
// variable.
// CHECK:STDERR: fail_var_self.carbon:[[@LINE+2]]:14-23: error: `self` can only be declared in a parameter list [SelfOutsideParamList]
// CHECK:STDERR:
var unused self: Self;
}
// --- fail_self_wrong_type.carbon
library "[[@TEST_NAME]]";
// The receiver's type can be any type, not just the enclosing class. Here `self`
// has a type that differs from the class the method is declared in, which is
// diagnosed when the method is called with a receiver of the class's type.
class Class {}
class WrongSelf {
fn F(self: Class);
}
fn CallWrongSelf(ws: WrongSelf) {
// CHECK:STDERR: fail_self_wrong_type.carbon:[[@LINE+4]]:3-4: error: cannot implicitly convert expression of type `WrongSelf` to `Class` [ConversionFailure]
// CHECK:STDERR: fail_self_wrong_type.carbon:[[@LINE+3]]:3-4: note: type `WrongSelf` does not implement interface `Core.ImplicitAs(Class)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: fail_self_wrong_type.carbon:[[@LINE-6]]:8-18: note: initializing function parameter [InCallToFunctionParam]
// CHECK:STDERR:
ws.F();
}
// --- fail_self_not_first.carbon
library "[[@TEST_NAME]]";
class Class {
// `self` must be the first explicit parameter.
// CHECK:STDERR: fail_self_not_first.carbon:[[@LINE+2]]:22-31: error: `self` must be the first explicit parameter [SelfNotFirstParam]
// CHECK:STDERR:
fn F(other: Class, self: Self);
}