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
This commit is contained in:
Richard Smith
2026-09-15 23:11:58 +00:00
committed by GitHub
parent d8c4fc51cd
commit f3d67de480
9 changed files with 435 additions and 146 deletions
+5
View File
@@ -19,6 +19,7 @@
#include "toolchain/sem_ir/singleton_insts.h"
#include "toolchain/sem_ir/specific_interface.h"
#include "toolchain/sem_ir/struct_type_field.h"
#include "toolchain/sem_ir/sugared_type.h"
#include "toolchain/sem_ir/type_info.h"
#include "toolchain/sem_ir/typed_insts.h"
@@ -876,6 +877,10 @@ auto StringifyConstantInst(const File& sem_ir, InstId outer_inst_id)
return Stringify(sem_ir, step_stack);
}
auto StringifyTypeOfInst(const File& sem_ir, InstId inst_id) -> std::string {
return StringifyConstantInst(sem_ir, GetSugaredTypeOfInst(sem_ir, inst_id));
}
auto StringifySpecific(const File& sem_ir, SpecificId specific_id)
-> std::string {
StepStack step_stack(&sem_ir);