Skip to content

Commit

Permalink
Fix formatting for imported impls. (#4194)
Browse files Browse the repository at this point in the history
`impl`s may be defined even if they have no scope if they were imported.
  • Loading branch information
zygoloid authored Aug 7, 2024
1 parent ab5fa93 commit 2a06c96
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion toolchain/check/testdata/impl/lookup/import.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ fn G(c: Impl.C) {
// CHECK:STDOUT: witness = (imports.%import_ref.4)
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: %C as %.2;
// CHECK:STDOUT: impl @impl: %C as %.2 {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: witness = imports.%import_ref.5
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ fn G(c: Impl.C) {
// CHECK:STDOUT: witness = (imports.%import_ref.4)
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: %C as %.2;
// CHECK:STDOUT: impl @impl: %C as %.2 {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: witness = imports.%import_ref.5
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
Expand Down
10 changes: 8 additions & 2 deletions toolchain/check/testdata/impl/no_prelude/interface_args.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ impl A as Factory(B) {
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: %A as %.6;
// CHECK:STDOUT: impl @impl: %A as %.6 {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: witness = imports.%import_ref.11
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A {
// CHECK:STDOUT: !members:
Expand Down Expand Up @@ -397,7 +400,10 @@ impl A as Factory(B) {
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: %A as %.6;
// CHECK:STDOUT: impl @impl: %A as %.6 {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: witness = imports.%import_ref.11
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A {
// CHECK:STDOUT: !members:
Expand Down
6 changes: 4 additions & 2 deletions toolchain/sem_ir/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class FormatterImpl {

llvm::SaveAndRestore impl_scope(scope_, inst_namer_->GetScopeFor(id));

if (impl_info.scope_id.is_valid()) {
if (impl_info.is_defined()) {
out_ << ' ';
OpenBrace();
FormatCodeBlock(impl_info.body_block_id);
Expand All @@ -242,7 +242,9 @@ class FormatterImpl {
// always list the witness in this section.
IndentLabel();
out_ << "!members:\n";
FormatNameScope(impl_info.scope_id);
if (impl_info.scope_id.is_valid()) {
FormatNameScope(impl_info.scope_id);
}

Indent();
out_ << "witness = ";
Expand Down

0 comments on commit 2a06c96

Please sign in to comment.