Accept generic parameter lists in class declarations. (#3933)

Check that they match across redeclarations.

Fix some importing bugs for symbolic bindings which were uncovered when
testing this.
This commit is contained in:
Richard Smith
2024-05-04 02:08:38 +00:00
committed by GitHub
parent 92fa0ac1ac
commit d5c0c9cfe3
57 changed files with 1006 additions and 287 deletions
+5 -129
View File
@@ -10,141 +10,17 @@
namespace Carbon::Check {
// Returns true if there was an error in declaring the function, which will have
// previously been diagnosed.
static auto FunctionDeclHasError(Context& context, const SemIR::Function& fn)
-> bool {
if (fn.return_type_id == SemIR::TypeId::Error) {
return true;
}
for (auto param_refs_id : {fn.implicit_param_refs_id, fn.param_refs_id}) {
if (param_refs_id != SemIR::InstBlockId::Empty) {
for (auto param_id : context.inst_blocks().Get(param_refs_id)) {
if (context.insts().Get(param_id).type_id() == SemIR::TypeId::Error) {
return true;
}
}
}
}
return false;
}
// Returns false if a param differs for a redeclaration. The caller is expected
// to provide a diagnostic.
static auto CheckRedeclParam(Context& context,
llvm::StringLiteral param_diag_label,
int32_t param_index,
SemIR::InstId new_param_ref_id,
SemIR::InstId prev_param_ref_id,
Substitutions substitutions) -> bool {
// TODO: Consider differentiating between type and name mistakes. For now,
// taking the simpler approach because I also think we may want to refactor
// params.
auto diagnose = [&]() {
CARBON_DIAGNOSTIC(FunctionRedeclParamDiffers, Error,
"Function redeclaration differs at {0}parameter {1}.",
llvm::StringLiteral, int32_t);
CARBON_DIAGNOSTIC(FunctionRedeclParamPrevious, Note,
"Previous declaration's corresponding {0}parameter here.",
llvm::StringLiteral);
context.emitter()
.Build(new_param_ref_id, FunctionRedeclParamDiffers, param_diag_label,
param_index + 1)
.Note(prev_param_ref_id, FunctionRedeclParamPrevious, param_diag_label)
.Emit();
};
auto new_param_ref = context.insts().Get(new_param_ref_id);
auto prev_param_ref = context.insts().Get(prev_param_ref_id);
if (new_param_ref.kind() != prev_param_ref.kind() ||
new_param_ref.type_id() !=
SubstType(context, prev_param_ref.type_id(), substitutions)) {
diagnose();
return false;
}
if (new_param_ref.Is<SemIR::AddrPattern>()) {
new_param_ref =
context.insts().Get(new_param_ref.As<SemIR::AddrPattern>().inner_id);
prev_param_ref =
context.insts().Get(prev_param_ref.As<SemIR::AddrPattern>().inner_id);
if (new_param_ref.kind() != prev_param_ref.kind()) {
diagnose();
return false;
}
}
if (new_param_ref.Is<SemIR::AnyBindName>()) {
new_param_ref =
context.insts().Get(new_param_ref.As<SemIR::AnyBindName>().value_id);
prev_param_ref =
context.insts().Get(prev_param_ref.As<SemIR::AnyBindName>().value_id);
}
auto new_param = new_param_ref.As<SemIR::Param>();
auto prev_param = prev_param_ref.As<SemIR::Param>();
if (new_param.name_id != prev_param.name_id) {
diagnose();
return false;
}
return true;
}
// Returns false if the param refs differ for a redeclaration.
static auto CheckRedeclParams(Context& context, SemIR::InstId new_decl_id,
SemIR::InstBlockId new_param_refs_id,
SemIR::InstId prev_decl_id,
SemIR::InstBlockId prev_param_refs_id,
llvm::StringLiteral param_diag_label,
Substitutions substitutions) -> bool {
// This will often occur for empty params.
if (new_param_refs_id == prev_param_refs_id) {
return true;
}
const auto new_param_ref_ids = context.inst_blocks().Get(new_param_refs_id);
const auto prev_param_ref_ids = context.inst_blocks().Get(prev_param_refs_id);
if (new_param_ref_ids.size() != prev_param_ref_ids.size()) {
CARBON_DIAGNOSTIC(
FunctionRedeclParamCountDiffers, Error,
"Function redeclaration differs because of {0}parameter count of {1}.",
llvm::StringLiteral, int32_t);
CARBON_DIAGNOSTIC(FunctionRedeclParamCountPrevious, Note,
"Previously declared with {0}parameter count of {1}.",
llvm::StringLiteral, int32_t);
context.emitter()
.Build(new_decl_id, FunctionRedeclParamCountDiffers, param_diag_label,
new_param_ref_ids.size())
.Note(prev_decl_id, FunctionRedeclParamCountPrevious, param_diag_label,
prev_param_ref_ids.size())
.Emit();
return false;
}
for (auto [index, new_param_ref_id, prev_param_ref_id] :
llvm::enumerate(new_param_ref_ids, prev_param_ref_ids)) {
if (!CheckRedeclParam(context, param_diag_label, index, new_param_ref_id,
prev_param_ref_id, substitutions)) {
return false;
}
}
return true;
}
// Returns false if the provided function declarations differ.
static auto CheckRedecl(Context& context, const SemIR::Function& new_function,
const SemIR::Function& prev_function,
Substitutions substitutions) -> bool {
if (FunctionDeclHasError(context, new_function) ||
FunctionDeclHasError(context, prev_function)) {
if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
DeclParams(prev_function), substitutions)) {
return false;
}
if (!CheckRedeclParams(
context, new_function.decl_id, new_function.implicit_param_refs_id,
prev_function.decl_id, prev_function.implicit_param_refs_id,
"implicit ", substitutions) ||
!CheckRedeclParams(context, new_function.decl_id,
new_function.param_refs_id, prev_function.decl_id,
prev_function.param_refs_id, "", substitutions)) {
if (new_function.return_type_id == SemIR::TypeId::Error ||
prev_function.return_type_id == SemIR::TypeId::Error) {
return false;
}
auto prev_return_type_id =