Files
carbon-lang/toolchain/check/handle_struct.cpp
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

180 lines
6.5 KiB
C++

// 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 "common/map.h"
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/type.h"
#include "toolchain/check/unused.h"
#include "toolchain/diagnostics/format_providers.h"
namespace Carbon::Check {
auto HandleParseNode(Context& context, Parse::StructLiteralStartId node_id)
-> bool {
context.scope_stack().PushForSameRegion();
context.node_stack().Push(node_id);
context.struct_type_fields_stack().PushArray();
context.param_and_arg_refs_stack().Push();
return true;
}
auto HandleParseNode(Context& context, Parse::StructTypeLiteralStartId node_id)
-> bool {
context.scope_stack().PushForSameRegion();
context.node_stack().Push(node_id);
context.struct_type_fields_stack().PushArray();
return true;
}
auto HandleParseNode(Context& context,
Parse::StructFieldDesignatorId /*node_id*/) -> bool {
// This leaves the designated name on top because the `.` isn't interesting.
CARBON_CHECK(context.node_stack().PeekIs<SemIR::NameId>());
return true;
}
auto HandleParseNode(Context& context, Parse::StructLiteralCommaId /*node_id*/)
-> bool {
context.param_and_arg_refs_stack().ApplyComma();
return true;
}
auto HandleParseNode(Context& /*context*/,
Parse::StructTypeLiteralCommaId /*node_id*/) -> bool {
return true;
}
auto HandleParseNode(Context& context, Parse::StructLiteralFieldId node_id)
-> bool {
auto value_inst_id = context.node_stack().PopExpr();
// Get the name while leaving it on the stack.
auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
// Store the name for the type.
auto value_type_inst_id = context.types().GetTypeInstId(
context.insts().Get(value_inst_id).type_id());
context.struct_type_fields_stack().AppendToTop(
{.name_id = name_id, .type_inst_id = value_type_inst_id});
// Push the value back on the stack as an argument.
context.node_stack().Push(node_id, value_inst_id);
return true;
}
auto HandleParseNode(Context& context,
Parse::StructTypeLiteralFieldId /*node_id*/) -> bool {
auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
auto cast_type_inst_id = ExprAsType(context, type_node, type_id).inst_id;
// Get the name while leaving it on the stack.
auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
context.struct_type_fields_stack().AppendToTop(
{.name_id = name_id, .type_inst_id = cast_type_inst_id});
return true;
}
// Diagnoses and returns true if there's a duplicate name.
static auto DiagnoseDuplicateNames(
Context& context, llvm::ArrayRef<Parse::NodeId> field_name_nodes,
llvm::ArrayRef<SemIR::StructTypeField> fields, bool is_struct_type_literal)
-> bool {
Map<SemIR::NameId, Parse::NodeId> names;
for (auto [field_name_node, field] :
llvm::zip_equal(field_name_nodes, fields)) {
auto result = names.Insert(field.name_id, field_name_node);
if (!result.is_inserted()) {
CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
"duplicated field name `{1}` in "
"{0:struct type literal|struct literal}",
Diagnostics::BoolAsSelect, SemIR::NameId);
CARBON_DIAGNOSTIC_LABEL(StructNamePrevious, Info,
"field with the same name here");
context.emitter()
.Build(result.value(), StructNameDuplicate, is_struct_type_literal,
field.name_id)
.Attach(field_name_node, StructNamePrevious)
.Emit();
return true;
}
}
return false;
}
// Pops the names of each field from the stack. These will have been left while
// handling struct fields.
static auto PopFieldNameNodes(Context& context, size_t field_count)
-> llvm::SmallVector<Parse::NodeId> {
llvm::SmallVector<Parse::NodeId> nodes;
nodes.reserve(field_count);
for ([[maybe_unused]] auto i : llvm::seq(field_count)) {
auto [name_node, _] =
context.node_stack().PopWithNodeId<Parse::NodeCategory::MemberName>();
nodes.push_back(name_node);
}
return nodes;
}
auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
if (!context.node_stack().PeekIs(Parse::NodeCategory::MemberName)) {
// Remove the last parameter from the node stack before collecting names.
context.param_and_arg_refs_stack().EndNoPop(
Parse::NodeKind::StructLiteralStart);
}
auto fields = context.struct_type_fields_stack().PeekArray();
llvm::SmallVector<Parse::NodeId> field_name_nodes =
PopFieldNameNodes(context, fields.size());
auto elements_id = context.param_and_arg_refs_stack().EndAndPop(
Parse::NodeKind::StructLiteralStart);
context.scope_stack().Pop(/*check_unused=*/true);
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
/*is_struct_type_literal=*/false)) {
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
} else {
auto type_id = GetStructType(
context, context.struct_type_fields().AddCanonical(fields));
auto value_id = AddInst<SemIR::StructLiteral>(
context, node_id, {.type_id = type_id, .elements_id = elements_id});
context.node_stack().Push(node_id, value_id);
}
context.struct_type_fields_stack().PopArray();
return true;
}
auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
-> bool {
auto fields = context.struct_type_fields_stack().PeekArray();
llvm::SmallVector<Parse::NodeId> field_name_nodes =
PopFieldNameNodes(context, fields.size());
context.scope_stack().Pop(/*check_unused=*/true);
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
/*is_struct_type_literal=*/true)) {
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
} else {
auto fields_id = context.struct_type_fields().AddCanonical(fields);
AddInstAndPush<SemIR::StructType>(
context, node_id,
{.type_id = SemIR::TypeType::TypeId, .fields_id = fields_id});
}
context.struct_type_fields_stack().PopArray();
return true;
}
} // namespace Carbon::Check