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
This commit is contained in:
Chandler Carruth authored and GitHub committed 2026-10-15 06:08:57 +00:00
1 parent e1c8c1f855
commit 2c9739799b
1205 files changed
+20291 -24988

No files matched your search

+17 -51
View File
@@ -45,60 +45,44 @@ private override default fn I();
// --- fail_identifier_instead_of_sig.carbon
// CHECK:STDERR: fail_identifier_instead_of_sig.carbon:[[@LINE+4]]:8: error: `fn` declarations must either end with a `;` or have a `{ ... }` block for a definition [ExpectedDeclSemiOrDefinition]
// CHECK:STDERR: fn foo bar;
// CHECK:STDERR: ^~~
// CHECK:STDERR: fail_identifier_instead_of_sig.carbon:[[@LINE+2]]:8-10: error: `fn` declarations must either end with a `;` or have a `{ ... }` block for a definition [ExpectedDeclSemiOrDefinition]
// CHECK:STDERR:
fn foo bar;
// --- fail_missing_implicit_close.carbon
// CHECK:STDERR: fail_missing_implicit_close.carbon:[[@LINE+7]]:7: error: opening symbol without a corresponding closing symbol [UnmatchedOpening]
// CHECK:STDERR: fn Div[();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_missing_implicit_close.carbon:[[@LINE+4]]:10: note: possibly missing `]` here [PossiblyMissingBracketHere]
// CHECK:STDERR: fn Div[();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_missing_implicit_close.carbon:[[@LINE+3]]:7: error: opening symbol without a corresponding closing symbol [UnmatchedOpening]
// CHECK:STDERR: fail_missing_implicit_close.carbon:[[@LINE+2]]:10: note: possibly missing `]` here [PossiblyMissingBracketHere]
// CHECK:STDERR:
fn Div[();
// --- fail_missing_name.carbon
// CHECK:STDERR: fail_missing_name.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn ();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_missing_name.carbon:[[@LINE+2]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn ();
// --- fail_no_sig_or_semi.carbon
fn foo
// CHECK:STDERR: fail_no_sig_or_semi.carbon:[[@LINE+4]]:1: error: `fn` declarations must either end with a `;` or have a `{ ... }` block for a definition [ExpectedDeclSemiOrDefinition]
// CHECK:STDERR:
// CHECK:STDERR: ^
// CHECK:STDERR: fail_no_sig_or_semi.carbon:[[@LINE+2]]:1: error: `fn` declarations must either end with a `;` or have a `{ ... }` block for a definition [ExpectedDeclSemiOrDefinition]
// CHECK:STDERR:
// --- fail_only_fn_and_semi.carbon
// CHECK:STDERR: fail_only_fn_and_semi.carbon:[[@LINE+4]]:3: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn;
// CHECK:STDERR: ^
// CHECK:STDERR: fail_only_fn_and_semi.carbon:[[@LINE+2]]:3: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn;
// --- fail_repeated_fn_and_semi.carbon
// CHECK:STDERR: fail_repeated_fn_and_semi.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn fn;
// CHECK:STDERR: ^~
// CHECK:STDERR: fail_repeated_fn_and_semi.carbon:[[@LINE+2]]:4-5: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn fn;
// --- fail_skip_indented_newline_until_outdent.carbon
// CHECK:STDERR: fail_skip_indented_newline_until_outdent.carbon:[[@LINE+4]]:6: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn (x,
// CHECK:STDERR: ^
// CHECK:STDERR: fail_skip_indented_newline_until_outdent.carbon:[[@LINE+2]]:6: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn (x,
y,
@@ -107,9 +91,7 @@ fn F();
// --- fail_skip_indented_newline_with_semi.carbon
// CHECK:STDERR: fail_skip_indented_newline_with_semi.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn (x,
// CHECK:STDERR: ^
// CHECK:STDERR: fail_skip_indented_newline_with_semi.carbon:[[@LINE+2]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn (x,
y,
@@ -118,9 +100,7 @@ fn F();
// --- fail_skip_indented_newline_without_semi.carbon
// CHECK:STDERR: fail_skip_indented_newline_without_semi.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn (x,
// CHECK:STDERR: ^
// CHECK:STDERR: fail_skip_indented_newline_without_semi.carbon:[[@LINE+2]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn (x,
y,
@@ -129,18 +109,14 @@ fn F();
// --- fail_skip_to_newline_without_semi.carbon
// CHECK:STDERR: fail_skip_to_newline_without_semi.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn ()
// CHECK:STDERR: ^
// CHECK:STDERR: fail_skip_to_newline_without_semi.carbon:[[@LINE+2]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn ()
fn F();
// --- fail_skip_without_semi_to_curly.carbon
// CHECK:STDERR: fail_skip_without_semi_to_curly.carbon:[[@LINE+4]]:1: error: unrecognized declaration introducer [UnrecognizedDecl]
// CHECK:STDERR: struct X { fn () }
// CHECK:STDERR: ^~~~~~
// CHECK:STDERR: fail_skip_without_semi_to_curly.carbon:[[@LINE+2]]:1-6: error: unrecognized declaration introducer [UnrecognizedDecl]
// CHECK:STDERR:
struct X { fn () }
fn F();
@@ -151,9 +127,7 @@ fn foo(bar);
// --- fail_without_name_and_many_tokens_in_params.carbon
// CHECK:STDERR: fail_without_name_and_many_tokens_in_params.carbon:[[@LINE+4]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR: fn (a tokens c d e f g h i j k l m n o p q r s t u v w x y z);
// CHECK:STDERR: ^
// CHECK:STDERR: fail_without_name_and_many_tokens_in_params.carbon:[[@LINE+2]]:4: error: `fn` introducer should be followed by a name [ExpectedDeclName]
// CHECK:STDERR:
fn (a tokens c d e f g h i j k l m n o p q r s t u v w x y z);
@@ -164,17 +138,13 @@ fn ComplexFormParam(x:? X.Y(Z));
// --- fail_ref_on_form_parameter.carbon
// CHECK:STDERR: fail_ref_on_form_parameter.carbon:[[@LINE+4]]:6: error: `ref` is only allowed on a runtime binding [ExpectedRuntimeBindingPatternAfterRef]
// CHECK:STDERR: fn F(ref x:? Form);
// CHECK:STDERR: ^~~
// CHECK:STDERR: fail_ref_on_form_parameter.carbon:[[@LINE+2]]:6-8: error: `ref` is only allowed on a runtime binding [ExpectedRuntimeBindingPatternAfterRef]
// CHECK:STDERR:
fn F(ref x:? Form);
// --- fail_form_within_var.carbon
// CHECK:STDERR: fail_form_within_var.carbon:[[@LINE+4]]:26: error: found `:?` binding inside `var` pattern [NonRegularBindingInVarDecl]
// CHECK:STDERR: fn FormInVar(var x:? Form);
// CHECK:STDERR: ^
// CHECK:STDERR: fail_form_within_var.carbon:[[@LINE+2]]:26: error: found `:?` binding inside `var` pattern [NonRegularBindingInVarDecl]
// CHECK:STDERR:
fn FormInVar(var x:? Form);
@@ -191,9 +161,7 @@ let a: auto = fn -> ref u32 => 0;
// --- fail_ref_return_missing_type.carbon
// CHECK:STDERR: fail_ref_return_missing_type.carbon:[[@LINE+4]]:22: error: expected expression [ExpectedExpr]
// CHECK:STDERR: fn RefReturn() -> ref;
// CHECK:STDERR: ^
// CHECK:STDERR: fail_ref_return_missing_type.carbon:[[@LINE+2]]:22: error: expected expression [ExpectedExpr]
// CHECK:STDERR:
fn RefReturn() -> ref;
@@ -207,9 +175,7 @@ let b: auto = fn -> var u32 => 0;
// --- fail_val_return_missing_type.carbon
// CHECK:STDERR: fail_val_return_missing_type.carbon:[[@LINE+4]]:22: error: expected expression [ExpectedExpr]
// CHECK:STDERR: fn ValReturn() -> val;
// CHECK:STDERR: ^
// CHECK:STDERR: fail_val_return_missing_type.carbon:[[@LINE+2]]:22: error: expected expression [ExpectedExpr]
// CHECK:STDERR:
fn ValReturn() -> val;
+4 -12
View File
@@ -36,9 +36,7 @@ fn F() {
// Note: this might become valid depending on the expression syntax. This test
// shouldn't be taken as a sign it should remain invalid.
bar
// CHECK:STDERR: fail_identifier_in_statements.carbon:[[@LINE+4]]:1: error: expected `;` after expression statement [ExpectedExprSemi]
// CHECK:STDERR: }
// CHECK:STDERR: ^
// CHECK:STDERR: fail_identifier_in_statements.carbon:[[@LINE+2]]:1: error: expected `;` after expression statement [ExpectedExprSemi]
// CHECK:STDERR:
}
@@ -61,25 +59,19 @@ impl T as I {
// --- fail_builtin_identifier.carbon
// CHECK:STDERR: fail_builtin_identifier.carbon:[[@LINE+4]]:18: error: expected builtin function name after `=` [ExpectedBuiltinName]
// CHECK:STDERR: fn NotString() = banana;
// CHECK:STDERR: ^~~~~~
// CHECK:STDERR: fail_builtin_identifier.carbon:[[@LINE+2]]:18-23: error: expected builtin function name after `=` [ExpectedBuiltinName]
// CHECK:STDERR:
fn NotString() = banana;
// --- fail_builtin_two_strings.carbon
// CHECK:STDERR: fail_builtin_two_strings.carbon:[[@LINE+4]]:32: error: `fn` declarations must end with a `;` [ExpectedDeclSemi]
// CHECK:STDERR: fn JunkAfterString() = "hello" "world";
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_builtin_two_strings.carbon:[[@LINE+2]]:32-38: error: `fn` declarations must end with a `;` [ExpectedDeclSemi]
// CHECK:STDERR:
fn JunkAfterString() = "hello" "world";
// --- fail_builtin_spurious_equals.carbon
// CHECK:STDERR: fail_builtin_spurious_equals.carbon:[[@LINE+4]]:23: error: expected builtin function name after `=` [ExpectedBuiltinName]
// CHECK:STDERR: fn SpuriousEquals() = { }
// CHECK:STDERR: ^
// CHECK:STDERR: fail_builtin_spurious_equals.carbon:[[@LINE+2]]:23: error: expected builtin function name after `=` [ExpectedBuiltinName]
// CHECK:STDERR:
fn SpuriousEquals() = { }
fn TestRecoveryFromSpuriousEquals();
+3 -9
View File
@@ -22,21 +22,15 @@ extern library default fn G();
// --- fail_name_missing.carbon
// CHECK:STDERR: fail_name_missing.carbon:[[@LINE+4]]:16: error: expected `default` or a string literal to specify the library name [ExpectedLibraryNameOrDefault]
// CHECK:STDERR: extern library fn G();
// CHECK:STDERR: ^~
// CHECK:STDERR: fail_name_missing.carbon:[[@LINE+2]]:16-17: error: expected `default` or a string literal to specify the library name [ExpectedLibraryNameOrDefault]
// CHECK:STDERR:
extern library fn G();
// --- fail_identifier.carbon
// CHECK:STDERR: fail_identifier.carbon:[[@LINE+8]]:16: error: expected `default` or a string literal to specify the library name [ExpectedLibraryNameOrDefault]
// CHECK:STDERR: extern library foo fn G();
// CHECK:STDERR: ^~~
// CHECK:STDERR: fail_identifier.carbon:[[@LINE+4]]:16-18: error: expected `default` or a string literal to specify the library name [ExpectedLibraryNameOrDefault]
// CHECK:STDERR:
// CHECK:STDERR: fail_identifier.carbon:[[@LINE+4]]:16: error: unrecognized declaration introducer [UnrecognizedDecl]
// CHECK:STDERR: extern library foo fn G();
// CHECK:STDERR: ^~~
// CHECK:STDERR: fail_identifier.carbon:[[@LINE+2]]:16-18: error: unrecognized declaration introducer [UnrecognizedDecl]
// CHECK:STDERR:
extern library foo fn G();
@@ -9,9 +9,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/function/fail_template_form_param.carbon
// `template` cannot be combined with a `:?` form binding.
// CHECK:STDERR: fail_template_form_param.carbon:[[@LINE+4]]:6: error: `template` is only allowed on a generic binding [ExpectedGenericBindingPatternAfterTemplate]
// CHECK:STDERR: fn F(template x:? Form);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_template_form_param.carbon:[[@LINE+2]]:6-13: error: `template` is only allowed on a generic binding [ExpectedGenericBindingPatternAfterTemplate]
// CHECK:STDERR:
fn F(template x:? Form);