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

+25 -25
View File
@@ -39,11 +39,12 @@ namespace Carbon::Check {
// Adds the location of the associated function to a diagnostic.
static auto NoteAssociatedFunction(Context& context, DiagnosticBuilder& builder,
SemIR::FunctionId function_id) -> void {
CARBON_DIAGNOSTIC(AssociatedFunctionHere, Note,
"associated function {0} declared here", SemIR::NameId);
CARBON_DIAGNOSTIC_LABEL(AssociatedFunctionHere, Info,
"associated function {0} declared here",
SemIR::NameId);
const auto& function = context.functions().Get(function_id);
builder.Note(function.latest_decl_id(), AssociatedFunctionHere,
function.name_id);
builder.Attach(function.latest_decl_id(), AssociatedFunctionHere,
function.name_id);
}
auto CheckAssociatedFunctionImplementation(
@@ -192,12 +193,12 @@ static auto VerifyImplRedecl(Context& context, const SemIR::Impl& new_impl,
CARBON_DIAGNOSTIC(ImplRedefinition, Error,
"redefinition of `impl {0} as {1}`", InstIdAsRawType,
InstIdAsRawType);
CARBON_DIAGNOSTIC(ImplPreviousDefinition, Note,
"previous definition was here");
CARBON_DIAGNOSTIC_LABEL(ImplPreviousDefinition, Info,
"previous definition was here");
context.emitter()
.Build(new_impl.latest_decl_id(), ImplRedefinition, new_impl.self_id,
new_impl.constraint_id)
.Note(prev_impl.definition_id, ImplPreviousDefinition)
.Attach(prev_impl.definition_id, ImplPreviousDefinition)
.Emit();
return ImplRedeclType::DiagnosedInvalidRedecl;
}
@@ -288,11 +289,11 @@ static auto ApplyExtendImplAs(Context& context, SemIR::LocId loc_id,
if (!RequireCompleteType(
context, context.types().GetTypeIdForTypeInstId(impl.constraint_id),
SemIR::LocId(impl.constraint_id), [&](auto& builder) {
CARBON_DIAGNOSTIC(ExtendImplAsIncomplete, Context,
"`extend impl as` incomplete facet type {0}",
InstIdAsType);
builder.Context(impl.latest_decl_id(), ExtendImplAsIncomplete,
impl.constraint_id);
CARBON_DIAGNOSTIC_CONTEXT(
ExtendImplAsIncomplete,
"`extend impl as` incomplete facet type {0}", InstIdAsType);
builder.Attach(impl.latest_decl_id(), ExtendImplAsIncomplete,
impl.constraint_id);
})) {
parent_scope.set_has_error();
return false;
@@ -636,13 +637,13 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
if (!RequireCompleteType(
context, context.types().GetTypeIdForTypeInstId(impl.constraint_id),
SemIR::LocId(impl.constraint_id), [&](auto& builder) {
CARBON_DIAGNOSTIC(
ImplAsIncompleteFacetTypeDefinition, Context,
CARBON_DIAGNOSTIC_CONTEXT(
ImplAsIncompleteFacetTypeDefinition,
"definition of impl as incomplete facet type {0}",
InstIdAsType);
builder.Context(SemIR::LocId(impl.latest_decl_id()),
ImplAsIncompleteFacetTypeDefinition,
impl.constraint_id);
builder.Attach(SemIR::LocId(impl.latest_decl_id()),
ImplAsIncompleteFacetTypeDefinition,
impl.constraint_id);
})) {
FillImplWitnessWithErrors(context, impl);
return;
@@ -695,15 +696,15 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
"associated constant {0} not given a value in impl "
"of interface {1}",
SemIR::NameId, SemIR::NameId);
CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
"associated constant declared here");
CARBON_DIAGNOSTIC_LABEL(AssociatedConstantHere, Info,
"associated constant declared here");
context.emitter()
.Build(impl.definition_id, ImplAssociatedConstantNeedsValue,
context.associated_constants()
.Get(decl->assoc_const_id)
.name_id,
interface.name_id)
.Note(assoc_entity, AssociatedConstantHere)
.Attach(assoc_entity, AssociatedConstantHere)
.Emit();
witness_value = SemIR::ErrorInst::InstId;
@@ -999,11 +1000,10 @@ auto CheckConstraintIsInterface(Context& context, SemIR::LocId loc_id,
context.constant_values().Get(self_id),
context.types().GetTypeIdForTypeInstId(full_constraint_id),
[&](auto& builder) {
CARBON_DIAGNOSTIC(ImplOfUnidentifiedFacetType, Context,
"facet type {0} cannot be identified in `impl as`",
InstIdAsType);
builder.Context(loc_id, ImplOfUnidentifiedFacetType,
full_constraint_id);
CARBON_DIAGNOSTIC_CONTEXT(
ImplOfUnidentifiedFacetType,
"facet type {0} cannot be identified in `impl as`", InstIdAsType);
builder.Attach(loc_id, ImplOfUnidentifiedFacetType, full_constraint_id);
});
if (!identified_id.has_value()) {
return SemIR::IdentifiedFacetTypeId::None;