Files
carbon-lang/toolchain/sem_ir/stringify.h
T
Richard Smith f3d67de480 Start to preserve type sugar in diagnostics. (#7768)
Instead of always printing types as canonical, attempt to find a sugared
type where possible, and include that type in the diagnostic. We can
only do this when given the instruction whose type is being printed
(`TypeOfInstId`) rather than the canonical type ID.

Initial support here is intentionally minimal: just looking through
calls to the callee's declared return type, and looking through pointer
dereferences and corresponding pointer types, to build out the initial
infrastructure. More cases can be added later; this degrades gracefully
to using the canonical type if a better type can't be found.

Assisted-by: Claude Opus 5 via Antigravity
2026-09-15 23:11:58 +00:00

53 lines
2.4 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
#ifndef CARBON_TOOLCHAIN_SEM_IR_STRINGIFY_H_
#define CARBON_TOOLCHAIN_SEM_IR_STRINGIFY_H_
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// Produces a string version of an instruction with a constant value. Generally,
// this should not be called directly. To format a constant value into a
// diagnostic, use a diagnostic parameter of type `InstIdAsConstant`. When the
// constant value is a type, use `InstIdAsType`, `InstIdAsRawType`, or
// `TypeOfInstId` where possible, or of type `TypeId` or `TypeIdAsRawType` if
// you don't have an expression describing the type.
auto StringifyConstantInst(const File& sem_ir, InstId outer_inst_id)
-> std::string;
// Produces a string version of the type of an instruction, describing the type
// the way it was written in the source where we can determine that. Generally,
// this should not be called directly. To format the type of an expression into
// a diagnostic, use a diagnostic parameter of type `TypeOfInstId`.
auto StringifyTypeOfInst(const File& sem_ir, InstId inst_id) -> std::string;
// Produces a string version of the name of a specific. Generally, this should
// not be called directly. To format a string into a diagnostic, use a
// diagnostic parameter of type `SpecificId`.
auto StringifySpecific(const File& sem_ir, SpecificId specific_id)
-> std::string;
// Produces a string version of the name of a specific interface. If the
// interface is not generic, this is just the name of the interface. Otherwise,
// it is the interface name and its generic arguments. Generally, this should
// not be called directly. To format a string into a diagnostic, use a
// diagnostic parameter of type `SpecificInterface`.
auto StringifySpecificInterface(const File& sem_ir,
SpecificInterface specific_interface)
-> std::string;
// Produces a string version of the declared facet type. This contains the name
// of the interfaces or named constraints that the facet type names, and any
// requirements such as rewrites.
auto StringifyDeclaredFacetType(const File& sem_ir,
DeclaredFacetTypeId declared_facet_type_id)
-> std::string;
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_STRINGIFY_H_