Reject generic virtual functions (#5356)

Not entirely sure what the SemIR representation for this should be - do
we put the bogus thing in the vtable, and just not lower it later? The
patch currently doesn't add the function to the SemIR vtable - which
then means you could find a virtual function that's not in the vtable,
which seems similarly confusing.

I guess we could make the function non-virtual?

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
David Blaikie
2025-04-25 00:07:35 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent a342e5c117
commit 1e2d4c3405
3 changed files with 415 additions and 4 deletions
+36 -4
View File
@@ -310,12 +310,36 @@ static auto ValidateForEntryPoint(Context& context,
}
}
static auto IsGenericFunction(Context& context,
SemIR::GenericId function_generic_id,
SemIR::GenericId class_generic_id) -> bool {
if (function_generic_id == SemIR::GenericId::None) {
return false;
}
if (class_generic_id == SemIR::GenericId::None) {
return true;
}
const auto& function_generic = context.generics().Get(function_generic_id);
const auto& class_generic = context.generics().Get(class_generic_id);
auto function_bindings =
context.inst_blocks().Get(function_generic.bindings_id);
auto class_bindings = context.inst_blocks().Get(class_generic.bindings_id);
// If the function's bindings are the same size as the class's bindings,
// then there are no extra bindings for the function, so it is effectively
// non-generic within the scope of a specific of the class.
return class_bindings.size() != function_bindings.size();
}
// Requests a vtable be created when processing a virtual function.
static auto RequestVtableIfVirtual(
Context& context, Parse::AnyFunctionDeclId node_id,
SemIR::Function::VirtualModifier virtual_modifier,
const std::optional<SemIR::Inst>& parent_scope_inst, SemIR::InstId decl_id)
-> void {
const std::optional<SemIR::Inst>& parent_scope_inst, SemIR::InstId decl_id,
SemIR::GenericId generic_id) -> void {
// In order to request a vtable, the function must be virtual, and in a class
// scope.
if (virtual_modifier == SemIR::Function::VirtualModifier::None ||
@@ -333,6 +357,13 @@ static auto RequestVtableIfVirtual(
CARBON_DIAGNOSTIC(ImplWithoutBase, Error, "impl without base class");
context.emitter().Emit(node_id, ImplWithoutBase);
}
if (IsGenericFunction(context, generic_id, class_info.generic_id)) {
CARBON_DIAGNOSTIC(GenericVirtual, Error, "generic virtual function");
context.emitter().Emit(node_id, GenericVirtual);
return;
}
// TODO: If this is an `impl` function, check there's a matching base
// function that's impl or virtual.
class_info.is_dynamic = true;
@@ -465,8 +496,6 @@ static auto BuildFunctionDecl(Context& context,
SemIR::FunctionId::None,
context.inst_block_stack().Pop()};
auto decl_id = AddPlaceholderInst(context, node_id, function_decl);
RequestVtableIfVirtual(context, node_id, virtual_modifier, parent_scope_inst,
decl_id);
// Build the function entity. This will be merged into an existing function if
// there is one, or otherwise added to the function store.
@@ -509,6 +538,9 @@ static auto BuildFunctionDecl(Context& context,
// TODO: Validate that the redeclaration doesn't set an access modifier.
}
RequestVtableIfVirtual(context, node_id, virtual_modifier, parent_scope_inst,
decl_id, function_info.generic_id);
// Write the function ID into the FunctionDecl.
ReplaceInstBeforeConstantUse(context, decl_id, function_decl);