Files
carbon-lang/toolchain/check/generic.h
T
Dana Jansens 6dbcc78e6c Rewrite symbolic constants in generic redeclarations (#5154)
When a generic function declaration was encountered for the second or
more time, we would FinishGenericRedecl() for the function decl, but
this just popped the generic region stack and moved on.

The issue with that is when the stack entry is gone, we lose the
symbolic constants from that declaration, and are unable to rewrite them
to point to the actual generic. This left us with a function declaration
with abstract symbolic values that were not useful, and in a function
call we use the declaration attached to the definition, which would be a
declaration with broken symbolic values. Then the function would be
uncallable since deduce would be unable to determine argument types
without the generic bindings.

This resolves the issue for functions, as well as ensuring the correct
generic id from a previous declaration is used for other generic entity
types that have redeclarations.

When a function declaration is qualified, such as defining a class
method outside the class body, we need only the function declaration to
contribute to its generic region stack. The code was collecting constant
values from all qualifier segments together incorrectly.

So when we PushNameQualifierScope(), we also drop the current generic
region stack and rewrite its constant values by calling
FinishGenericRedecl(), and open a new stack entry for the next part of
the qualified declaration.

If a generic declaration somehow has more dependent instruction than a
previous declaration, it would add new instructions to its eval block
with indices beyond the elements in the actual declaration eval block,
since we only store the block from the first declaration found. To avoid
this we plumb through that we are in a redeclaration, and terminate with
an ICE instead of adding new instructions to crash on later.

Fixes #5136.
2025-03-21 22:26:08 +00:00

98 lines
4.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_CHECK_GENERIC_H_
#define CARBON_TOOLCHAIN_CHECK_GENERIC_H_
#include "toolchain/check/context.h"
#include "toolchain/sem_ir/entity_with_params_base.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
// Start processing a declaration or definition that might be a generic entity.
auto StartGenericDecl(Context& context) -> void;
// Start processing a declaration or definition that might be a generic entity.
auto StartGenericDefinition(Context& context) -> void;
// Discard the information about the current generic entity. This should be
// called instead of `FinishGenericDecl` if the corresponding `Generic` object
// would not actually be used, or when recovering from an error.
auto DiscardGenericDecl(Context& context) -> void;
// Finish processing a potentially generic declaration and produce a
// corresponding generic object. Returns SemIR::GenericId::None if this
// declaration is not actually generic.
auto BuildGeneric(Context& context, SemIR::InstId decl_id) -> SemIR::GenericId;
// Builds eval block for the declaration.
auto FinishGenericDecl(Context& context, SemIRLoc loc,
SemIR::GenericId generic_id) -> void;
// BuildGeneric() and FinishGenericDecl() combined. Normally you would call this
// function unless the caller has work to do between the two steps.
auto BuildGenericDecl(Context& context, SemIR::InstId decl_id)
-> SemIR::GenericId;
// Merge a redeclaration of an entity that might be a generic into the original
// declaration.
auto FinishGenericRedecl(Context& context, SemIR::GenericId generic_id) -> void;
// Finish processing a potentially generic definition.
auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
-> void;
// Builds and returns an eval block, given the list of canonical symbolic
// constants that the instructions in the eval block should produce. This is
// used when importing a generic.
auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
SemIR::GenericInstIndex::Region region,
llvm::ArrayRef<SemIR::InstId> const_ids)
-> SemIR::InstBlockId;
// Builds a new specific with a given argument list, or finds an existing one if
// this generic has already been referenced with these arguments. Performs
// substitution into the declaration, but not the definition, of the generic.
auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
llvm::ArrayRef<SemIR::InstId> args) -> SemIR::SpecificId;
// Builds a new specific or finds an existing one in the case where the argument
// list has already been converted into an instruction block. `args_id` should
// be a canonical instruction block referring to constants.
auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
SemIR::InstBlockId args_id) -> SemIR::SpecificId;
// Builds the specific that describes how the generic should refer to itself.
// For example, for a generic `G(T:! type)`, this is the specific `G(T)`. If
// `generic_id` is `None`, returns `None`.
auto MakeSelfSpecific(Context& context, SemIRLoc loc,
SemIR::GenericId generic_id) -> SemIR::SpecificId;
// Resolve the declaration of the given specific, by evaluating the eval block
// of the corresponding generic and storing a corresponding value block in the
// specific.
auto ResolveSpecificDeclaration(Context& context, SemIRLoc loc,
SemIR::SpecificId specific_id) -> void;
// Attempts to resolve the definition of the given specific, by evaluating the
// eval block of the corresponding generic and storing a corresponding value
// block in the specific. Returns false if a definition is not available.
auto ResolveSpecificDefinition(Context& context, SemIRLoc loc,
SemIR::SpecificId specific_id) -> bool;
// Returns an instruction describing the entity named by the given specific.
// This is used to name the entity in diagnostics.
auto GetInstForSpecific(Context& context, SemIR::SpecificId specific_id)
-> SemIR::InstId;
// Diagnoses if an entity has implicit parameters, indicating it's generic, but
// is missing explicit parameters.
auto DiagnoseIfGenericMissingExplicitParameters(
Context& context, SemIR::EntityWithParamsBase& entity_base) -> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_