You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Diagnose using named constraint's name inside its definition (#6906)
Using a named constraint inside itself is problematic:
- If there were not require decls written above, it identifies as an
empty set. This makes `Z(Self)` essentially disappear in the identified
facet type, which produces "no use of Self" diagnostics while the user
can see a use of Self in the code.
- It won't include require decls that are written after, and so `require
T impls Z` won't actually enforce that `T` impls all of `Z`.
Previously this was an error because using the named constraint would
require it to be identified, and it's not identified until it is
complete. But this will change in proposal #6902. So that proposal also
includes changes to preserve diagnostics for incorrect use of a named
constraint before it's complete, which is implemented here.
Discussed in open discussion [on
2026-03-12](https://docs.google.com/document/d/1mjllGO3ZCL4qGt9uJHUtcxKoHAGEY7Y999ie4EtBWB8/edit?tab=t.0#heading=h.1dvbbrp5a6t3).
The new tests exposed a bug where we're not copying named constraints in
a facet type on the RHS of `where .Self impls` into the facet type on
the left, which is now fixed. The
`fail_require_impls_incomplete_self_in_period_self_impls.carbon` test
would not diagnose its error without this fix.
Copy file name to clipboardExpand all lines: toolchain/check/testdata/named_constraint/require.carbon
+60-93Lines changed: 60 additions & 93 deletions
Original file line number
Diff line number
Diff line change
@@ -175,34 +175,82 @@ constraint Z {
175
175
// --- fail_require_impls_incomplete_self.carbon
176
176
library "[[@TEST_NAME]]";
177
177
178
-
//@dump-sem-ir-begin
179
178
constraint Z {
180
-
// CHECK:STDERR: fail_require_impls_incomplete_self.carbon:[[@LINE+7]]:17: error: facet type `Z` cannot be identified in `require` declaration [RequireImplsUnidentifiedFacetType]
179
+
// CHECK:STDERR: fail_require_impls_incomplete_self.carbon:[[@LINE+4]]:17: error: facet type in `require` declaration refers to the named constraint `Z` from within its definition [RequireImplsReferenceCycle]
181
180
// CHECK:STDERR: require impls Z;
182
181
// CHECK:STDERR: ^
183
-
// CHECK:STDERR: fail_require_impls_incomplete_self.carbon:[[@LINE-4]]:1: note: constraint is currently being defined [NamedConstraintIncompleteWithinDefinition]
// CHECK:STDERR: fail_require_impls_incomplete_self_forward_declared.carbon:[[@LINE+4]]:17: error: facet type in `require` declaration refers to the named constraint `Z` from within its definition [RequireImplsReferenceCycle]
// CHECK:STDERR: fail_require_impls_incomplete_self_in_period_self_impls.carbon:[[@LINE+4]]:17: error: facet type in `require` declaration refers to the named constraint `Z` from within its definition [RequireImplsReferenceCycle]
204
+
// CHECK:STDERR: require impls type where .Self impls Z;
// TODO: This should be a RequireImplsReferenceCycle error since `Z` is being
216
+
// used inside `Z`.
217
+
// CHECK:STDERR: fail_require_impls_incomplete_self_in_class_impls.carbon:[[@LINE+4]]:17: error: semantics TODO: `facet type has constraints that we don't handle yet` [SemanticsTodo]
218
+
// CHECK:STDERR: require impls type where C impls Z;
// CHECK:STDERR: fail_require_impls_incomplete_indirect.carbon:[[@LINE+7]]:17: error: facet type `Z` cannot be identified in `require` declaration [RequireImplsUnidentifiedFacetType]
231
+
// CHECK:STDERR: require impls Z;
232
+
// CHECK:STDERR: ^
233
+
// CHECK:STDERR: fail_require_impls_incomplete_indirect.carbon:[[@LINE-6]]:1: note: constraint was forward declared here [NamedConstraintForwardDeclaredHere]
// CHECK:STDERR: fail_require_impls_incomplete_self_specific.carbon:[[@LINE+7]]:19: error: facet type `Z(Self)` cannot be identified in `require` declaration [RequireImplsUnidentifiedFacetType]
248
+
// CHECK:STDERR: fail_require_impls_incomplete_self_specific.carbon:[[@LINE+4]]:19: error: facet type in `require` declaration refers to the named constraint `Z` from within its definition [RequireImplsReferenceCycle]
197
249
// CHECK:STDERR: require T impls Z(Self);
198
250
// CHECK:STDERR: ^~~~~~~
199
-
// CHECK:STDERR: fail_require_impls_incomplete_self_specific.carbon:[[@LINE-4]]:1: note: constraint is currently being defined [NamedConstraintIncompleteWithinDefinition]
200
-
// CHECK:STDERR: constraint Z(T:! type) {
201
-
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
202
251
// CHECK:STDERR:
203
252
require T impls Z(Self);
204
253
}
205
-
//@dump-sem-ir-end
206
254
207
255
// --- fail_require_impls_without_self.carbon
208
256
library "[[@TEST_NAME]]";
@@ -1064,87 +1112,6 @@ fn F() {
1064
1112
// CHECK:STDOUT:
1065
1113
// CHECK:STDOUT: specific @Z.WithSelf(constants.%Self) {}
0 commit comments