From 2b5f90cd744cc65db8871a1b90f615280ea9e131 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Mon, 19 Oct 2026 19:35:11 +0000 Subject: [PATCH] Diagnose a witness table entry being set to multiple different values (#7996) If the entry is set to the same value, accept it. If it's a different value, diagnose it. --- toolchain/check/impl.cpp | 27 +++++++++++--- .../impl/impl_as_named_constraint.carbon | 35 +++++++++++++++++++ toolchain/diagnostics/kind.def | 1 + 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/toolchain/check/impl.cpp b/toolchain/check/impl.cpp index 47397a1b8b5a..fa3812985ddc 100644 --- a/toolchain/check/impl.cpp +++ b/toolchain/check/impl.cpp @@ -569,11 +569,6 @@ auto AddImplWitnessForDeclaration(Context& context, SemIR::LocId loc_id, continue; } - // FacetTypes resolution disallows two rewrites to the same associated - // constant, so we won't ever have a facet write twice to the same position - // in the witness table. - CARBON_CHECK(table_entry == SemIR::InstId::ImplWitnessTablePlaceholder); - // If the associated constant has a symbolic type, convert the rewrite // value to that type now we know the value of `Self`. SemIR::TypeId assoc_const_type_id = assoc_constant_decl->type_id; @@ -618,6 +613,28 @@ auto AddImplWitnessForDeclaration(Context& context, SemIR::LocId loc_id, rewrite_inst_id), "Rewritten value for associated constant is not canonical."); + // The same entry may be written by a named constraint and the impl, or by + // two named constraints, etc. They need to all match. + if (table_entry != SemIR::InstId::ImplWitnessTablePlaceholder) { + if (context.constant_values().Get(table_entry) != + context.constant_values().Get(rewrite_inst_id)) { + if (table_entry != SemIR::ErrorInst::InstId) { + const auto& assoc_const = context.associated_constants().Get( + assoc_constant_decl->assoc_const_id); + CARBON_DIAGNOSTIC( + AssociatedConstantGivenDifferentValues, Error, + "associated constant {0} given value {1} that is different from " + "previously given value {2}", + SemIR::NameId, InstIdAsConstant, InstIdAsConstant); + context.emitter().Emit( + constraint_loc_id, AssociatedConstantGivenDifferentValues, + assoc_const.name_id, rewrite_inst_id, table_entry); + } + table_entry = SemIR::ErrorInst::InstId; + } + continue; + } + table_entry = AddInst( context, loc_id, {.type_id = context.insts().Get(rewrite_inst_id).type_id(), diff --git a/toolchain/check/testdata/impl/impl_as_named_constraint.carbon b/toolchain/check/testdata/impl/impl_as_named_constraint.carbon index 6cf33a912fca..87c36217bfe1 100644 --- a/toolchain/check/testdata/impl/impl_as_named_constraint.carbon +++ b/toolchain/check/testdata/impl/impl_as_named_constraint.carbon @@ -383,6 +383,41 @@ fn F() -> G(C).(Z(G(C)).Result) { return (); } +// --- rewrite_twice_same.carbon +library "[[@TEST_NAME]]"; + +interface Z { let Z1: type; } +constraint J { + extend require impls Z where .Z1 = (); +} +impl () as J where .Z1 = () {} + +// --- fail_rewrite_twice_different.carbon +library "[[@TEST_NAME]]"; + +interface Z { let Z1: type; } +constraint J { + extend require impls Z where .Z1 = {}; +} +// CHECK:STDERR: fail_rewrite_twice_different.carbon:[[@LINE+2]]:12-27: error: associated constant Z1 given value `()` that is different from previously given value `{}` [AssociatedConstantGivenDifferentValues] +// CHECK:STDERR: +impl () as J where .Z1 = () {} + +// --- fail_rewrite_twice_different_named_constraints.carbon +library "[[@TEST_NAME]]"; + +interface Z { let Z1: type; } +constraint K { + extend require impls Z where .Z1 = (); +} +constraint J { + extend require impls Z where .Z1 = {}; + extend require impls K; +} +// CHECK:STDERR: fail_rewrite_twice_different_named_constraints.carbon:[[@LINE+2]]:12: error: associated constant Z1 given value `{}` that is different from previously given value `()` [AssociatedConstantGivenDifferentValues] +// CHECK:STDERR: +impl () as J {} + // CHECK:STDOUT: --- fail_error_self_in_require.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { diff --git a/toolchain/diagnostics/kind.def b/toolchain/diagnostics/kind.def index 24d0700d33a8..9d06dd09aefd 100644 --- a/toolchain/diagnostics/kind.def +++ b/toolchain/diagnostics/kind.def @@ -358,6 +358,7 @@ CARBON_DIAGNOSTIC_KIND(FinalImplInMatchFirst) CARBON_DIAGNOSTIC_KIND(FinalImplInvalidFile) CARBON_DIAGNOSTIC_KIND(FinalImplOverlapsSameFile) CARBON_DIAGNOSTIC_KIND(FinalImplOverlapsDifferentFile) +CARBON_DIAGNOSTIC_KIND(AssociatedConstantGivenDifferentValues) // Impl lookup. CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccess)