Mangle the signature decl when mangling a thunk. (#7177)

Fixes mangling collisions when two thunks with the same name (eg, `Op`)
are created in the same context, which in turn would lead to LLVM
verifier failures and miscompiles.

To support this, add a new value store to track a little more
information about thunks beyond what's in the `Function`.
This commit is contained in:
Richard Smith
2026-05-07 21:58:29 +00:00
committed by GitHub
parent 031ec0a140
commit bc06f6c5ec
40 changed files with 637 additions and 341 deletions
+12 -6
View File
@@ -29,6 +29,7 @@
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/pattern.h"
#include "toolchain/sem_ir/thunk.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
@@ -489,12 +490,17 @@ auto BuildThunk(Context& context, SemIR::FunctionId signature_id,
// We can't use the function directly. Build a thunk.
// TODO: Check for and diagnose obvious reasons why this will fail, such as
// arity mismatch, before trying to build the thunk.
auto [function_id, thunk_id] =
auto [function_id, thunk_inst_id] =
CloneFunctionDecl(context, SemIR::LocId(callee_id), signature_id,
signature_specific_id, callee.function_id);
auto thunk_id =
context.sem_ir().thunks().Add({.callee_id = callee_id,
.signature_id = signature_id,
.specific_id = signature_specific_id});
// Track that this function is a thunk.
context.functions().Get(function_id).SetThunk(callee_id);
context.functions().Get(function_id).SetThunk(thunk_id);
if (defer_definition) {
// Register the thunk to be defined when we reach the end of the enclosing
@@ -504,16 +510,16 @@ auto BuildThunk(Context& context, SemIR::FunctionId signature_id,
context, {
.signature_id = signature_id,
.function_id = function_id,
.decl_id = thunk_id,
.decl_id = thunk_inst_id,
.callee_id = callee_id,
});
} else {
BuildThunkDefinition(context, signature_id, function_id, thunk_id,
BuildThunkDefinition(context, signature_id, function_id, thunk_inst_id,
callee_id);
context.scope_stack().Pop();
}
return thunk_id;
return thunk_inst_id;
}
auto BuildDestroyThunk(Context& context, SemIR::LocId loc_id,
@@ -528,7 +534,7 @@ auto BuildDestroyThunk(Context& context, SemIR::LocId loc_id,
.self_type_id = class_info.self_type_id});
auto& thunk_function = context.functions().Get(thunk_function_id);
thunk_function.SetThunk(SemIR::InstId::None);
thunk_function.SetThunk(SemIR::ThunkId::None);
context.scope_stack().PushForDeclName();
StartFunctionDefinition(context, thunk_inst_id, thunk_function_id);