Repository: carbon-language/carbon-lang
File: toolchain/check/check_unit.cpp (C++)

Code before:

      // We can get the `Impl` from the resulting witness here, which is the
      // `Impl` that conflicts with the previous poison query.
      auto bad_impl_id = witness_to_impl_id(found_witness_id);
      const auto& bad_impl = context_.impls().Get(bad_impl_id);

      auto prev_impl_id = witness_to_impl_id(poison.impl_witness);
      const auto& prev_impl = context_.impls().Get(prev_impl_id);

      // Both impls will give the same interface name, so doesn't matter which
      // we used here.
      auto interface_name_id =
          context_.interfaces().Get(bad_impl.interface.interface_id).name_id;

      CARBON_DIAGNOSTIC(PoisonedImplLookupConcreteResult, Error,
                        "found `impl` that would change the result of a "
                        "previous use of `{0} as {1}`",
                        InstIdAsRawType, SemIR::NameId);
      auto builder = emitter_.Build(
          bad_impl.first_decl_id(), PoisonedImplLookupConcreteResult,
          poison.query.query_self_inst_id, interface_name_id);
      CARBON_DIAGNOSTIC(PoisonedImplLookupConcreteResultNoteUse, Note,
                        "use of impl here");
      builder.Note(poison.loc_id, PoisonedImplLookupConcreteResultNoteUse);
      CARBON_DIAGNOSTIC(PoisonedImplLookupConcreteResultNotePreviousImpl, Note,
                        "previously got result from `impl` here");
      builder.Note(prev_impl.first_decl_id(),
                   PoisonedImplLookupConcreteResultNotePreviousImpl);
      builder.Emit();
    }
  }
  context_.inst_block_stack().PopAndDiscard();
}

auto CheckUnit::FinishRun() -> void {
  CheckRequiredDeclarations();
  CheckRequiredDefinitions();
  CheckPoisonedConcreteImplLookupQueries();

  // Pop information for the file-level scope.
  context_.sem_ir().set_top_inst_block_id(context_.inst_block_stack().Pop());
  context_.scope_stack().Pop();

  // Finalizes the list of exports on the IR.
  context_.inst_blocks().ReplacePlaceholder(SemIR::InstBlockId::Exports,
                                            context_.exports());
  // Finalizes the ImportRef inst block.
  context_.inst_blocks().ReplacePlaceholder(SemIR::InstBlockId::ImportRefs,
                                            context_.import_ref_ids());
  // Finalizes __global_init.
  context_.global_init().Finalize();

Review comment (suggestion):
Perhaps:
```suggestion
                        "previous use selected this `impl`");
```

Code after:
      };

      // We can get the `Impl` from the resulting witness here, which is the
      // `Impl` that conflicts with the previous poison query.
      auto bad_impl_id = witness_to_impl_id(found_witness_id);
      const auto& bad_impl = context_.impls().Get(bad_impl_id);

      auto prev_impl_id = witness_to_impl_id(poison.impl_witness);
      const auto& prev_impl = context_.impls().Get(prev_impl_id);

      CARBON_DIAGNOSTIC(
          PoisonedImplLookupConcreteResult, Error,
          "found `impl` that would change the result of an earlier "
          "use of `{0} as {1}`",
          InstIdAsRawType, SpecificInterfaceIdAsRawType);
      auto builder =
          emitter_.Build(poison.loc_id, PoisonedImplLookupConcreteResult,
                         poison.query.query_self_inst_id,
                         poison.query.query_specific_interface_id);
      CARBON_DIAGNOSTIC(
          PoisonedImplLookupConcreteResultNoteBadImpl, Note,
          "the use would select the `impl` here but it was not found yet");
      builder.Note(bad_impl.first_decl_id(),
                   PoisonedImplLookupConcreteResultNoteBadImpl);
      CARBON_DIAGNOSTIC(PoisonedImplLookupConcreteResultNotePreviousImpl, Note,
                        "the use had selected the `impl` here");
      builder.Note(prev_impl.first_decl_id(),
                   PoisonedImplLookupConcreteResultNotePreviousImpl);
      builder.Emit();
    }
  }
  context_.inst_block_stack().PopAndDiscard();
}

auto CheckUnit::FinishRun() -> void {
  CheckRequiredDeclarations();
  CheckRequiredDefinitions();
  CheckPoisonedConcreteImplLookupQueries();

  // Pop information for the file-level scope.
  context_.sem_ir().set_top_inst_block_id(context_.inst_block_stack().Pop());
  context_.scope_stack().Pop();

  // Finalizes the list of exports on the IR.
  context_.inst_blocks().ReplacePlaceholder(SemIR::InstBlockId::Exports,
                                            context_.exports());
  // Finalizes the ImportRef inst block.
  context_.inst_blocks().ReplacePlaceholder(SemIR::InstBlockId::ImportRefs,
                                            context_.import_ref_ids());
  // Finalizes __global_init.
  context_.global_init().Finalize();