Change Destroy to use a CustomWitness instead of a blanket impl (#6512)

Pursuant to recent decisions on #6124, switch `Destroy` to use a
`CustomWitness` for its implementation. Right now this is manufacturing
no-op implementation functions on each lookup, which obviously isn't
ideal but is intended as a first pass. I'm mostly trying to find the
right balance between updating the approach to reflect new decisions,
while still breaking apart work in a way.

The `CoreInterface` logic is intended to build on `CoreIdentifier`
support. We have a number of additional interfaces that require
specialized logic, and that'll extend pretty far with C++ interop, so it
seemed easiest to have a generic function for it. That's what's
replacing the logic inside C++ interop that was doing string comparisons
(which could have already been moved to `CoreIdentifier`, I just missed
it in my first pass).

This adds `CustomWitness` support because the `Destroy` witnesses can be
imported cross-file. `CustomWitness` was previously only used for C++
types, which don't yet support import, which is why that wasn't
previously an issue. The addition of `query_specific_interface_id` is
similarly needed in order to get correct sorting of witness blocks when
imported.

This PR also removes builtin constraint logic (note this is in a
separate commit to help review; it's not a separate PR because it's
difficult to split apart without tests breaking). This had been made
generic with the expectation that destroy, copy, move, and conversions
would all need related support. Under the new decision, we are not going
to do blanket impls and will instead just manufacture a `CustomWitness`
for everything.

A lot of SemIR fingerprints change, but that's probably because the
addition of `Destroy` on core classes is yielding structural changes.
This commit is contained in:
Jon Ross-Perkins
2025-12-19 18:36:06 +00:00
committed by GitHub
parent 1f0a3dcf37
commit c5eba90317
445 changed files with 22297 additions and 23686 deletions
+15 -15
View File
@@ -53,7 +53,7 @@ static auto BuildSingleFunctionWitness(
Context& context, SemIR::LocId loc_id, clang::FunctionDecl* cpp_fn,
clang::DeclAccessPair found_decl, int num_params,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterface specific_interface) -> SemIR::InstId {
SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
auto fn_id = context.clang_sema().DiagnoseUseOfOverloadedDecl(
cpp_fn, GetCppLocation(context, loc_id))
? SemIR::ErrorInst::InstId
@@ -66,13 +66,13 @@ static auto BuildSingleFunctionWitness(
return SemIR::ErrorInst::InstId;
}
return BuildCustomWitness(context, loc_id, query_self_const_id,
specific_interface, {fn_id});
query_specific_interface_id, {fn_id});
}
static auto LookupCopyImpl(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterface specific_interface)
-> SemIR::InstId {
static auto LookupCopyImpl(
Context& context, SemIR::LocId loc_id,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
if (!class_decl) {
// TODO: Should we also provide a `Copy` implementation for enumerations?
@@ -90,13 +90,13 @@ static auto LookupCopyImpl(Context& context, SemIR::LocId loc_id,
return BuildSingleFunctionWitness(
context, loc_id, ctor,
clang::DeclAccessPair::make(ctor, ctor->getAccess()), /*num_params=*/1,
query_self_const_id, specific_interface);
query_self_const_id, query_specific_interface_id);
}
static auto LookupDestroyImpl(Context& context, SemIR::LocId loc_id,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterface specific_interface)
-> SemIR::InstId {
static auto LookupDestroyImpl(
Context& context, SemIR::LocId loc_id,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
if (!class_decl) {
return SemIR::InstId::None;
@@ -112,23 +112,23 @@ static auto LookupDestroyImpl(Context& context, SemIR::LocId loc_id,
return BuildSingleFunctionWitness(
context, loc_id, dtor,
clang::DeclAccessPair::make(dtor, dtor->getAccess()), /*num_params=*/0,
query_self_const_id, specific_interface);
query_self_const_id, query_specific_interface_id);
}
auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
CoreInterface core_interface,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterface specific_interface,
SemIR::SpecificInterfaceId query_specific_interface_id,
const TypeStructure* best_impl_type_structure,
SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
// TODO: Handle other interfaces.
switch (core_interface) {
case CoreInterface::Copy:
return LookupCopyImpl(context, loc_id, query_self_const_id,
specific_interface);
query_specific_interface_id);
case CoreInterface::Destroy:
return LookupDestroyImpl(context, loc_id, query_self_const_id,
specific_interface);
query_specific_interface_id);
case CoreInterface::Unknown:
CARBON_FATAL("shouldn't be called with `Unknown`");
+6 -3
View File
@@ -8,14 +8,17 @@
#include "toolchain/check/context.h"
#include "toolchain/check/custom_witness.h"
#include "toolchain/check/impl_lookup.h"
#include "toolchain/check/interface.h"
#include "toolchain/check/type_structure.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/specific_interface.h"
namespace Carbon::Check {
// Performs lookup for an impl witness for a query involving C++ types. Returns
// a witness value, or `None` if a synthesized C++ witness should not be used.
// Performs lookup for an impl witness for a query involving C++ types.
// Shouldn't be called with `CoreInterface::Unknown`, because only core
// interfaces can have lookup results. Returns a witness value, or `None` if a
// synthesized C++ witness should not be used.
//
// Given a known `core_interface`, we can synthesize a witness based on C++
// operator overloads or special member functions. Performs the suitable C++
@@ -37,7 +40,7 @@ namespace Carbon::Check {
auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
CoreInterface core_interface,
SemIR::ConstantId query_self_const_id,
SemIR::SpecificInterface specific_interface,
SemIR::SpecificInterfaceId query_specific_interface_id,
const TypeStructure* best_impl_type_structure,
SemIR::LocId best_impl_loc_id) -> SemIR::InstId;