Skip to content

Commit

Permalink
Support stringifying tuple values. (#4664)
Browse files Browse the repository at this point in the history
  • Loading branch information
zygoloid authored Dec 12, 2024
1 parent 0d83569 commit e71fd07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions toolchain/check/testdata/tuple/import.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ var c_bad: C((1, 2, 3)) = F();

impl package Implicit;

// CHECK:STDERR: fail_bad_value.impl.carbon:[[@LINE+6]]:1: error: cannot implicitly convert from `C(<cannot stringify inst376 kind TupleValue>)` to `C(<cannot stringify inst364 kind TupleValue>)` [ImplicitAsConversionFailure]
// CHECK:STDERR: fail_bad_value.impl.carbon:[[@LINE+6]]:1: error: cannot implicitly convert from `C((1, 2))` to `C((3, 4))` [ImplicitAsConversionFailure]
// CHECK:STDERR: var c_bad: C((3, 4)) = F();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_bad_value.impl.carbon:[[@LINE+3]]:1: note: type `C(<cannot stringify inst376 kind TupleValue>)` does not implement interface `Core.ImplicitAs(C(<cannot stringify inst364 kind TupleValue>))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: fail_bad_value.impl.carbon:[[@LINE+3]]:1: note: type `C((1, 2))` does not implement interface `Core.ImplicitAs(C((3, 4)))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: var c_bad: C((3, 4)) = F();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
var c_bad: C((3, 4)) = F();
Expand Down
22 changes: 21 additions & 1 deletion toolchain/sem_ir/stringify_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ auto StringifyTypeExpr(const SemIR::File& sem_ir, InstId outer_inst_id)
}
break;
}
case CARBON_KIND(TupleValue inst): {
auto refs = sem_ir.inst_blocks().Get(inst.elements_id);
if (refs.empty()) {
out << "()";
break;
}
out << "(";
step_stack.PushString(")");
// A tuple of one element has a comma to disambiguate from an
// expression.
if (refs.size() == 1) {
step_stack.PushString(",");
}
for (auto i : llvm::reverse(llvm::seq(refs.size()))) {
step_stack.PushInstId(refs[i]);
if (i > 0) {
step_stack.PushString(", ");
}
}
break;
}
case CARBON_KIND(UnboundElementType inst): {
out << "<unbound element of class ";
step_stack.PushString(">");
Expand Down Expand Up @@ -522,7 +543,6 @@ auto StringifyTypeExpr(const SemIR::File& sem_ir, InstId outer_inst_id)
case TupleAccess::Kind:
case TupleInit::Kind:
case TupleLiteral::Kind:
case TupleValue::Kind:
case UnaryOperatorNot::Kind:
case ValueAsRef::Kind:
case ValueOfInitializer::Kind:
Expand Down

0 comments on commit e71fd07

Please sign in to comment.