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

@@ -45,13 +45,9 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_too_few_arguments.carbon:[[@LINE+8]]:26: error: too few template arguments for class template 'TwoTypes' [CppInteropParseError]
// CHECK:STDERR: 14 | var x: Cpp.TwoTypes(Cpp.A);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_too_few_arguments.carbon:[[@LINE-5]]:10: in file included here [InCppInclude]
// CHECK:STDERR: fail_too_few_arguments.carbon:[[@LINE+4]]:26: error: too few template arguments for class template 'TwoTypes' [CppInteropParseError]
// CHECK:STDERR: included from: fail_too_few_arguments.carbon:[[@LINE-3]]:10 [InCppInclude]
// CHECK:STDERR: ./templates.h:5:41: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 5 | template<typename T, typename U> struct TwoTypes {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var x: Cpp.TwoTypes(Cpp.A);
@@ -61,13 +57,9 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_too_many_arguments.carbon:[[@LINE+8]]:40: error: too many template arguments for class template 'TwoTypes' [CppInteropParseError]
// CHECK:STDERR: 14 | var x: Cpp.TwoTypes(Cpp.A, Cpp.A, Cpp.A);
// CHECK:STDERR: | ~~^
// CHECK:STDERR: fail_too_many_arguments.carbon:[[@LINE-5]]:10: in file included here [InCppInclude]
// CHECK:STDERR: fail_too_many_arguments.carbon:[[@LINE+4]]:38-40: error: too many template arguments for class template 'TwoTypes' [CppInteropParseError]
// CHECK:STDERR: included from: fail_too_many_arguments.carbon:[[@LINE-3]]:10 [InCppInclude]
// CHECK:STDERR: ./templates.h:5:41: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 5 | template<typename T, typename U> struct TwoTypes {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var x: Cpp.TwoTypes(Cpp.A, Cpp.A, Cpp.A);
@@ -98,23 +90,15 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE+8]]:57: error: too few template arguments for class template 'Inner' [CppInteropParseError]
// CHECK:STDERR: 14 | var too_few: Cpp.FixedSizePack(Cpp.A, Cpp.A).Inner(Cpp.B);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE-5]]:10: in file included here [InCppInclude]
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE+4]]:57: error: too few template arguments for class template 'Inner' [CppInteropParseError]
// CHECK:STDERR: included from: fail_fixed_size_pack_wrong_size.carbon:[[@LINE-3]]:10 [InCppInclude]
// CHECK:STDERR: ./templates.h:15:33: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 15 | template<True<T> ...U> struct Inner {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var too_few: Cpp.FixedSizePack(Cpp.A, Cpp.A).Inner(Cpp.B);
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE+8]]:72: error: too many template arguments for class template 'Inner' [CppInteropParseError]
// CHECK:STDERR: 24 | var too_many: Cpp.FixedSizePack(Cpp.A, Cpp.A).Inner(Cpp.B, Cpp.B, Cpp.B);
// CHECK:STDERR: | ~~^
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE-15]]:10: in file included here [InCppInclude]
// CHECK:STDERR: fail_fixed_size_pack_wrong_size.carbon:[[@LINE+4]]:70-72: error: too many template arguments for class template 'Inner' [CppInteropParseError]
// CHECK:STDERR: included from: fail_fixed_size_pack_wrong_size.carbon:[[@LINE-9]]:10 [InCppInclude]
// CHECK:STDERR: ./templates.h:15:33: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 15 | template<True<T> ...U> struct Inner {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var too_many: Cpp.FixedSizePack(Cpp.A, Cpp.A).Inner(Cpp.B, Cpp.B, Cpp.B);
@@ -41,12 +41,8 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_int_param_mismatch.carbon:[[@LINE+7]]:1: error: cannot implicitly convert expression of type `Cpp.Simple` to `Cpp.Simple` [ConversionFailure]
// CHECK:STDERR: var s: Cpp.Simple(456) = Cpp.MakeSimple123();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_int_param_mismatch.carbon:[[@LINE+4]]:1: note: type `Cpp.Simple` does not implement interface `Core.ImplicitAs(Cpp.Simple)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: var s: Cpp.Simple(456) = Cpp.MakeSimple123();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_int_param_mismatch.carbon:[[@LINE+3]]:1-22: error: cannot implicitly convert expression of type `Cpp.Simple` to `Cpp.Simple` [ConversionFailure]
// CHECK:STDERR: fail_int_param_mismatch.carbon:[[@LINE+2]]:1-22: note: type `Cpp.Simple` does not implement interface `Core.ImplicitAs(Cpp.Simple)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR:
var s: Cpp.Simple(456) = Cpp.MakeSimple123();
@@ -139,12 +135,8 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+7]]:26: error: cannot implicitly convert expression of type `type` to `i32` [ConversionFailure]
// CHECK:STDERR: var x: Cpp.TwoNonType(1, Cpp.A) = {};
// CHECK:STDERR: ^~~~~
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+4]]:26: note: type `type` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: var x: Cpp.TwoNonType(1, Cpp.A) = {};
// CHECK:STDERR: ^~~~~
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+3]]:26-30: error: cannot implicitly convert expression of type `type` to `i32` [ConversionFailure]
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+2]]:26-30: note: type `type` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR:
var x: Cpp.TwoNonType(1, Cpp.A) = {};
@@ -156,12 +148,8 @@ import Cpp library "templates.h";
var n: i32;
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+7]]:26: error: cannot implicitly convert expression of type `i32*` to `i32` [ConversionFailure]
// CHECK:STDERR: var x: Cpp.TwoNonType(1, &n) = {};
// CHECK:STDERR: ^~
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+4]]:26: note: type `i32*` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: var x: Cpp.TwoNonType(1, &n) = {};
// CHECK:STDERR: ^~
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+3]]:26-27: error: cannot implicitly convert expression of type `i32*` to `i32` [ConversionFailure]
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+2]]:26-27: note: type `i32*` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR:
var x: Cpp.TwoNonType(1, &n) = {};
@@ -36,20 +36,11 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
//@dump-sem-ir-begin
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+15]]:35: error: too few template arguments for class template 'C' [CppInteropParseError]
// CHECK:STDERR: 22 | var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+12]]:35: note: template template argument has different template parameters than its corresponding template template parameter [CppInteropParseNote]
// CHECK:STDERR: 22 | var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE-9]]:10: in file included here [InCppInclude]
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+6]]:35: error: too few template arguments for class template 'C' [CppInteropParseError]
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+5]]:35: note: template template argument has different template parameters than its corresponding template template parameter [CppInteropParseNote]
// CHECK:STDERR: included from: fail_template_mismatch.carbon:[[@LINE-5]]:10 [InCppInclude]
// CHECK:STDERR: ./templates.h:6:66: note: previous template template parameter is here [CppInteropParseNote]
// CHECK:STDERR: 6 | template<template<typename> typename, template<typename> typename>
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE-13]]:10: in file included here [InCppInclude]
// CHECK:STDERR: ./templates.h:4:37: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 4 | template<typename, typename> struct C {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
//@dump-sem-ir-end
@@ -62,9 +53,7 @@ import Cpp library "templates.h";
class X {}
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+4]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR: 12 | var x: Cpp.TwoTemplates(Cpp.A, X);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+2]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, X);
@@ -74,9 +63,7 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+4]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR: 10 | var x: Cpp.TwoTemplates(Cpp.A, true);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+2]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, true);
@@ -43,12 +43,8 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+7]]:28: error: cannot implicitly convert non-type value of type `bool` to `type` [ConversionFailureNonTypeToFacet]
// CHECK:STDERR: var x: Cpp.TwoTypes(Cpp.A, true);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+4]]:28: note: type `bool` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: var x: Cpp.TwoTypes(Cpp.A, true);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+3]]:28-31: error: cannot implicitly convert non-type value of type `bool` to `type` [ConversionFailureNonTypeToFacet]
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+2]]:28-31: note: type `bool` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR:
var x: Cpp.TwoTypes(Cpp.A, true);
@@ -58,9 +54,7 @@ library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_unsupported_type.carbon:[[@LINE+4]]:28: error: semantics TODO: `unsupported type used as template argument` [SemanticsTodo]
// CHECK:STDERR: var x: Cpp.TwoTypes(Cpp.A, {});
// CHECK:STDERR: ^~
// CHECK:STDERR: fail_unsupported_type.carbon:[[@LINE+2]]:28-29: error: semantics TODO: `unsupported type used as template argument` [SemanticsTodo]
// CHECK:STDERR:
var x: Cpp.TwoTypes(Cpp.A, {});