mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 19:55:11 +00:00
This is the final step in landing the _structural_ updates to diagnostic rendering. The key idea here is that we want the primary diagnostic to have a message clearly explaining the problem and attached to the code that has that problem. This should be the starting point for readers working to address the issue, but is rendered attached to the code snippet in question so errors are always "in context" of the code with the error. Everything else in a diagnostic is an additional label attached to code to explain or help the reader understand the problem in the primary message. Previously, `ContextScope` resulted in some confusion here, as diagnostic consumers turned messages into labels and made the message in the context scope the message. In essence, this isn't a scope with context _for_ a diagnostic (with two exceptions that should probably have been an `AnnotationScope`), but a context within `check` where we have a preferred message for a diagnostic emitted by general purpose code we call into. For example, "class was forward declared here" starts as an error, and then consumers render it as a label to make the message from the context scope the error. We also grew an `AnnotationScope` to handle the contextual information we want to show for a diagnostic (what I would have assumed `ContextScope` actually did). Now we have a `Diagnoser`: code in `check` that can better explain the problem than shared logic it is calling into passes a callback that builds a caller-specific diagnostic. The callee then can attach relevant labels and emit it. Diagnostics no longer store `contexts` and consumers no longer need the logic to restructure things. We move all (but two) of the old contexts to be normal messages emitted in this way, and we move some diagnostics to become labels. Each of the new normal messages corresponds to a new diagnostic kind as usual. Common code following this pattern now _requires_ the `Diagnoser` to be passed (or emit nothing), and this uncovered three places where an empty context was used that made the rendered form miss key information. All three are fixed with new diagnostics, one already covered by tests, and two with TODOs to add test coverage. We also completely remove the old `ContextScope` and `CARBON_DIAGNOSTIC_CONTEXT` and rename `AnnotationScope` to `ContextScope`. It is only used to attach information about when/where a diagnostic occurred. Labels also no longer have categories. What were `Primary` are now `ExtraRange`s on the diagnostic rather than labels. Labels are now always informational. `DeclaredName` now narrows the range of a label when the range is a declaration of a named entity to only be the range of that name. For entities that don't have a declared name, the entire declaration remains the range. The code building and emitting these diagnostics can skip them if the declared location isn't distinct from the diagnostic location. Wording is largely unchanged here so we can just review the _structural_ change. #7991 iterates using this to improve locations and adds missing labels. A subsequent PR will iterate on the wording to increasingly fit within this structural model. This is separated out as a way to review incrementally, and so that subsequently we don't have to review wording improvements that only make sense in a new structure that you cannot see yet. It seems easier to look at the structure here even with awkward wording. Assisted-by: Claude Code
257 lines
10 KiB
C++
257 lines
10 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 "toolchain/check/literal.h"
|
|
|
|
#include "toolchain/check/call.h"
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/convert.h"
|
|
#include "toolchain/check/diagnostic_helpers.h"
|
|
#include "toolchain/check/name_lookup.h"
|
|
#include "toolchain/check/type.h"
|
|
#include "toolchain/check/type_completion.h"
|
|
#include "toolchain/diagnostics/diagnostic.h"
|
|
#include "toolchain/lex/token_info.h"
|
|
#include "toolchain/parse/node_ids.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Adds a TypeLiteral instruction to represent a syntactic type literal.
|
|
static auto MakeTypeLiteral(Context& context, SemIR::LocId loc_id,
|
|
SemIR::InstId value_id) -> SemIR::TypeInstId {
|
|
auto type_inst_id = ExprAsType(context, loc_id, value_id).inst_id;
|
|
return AddTypeInst<SemIR::TypeLiteral>(
|
|
context, loc_id,
|
|
{.type_id = SemIR::TypeType::TypeId, .value_id = type_inst_id});
|
|
}
|
|
|
|
auto MakeTypeTypeLiteral(Context& context, Parse::NodeId node_id)
|
|
-> SemIR::TypeInstId {
|
|
return MakeTypeLiteral(context, node_id, SemIR::TypeType::TypeInstId);
|
|
}
|
|
|
|
auto MakeBoolTypeLiteral(Context& context, Parse::NodeId node_id)
|
|
-> SemIR::TypeInstId {
|
|
auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
|
|
auto inst_id =
|
|
LookupNameInCore(context, desugared_loc_id, CoreIdentifier::Bool);
|
|
return MakeTypeLiteral(context, node_id, inst_id);
|
|
}
|
|
|
|
auto MakeBoolLiteral(Context& context, SemIR::LocId loc_id,
|
|
SemIR::BoolValue value) -> SemIR::InstId {
|
|
return AddInst<SemIR::BoolLiteral>(
|
|
context, loc_id,
|
|
{.type_id = GetSingletonType(context, SemIR::BoolType::TypeInstId),
|
|
.value = value});
|
|
}
|
|
|
|
auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
|
|
-> SemIR::InstId {
|
|
return AddInst<SemIR::IntValue>(
|
|
context, node_id,
|
|
{.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
|
|
.int_id = int_id});
|
|
}
|
|
|
|
// Returns an instruction with the given constant integer value.
|
|
static auto GetOrAddIntValue(Context& context, SemIR::LocId loc_id,
|
|
IntId int_id) -> SemIR::InstId {
|
|
return GetOrAddInst<SemIR::IntValue>(
|
|
context, loc_id,
|
|
{.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
|
|
.int_id = int_id});
|
|
}
|
|
|
|
auto MakeCharTypeLiteral(Context& context, Parse::NodeId node_id)
|
|
-> SemIR::TypeInstId {
|
|
auto inst_id = LookupNameInCore(context, node_id, CoreIdentifier::Char);
|
|
return MakeTypeLiteral(context, node_id, inst_id);
|
|
}
|
|
|
|
// Returns an instruction representing the type `iN` or `uN`.
|
|
static auto GetOrAddIntTypeInst(Context& context, SemIR::LocId loc_id,
|
|
SemIR::IntKind int_kind, IntId size_id)
|
|
-> SemIR::InstId {
|
|
auto width_id = GetOrAddIntValue(context, loc_id, size_id);
|
|
auto fn_inst_id = LookupNameInCore(context, loc_id,
|
|
int_kind == SemIR::IntKind::Signed
|
|
? CoreIdentifier::Int
|
|
: CoreIdentifier::UInt);
|
|
return PerformCall(context, loc_id, fn_inst_id, {width_id});
|
|
}
|
|
|
|
auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
|
|
SemIR::IntKind int_kind, IntId size_id)
|
|
-> SemIR::TypeInstId {
|
|
auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
|
|
auto type_inst_id =
|
|
GetOrAddIntTypeInst(context, desugared_loc_id, int_kind, size_id);
|
|
return MakeTypeLiteral(context, node_id, type_inst_id);
|
|
}
|
|
|
|
auto MakeIntType(Context& context, Parse::NodeId node_id,
|
|
SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId {
|
|
auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
|
|
auto type_inst_id =
|
|
GetOrAddIntTypeInst(context, desugared_loc_id, int_kind, size_id);
|
|
return ExprAsType(context, node_id, type_inst_id).type_id;
|
|
}
|
|
|
|
auto MakeFloatTypeLiteral(Context& context, Parse::NodeId node_id,
|
|
IntId size_id) -> SemIR::TypeInstId {
|
|
auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
|
|
auto width_id = GetOrAddIntValue(context, desugared_loc_id, size_id);
|
|
auto fn_inst_id =
|
|
LookupNameInCore(context, desugared_loc_id, CoreIdentifier::Float);
|
|
auto call_id = PerformCall(context, desugared_loc_id, fn_inst_id, {width_id});
|
|
return MakeTypeLiteral(context, node_id, call_id);
|
|
}
|
|
|
|
namespace {
|
|
// The extracted representation of the type `Core.String`.
|
|
struct StringRepr {
|
|
SemIR::TypeId ptr_field_type_id;
|
|
SemIR::TypeId size_field_type_id;
|
|
SemIR::TypeStore::IntTypeInfo size_field_type_info;
|
|
};
|
|
} // namespace
|
|
|
|
// Extracts information about the representation of the `Core.String` type
|
|
// necessary for building a string literal.
|
|
static auto GetStringLiteralRepr(Context& context, SemIR::LocId loc_id,
|
|
SemIR::TypeId type_id)
|
|
-> std::optional<StringRepr> {
|
|
// The object representation should be a struct type.
|
|
auto object_repr_id = context.types().GetObjectRepr(type_id);
|
|
auto struct_repr =
|
|
context.types().TryGetAs<SemIR::StructType>(object_repr_id);
|
|
if (!struct_repr) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
// The struct should have two fields.
|
|
auto fields = context.struct_type_fields().Get(struct_repr->fields_id);
|
|
if (fields.size() != 2) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
// The first field should be a pointer to 8-bit integers.
|
|
auto ptr_type =
|
|
context.insts().TryGetAs<SemIR::PointerType>(fields[0].type_inst_id);
|
|
if (!ptr_type) {
|
|
return std::nullopt;
|
|
}
|
|
auto pointee_type_id =
|
|
context.types().GetTypeIdForTypeInstId(ptr_type->pointee_id);
|
|
if (!TryToCompleteType(context, pointee_type_id, loc_id)) {
|
|
return std::nullopt;
|
|
}
|
|
auto elem_type_info = context.types().TryGetIntTypeInfo(pointee_type_id);
|
|
if (!elem_type_info || context.ints().Get(elem_type_info->bit_width) != 8) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
// The second field should be an integer type.
|
|
auto size_field_type_id =
|
|
context.types().GetTypeIdForTypeInstId(fields[1].type_inst_id);
|
|
auto size_type_info = context.types().TryGetIntTypeInfo(size_field_type_id);
|
|
if (!size_type_info) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
return StringRepr{.ptr_field_type_id = context.types().GetTypeIdForTypeInstId(
|
|
fields[0].type_inst_id),
|
|
.size_field_type_id = size_field_type_id,
|
|
.size_field_type_info = *size_type_info};
|
|
}
|
|
|
|
auto MakeStringLiteral(Context& context, Parse::StringLiteralId node_id,
|
|
StringLiteralValueId value_id) -> SemIR::InstId {
|
|
auto str_type = MakeStringType(context, SemIR::LocId(node_id).AsDesugared());
|
|
if (!RequireCompleteType(context, str_type.type_id, node_id, [&] {
|
|
CARBON_DIAGNOSTIC(StringLiteralTypeIncomplete, Error,
|
|
"type {0} is incomplete", InstIdAsType);
|
|
return context.emitter().Build(node_id, StringLiteralTypeIncomplete,
|
|
str_type.inst_id);
|
|
})) {
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
|
|
auto repr = GetStringLiteralRepr(context, node_id, str_type.type_id);
|
|
if (!repr) {
|
|
if (str_type.type_id != SemIR::ErrorInst::TypeId) {
|
|
CARBON_DIAGNOSTIC(StringLiteralTypeUnexpected, Error,
|
|
"unexpected representation for type {0}", InstIdAsType);
|
|
context.emitter().Emit(node_id, StringLiteralTypeUnexpected,
|
|
str_type.inst_id);
|
|
}
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
|
|
// The pointer field is a `StringLiteral` object.
|
|
// TODO: Perhaps `StringLiteral` should instead produce a durable reference,
|
|
// and we should take its address here?
|
|
auto ptr_value_id = AddInst<SemIR::StringLiteral>(
|
|
context, node_id,
|
|
{.type_id = repr->ptr_field_type_id, .string_literal_id = value_id});
|
|
|
|
// The size field is an integer literal.
|
|
auto size = context.string_literal_values().Get(value_id).size();
|
|
if (repr->size_field_type_info.bit_width.has_value()) {
|
|
// Check that the size value fits in the size field.
|
|
auto width = context.ints()
|
|
.Get(repr->size_field_type_info.bit_width)
|
|
.getLimitedValue();
|
|
if (repr->size_field_type_info.is_signed ? !llvm::isIntN(width, size)
|
|
: !llvm::isUIntN(width, size)) {
|
|
CARBON_DIAGNOSTIC(StringLiteralTooLong, Error,
|
|
"string literal is too long");
|
|
context.emitter().Emit(node_id, StringLiteralTooLong);
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
}
|
|
auto size_value_id =
|
|
AddInst<SemIR::IntValue>(context, node_id,
|
|
{.type_id = repr->size_field_type_id,
|
|
.int_id = context.ints().Add(size)});
|
|
|
|
// Build the representation struct.
|
|
auto elements_id = context.inst_blocks().Add({ptr_value_id, size_value_id});
|
|
return AddInst<SemIR::StructValue>(
|
|
context, node_id,
|
|
{.type_id = str_type.type_id, .elements_id = elements_id});
|
|
}
|
|
|
|
// Returns an instruction with the value `str`.
|
|
static auto GetOrAddStringTypeInst(Context& context, SemIR::LocId loc_id)
|
|
-> SemIR::InstId {
|
|
return LookupNameInCore(context, loc_id, CoreIdentifier::String);
|
|
}
|
|
|
|
auto MakeStringTypeLiteral(Context& context, Parse::StringTypeLiteralId node_id)
|
|
-> SemIR::TypeInstId {
|
|
auto inst_id = GetOrAddStringTypeInst(context, node_id);
|
|
return MakeTypeLiteral(context, node_id, inst_id);
|
|
}
|
|
|
|
auto MakeStringType(Context& context, SemIR::LocId loc_id) -> TypeExpr {
|
|
auto type_inst_id = GetOrAddStringTypeInst(context, loc_id);
|
|
return ExprAsType(context, loc_id, type_inst_id);
|
|
}
|
|
|
|
auto MakeArrayType(Context& context, SemIR::LocId loc_id,
|
|
SemIR::InstId element_type_inst_id, SemIR::InstId bound_id)
|
|
-> TypeExpr {
|
|
auto desugared_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
|
|
auto fn_inst_id =
|
|
LookupNameInCore(context, desugared_loc_id, CoreIdentifier::Array);
|
|
auto call_id = PerformCall(context, loc_id, fn_inst_id,
|
|
{element_type_inst_id, bound_id});
|
|
return ExprAsType(context, loc_id, call_id);
|
|
}
|
|
|
|
} // namespace Carbon::Check
|