Skip to content

Commit

Permalink
Address a few more cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
zygoloid committed Dec 11, 2024
1 parent 9245f7e commit ba610d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions toolchain/check/testdata/class/generic/stringify.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Outer(T:! type) {
var v: Outer({}*);

// TODO: It would be nice to include the `Outer({}*).` prefix in the name of `Inner`.
// CHECK:STDERR: fail_nested.carbon:[[@LINE+7]]:1: error: cannot implicitly convert from `Outer({}*)` to `<invalid>.Inner({.a: i32}*)` [ImplicitAsConversionFailure]
// CHECK:STDERR: fail_nested.carbon:[[@LINE+7]]:1: error: cannot implicitly convert from `Outer({}*)` to `Inner({.a: i32}*)` [ImplicitAsConversionFailure]
// CHECK:STDERR: var w: Outer({}*).Inner({.a: i32}*) = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_nested.carbon:[[@LINE+4]]:1: note: type `Outer({}*)` does not implement interface `Core.ImplicitAs(<invalid>.Inner({.a: i32}*))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: fail_nested.carbon:[[@LINE+4]]:1: note: type `Outer({}*)` does not implement interface `Core.ImplicitAs(Inner({.a: i32}*))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: var w: Outer({}*).Inner({.a: i32}*) = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
Expand Down
27 changes: 17 additions & 10 deletions toolchain/sem_ir/stringify_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "toolchain/base/kind_switch.h"
#include "toolchain/sem_ir/entity_with_params_base.h"
#include "toolchain/sem_ir/ids.h"

namespace Carbon::SemIR {

Expand Down Expand Up @@ -68,11 +69,14 @@ class StepStack {
auto PushQualifiedName(NameScopeId name_scope_id, NameId name_id) -> void {
PushNameId(name_id);
while (name_scope_id.is_valid() && name_scope_id != NameScopeId::Package) {
PushString(".");
const auto& name_scope = sem_ir->name_scopes().Get(name_scope_id);
// TODO: For a generic scope, pass a SpecificId to this function and
// include the relevant arguments.
PushNameId(name_scope.name_id());
// TODO: Decide how to print unnamed scopes.
if (name_scope.name_id().is_valid()) {
PushString(".");
// TODO: For a generic scope, pass a SpecificId to this function and
// include the relevant arguments.
PushNameId(name_scope.name_id());
}
name_scope_id = name_scope.parent_scope_id();
}
}
Expand All @@ -81,6 +85,10 @@ class StepStack {
PushSpecificId(entity, specific_id);
PushQualifiedName(entity.parent_scope_id, entity.name_id);
}
auto PushEntityName(EntityNameId entity_name_id) -> void {
const auto& entity_name = sem_ir->entity_names().Get(entity_name_id);
PushQualifiedName(entity_name.parent_scope_id, entity_name.name_id);
}
auto PushTypeId(TypeId type_id) -> void {
PushInstId(sem_ir->types().GetInstId(type_id));
}
Expand Down Expand Up @@ -190,8 +198,7 @@ auto StringifyTypeExpr(const SemIR::File& sem_ir, InstId outer_inst_id)
case ExportDecl::Kind: {
auto name_id =
untyped_inst.As<AnyBindNameOrExportDecl>().entity_name_id;
out << sem_ir.names().GetFormatted(
sem_ir.entity_names().Get(name_id).name_id);
step_stack.PushEntityName(name_id);
break;
}
case CARBON_KIND(ClassType inst): {
Expand Down Expand Up @@ -307,8 +314,7 @@ auto StringifyTypeExpr(const SemIR::File& sem_ir, InstId outer_inst_id)
}
case CARBON_KIND(ImportRefUnloaded inst): {
if (inst.entity_name_id.is_valid()) {
auto name_id = sem_ir.entity_names().Get(inst.entity_name_id).name_id;
out << sem_ir.names().GetFormatted(name_id);
step_stack.PushEntityName(inst.entity_name_id);
} else {
out << "<import ref unloaded invalid entity name>";
}
Expand Down Expand Up @@ -387,8 +393,9 @@ auto StringifyTypeExpr(const SemIR::File& sem_ir, InstId outer_inst_id)
break;
}
case CARBON_KIND(Namespace inst): {
out << sem_ir.names().GetFormatted(
sem_ir.name_scopes().Get(inst.name_scope_id).name_id());
const auto& name_scope = sem_ir.name_scopes().Get(inst.name_scope_id);
step_stack.PushQualifiedName(name_scope.parent_scope_id(),
name_scope.name_id());
break;
}
case CARBON_KIND(PointerType inst): {
Expand Down

0 comments on commit ba610d7

Please sign in to comment.