From 9c7e0f6bd5af8cbf0751c24fa46cfaba6a38cbc4 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Wed, 6 Aug 2025 16:14:49 -0400 Subject: [PATCH] Add some tests for .Self in the interface params, and comments about implied constraints (#5919) Co-authored-by: Richard Smith --- .../fail_todo_self_in_interface_param.carbon | 25 +++++++++++ .../facet/validate_impl_constraints.carbon | 42 +++++++++++++++++++ .../testdata/impl/impl_assoc_const.carbon | 10 +++++ 3 files changed, 77 insertions(+) create mode 100644 toolchain/check/testdata/facet/fail_todo_self_in_interface_param.carbon diff --git a/toolchain/check/testdata/facet/fail_todo_self_in_interface_param.carbon b/toolchain/check/testdata/facet/fail_todo_self_in_interface_param.carbon new file mode 100644 index 000000000000..e2ebfd0436b6 --- /dev/null +++ b/toolchain/check/testdata/facet/fail_todo_self_in_interface_param.carbon @@ -0,0 +1,25 @@ +// 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/facet/fail_todo_self_in_interface_param.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/fail_todo_self_in_interface_param.carbon + +interface I(T:! type) { + let I1:! type; +} + +// @dump-sem-ir-begin +// CHECK:STDERR: fail_todo_self_in_interface_param.carbon:[[@LINE+4]]:12: error: name `.Self` not found [NameNotFound] +// CHECK:STDERR: fn F(T:! I(.Self) where .I1 = ()) -> T.I1 { +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn F(T:! I(.Self) where .I1 = ()) -> T.I1 { + return (); +} +// @dump-sem-ir-end diff --git a/toolchain/check/testdata/facet/validate_impl_constraints.carbon b/toolchain/check/testdata/facet/validate_impl_constraints.carbon index 6d82e59d9c82..5610cef28095 100644 --- a/toolchain/check/testdata/facet/validate_impl_constraints.carbon +++ b/toolchain/check/testdata/facet/validate_impl_constraints.carbon @@ -90,3 +90,45 @@ fn G(T:! L) { // CHECK:STDERR: F(T); } + +// --- fail_todo_self_in_interface_generic_param_unconstrained.carbon +library "[[@TEST_NAME]]"; + +interface Z {} +interface I(T:! type) {} + +// CHECK:STDERR: fail_todo_self_in_interface_generic_param_unconstrained.carbon:[[@LINE+4]]:12: error: name `.Self` not found [NameNotFound] +// CHECK:STDERR: fn F(T:! I(.Self) where .Self impls Z) {} +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn F(T:! I(.Self) where .Self impls Z) {} + +// CHECK:STDERR: fail_todo_self_in_interface_generic_param_unconstrained.carbon:[[@LINE+4]]:16: error: name `.Self` not found [NameNotFound] +// CHECK:STDERR: fn G(T:! Z & I(.Self)) { +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn G(T:! Z & I(.Self)) { + F(T); +} + +// --- fail_todo_self_in_interface_generic_param_constrained.carbon +library "[[@TEST_NAME]]"; + +interface Z {} +interface I(T:! Z) {} + +// Implied constraint: .Self impls Z, which is satisfied and checked at the end +// of the fn signature. +// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+4]]:12: error: name `.Self` not found [NameNotFound] +// CHECK:STDERR: fn F(T:! I(.Self) where .Self impls Z) {} +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn F(T:! I(.Self) where .Self impls Z) {} + +// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+4]]:16: error: name `.Self` not found [NameNotFound] +// CHECK:STDERR: fn G(T:! Z & I(.Self)) { +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn G(T:! Z & I(.Self)) { + F(T); +} diff --git a/toolchain/check/testdata/impl/impl_assoc_const.carbon b/toolchain/check/testdata/impl/impl_assoc_const.carbon index 519987fa34ff..f236ca7e98df 100644 --- a/toolchain/check/testdata/impl/impl_assoc_const.carbon +++ b/toolchain/check/testdata/impl/impl_assoc_const.carbon @@ -354,6 +354,10 @@ interface Z { class C; impl C as Y {} +// Implied constraint: .Self impls Y. Should be verified when finishing the impl +// decl (where we know .Self is C). See: +// https://discord.com/channels/655572317891461132/941071822756143115/1400953094015160370 +// // CHECK:STDERR: fail_todo_period_self_impl_lookup.carbon:[[@LINE+4]]:25: error: cannot convert type `.Self` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] // CHECK:STDERR: impl C as Z where .Z1 = .Self {} // CHECK:STDERR: ^~~~~ @@ -372,6 +376,9 @@ class C; impl C as Y {} impl C as Z where .Z1 = C {} +// Implied constraint: .Self impls Y. Should be verified against the type of the +// facet value replacing T when calling F. +// // CHECK:STDERR: fail_todo_period_self_compared_with_concrete_self.carbon:[[@LINE+4]]:24: error: cannot convert type `.Self` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] // CHECK:STDERR: fn F(T:! Z where .Z1 = .Self) {} // CHECK:STDERR: ^~~~~ @@ -380,6 +387,9 @@ fn F(T:! Z where .Z1 = .Self) {} fn G() { F(C); + // Implied constraint: .Self impls Y. Should be verified against the type of + // the facet value of C when casting. + // // CHECK:STDERR: fail_todo_period_self_compared_with_concrete_self.carbon:[[@LINE+4]]:23: error: cannot convert type `.Self` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet] // CHECK:STDERR: C as (Z where .Z1 = .Self); // CHECK:STDERR: ^~~~~