From 7c3301c6afab0f4b6680a7e9424b3777ef02785e Mon Sep 17 00:00:00 2001 From: danakj Date: Fri, 24 Oct 2025 13:51:41 -0400 Subject: [PATCH 1/9] require-check --- toolchain/check/handle_require.cpp | 224 +++++++- toolchain/check/name_lookup.cpp | 2 + toolchain/check/node_stack.h | 6 +- .../testdata/facet/require_invalid.carbon | 64 ++- .../check/testdata/interface/require.carbon | 418 +++++++++++--- .../require_invalid_modifiers.carbon | 158 ++++++ .../testdata/named_constraint/convert.carbon | 4 - .../testdata/named_constraint/require.carbon | 514 ++++++++++++++++++ .../require_invalid_modifiers.carbon | 158 ++++++ toolchain/diagnostics/diagnostic_kind.def | 7 + toolchain/lex/token_kind.def | 2 +- 11 files changed, 1459 insertions(+), 98 deletions(-) create mode 100644 toolchain/check/testdata/interface/require_invalid_modifiers.carbon create mode 100644 toolchain/check/testdata/named_constraint/require.carbon create mode 100644 toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index 67e50c66efc71..1ca4c12fcf69c 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -2,29 +2,245 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include "toolchain/base/kind_switch.h" #include "toolchain/check/context.h" +#include "toolchain/check/convert.h" #include "toolchain/check/handle.h" +#include "toolchain/check/modifiers.h" +#include "toolchain/check/name_lookup.h" +#include "toolchain/check/subst.h" #include "toolchain/parse/node_ids.h" +#include "toolchain/sem_ir/named_constraint.h" +#include "toolchain/sem_ir/type_iterator.h" +#include "toolchain/sem_ir/typed_insts.h" namespace Carbon::Check { auto HandleParseNode(Context& context, Parse::RequireIntroducerId node_id) -> bool { - return context.TODO(node_id, "require"); + // Create an instruction block to hold the instructions created for the type + // and constraint. + context.inst_block_stack().Push(); + + // Optional modifiers follow. + context.decl_introducer_state_stack().Push(); + + auto scope_id = context.scope_stack().PeekNameScopeId(); + auto scope_inst_id = context.name_scopes().Get(scope_id).inst_id(); + auto scope_inst = context.insts().Get(scope_inst_id); + if (!scope_inst.Is() && + !scope_inst.Is()) { + CARBON_DIAGNOSTIC( + RequireInWrongScope, Error, + "`require` can only be used in an `interface` or `constraint`"); + context.emitter().Emit(node_id, RequireInWrongScope); + scope_inst_id = SemIR::ErrorInst::InstId; + } + + context.node_stack().Push(node_id, scope_inst_id); + return true; } auto HandleParseNode(Context& context, Parse::RequireDefaultSelfImplsId node_id) -> bool { - return context.TODO(node_id, "require"); + auto scope_inst_id = + context.node_stack().Peek(); + if (scope_inst_id == SemIR::ErrorInst::InstId) { + context.node_stack().Push(node_id, SemIR::ErrorInst::TypeInstId); + return true; + } + + auto scope_id = context.scope_stack().PeekNameScopeId(); + auto lookup_result = + LookupNameInExactScope(context, node_id, SemIR::NameId::SelfType, + scope_id, context.name_scopes().Get(scope_id), + /*is_being_declared=*/false); + CARBON_CHECK(lookup_result.is_found()); + + auto self_inst_id = lookup_result.target_inst_id(); + auto self_type_id = context.insts().Get(self_inst_id).type_id(); + CARBON_CHECK(context.types().Is(self_type_id)); + + auto self_facet_as_type = AddTypeInst( + context, node_id, + {.type_id = SemIR::TypeType::TypeId, + .facet_value_inst_id = self_inst_id}); + context.node_stack().Push(node_id, self_facet_as_type); + return true; } auto HandleParseNode(Context& context, Parse::RequireTypeImplsId node_id) -> bool { - return context.TODO(node_id, "require"); + auto [self_node_id, self_inst_id] = context.node_stack().PopExprWithNodeId(); + auto self_type = ExprAsType(context, self_node_id, self_inst_id); + context.node_stack().Push(node_id, self_type.inst_id); + return true; +} + +static auto ConstraintHasInterface(Context& context, + SemIR::FacetType facet_type) -> bool { + const auto& facet_type_info = + context.facet_types().Get(facet_type.facet_type_id); + + return !facet_type_info.extend_constraints.empty() || + !facet_type_info.self_impls_constraints.empty(); +} + +static auto TypeOrInterfaceReferencesSelf(Context& context, + SemIR::TypeInstId inst_id, + SemIR::FacetType facet_type) -> bool { + if (inst_id == SemIR::ErrorInst::TypeInstId) { + // Don't generate more diagnostics. + return true; + } + + SemIR::TypeIterator type_iter(&context.sem_ir()); + type_iter.Add(context.constant_values().GetConstantTypeInstId(inst_id)); + + const auto& facet_type_info = + context.facet_types().Get(facet_type.facet_type_id); + for (auto extend : facet_type_info.extend_constraints) { + type_iter.Add(extend); + } + for (auto self_impls : facet_type_info.self_impls_constraints) { + type_iter.Add(self_impls); + } + + while (true) { + auto step = type_iter.Next(); + if (step.Is()) { + break; + } + CARBON_KIND_SWITCH(step.any) { + case CARBON_KIND(SemIR::TypeIterator::Step::Error _): { + // Don't generate more diagnostics. + return true; + } + case CARBON_KIND(SemIR::TypeIterator::Step::SymbolicBinding bind): { + if (context.entity_names().Get(bind.entity_name_id).name_id == + SemIR::NameId::SelfType) { + return true; + } + break; + } + default: + break; + } + } + return false; +} + +static auto RequirementReferencesSelf( + Context& context, const SemIR::FacetTypeInfo& facet_type_info) -> bool { + class FindSelfCallbacks : public SubstInstCallbacks { + public: + explicit FindSelfCallbacks(Context* context, bool* found) + : SubstInstCallbacks(context), found_(found) {} + auto Subst(SemIR::InstId& inst_id) -> SubstResult override { + if (*found_ || context().constant_values().Get(inst_id).is_concrete()) { + return FullySubstituted; + } + if (auto bind = + context().insts().TryGetAs(inst_id)) { + const auto& entity_name = + context().entity_names().Get(bind->entity_name_id); + if (entity_name.name_id == SemIR::NameId::SelfType) { + // It would be nice to return a location, but we're working with + // canonical instructions so there's no location available here. + *found_ = true; + return FullySubstituted; + } + } + return SubstOperands; + } + auto Rebuild(SemIR::InstId /*orig_inst_id*/, SemIR::Inst /*new_inst*/) + -> SemIR::InstId override { + CARBON_FATAL(); + } + + bool* found_; + }; + + bool found = false; + FindSelfCallbacks callbacks(&context, &found); + for (const auto& rewrite : facet_type_info.rewrite_constraints) { + SubstInst(context, rewrite.lhs_id, callbacks); + if (found) { + return true; + } + SubstInst(context, rewrite.rhs_id, callbacks); + if (found) { + return true; + } + } + + return false; } auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { - return context.TODO(node_id, "require"); + auto [constraint_node_id, constraint_inst_id] = + context.node_stack().PopExprWithNodeId(); + auto [self_node_id, self_inst_id] = + context.node_stack().PopWithNodeId(); + + auto constraint_constant_value_inst_id = + context.constant_values().GetConstantInstId(constraint_inst_id); + auto constraint_facet_type = context.insts().TryGetAs( + constraint_constant_value_inst_id); + if (constraint_constant_value_inst_id == SemIR::ErrorInst::InstId) { + constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + } else if (!constraint_facet_type) { + CARBON_DIAGNOSTIC( + RequireImplsMissingFacetType, Error, + "`require` declaration constrained by a non-facet type; " + "expected an `interface` or `constraint` name after `impls`"); + context.emitter().Emit(constraint_node_id, RequireImplsMissingFacetType); + constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + } else if (!ConstraintHasInterface(context, *constraint_facet_type)) { + CARBON_DIAGNOSTIC( + RequireImplsHasEmptyFacetType, Error, + "`require` declaration constrained by an empty constraint; " + "expected an `interface` or a non-empty `constraint`"); + context.emitter().Emit(constraint_node_id, RequireImplsHasEmptyFacetType); + constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + } else if (!TypeOrInterfaceReferencesSelf(context, self_inst_id, + *constraint_facet_type)) { + CARBON_DIAGNOSTIC(RequireImplsMissingSelf, Error, + "no `Self` reference found in `require` declaration; " + "`Self` must appear in the self-type or as a generic " + "parameter for an `interface` or `constraint`"); + context.emitter().Emit(node_id, RequireImplsMissingSelf); + constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + } else if (RequirementReferencesSelf( + context, context.facet_types().Get( + constraint_facet_type->facet_type_id))) { + // TODO: Should this be allowed? For now, no, but leads question: + // https://github.com/carbon-language/carbon-lang/issues/6285 + CARBON_DIAGNOSTIC(RequireImplsSelfInWhereExpr, Error, + "`require` declaration with `Self` in the `where` " + "expression of the constraint"); + context.emitter().Emit(constraint_node_id, RequireImplsSelfInWhereExpr); + constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + } + + [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop(); + + // Process modifiers. + auto introducer = + context.decl_introducer_state_stack().Pop(); + LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend); + + auto scope_inst_id = + context.node_stack().Pop(); + if (scope_inst_id == SemIR::ErrorInst::InstId) { + // `require` is in the wrong scope. + return true; + } + + // TODO: Add the `require` constraint to the InterfaceDecl or ConstraintDecl + // from `scope_inst_id`. + + return true; } } // namespace Carbon::Check diff --git a/toolchain/check/name_lookup.cpp b/toolchain/check/name_lookup.cpp index aa8398f7b195f..4489824e505cb 100644 --- a/toolchain/check/name_lookup.cpp +++ b/toolchain/check/name_lookup.cpp @@ -161,6 +161,8 @@ auto LookupUnqualifiedName(Context& context, SemIR::LocId loc_id, DiagnoseNameNotFound(context, loc_id, name_id); } + // TODO: Should this return MakeNotFound if `required` is false, so that + // `is_found()` would be false? return {.specific_id = SemIR::SpecificId::None, .scope_result = SemIR::ScopeLookupResult::MakeError()}; } diff --git a/toolchain/check/node_stack.h b/toolchain/check/node_stack.h index 1428ae19d8501..1b001bcd2a43a 100644 --- a/toolchain/check/node_stack.h +++ b/toolchain/check/node_stack.h @@ -397,6 +397,8 @@ class NodeStack { Id::KindFor()); set_id_if_category_is(Parse::NodeCategory::ImplAs, Id::KindFor()); + set_id_if_category_is(Parse::NodeCategory::RequireImpls, + Id::KindFor()); set_id_if_category_is(Parse::NodeCategory::Decl | Parse::NodeCategory::Statement | Parse::NodeCategory::Modifier, @@ -412,6 +414,7 @@ class NodeStack { case Parse::NodeKind::CallExprStart: case Parse::NodeKind::FieldNameAndType: case Parse::NodeKind::IfExprThen: + case Parse::NodeKind::RequireIntroducer: case Parse::NodeKind::ReturnType: case Parse::NodeKind::ShortCircuitOperandAnd: case Parse::NodeKind::ShortCircuitOperandOr: @@ -523,9 +526,6 @@ class NodeStack { case Parse::NodeKind::ParenExprStart: case Parse::NodeKind::PatternListComma: case Parse::NodeKind::Placeholder: - case Parse::NodeKind::RequireIntroducer: - case Parse::NodeKind::RequireDefaultSelfImpls: - case Parse::NodeKind::RequireTypeImpls: case Parse::NodeKind::RequirementAnd: case Parse::NodeKind::RequirementEqual: case Parse::NodeKind::RequirementEqualEqual: diff --git a/toolchain/check/testdata/facet/require_invalid.carbon b/toolchain/check/testdata/facet/require_invalid.carbon index 0589354e27820..16bc43183ef92 100644 --- a/toolchain/check/testdata/facet/require_invalid.carbon +++ b/toolchain/check/testdata/facet/require_invalid.carbon @@ -15,7 +15,7 @@ library "[[@TEST_NAME]]"; interface Y {} -// CHECK:STDERR: fail_todo_require_outside_scope.carbon:[[@LINE+4]]:1: error: semantics TODO: `require` [SemanticsTodo] +// CHECK:STDERR: fail_todo_require_outside_scope.carbon:[[@LINE+4]]:1: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] // CHECK:STDERR: require impls Y; // CHECK:STDERR: ^~~~~~~ // CHECK:STDERR: @@ -27,8 +27,7 @@ library "[[@TEST_NAME]]"; interface Y {} class C { - // TODO: require is not allowed outside of `interface` or `constraint`. - // CHECK:STDERR: fail_todo_require_in_class.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] + // CHECK:STDERR: fail_todo_require_in_class.carbon:[[@LINE+4]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] // CHECK:STDERR: require impls Y; // CHECK:STDERR: ^~~~~~~ // CHECK:STDERR: @@ -42,6 +41,7 @@ interface Y {} fn F() { // require is not allowed outside of `interface` or `constraint`. + // // CHECK:STDERR: fail_require_in_fn.carbon:[[@LINE+8]]:3: error: expected expression [ExpectedExpr] // CHECK:STDERR: require impls Y; // CHECK:STDERR: ^~~~~~~ @@ -52,3 +52,61 @@ fn F() { // CHECK:STDERR: require impls Y; } + +// --- fail_require_in_nested_class.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // TODO: Add `default` modifier. + fn F() { + class C { + // CHECK:STDERR: fail_require_in_nested_class.carbon:[[@LINE+4]]:7: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] + // CHECK:STDERR: require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + require impls Y; + } + } +} + +// --- fail_require_in_nested_fn.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // TODO: Add `default` modifier. + fn F() { + // require is not allowed outside of `interface` or `constraint`. + // + // CHECK:STDERR: fail_require_in_nested_fn.carbon:[[@LINE+8]]:5: error: expected expression [ExpectedExpr] + // CHECK:STDERR: require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + // CHECK:STDERR: fail_require_in_nested_fn.carbon:[[@LINE+4]]:5: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo] + // CHECK:STDERR: require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + require impls Y; + } +} + +// --- fail_errors_in_require_still_found.carbon +library "[[@TEST_NAME]]"; + +class C { + // The `require` is diagnosed as being in the wrong place. But we can still + // diagnose issues inside the decl too. + // + // CHECK:STDERR: fail_errors_in_require_still_found.carbon:[[@LINE+8]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] + // CHECK:STDERR: require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + // CHECK:STDERR: fail_errors_in_require_still_found.carbon:[[@LINE+4]]:17: error: name `Y` not found [NameNotFound] + // CHECK:STDERR: require impls Y; + // CHECK:STDERR: ^ + // CHECK:STDERR: + require impls Y; +} diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index 50e7efd2deb53..1fb156b6adafe 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -2,7 +2,7 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon // // AUTOUPDATE // TIP: To test this file alone, run: @@ -10,7 +10,7 @@ // TIP: To dump output, run: // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/require.carbon -// --- fail_todo_implicit_self_impls.carbon +// --- fail_todo_extend.carbon library "[[@TEST_NAME]]"; interface Y { @@ -19,15 +19,41 @@ interface Y { //@dump-sem-ir-begin interface Z { - // CHECK:STDERR: fail_todo_implicit_self_impls.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require impls Y; - // CHECK:STDERR: ^~~~~~~ + extend require impls Y; +} +//@dump-sem-ir-end + +fn F(T:! Z) { + // CHECK:STDERR: fail_todo_extend.carbon:[[@LINE+4]]:3: error: member name `YY` not found in `Z` [MemberNameNotFoundInInstScope] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ // CHECK:STDERR: + T.YY(); + // CHECK:STDERR: fail_todo_extend.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + T.(Y.YY)(); +} + +// --- fail_todo_implicit_self_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +//@dump-sem-ir-begin +interface Z { require impls Y; } //@dump-sem-ir-end fn F(T:! Z) { + // CHECK:STDERR: fail_todo_implicit_self_impls.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: T.(Y.YY)(); } @@ -40,19 +66,19 @@ interface Y { //@dump-sem-ir-begin interface Z { - // CHECK:STDERR: fail_todo_explicit_self_impls.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require Self impls Y; - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require Self impls Y; } //@dump-sem-ir-end fn F(T:! Z) { + // CHECK:STDERR: fail_todo_explicit_self_impls.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: T.(Y.YY)(); } -// --- fail_todo_implicit_self_no_extend_name_lookup_fails.carbon +// --- fail_implicit_self_no_extend_name_lookup_fails.carbon library "[[@TEST_NAME]]"; interface Y { @@ -60,19 +86,20 @@ interface Y { } interface Z { - // CHECK:STDERR: fail_todo_implicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require impls Y; - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require impls Y; } fn F(T:! Z) { - // TODO: This should fail name lookup since Z does not extend Y. + // This should fail name lookup since Z does not extend Y. + // + // CHECK:STDERR: fail_implicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: member name `YY` not found in `Z` [MemberNameNotFoundInInstScope] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: T.YY(); } -// --- fail_todo_explicit_self_no_extend_name_lookup_fails.carbon +// --- fail_explicit_self_no_extend_name_lookup_fails.carbon library "[[@TEST_NAME]]"; interface Y { @@ -80,19 +107,20 @@ interface Y { } interface Z { - // CHECK:STDERR: fail_todo_explicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require Self impls Y; - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require Self impls Y; } fn F(T:! Z) { - // TODO: This should fail name lookup since Z does not extend Y. + // This should fail name lookup since Z does not extend Y. + // + // CHECK:STDERR: fail_explicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: member name `YY` not found in `Z` [MemberNameNotFoundInInstScope] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: T.YY(); } -// --- fail_todo_explicit_self_specific_impls.carbon +// --- explicit_self_specific_impls.carbon library "[[@TEST_NAME]]"; interface Y {} @@ -101,175 +129,399 @@ class C(T:! type); //@dump-sem-ir-begin interface Z { - // CHECK:STDERR: fail_todo_explicit_self_specific_impls.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require C(Self) impls Y; - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require C(Self) impls Y; } //@dump-sem-ir-end -// --- fail_todo_impls_where.carbon +// --- require_impls_where.carbon library "[[@TEST_NAME]]"; interface Y { let Y1:! type; } //@dump-sem-ir-begin interface Z { - // CHECK:STDERR: fail_todo_impls_where.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require impls Y where .Y1 = (); - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require impls Y where .Y1 = (); } //@dump-sem-ir-end -// --- fail_todo_other_impls_with_self.carbon +// --- require_impls_self_specific.carbon library "[[@TEST_NAME]]"; -interface Y {} - //@dump-sem-ir-begin interface Z(T:! type) { - // CHECK:STDERR: fail_todo_other_impls_with_self.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require T impls Z(Self); - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require T impls Z(Self); } //@dump-sem-ir-end -// --- fail_todo_other_impls_without_self.carbon +// --- fail_require_impls_without_self.carbon library "[[@TEST_NAME]]"; -interface Y {} +interface Y { + let Y1:! type; +} //@dump-sem-ir-begin interface Z(T:! type) { - // TODO: Either the type `T` or the facet type `Y` must mention `Self`, but - // they don't. - // CHECK:STDERR: fail_todo_other_impls_without_self.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require T impls Y; - // CHECK:STDERR: ^~~~~~~ + // Either the type `T` or the facet type `Y` must mention `Self` in a way that + // it would appear in the type structure used for impl lookup (so inside a + // `where` does not count). But they don't. + // + // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for an `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y where .Y1 = Self; + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CHECK:STDERR: - require T impls Y; + require T impls Y where .Y1 = Self; } //@dump-sem-ir-end -// --- fail_todo_extend.carbon +// --- fail_require_self_in_requirement.carbon library "[[@TEST_NAME]]"; interface Y { - fn YY(); + let Y1:! type; } //@dump-sem-ir-begin -interface Z { - // CHECK:STDERR: fail_todo_extend.carbon:[[@LINE+4]]:10: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: extend require impls Y; - // CHECK:STDERR: ^~~~~~~ +interface Z(T:! type) { + // Self can not appear in a requirement. It can appear in the self-type or in + // an interface parameter. + // + // CHECK:STDERR: fail_require_self_in_requirement.carbon:[[@LINE+4]]:17: error: `require` declaration with `Self` in the `where` expression of the constraint [RequireImplsSelfInWhereExpr] + // CHECK:STDERR: require impls Y where .Y1 = Self; + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~ // CHECK:STDERR: - extend require impls Y; + require impls Y where .Y1 = Self; } //@dump-sem-ir-end -fn F(T:! Z) { - T.YY(); - T.(Y.YY)(); +// --- fail_self_impls_self.carbon +library "[[@TEST_NAME]]"; + +interface Z(T:! type) { + // CHECK:STDERR: fail_self_impls_self.carbon:[[@LINE+4]]:17: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require impls Self; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + require impls Self; +} + +// --- fail_impls_type.carbon +library "[[@TEST_NAME]]"; + +class C(T:! type) {} + +interface Z(T:! type) { + // CHECK:STDERR: fail_impls_type.carbon:[[@LINE+4]]:19: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require T impls C(Self); + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + require T impls C(Self); +} + +// --- fail_non_type_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z(T:! type) { + // CHECK:STDERR: fail_non_type_impls.carbon:[[@LINE+7]]:11: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet] + // CHECK:STDERR: require 1 impls Y; + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_non_type_impls.carbon:[[@LINE+4]]:11: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: require 1 impls Y; + // CHECK:STDERR: ^ + // CHECK:STDERR: + require 1 impls Y; +} + +// --- fail_impls_non_type.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z(T:! type) { + // CHECK:STDERR: fail_impls_non_type.carbon:[[@LINE+4]]:17: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require impls 1; + // CHECK:STDERR: ^ + // CHECK:STDERR: + require impls 1; +} + +// --- require_same.carbon +library "[[@TEST_NAME]]"; + +interface Y { + require impls Y; +} + +interface Z(T:! type) { + require impls Z(T); } +// CHECK:STDOUT: --- fail_todo_extend.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: .YY = +// CHECK:STDOUT: witness = () +// CHECK:STDOUT: } +// CHECK:STDOUT: // CHECK:STDOUT: --- fail_todo_implicit_self_impls.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst6000001D -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: --- fail_todo_explicit_self_impls.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst5000001D -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_explicit_self_specific_impls.carbon +// CHECK:STDOUT: --- explicit_self_specific_impls.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst68000023 -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .C = +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_impls_where.carbon +// CHECK:STDOUT: --- require_impls_where.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst5800001A -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_other_impls_with_self.carbon +// CHECK:STDOUT: --- require_impls_self_specific.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type.9fb: type = generic_interface_type @Z [concrete] +// CHECK:STDOUT: %Z.generic: %Z.type.9fb = struct_value () [concrete] +// CHECK:STDOUT: %Z.type.dd5: type = facet_type <@Z, @Z(%T)> [symbolic] +// CHECK:STDOUT: %Self: %Z.type.dd5 = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self [symbolic] +// CHECK:STDOUT: %Z.type.f19: type = facet_type <@Z, @Z(%Self.binding.as_type)> [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: generic interface @Z(.inst78000017.loc6_13: type) { -// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)] +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type.9fb = interface_decl @Z [concrete = constants.%Z.generic] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_13.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic interface @Z(%T.loc4_13.2: type) { +// CHECK:STDOUT: %T.loc4_13.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_13.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Z.type.loc5: type = facet_type <@Z, @Z(%Self.binding.as_type)> [symbolic = %Z.type.loc5 (constants.%Z.type.f19)] // CHECK:STDOUT: // CHECK:STDOUT: interface { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst78000021 -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .T = +// CHECK:STDOUT: .Z = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: specific @Z(constants.%T) { -// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: %T.loc4_13.1 => constants.%T // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_other_impls_without_self.carbon +// CHECK:STDOUT: specific @Z(constants.%Self.binding.as_type) { +// CHECK:STDOUT: %T.loc4_13.1 => constants.%Self.binding.as_type +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_require_impls_without_self.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type.9fb: type = generic_interface_type @Z [concrete] +// CHECK:STDOUT: %Z.generic: %Z.type.9fb = struct_value () [concrete] +// CHECK:STDOUT: %Z.type.dd5: type = facet_type <@Z, @Z(%T)> [symbolic] +// CHECK:STDOUT: %Self.cbb: %Z.type.dd5 = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] +// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] +// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.cbb [symbolic] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: generic interface @Z(.inst44000017.loc6_13: type) { -// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)] +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type.9fb = interface_decl @Z [concrete = constants.%Z.generic] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc8_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic interface @Z(%T.loc8_13.2: type) { +// CHECK:STDOUT: %T.loc8_13.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] // CHECK:STDOUT: // CHECK:STDOUT: interface { +// CHECK:STDOUT: +// CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst44000021 -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .T = +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: specific @Z(constants.%T) { -// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: %T.loc8_13.1 => constants.%T // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_extend.carbon +// CHECK:STDOUT: --- fail_require_self_in_requirement.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { +// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] +// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type.9fb: type = generic_interface_type @Z [concrete] +// CHECK:STDOUT: %Z.generic: %Z.type.9fb = struct_value () [concrete] +// CHECK:STDOUT: %Z.type.dd5: type = facet_type <@Z, @Z(%T)> [symbolic] +// CHECK:STDOUT: %Self.cbb: %Z.type.dd5 = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.cbb [symbolic] +// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] +// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] +// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: interface @Z { -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = .inst6400001D -// CHECK:STDOUT: witness = invalid +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type.9fb = interface_decl @Z [concrete = constants.%Z.generic] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc8_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic interface @Z(%T.loc8_13.2: type) { +// CHECK:STDOUT: %T.loc8_13.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] +// CHECK:STDOUT: +// CHECK:STDOUT: interface { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Z(constants.%T) { +// CHECK:STDOUT: %T.loc8_13.1 => constants.%T // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/check/testdata/interface/require_invalid_modifiers.carbon b/toolchain/check/testdata/interface/require_invalid_modifiers.carbon new file mode 100644 index 0000000000000..e4b16c042406f --- /dev/null +++ b/toolchain/check/testdata/interface/require_invalid_modifiers.carbon @@ -0,0 +1,158 @@ +// 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 +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interface/require_invalid_modifiers.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/require_invalid_modifiers.carbon + +// --- fail_abstract_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_abstract_require.carbon:[[@LINE+4]]:3: error: `abstract` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: abstract require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + abstract require impls Y; +} + +// --- fail_base_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_base_require.carbon:[[@LINE+4]]:3: error: `base` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: base require impls Y; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + base require impls Y; +} + +// --- fail_default_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_default_require.carbon:[[@LINE+4]]:3: error: `default` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: default require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + default require impls Y; +} + +// --- fail_extern_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_extern_require.carbon:[[@LINE+4]]:3: error: `extern` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: extern require impls Y; + // CHECK:STDERR: ^~~~~~ + // CHECK:STDERR: + extern require impls Y; +} + +// --- fail_final_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_final_require.carbon:[[@LINE+4]]:3: error: `final` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: final require impls Y; + // CHECK:STDERR: ^~~~~ + // CHECK:STDERR: + final require impls Y; +} + +// --- fail_impl_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_impl_require.carbon:[[@LINE+4]]:3: error: `impl` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: impl require impls Y; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + impl require impls Y; +} + +// --- fail_override_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_override_require.carbon:[[@LINE+4]]:3: error: `override` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: override require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + override require impls Y; +} + +// --- fail_returned_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_returned_require.carbon:[[@LINE+8]]:3: error: unrecognized declaration introducer [UnrecognizedDecl] + // CHECK:STDERR: returned require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + // CHECK:STDERR: fail_returned_require.carbon:[[@LINE+4]]:3: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo] + // CHECK:STDERR: returned require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + returned require impls Y; +} + +// --- fail_private_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_private_require.carbon:[[@LINE+4]]:3: error: `private` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: private require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + private require impls Y; +} + +// --- fail_protected_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_protected_require.carbon:[[@LINE+4]]:3: error: `protected` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: protected require impls Y; + // CHECK:STDERR: ^~~~~~~~~ + // CHECK:STDERR: + protected require impls Y; +} + +// --- fail_override_virtual.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +interface Z { + // CHECK:STDERR: fail_override_virtual.carbon:[[@LINE+4]]:3: error: `virtual` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: virtual require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + virtual require impls Y; +} diff --git a/toolchain/check/testdata/named_constraint/convert.carbon b/toolchain/check/testdata/named_constraint/convert.carbon index b0b23ce3ad8c8..d7df3cf7a38c8 100644 --- a/toolchain/check/testdata/named_constraint/convert.carbon +++ b/toolchain/check/testdata/named_constraint/convert.carbon @@ -43,10 +43,6 @@ library "[[@TEST_NAME]]"; interface Z {} constraint E { - // CHECK:STDERR: fail_todo_unspecified.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo] - // CHECK:STDERR: require impls Z; - // CHECK:STDERR: ^~~~~~~ - // CHECK:STDERR: require impls Z; } diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon new file mode 100644 index 0000000000000..b7474abb16310 --- /dev/null +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -0,0 +1,514 @@ +// 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 +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/named_constraint/require.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/require.carbon + +// --- fail_todo_extend.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +//@dump-sem-ir-begin +constraint Z { + extend require impls Y; +} +//@dump-sem-ir-end + +fn F(T:! Z) { + // CHECK:STDERR: fail_todo_extend.carbon:[[@LINE+4]]:3: error: member name `YY` not found [MemberNameNotFound] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + T.YY(); + // CHECK:STDERR: fail_todo_extend.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `type` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + T.(Y.YY)(); +} + +// --- fail_todo_implicit_self_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +//@dump-sem-ir-begin +constraint Z { + require impls Y; +} +//@dump-sem-ir-end + +fn F(T:! Z) { + // CHECK:STDERR: fail_todo_implicit_self_impls.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `type` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + T.(Y.YY)(); +} + +// --- fail_todo_explicit_self_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +//@dump-sem-ir-begin +constraint Z { + require Self impls Y; +} +//@dump-sem-ir-end + +fn F(T:! Z) { + // CHECK:STDERR: fail_todo_explicit_self_impls.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `type` into type implementing `Y` [ConversionFailureFacetToFacet] + // CHECK:STDERR: T.(Y.YY)(); + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + T.(Y.YY)(); +} + +// --- fail_implicit_self_no_extend_name_lookup_fails.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +constraint Z { + require impls Y; +} + +fn F(T:! Z) { + // This should fail name lookup since Z does not extend Y. + // + // CHECK:STDERR: fail_implicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: member name `YY` not found [MemberNameNotFound] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + T.YY(); +} + +// --- fail_explicit_self_no_extend_name_lookup_fails.carbon +library "[[@TEST_NAME]]"; + +interface Y { + fn YY(); +} + +constraint Z { + require Self impls Y; +} + +fn F(T:! Z) { + // This should fail name lookup since Z does not extend Y. + // + // CHECK:STDERR: fail_explicit_self_no_extend_name_lookup_fails.carbon:[[@LINE+4]]:3: error: member name `YY` not found [MemberNameNotFound] + // CHECK:STDERR: T.YY(); + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + T.YY(); +} + +// --- explicit_self_specific_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +class C(T:! type); + +//@dump-sem-ir-begin +constraint Z { + require C(Self) impls Y; +} +//@dump-sem-ir-end + +// --- require_impls_where.carbon +library "[[@TEST_NAME]]"; + +interface Y { let Y1:! type; } + +//@dump-sem-ir-begin +constraint Z { + require impls Y where .Y1 = (); +} +//@dump-sem-ir-end + +// --- fail_require_impls_self_specific.carbon +library "[[@TEST_NAME]]"; + +//@dump-sem-ir-begin +constraint Z(T:! type) { + // Z has no requirements yet, so requiring itself on T adds nothing. We don't + // want an error saying `Self` was not used (since the resulting facet type of + // `Z(Self)` is empty, dropping the usage of `Self`), as that's confusing. So + // we get an error that the constraint is not doing anything. + // + // CHECK:STDERR: fail_require_impls_self_specific.carbon:[[@LINE+4]]:19: error: `require` declaration constrained by an empty constraint; expected an `interface` or a non-empty `constraint` [RequireImplsHasEmptyFacetType] + // CHECK:STDERR: require T impls Z(Self); + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + require T impls Z(Self); +} +//@dump-sem-ir-end + +// --- fail_require_impls_without_self.carbon +library "[[@TEST_NAME]]"; + +interface Y { + let Y1:! type; +} + +//@dump-sem-ir-begin +constraint Z(T:! type) { + // Either the type `T` or the facet type `Y` must mention `Self` in a way that + // it would appear in the type structure used for impl lookup (so inside a + // `where` does not count). But they don't. + // + // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for an `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y where .Y1 = Self; + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + require T impls Y where .Y1 = Self; +} +//@dump-sem-ir-end + +// --- fail_require_self_in_requirement.carbon +library "[[@TEST_NAME]]"; + +interface Y { + let Y1:! type; +} + +//@dump-sem-ir-begin +constraint Z(T:! type) { + // Self can not appear in a requirement. It can appear in the self-type or in + // an interface parameter. + // + // CHECK:STDERR: fail_require_self_in_requirement.carbon:[[@LINE+4]]:17: error: `require` declaration with `Self` in the `where` expression of the constraint [RequireImplsSelfInWhereExpr] + // CHECK:STDERR: require impls Y where .Y1 = Self; + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + require impls Y where .Y1 = Self; +} +//@dump-sem-ir-end + +// --- fail_self_impls_self.carbon +library "[[@TEST_NAME]]"; + +constraint Z(T:! type) { + // CHECK:STDERR: fail_self_impls_self.carbon:[[@LINE+4]]:17: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require impls Self; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + require impls Self; +} + +// --- fail_impls_type.carbon +library "[[@TEST_NAME]]"; + +class C(T:! type) {} + +constraint Z(T:! type) { + // CHECK:STDERR: fail_impls_type.carbon:[[@LINE+4]]:19: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require T impls C(Self); + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + require T impls C(Self); +} + +// --- fail_non_type_impls.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z(T:! type) { + // CHECK:STDERR: fail_non_type_impls.carbon:[[@LINE+7]]:11: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet] + // CHECK:STDERR: require 1 impls Y; + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_non_type_impls.carbon:[[@LINE+4]]:11: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: require 1 impls Y; + // CHECK:STDERR: ^ + // CHECK:STDERR: + require 1 impls Y; +} + +// --- fail_impls_non_type.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z(T:! type) { + // CHECK:STDERR: fail_impls_non_type.carbon:[[@LINE+4]]:17: error: `require` declaration constrained by a non-facet type; expected an `interface` or `constraint` name after `impls` [RequireImplsMissingFacetType] + // CHECK:STDERR: require impls 1; + // CHECK:STDERR: ^ + // CHECK:STDERR: + require impls 1; +} + +// CHECK:STDOUT: --- fail_todo_extend.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_implicit_self_impls.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_explicit_self_impls.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- explicit_self_specific_impls.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .C = +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- require_impls_where.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_require_impls_self_specific.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type: type = generic_named_constaint_type @Z [concrete] +// CHECK:STDOUT: %empty_struct: %Z.type = struct_value () [concrete] +// CHECK:STDOUT: %Self: %type = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type = constraint_decl @Z [concrete = constants.%empty_struct] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc4_14.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_14.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic constraint @Z(%T.loc4_14.2: type) { +// CHECK:STDOUT: %T.loc4_14.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_14.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: +// CHECK:STDOUT: constraint { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .T = +// CHECK:STDOUT: .Z = +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Z(constants.%T) { +// CHECK:STDOUT: %T.loc4_14.1 => constants.%T +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Z(constants.%Self.binding.as_type) { +// CHECK:STDOUT: %T.loc4_14.1 => constants.%Self.binding.as_type +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_require_impls_without_self.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type: type = generic_named_constaint_type @Z [concrete] +// CHECK:STDOUT: %empty_struct: %Z.type = struct_value () [concrete] +// CHECK:STDOUT: %Self.eca: %type = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] +// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] +// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.eca [symbolic] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type = constraint_decl @Z [concrete = constants.%empty_struct] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc8_14.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic constraint @Z(%T.loc8_14.2: type) { +// CHECK:STDOUT: %T.loc8_14.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] +// CHECK:STDOUT: +// CHECK:STDOUT: constraint { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .T = +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Z(constants.%T) { +// CHECK:STDOUT: %T.loc8_14.1 => constants.%T +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_require_self_in_requirement.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] +// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] +// CHECK:STDOUT: %Z.type: type = generic_named_constaint_type @Z [concrete] +// CHECK:STDOUT: %empty_struct: %Z.type = struct_value () [concrete] +// CHECK:STDOUT: %Self.eca: %type = bind_symbolic_name Self, 1 [symbolic] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.eca [symbolic] +// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] +// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] +// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: %Z.type = constraint_decl @Z [concrete = constants.%empty_struct] { +// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: %T.loc8_14.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic constraint @Z(%T.loc8_14.2: type) { +// CHECK:STDOUT: %T.loc8_14.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] +// CHECK:STDOUT: +// CHECK:STDOUT: constraint { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Z(constants.%T) { +// CHECK:STDOUT: %T.loc8_14.1 => constants.%T +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon b/toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon new file mode 100644 index 0000000000000..8a2865d79be0b --- /dev/null +++ b/toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon @@ -0,0 +1,158 @@ +// 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 +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/require_invalid_modifiers.carbon + +// --- fail_abstract_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_abstract_require.carbon:[[@LINE+4]]:3: error: `abstract` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: abstract require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + abstract require impls Y; +} + +// --- fail_base_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_base_require.carbon:[[@LINE+4]]:3: error: `base` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: base require impls Y; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + base require impls Y; +} + +// --- fail_default_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_default_require.carbon:[[@LINE+4]]:3: error: `default` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: default require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + default require impls Y; +} + +// --- fail_extern_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_extern_require.carbon:[[@LINE+4]]:3: error: `extern` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: extern require impls Y; + // CHECK:STDERR: ^~~~~~ + // CHECK:STDERR: + extern require impls Y; +} + +// --- fail_final_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_final_require.carbon:[[@LINE+4]]:3: error: `final` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: final require impls Y; + // CHECK:STDERR: ^~~~~ + // CHECK:STDERR: + final require impls Y; +} + +// --- fail_impl_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_impl_require.carbon:[[@LINE+4]]:3: error: `impl` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: impl require impls Y; + // CHECK:STDERR: ^~~~ + // CHECK:STDERR: + impl require impls Y; +} + +// --- fail_override_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_override_require.carbon:[[@LINE+4]]:3: error: `override` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: override require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + override require impls Y; +} + +// --- fail_returned_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_returned_require.carbon:[[@LINE+8]]:3: error: unrecognized declaration introducer [UnrecognizedDecl] + // CHECK:STDERR: returned require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + // CHECK:STDERR: fail_returned_require.carbon:[[@LINE+4]]:3: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo] + // CHECK:STDERR: returned require impls Y; + // CHECK:STDERR: ^~~~~~~~ + // CHECK:STDERR: + returned require impls Y; +} + +// --- fail_private_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_private_require.carbon:[[@LINE+4]]:3: error: `private` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: private require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + private require impls Y; +} + +// --- fail_protected_require.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_protected_require.carbon:[[@LINE+4]]:3: error: `protected` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: protected require impls Y; + // CHECK:STDERR: ^~~~~~~~~ + // CHECK:STDERR: + protected require impls Y; +} + +// --- fail_override_virtual.carbon +library "[[@TEST_NAME]]"; + +interface Y {} + +constraint Z { + // CHECK:STDERR: fail_override_virtual.carbon:[[@LINE+4]]:3: error: `virtual` not allowed on `require` declaration [ModifierNotAllowedOnDeclaration] + // CHECK:STDERR: virtual require impls Y; + // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: + virtual require impls Y; +} diff --git a/toolchain/diagnostics/diagnostic_kind.def b/toolchain/diagnostics/diagnostic_kind.def index ab17f5f97b2cf..8bd16b6f9a86d 100644 --- a/toolchain/diagnostics/diagnostic_kind.def +++ b/toolchain/diagnostics/diagnostic_kind.def @@ -349,6 +349,13 @@ CARBON_DIAGNOSTIC_KIND(FinalImplOverlapsDifferentFileNote) CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccess) CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccessNote) +// Require checking. +CARBON_DIAGNOSTIC_KIND(RequireImplsHasEmptyFacetType) +CARBON_DIAGNOSTIC_KIND(RequireImplsMissingFacetType) +CARBON_DIAGNOSTIC_KIND(RequireImplsMissingSelf) +CARBON_DIAGNOSTIC_KIND(RequireImplsSelfInWhereExpr) +CARBON_DIAGNOSTIC_KIND(RequireInWrongScope) + // Let declaration checking. CARBON_DIAGNOSTIC_KIND(ExpectedInitializerAfterLet) diff --git a/toolchain/lex/token_kind.def b/toolchain/lex/token_kind.def index bc124d51b0836..d3626b18c9e7d 100644 --- a/toolchain/lex/token_kind.def +++ b/toolchain/lex/token_kind.def @@ -163,6 +163,7 @@ CARBON_DECL_INTRODUCER_TOKEN(Let, "let") CARBON_DECL_INTRODUCER_TOKEN(Library, "library") CARBON_DECL_INTRODUCER_TOKEN(Namespace, "namespace") CARBON_DECL_INTRODUCER_TOKEN(Package, "package") +CARBON_DECL_INTRODUCER_TOKEN(Require, "require") CARBON_TOKEN_WITH_VIRTUAL_NODE( CARBON_DECL_INTRODUCER_TOKEN(Var, "var")) @@ -203,7 +204,6 @@ CARBON_KEYWORD_TOKEN(Override, "override") CARBON_KEYWORD_TOKEN(Partial, "partial") CARBON_KEYWORD_TOKEN(Private, "private") CARBON_KEYWORD_TOKEN(Protected, "protected") -CARBON_KEYWORD_TOKEN(Require, "require") CARBON_KEYWORD_TOKEN(Return, "return") CARBON_KEYWORD_TOKEN(Returned, "returned") CARBON_KEYWORD_TOKEN(SelfTypeIdentifier, "Self") From aa40e75c2d5454ab2c60e61aa451753c624cb063 Mon Sep 17 00:00:00 2001 From: danakj Date: Tue, 28 Oct 2025 11:58:33 -0400 Subject: [PATCH 2/9] test-passes-now --- toolchain/check/testdata/named_constraint/convert.carbon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchain/check/testdata/named_constraint/convert.carbon b/toolchain/check/testdata/named_constraint/convert.carbon index d7df3cf7a38c8..209aba6914931 100644 --- a/toolchain/check/testdata/named_constraint/convert.carbon +++ b/toolchain/check/testdata/named_constraint/convert.carbon @@ -37,7 +37,7 @@ fn G(T:! E2) { } //@dump-sem-ir-end -// --- fail_todo_unspecified.carbon +// --- unspecified.carbon library "[[@TEST_NAME]]"; interface Z {} From bb1af2f1d0b7304511d1d1bc8d1d7fb438f52e96 Mon Sep 17 00:00:00 2001 From: danakj Date: Tue, 28 Oct 2025 18:11:18 -0400 Subject: [PATCH 3/9] review --- toolchain/check/handle_require.cpp | 77 +++++++++------- .../testdata/facet/require_invalid.carbon | 8 +- .../check/testdata/interface/require.carbon | 88 +++++-------------- .../testdata/named_constraint/require.carbon | 17 +++- .../generics/interface/require.carbon | 27 ++++++ 5 files changed, 116 insertions(+), 101 deletions(-) diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index 1ca4c12fcf69c..5d37ae85e6221 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -86,48 +86,65 @@ static auto ConstraintHasInterface(Context& context, !facet_type_info.self_impls_constraints.empty(); } -static auto TypeOrInterfaceReferencesSelf(Context& context, - SemIR::TypeInstId inst_id, - SemIR::FacetType facet_type) -> bool { +static auto TypeStructureReferencesSelf(Context& context, + SemIR::TypeInstId inst_id, + SemIR::FacetType facet_type) -> bool { if (inst_id == SemIR::ErrorInst::TypeInstId) { // Don't generate more diagnostics. return true; } - SemIR::TypeIterator type_iter(&context.sem_ir()); - type_iter.Add(context.constant_values().GetConstantTypeInstId(inst_id)); + auto find_self = [&](SemIR::TypeIterator& type_iter) -> bool { + while (true) { + auto step = type_iter.Next(); + if (step.Is()) { + break; + } + CARBON_KIND_SWITCH(step.any) { + case CARBON_KIND(SemIR::TypeIterator::Step::Error _): { + // Don't generate more diagnostics. + return true; + } + case CARBON_KIND(SemIR::TypeIterator::Step::SymbolicBinding bind): { + if (context.entity_names().Get(bind.entity_name_id).name_id == + SemIR::NameId::SelfType) { + return true; + } + break; + } + default: + break; + } + } + return false; + }; + + { + SemIR::TypeIterator type_iter(&context.sem_ir()); + type_iter.Add(context.constant_values().GetConstantTypeInstId(inst_id)); + if (find_self(type_iter)) { + return true; + } + } const auto& facet_type_info = context.facet_types().Get(facet_type.facet_type_id); for (auto extend : facet_type_info.extend_constraints) { + SemIR::TypeIterator type_iter(&context.sem_ir()); type_iter.Add(extend); + if (!find_self(type_iter)) { + return false; + } } for (auto self_impls : facet_type_info.self_impls_constraints) { + SemIR::TypeIterator type_iter(&context.sem_ir()); type_iter.Add(self_impls); - } - - while (true) { - auto step = type_iter.Next(); - if (step.Is()) { - break; - } - CARBON_KIND_SWITCH(step.any) { - case CARBON_KIND(SemIR::TypeIterator::Step::Error _): { - // Don't generate more diagnostics. - return true; - } - case CARBON_KIND(SemIR::TypeIterator::Step::SymbolicBinding bind): { - if (context.entity_names().Get(bind.entity_name_id).name_id == - SemIR::NameId::SelfType) { - return true; - } - break; - } - default: - break; + if (!find_self(type_iter)) { + return false; } } - return false; + + return true; } static auto RequirementReferencesSelf( @@ -203,12 +220,12 @@ auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { "expected an `interface` or a non-empty `constraint`"); context.emitter().Emit(constraint_node_id, RequireImplsHasEmptyFacetType); constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; - } else if (!TypeOrInterfaceReferencesSelf(context, self_inst_id, - *constraint_facet_type)) { + } else if (!TypeStructureReferencesSelf(context, self_inst_id, + *constraint_facet_type)) { CARBON_DIAGNOSTIC(RequireImplsMissingSelf, Error, "no `Self` reference found in `require` declaration; " "`Self` must appear in the self-type or as a generic " - "parameter for an `interface` or `constraint`"); + "parameter for each `interface` or `constraint`"); context.emitter().Emit(node_id, RequireImplsMissingSelf); constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; } else if (RequirementReferencesSelf( diff --git a/toolchain/check/testdata/facet/require_invalid.carbon b/toolchain/check/testdata/facet/require_invalid.carbon index 16bc43183ef92..aa791ba75e69b 100644 --- a/toolchain/check/testdata/facet/require_invalid.carbon +++ b/toolchain/check/testdata/facet/require_invalid.carbon @@ -10,24 +10,24 @@ // TIP: To dump output, run: // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/require_invalid.carbon -// --- fail_todo_require_outside_scope.carbon +// --- fail_require_outside_scope.carbon library "[[@TEST_NAME]]"; interface Y {} -// CHECK:STDERR: fail_todo_require_outside_scope.carbon:[[@LINE+4]]:1: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] +// CHECK:STDERR: fail_require_outside_scope.carbon:[[@LINE+4]]:1: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] // CHECK:STDERR: require impls Y; // CHECK:STDERR: ^~~~~~~ // CHECK:STDERR: require impls Y; -// --- fail_todo_require_in_class.carbon +// --- fail_require_in_class.carbon library "[[@TEST_NAME]]"; interface Y {} class C { - // CHECK:STDERR: fail_todo_require_in_class.carbon:[[@LINE+4]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] + // CHECK:STDERR: fail_require_in_class.carbon:[[@LINE+4]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope] // CHECK:STDERR: require impls Y; // CHECK:STDERR: ^~~~~~~ // CHECK:STDERR: diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index 1fb156b6adafe..4a2c5ed30af92 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -160,19 +160,17 @@ interface Y { let Y1:! type; } -//@dump-sem-ir-begin interface Z(T:! type) { // Either the type `T` or the facet type `Y` must mention `Self` in a way that // it would appear in the type structure used for impl lookup (so inside a // `where` does not count). But they don't. // - // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for an `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] // CHECK:STDERR: require T impls Y where .Y1 = Self; // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CHECK:STDERR: require T impls Y where .Y1 = Self; } -//@dump-sem-ir-end // --- fail_require_self_in_requirement.carbon library "[[@TEST_NAME]]"; @@ -181,7 +179,6 @@ interface Y { let Y1:! type; } -//@dump-sem-ir-begin interface Z(T:! type) { // Self can not appear in a requirement. It can appear in the self-type or in // an interface parameter. @@ -192,7 +189,21 @@ interface Z(T:! type) { // CHECK:STDERR: require impls Y where .Y1 = Self; } -//@dump-sem-ir-end + +// --- fail_require_impls_without_self_in_one_interface.carbon +library "[[@TEST_NAME]]"; + +interface Y(T:! type) {} + +interface Z(T:! type) { + // Self is in one interface but not the other. + // + // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y(Self) & Y({}); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + require T impls Y(Self) & Y({}); +} // --- fail_self_impls_self.carbon library "[[@TEST_NAME]]"; @@ -254,9 +265,11 @@ interface Y { require impls Y; } +//@dump-sem-ir-begin interface Z(T:! type) { require impls Z(T); } +//@dump-sem-ir-end // CHECK:STDOUT: --- fail_todo_extend.carbon // CHECK:STDOUT: @@ -422,21 +435,16 @@ interface Z(T:! type) { // CHECK:STDOUT: %T.loc4_13.1 => constants.%Self.binding.as_type // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_require_impls_without_self.carbon +// CHECK:STDOUT: --- require_same.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { -// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] // CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] // CHECK:STDOUT: %Z.type.9fb: type = generic_interface_type @Z [concrete] // CHECK:STDOUT: %Z.generic: %Z.type.9fb = struct_value () [concrete] // CHECK:STDOUT: %Z.type.dd5: type = facet_type <@Z, @Z(%T)> [symbolic] // CHECK:STDOUT: %Self.cbb: %Z.type.dd5 = bind_symbolic_name Self, 1 [symbolic] -// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] -// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] -// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.cbb [symbolic] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] +// CHECK:STDOUT: %Self.binding.as_type.be9: type = symbolic_binding_type Self, 1, %Self.cbb [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -456,67 +464,15 @@ interface Z(T:! type) { // CHECK:STDOUT: // CHECK:STDOUT: !definition: // CHECK:STDOUT: -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type.be9)] // CHECK:STDOUT: // CHECK:STDOUT: interface { // CHECK:STDOUT: // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .Z = // CHECK:STDOUT: .T = -// CHECK:STDOUT: .Y = -// CHECK:STDOUT: witness = () -// CHECK:STDOUT: } -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: specific @Z(constants.%T) { -// CHECK:STDOUT: %T.loc8_13.1 => constants.%T -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: --- fail_require_self_in_requirement.carbon -// CHECK:STDOUT: -// CHECK:STDOUT: constants { -// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] -// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] -// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] -// CHECK:STDOUT: %Z.type.9fb: type = generic_interface_type @Z [concrete] -// CHECK:STDOUT: %Z.generic: %Z.type.9fb = struct_value () [concrete] -// CHECK:STDOUT: %Z.type.dd5: type = facet_type <@Z, @Z(%T)> [symbolic] -// CHECK:STDOUT: %Self.cbb: %Z.type.dd5 = bind_symbolic_name Self, 1 [symbolic] -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.cbb [symbolic] -// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] -// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] -// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: imports { -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: file { -// CHECK:STDOUT: %Z.decl: %Z.type.9fb = interface_decl @Z [concrete = constants.%Z.generic] { -// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] -// CHECK:STDOUT: } { -// CHECK:STDOUT: -// CHECK:STDOUT: %T.loc8_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] -// CHECK:STDOUT: } -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: generic interface @Z(%T.loc8_13.2: type) { -// CHECK:STDOUT: %T.loc8_13.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_13.1 (constants.%T)] -// CHECK:STDOUT: -// CHECK:STDOUT: !definition: -// CHECK:STDOUT: -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] -// CHECK:STDOUT: -// CHECK:STDOUT: interface { -// CHECK:STDOUT: -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = %Self.1 -// CHECK:STDOUT: .Y = // CHECK:STDOUT: witness = () // CHECK:STDOUT: } // CHECK:STDOUT: } diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index b7474abb16310..ea5435ad010c7 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -175,7 +175,7 @@ constraint Z(T:! type) { // it would appear in the type structure used for impl lookup (so inside a // `where` does not count). But they don't. // - // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for an `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: fail_require_impls_without_self.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] // CHECK:STDERR: require T impls Y where .Y1 = Self; // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CHECK:STDERR: @@ -203,6 +203,21 @@ constraint Z(T:! type) { } //@dump-sem-ir-end +// --- fail_require_impls_without_self_in_one_interface.carbon +library "[[@TEST_NAME]]"; + +interface Y(T:! type) {} + +constraint Z(T:! type) { + // Self is in one interface but not the other. + // + // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y(Self) & Y({}); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + require T impls Y(Self) & Y({}); +} + // --- fail_self_impls_self.carbon library "[[@TEST_NAME]]"; diff --git a/toolchain/parse/testdata/generics/interface/require.carbon b/toolchain/parse/testdata/generics/interface/require.carbon index 3db3d05861973..7760b3ee89b0b 100644 --- a/toolchain/parse/testdata/generics/interface/require.carbon +++ b/toolchain/parse/testdata/generics/interface/require.carbon @@ -48,6 +48,14 @@ interface Z { extend require impls Y; } +// --- type_and.carbon + +interface Y {} + +interface Z { + require impls Y & Y; +} + // --- fail_missing_impls.carbon interface Y {} @@ -190,6 +198,25 @@ interface Z { // CHECK:STDOUT: {kind: 'InterfaceDefinition', text: '}', subtree_size: 9}, // CHECK:STDOUT: {kind: 'FileEnd', text: ''}, // CHECK:STDOUT: ] +// CHECK:STDOUT: - filename: type_and.carbon +// CHECK:STDOUT: parse_tree: [ +// CHECK:STDOUT: {kind: 'FileStart', text: ''}, +// CHECK:STDOUT: {kind: 'InterfaceIntroducer', text: 'interface'}, +// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Y'}, +// CHECK:STDOUT: {kind: 'InterfaceDefinitionStart', text: '{', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'InterfaceDefinition', text: '}', subtree_size: 4}, +// CHECK:STDOUT: {kind: 'InterfaceIntroducer', text: 'interface'}, +// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Z'}, +// CHECK:STDOUT: {kind: 'InterfaceDefinitionStart', text: '{', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'RequireIntroducer', text: 'require'}, +// CHECK:STDOUT: {kind: 'RequireDefaultSelfImpls', text: 'impls'}, +// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'Y'}, +// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'Y'}, +// CHECK:STDOUT: {kind: 'InfixOperatorAmp', text: '&', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'RequireDecl', text: ';', subtree_size: 6}, +// CHECK:STDOUT: {kind: 'InterfaceDefinition', text: '}', subtree_size: 10}, +// CHECK:STDOUT: {kind: 'FileEnd', text: ''}, +// CHECK:STDOUT: ] // CHECK:STDOUT: - filename: fail_missing_impls.carbon // CHECK:STDOUT: parse_tree: [ // CHECK:STDOUT: {kind: 'FileStart', text: ''}, From 777ff646451aac15e3c81896b07657c3c1d05473 Mon Sep 17 00:00:00 2001 From: danakj Date: Wed, 29 Oct 2025 11:07:59 -0400 Subject: [PATCH 4/9] remove-RequireImplsSelfInWhereExpr-diag --- toolchain/check/handle_require.cpp | 57 --------------- .../check/testdata/interface/require.carbon | 54 +++++++++----- .../testdata/named_constraint/require.carbon | 73 +++++-------------- toolchain/diagnostics/diagnostic_kind.def | 1 - 4 files changed, 55 insertions(+), 130 deletions(-) diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index 5d37ae85e6221..f97e8070049c0 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -147,53 +147,6 @@ static auto TypeStructureReferencesSelf(Context& context, return true; } -static auto RequirementReferencesSelf( - Context& context, const SemIR::FacetTypeInfo& facet_type_info) -> bool { - class FindSelfCallbacks : public SubstInstCallbacks { - public: - explicit FindSelfCallbacks(Context* context, bool* found) - : SubstInstCallbacks(context), found_(found) {} - auto Subst(SemIR::InstId& inst_id) -> SubstResult override { - if (*found_ || context().constant_values().Get(inst_id).is_concrete()) { - return FullySubstituted; - } - if (auto bind = - context().insts().TryGetAs(inst_id)) { - const auto& entity_name = - context().entity_names().Get(bind->entity_name_id); - if (entity_name.name_id == SemIR::NameId::SelfType) { - // It would be nice to return a location, but we're working with - // canonical instructions so there's no location available here. - *found_ = true; - return FullySubstituted; - } - } - return SubstOperands; - } - auto Rebuild(SemIR::InstId /*orig_inst_id*/, SemIR::Inst /*new_inst*/) - -> SemIR::InstId override { - CARBON_FATAL(); - } - - bool* found_; - }; - - bool found = false; - FindSelfCallbacks callbacks(&context, &found); - for (const auto& rewrite : facet_type_info.rewrite_constraints) { - SubstInst(context, rewrite.lhs_id, callbacks); - if (found) { - return true; - } - SubstInst(context, rewrite.rhs_id, callbacks); - if (found) { - return true; - } - } - - return false; -} - auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { auto [constraint_node_id, constraint_inst_id] = context.node_stack().PopExprWithNodeId(); @@ -228,16 +181,6 @@ auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { "parameter for each `interface` or `constraint`"); context.emitter().Emit(node_id, RequireImplsMissingSelf); constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; - } else if (RequirementReferencesSelf( - context, context.facet_types().Get( - constraint_facet_type->facet_type_id))) { - // TODO: Should this be allowed? For now, no, but leads question: - // https://github.com/carbon-language/carbon-lang/issues/6285 - CARBON_DIAGNOSTIC(RequireImplsSelfInWhereExpr, Error, - "`require` declaration with `Self` in the `where` " - "expression of the constraint"); - context.emitter().Emit(constraint_node_id, RequireImplsSelfInWhereExpr); - constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; } [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop(); diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index 4a2c5ed30af92..e82820011d8fe 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -172,24 +172,6 @@ interface Z(T:! type) { require T impls Y where .Y1 = Self; } -// --- fail_require_self_in_requirement.carbon -library "[[@TEST_NAME]]"; - -interface Y { - let Y1:! type; -} - -interface Z(T:! type) { - // Self can not appear in a requirement. It can appear in the self-type or in - // an interface parameter. - // - // CHECK:STDERR: fail_require_self_in_requirement.carbon:[[@LINE+4]]:17: error: `require` declaration with `Self` in the `where` expression of the constraint [RequireImplsSelfInWhereExpr] - // CHECK:STDERR: require impls Y where .Y1 = Self; - // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~ - // CHECK:STDERR: - require impls Y where .Y1 = Self; -} - // --- fail_require_impls_without_self_in_one_interface.carbon library "[[@TEST_NAME]]"; @@ -258,6 +240,20 @@ interface Z(T:! type) { require impls 1; } +// --- require_self_in_requirement.carbon +library "[[@TEST_NAME]]"; + +interface Y { + let Y1:! type; +} + +//@dump-sem-ir-begin +interface Z { + // Self can appear in a requirement. + require impls Y where .Y1 = Self; +} +//@dump-sem-ir-end + // --- require_same.carbon library "[[@TEST_NAME]]"; @@ -435,6 +431,28 @@ interface Z(T:! type) { // CHECK:STDOUT: %T.loc4_13.1 => constants.%Self.binding.as_type // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: --- require_self_in_requirement.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: witness = () +// CHECK:STDOUT: } +// CHECK:STDOUT: // CHECK:STDOUT: --- require_same.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index ea5435ad010c7..836c8e9cb28a9 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -183,26 +183,6 @@ constraint Z(T:! type) { } //@dump-sem-ir-end -// --- fail_require_self_in_requirement.carbon -library "[[@TEST_NAME]]"; - -interface Y { - let Y1:! type; -} - -//@dump-sem-ir-begin -constraint Z(T:! type) { - // Self can not appear in a requirement. It can appear in the self-type or in - // an interface parameter. - // - // CHECK:STDERR: fail_require_self_in_requirement.carbon:[[@LINE+4]]:17: error: `require` declaration with `Self` in the `where` expression of the constraint [RequireImplsSelfInWhereExpr] - // CHECK:STDERR: require impls Y where .Y1 = Self; - // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~ - // CHECK:STDERR: - require impls Y where .Y1 = Self; -} -//@dump-sem-ir-end - // --- fail_require_impls_without_self_in_one_interface.carbon library "[[@TEST_NAME]]"; @@ -271,6 +251,20 @@ constraint Z(T:! type) { require impls 1; } +// --- fail_require_self_in_requirement.carbon +library "[[@TEST_NAME]]"; + +interface Y { + let Y1:! type; +} + +//@dump-sem-ir-begin +constraint Z { + // Self can appear in a requirement. + require impls Y where .Y1 = Self; +} +//@dump-sem-ir-end + // CHECK:STDOUT: --- fail_todo_extend.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -480,50 +474,21 @@ constraint Z(T:! type) { // CHECK:STDOUT: --- fail_require_self_in_requirement.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { -// CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete] // CHECK:STDOUT: %type: type = facet_type [concrete] -// CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic] -// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete] -// CHECK:STDOUT: %Z.type: type = generic_named_constaint_type @Z [concrete] -// CHECK:STDOUT: %empty_struct: %Z.type = struct_value () [concrete] -// CHECK:STDOUT: %Self.eca: %type = bind_symbolic_name Self, 1 [symbolic] -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.eca [symbolic] -// CHECK:STDOUT: %.Self.f65: %Y.type = bind_symbolic_name .Self [symbolic_self] -// CHECK:STDOUT: %Y.lookup_impl_witness: = lookup_impl_witness %.Self.f65, @Y [symbolic_self] -// CHECK:STDOUT: %impl.elem0: type = impl_witness_access %Y.lookup_impl_witness, element0 [symbolic_self] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where %impl.elem0 = %Self.binding.as_type> [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: file { -// CHECK:STDOUT: %Z.decl: %Z.type = constraint_decl @Z [concrete = constants.%empty_struct] { -// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete] -// CHECK:STDOUT: } { -// CHECK:STDOUT: -// CHECK:STDOUT: %T.loc8_14.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] -// CHECK:STDOUT: } +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: generic constraint @Z(%T.loc8_14.2: type) { -// CHECK:STDOUT: %T.loc8_14.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] -// CHECK:STDOUT: -// CHECK:STDOUT: !definition: +// CHECK:STDOUT: constraint @Z { // CHECK:STDOUT: -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] -// CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] -// CHECK:STDOUT: -// CHECK:STDOUT: constraint { -// CHECK:STDOUT: -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = %Self.1 -// CHECK:STDOUT: .Y = -// CHECK:STDOUT: } -// CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: specific @Z(constants.%T) { -// CHECK:STDOUT: %T.loc8_14.1 => constants.%T +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/diagnostics/diagnostic_kind.def b/toolchain/diagnostics/diagnostic_kind.def index 8bd16b6f9a86d..ee5f41acf1c0d 100644 --- a/toolchain/diagnostics/diagnostic_kind.def +++ b/toolchain/diagnostics/diagnostic_kind.def @@ -353,7 +353,6 @@ CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccessNote) CARBON_DIAGNOSTIC_KIND(RequireImplsHasEmptyFacetType) CARBON_DIAGNOSTIC_KIND(RequireImplsMissingFacetType) CARBON_DIAGNOSTIC_KIND(RequireImplsMissingSelf) -CARBON_DIAGNOSTIC_KIND(RequireImplsSelfInWhereExpr) CARBON_DIAGNOSTIC_KIND(RequireInWrongScope) // Let declaration checking. From 32c8e1e8aee7a313fa8ddbe899052ce59c471ae8 Mon Sep 17 00:00:00 2001 From: danakj Date: Wed, 29 Oct 2025 14:54:01 -0400 Subject: [PATCH 5/9] fix-test-name --- toolchain/check/testdata/named_constraint/require.carbon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index 32f04d58e415c..7052636c2f818 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -251,7 +251,7 @@ constraint Z(T:! type) { require impls 1; } -// --- fail_require_self_in_requirement.carbon +// --- require_self_in_requirement.carbon library "[[@TEST_NAME]]"; interface Y { @@ -471,7 +471,7 @@ constraint Z { // CHECK:STDOUT: %T.loc8_14.1 => constants.%T // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_require_self_in_requirement.carbon +// CHECK:STDOUT: --- require_self_in_requirement.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] From 41ac0aadd9b8ddc85d7273340e9f8164152daada Mon Sep 17 00:00:00 2001 From: danakj Date: Fri, 31 Oct 2025 13:34:08 -0400 Subject: [PATCH 6/9] identify facet type --- toolchain/check/handle_require.cpp | 97 ++++++++----------- .../check/testdata/interface/require.carbon | 4 - .../testdata/named_constraint/require.carbon | 82 +++++++++++++--- toolchain/diagnostics/diagnostic_kind.def | 1 - 4 files changed, 112 insertions(+), 72 deletions(-) diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index f97e8070049c0..029c93862977f 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -9,6 +9,7 @@ #include "toolchain/check/modifiers.h" #include "toolchain/check/name_lookup.h" #include "toolchain/check/subst.h" +#include "toolchain/check/type_completion.h" #include "toolchain/parse/node_ids.h" #include "toolchain/sem_ir/named_constraint.h" #include "toolchain/sem_ir/type_iterator.h" @@ -77,18 +78,9 @@ auto HandleParseNode(Context& context, Parse::RequireTypeImplsId node_id) return true; } -static auto ConstraintHasInterface(Context& context, - SemIR::FacetType facet_type) -> bool { - const auto& facet_type_info = - context.facet_types().Get(facet_type.facet_type_id); - - return !facet_type_info.extend_constraints.empty() || - !facet_type_info.self_impls_constraints.empty(); -} - -static auto TypeStructureReferencesSelf(Context& context, - SemIR::TypeInstId inst_id, - SemIR::FacetType facet_type) -> bool { +static auto TypeStructureReferencesSelf( + Context& context, SemIR::TypeInstId inst_id, + const SemIR::IdentifiedFacetType& identified_facet_type) -> bool { if (inst_id == SemIR::ErrorInst::TypeInstId) { // Don't generate more diagnostics. return true; @@ -127,24 +119,15 @@ static auto TypeStructureReferencesSelf(Context& context, } } - const auto& facet_type_info = - context.facet_types().Get(facet_type.facet_type_id); - for (auto extend : facet_type_info.extend_constraints) { - SemIR::TypeIterator type_iter(&context.sem_ir()); - type_iter.Add(extend); - if (!find_self(type_iter)) { - return false; - } - } - for (auto self_impls : facet_type_info.self_impls_constraints) { + for (auto specific_interface : identified_facet_type.required_interfaces()) { SemIR::TypeIterator type_iter(&context.sem_ir()); - type_iter.Add(self_impls); - if (!find_self(type_iter)) { - return false; + type_iter.Add(specific_interface); + if (find_self(type_iter)) { + return true; } } - return true; + return false; } auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { @@ -153,50 +136,56 @@ auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { auto [self_node_id, self_inst_id] = context.node_stack().PopWithNodeId(); + [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop(); + + // Process modifiers. + auto introducer = + context.decl_introducer_state_stack().Pop(); + LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend); + + auto scope_inst_id = + context.node_stack().Pop(); + auto constraint_constant_value_inst_id = context.constant_values().GetConstantInstId(constraint_inst_id); auto constraint_facet_type = context.insts().TryGetAs( constraint_constant_value_inst_id); - if (constraint_constant_value_inst_id == SemIR::ErrorInst::InstId) { - constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; - } else if (!constraint_facet_type) { - CARBON_DIAGNOSTIC( - RequireImplsMissingFacetType, Error, - "`require` declaration constrained by a non-facet type; " - "expected an `interface` or `constraint` name after `impls`"); - context.emitter().Emit(constraint_node_id, RequireImplsMissingFacetType); - constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; - } else if (!ConstraintHasInterface(context, *constraint_facet_type)) { - CARBON_DIAGNOSTIC( - RequireImplsHasEmptyFacetType, Error, - "`require` declaration constrained by an empty constraint; " - "expected an `interface` or a non-empty `constraint`"); - context.emitter().Emit(constraint_node_id, RequireImplsHasEmptyFacetType); - constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; - } else if (!TypeStructureReferencesSelf(context, self_inst_id, - *constraint_facet_type)) { + if (!constraint_facet_type) { + if (constraint_constant_value_inst_id != SemIR::ErrorInst::InstId) { + CARBON_DIAGNOSTIC( + RequireImplsMissingFacetType, Error, + "`require` declaration constrained by a non-facet type; " + "expected an `interface` or `constraint` name after `impls`"); + context.emitter().Emit(constraint_node_id, RequireImplsMissingFacetType); + } + // Can't continue without a constraint to use. + return true; + } + + auto identified_facet_type_id = + RequireIdentifiedFacetType(context, *constraint_facet_type); + const auto& identified = + context.identified_facet_types().Get(identified_facet_type_id); + + if (!TypeStructureReferencesSelf(context, self_inst_id, identified)) { CARBON_DIAGNOSTIC(RequireImplsMissingSelf, Error, "no `Self` reference found in `require` declaration; " "`Self` must appear in the self-type or as a generic " "parameter for each `interface` or `constraint`"); context.emitter().Emit(node_id, RequireImplsMissingSelf); - constraint_inst_id = self_inst_id = SemIR::ErrorInst::TypeInstId; + return true; } - [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop(); - - // Process modifiers. - auto introducer = - context.decl_introducer_state_stack().Pop(); - LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend); - - auto scope_inst_id = - context.node_stack().Pop(); if (scope_inst_id == SemIR::ErrorInst::InstId) { // `require` is in the wrong scope. return true; } + if (identified.required_interfaces().empty()) { + // A `require T impls type` adds no actual constraints. + return true; + } + // TODO: Add the `require` constraint to the InterfaceDecl or ConstraintDecl // from `scope_inst_id`. diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index 3d3b31f44f7d3..f08162b849626 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -180,10 +180,6 @@ interface Y(T:! type) {} interface Z(T:! type) { // Self is in one interface but not the other. // - // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] - // CHECK:STDERR: require T impls Y(Self) & Y({}); - // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // CHECK:STDERR: require T impls Y(Self) & Y({}); } diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index 7052636c2f818..493a156c155fb 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -144,19 +144,37 @@ constraint Z { } //@dump-sem-ir-end -// --- fail_require_impls_self_specific.carbon +// --- todo_fail_require_impls_incomplete_constraint.carbon +library "[[@TEST_NAME]]"; + +constraint Y; + +//@dump-sem-ir-begin +constraint Z { + // TODO: This should fail since `Y` cannot be identified. + require impls Y; +} +//@dump-sem-ir-end + +// --- todo_fail_require_impls_incomplete_self.carbon +library "[[@TEST_NAME]]"; + +//@dump-sem-ir-begin +constraint Z { + // TODO: This should fail since `Z` cannot be identified. + require impls Z; +} +//@dump-sem-ir-end + +// --- fail_require_impls_incomplete_self_specific.carbon library "[[@TEST_NAME]]"; //@dump-sem-ir-begin constraint Z(T:! type) { - // Z has no requirements yet, so requiring itself on T adds nothing. We don't - // want an error saying `Self` was not used (since the resulting facet type of - // `Z(Self)` is empty, dropping the usage of `Self`), as that's confusing. So - // we get an error that the constraint is not doing anything. - // - // CHECK:STDERR: fail_require_impls_self_specific.carbon:[[@LINE+4]]:19: error: `require` declaration constrained by an empty constraint; expected an `interface` or a non-empty `constraint` [RequireImplsHasEmptyFacetType] + // TODO: This should fail since `Z` cannot be identified. + // CHECK:STDERR: fail_require_impls_incomplete_self_specific.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] // CHECK:STDERR: require T impls Z(Self); - // CHECK:STDERR: ^~~~~~~ + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~ // CHECK:STDERR: require T impls Z(Self); } @@ -191,10 +209,6 @@ interface Y(T:! type) {} constraint Z(T:! type) { // Self is in one interface but not the other. // - // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] - // CHECK:STDERR: require T impls Y(Self) & Y({}); - // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // CHECK:STDERR: require T impls Y(Self) & Y({}); } @@ -371,7 +385,49 @@ constraint Z { // CHECK:STDOUT: .Y = // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_require_impls_self_specific.carbon +// CHECK:STDOUT: --- todo_fail_require_impls_incomplete_constraint.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Y = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- todo_fail_require_impls_incomplete_self.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Z.decl: type = constraint_decl @Z [concrete = constants.%type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: constraint @Z { +// CHECK:STDOUT: +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Z = +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_require_impls_incomplete_self_specific.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] diff --git a/toolchain/diagnostics/diagnostic_kind.def b/toolchain/diagnostics/diagnostic_kind.def index 7299f2e179307..1fdb671634616 100644 --- a/toolchain/diagnostics/diagnostic_kind.def +++ b/toolchain/diagnostics/diagnostic_kind.def @@ -353,7 +353,6 @@ CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccess) CARBON_DIAGNOSTIC_KIND(MissingImplInMemberAccessNote) // Require checking. -CARBON_DIAGNOSTIC_KIND(RequireImplsHasEmptyFacetType) CARBON_DIAGNOSTIC_KIND(RequireImplsMissingFacetType) CARBON_DIAGNOSTIC_KIND(RequireImplsMissingSelf) CARBON_DIAGNOSTIC_KIND(RequireInWrongScope) From 594879582a19395e9d1034b3c5163858c7198fe8 Mon Sep 17 00:00:00 2001 From: danakj Date: Fri, 31 Oct 2025 13:40:44 -0400 Subject: [PATCH 7/9] add-test-comment --- toolchain/check/testdata/interface/require.carbon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index c605bb2184f1b..f0c5d2ebb0dcc 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -259,6 +259,8 @@ interface Y { //@dump-sem-ir-begin interface Z(T:! type) { + // This is okay because an interface Z can be identified before it's complete, + // unlike a named constraint. require impls Z(T); } //@dump-sem-ir-end From 949622a0d129bf836763d340ad88e0d7bb6072ce Mon Sep 17 00:00:00 2001 From: danakj Date: Fri, 31 Oct 2025 13:40:48 -0400 Subject: [PATCH 8/9] autoupdate --- .../testdata/named_constraint/require.carbon | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index 493a156c155fb..c37c699cf4f34 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -283,6 +283,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -293,7 +294,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -304,6 +305,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -314,7 +316,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -325,6 +327,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -335,7 +338,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -346,6 +349,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -356,7 +360,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -368,6 +372,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -378,7 +383,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -389,6 +394,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -399,7 +405,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -410,6 +416,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -420,7 +427,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self @@ -455,14 +462,14 @@ constraint Z { // CHECK:STDOUT: %T.loc4_14.1: type = symbolic_binding T, 0 [symbolic = %T.loc4_14.1 (constants.%T)] // CHECK:STDOUT: // CHECK:STDOUT: !definition: -// CHECK:STDOUT: -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Self.loc4_24.2: %type = symbolic_binding Self, 1 [symbolic = %Self.loc4_24.2 (constants.%Self)] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.loc4_24.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] // CHECK:STDOUT: // CHECK:STDOUT: constraint { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self.loc4_24.1: %type = symbolic_binding Self, 1 [symbolic = %Self.loc4_24.2 (constants.%Self)] // CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .Self = %Self.loc4_24.1 // CHECK:STDOUT: .T = // CHECK:STDOUT: .Z = // CHECK:STDOUT: } @@ -509,15 +516,15 @@ constraint Z { // CHECK:STDOUT: %T.loc8_14.1: type = symbolic_binding T, 0 [symbolic = %T.loc8_14.1 (constants.%T)] // CHECK:STDOUT: // CHECK:STDOUT: !definition: -// CHECK:STDOUT: -// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] +// CHECK:STDOUT: %Self.loc8_24.2: %type = symbolic_binding Self, 1 [symbolic = %Self.loc8_24.2 (constants.%Self.aa1)] +// CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 1, %Self.loc8_24.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type)] // CHECK:STDOUT: %Y_where.type: type = facet_type <@Y where constants.%impl.elem0 = %Self.binding.as_type> [symbolic = %Y_where.type (constants.%Y_where.type)] // CHECK:STDOUT: // CHECK:STDOUT: constraint { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self.loc8_24.1: %type = symbolic_binding Self, 1 [symbolic = %Self.loc8_24.2 (constants.%Self.aa1)] // CHECK:STDOUT: // CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = %Self.1 +// CHECK:STDOUT: .Self = %Self.loc8_24.1 // CHECK:STDOUT: .T = // CHECK:STDOUT: .Y = // CHECK:STDOUT: } @@ -531,6 +538,7 @@ constraint Z { // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %type: type = facet_type [concrete] +// CHECK:STDOUT: %Self.861: %type = symbolic_binding Self, 0 [symbolic] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -541,7 +549,7 @@ constraint Z { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: constraint @Z { -// CHECK:STDOUT: +// CHECK:STDOUT: %Self: %type = symbolic_binding Self, 0 [symbolic = constants.%Self.861] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self From d5eee022e6a0065b24a9110701a02350d142f199 Mon Sep 17 00:00:00 2001 From: danakj Date: Fri, 31 Oct 2025 13:49:12 -0400 Subject: [PATCH 9/9] fix-test --- toolchain/check/handle_require.cpp | 10 +++++++--- toolchain/check/testdata/interface/require.carbon | 4 ++++ .../check/testdata/named_constraint/require.carbon | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index 029c93862977f..4d5912095f7cf 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -119,15 +119,19 @@ static auto TypeStructureReferencesSelf( } } + if (identified_facet_type.required_interfaces().empty()) { + return false; + } + for (auto specific_interface : identified_facet_type.required_interfaces()) { SemIR::TypeIterator type_iter(&context.sem_ir()); type_iter.Add(specific_interface); - if (find_self(type_iter)) { - return true; + if (!find_self(type_iter)) { + return false; } } - return false; + return true; } auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool { diff --git a/toolchain/check/testdata/interface/require.carbon b/toolchain/check/testdata/interface/require.carbon index f0c5d2ebb0dcc..7fc6328ddb494 100644 --- a/toolchain/check/testdata/interface/require.carbon +++ b/toolchain/check/testdata/interface/require.carbon @@ -180,6 +180,10 @@ interface Y(T:! type) {} interface Z(T:! type) { // Self is in one interface but not the other. // + // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y(Self) & Y({}); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: require T impls Y(Self) & Y({}); } diff --git a/toolchain/check/testdata/named_constraint/require.carbon b/toolchain/check/testdata/named_constraint/require.carbon index c37c699cf4f34..892bad451d46f 100644 --- a/toolchain/check/testdata/named_constraint/require.carbon +++ b/toolchain/check/testdata/named_constraint/require.carbon @@ -209,6 +209,10 @@ interface Y(T:! type) {} constraint Z(T:! type) { // Self is in one interface but not the other. // + // CHECK:STDERR: fail_require_impls_without_self_in_one_interface.carbon:[[@LINE+4]]:3: error: no `Self` reference found in `require` declaration; `Self` must appear in the self-type or as a generic parameter for each `interface` or `constraint` [RequireImplsMissingSelf] + // CHECK:STDERR: require T impls Y(Self) & Y({}); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: require T impls Y(Self) & Y({}); }