From 1a5107efa4fd01a6dc015507cea3f02a091ffeac Mon Sep 17 00:00:00 2001 From: josh11b <15258583+josh11b@users.noreply.github.com> Date: Mon, 23 Dec 2024 15:05:12 -0800 Subject: [PATCH] Clarify the logic for invalid impl redeclarations (#4738) Follow up to #4179, specifically re: https://github.com/carbon-language/carbon-lang/pull/4719#discussion_r1894364691 . Co-authored-by: Josh L --- toolchain/check/handle_impl.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/toolchain/check/handle_impl.cpp b/toolchain/check/handle_impl.cpp index 9d257526a8c9b..20e6d0afe93ce 100644 --- a/toolchain/check/handle_impl.cpp +++ b/toolchain/check/handle_impl.cpp @@ -326,13 +326,16 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id, {.self_id = self_inst_id, .constraint_id = constraint_inst_id}}; // Add the impl declaration. - bool valid_redeclaration = true; + bool invalid_redeclaration = false; auto lookup_bucket_ref = context.impls().GetOrAddLookupBucket(impl_info); for (auto prev_impl_id : lookup_bucket_ref) { if (MergeImplRedecl(context, impl_info, prev_impl_id)) { - valid_redeclaration = IsValidImplRedecl(context, impl_info, prev_impl_id); - if (valid_redeclaration) { + if (IsValidImplRedecl(context, impl_info, prev_impl_id)) { impl_decl.impl_id = prev_impl_id; + } else { + // IsValidImplRedecl() has issued a diagnostic, avoid generating more + // diagnostics for this declaration. + invalid_redeclaration = true; } break; } @@ -369,8 +372,9 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id, constraint_type_id); } - // Impl definitions are required in the same file as the declaration. - if (!is_definition && valid_redeclaration) { + // Impl definitions are required in the same file as the declaration. We skip + // this requirement if we've already issued an invalid redeclaration error. + if (!is_definition && !invalid_redeclaration) { context.definitions_required().push_back(impl_decl_id); }