incomplete.carbon 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/named_constraint/incomplete.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/incomplete.carbon
  12. // --- fail_incomplete_constraint_in_impl_lookup.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Z {}
  15. // An incomplete constraint is a valid type for a symbolic facet, and is just
  16. // not able to provide any witness for an impl lookup on that facet.
  17. constraint N;
  18. fn F(T:! N) {
  19. // CHECK:STDERR: fail_incomplete_constraint_in_impl_lookup.carbon:[[@LINE+4]]:3: error: cannot convert type `T` that implements `N` into type implementing `Z` [ConversionFailureFacetToFacet]
  20. // CHECK:STDERR: T as Z;
  21. // CHECK:STDERR: ^~~~~~
  22. // CHECK:STDERR:
  23. T as Z;
  24. }
  25. // --- fail_does_not_diagnose_identify_failure_in_deduce.carbon
  26. library "[[@TEST_NAME]]";
  27. constraint N;
  28. interface Z {}
  29. // Since N is an incomplete constraint, it can not be identified as a target for
  30. // impl lookup to determine if C implements N. No diagnostic is attached to the
  31. // impl when the lookup fails, we simply fail to convert.
  32. impl forall [T:! N] T as Z {}
  33. class C;
  34. fn F() {
  35. // CHECK:STDERR: fail_does_not_diagnose_identify_failure_in_deduce.carbon:[[@LINE+4]]:3: error: cannot convert type `C` into type implementing `Z` [ConversionFailureTypeToFacet]
  36. // CHECK:STDERR: C as Z;
  37. // CHECK:STDERR: ^~~~~~
  38. // CHECK:STDERR:
  39. C as Z;
  40. }
  41. // --- constraint_completed_after_deducing_impl_refers_to_it.carbon
  42. library "[[@TEST_NAME]]";
  43. constraint N;
  44. interface Z {}
  45. // N is incomplete here, but that's okay. We don't need it to be identified
  46. // until a lookup tries to use this impl.
  47. impl forall [T:! N] T as Z {}
  48. class C;
  49. // N is now identified, so it can be the target of a convertion, such as for
  50. // deducing `T` in the impl above.
  51. constraint N {}
  52. fn F() {
  53. C as Z;
  54. }
  55. // --- fail_impl_lookup_target_not_identified.carbon
  56. library "[[@TEST_NAME]]";
  57. constraint Z;
  58. fn AsZ(unused T:! Z) {}
  59. fn F() {
  60. // Converting to facet type `Z` requires `Z` to be identified.
  61. // CHECK:STDERR: fail_impl_lookup_target_not_identified.carbon:[[@LINE+10]]:3: error: facet type `Z` can not be identified [ImplLookupInUnidentifiedFacetType]
  62. // CHECK:STDERR: AsZ(());
  63. // CHECK:STDERR: ^~~~~~~
  64. // CHECK:STDERR: fail_impl_lookup_target_not_identified.carbon:[[@LINE-9]]:1: note: constraint was forward declared here [NamedConstraintForwardDeclaredHere]
  65. // CHECK:STDERR: constraint Z;
  66. // CHECK:STDERR: ^~~~~~~~~~~~~
  67. // CHECK:STDERR: fail_impl_lookup_target_not_identified.carbon:[[@LINE-10]]:15: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  68. // CHECK:STDERR: fn AsZ(unused T:! Z) {}
  69. // CHECK:STDERR: ^
  70. // CHECK:STDERR:
  71. AsZ(());
  72. }
  73. // --- fail_incomplete_self.carbon
  74. library "[[@TEST_NAME]]";
  75. // Returning a facet of type `W` requires `W` to be complete, but it's currently
  76. // being defined where this is called. This causes an error diagnostic at the
  77. // call.
  78. //
  79. // TODO: Define the function to `return U` once we can write `T:! Core.Copy`.
  80. // Right now, due to #5750, facets are not seen to implement `Core.Copy`.
  81. eval fn ReturnFacet[T:! type](U:! T) -> T;
  82. interface Y { let YT:! type; }
  83. interface Z {}
  84. interface X { let XZ:! Z; }
  85. constraint W {
  86. require impls Z;
  87. // CHECK:STDERR: fail_incomplete_self.carbon:[[@LINE+17]]:31: error: function returns incomplete type `W` [IncompleteTypeInFunctionReturnType]
  88. // CHECK:STDERR: require impls X where .XZ = ReturnFacet(Self);
  89. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  90. // CHECK:STDERR: fail_incomplete_self.carbon:[[@LINE-5]]:1: note: constraint is currently being defined [NamedConstraintIncompleteWithinDefinition]
  91. // CHECK:STDERR: constraint W {
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~
  93. // CHECK:STDERR: fail_incomplete_self.carbon:[[@LINE-15]]:41: note: return type declared here [IncompleteReturnTypeHere]
  94. // CHECK:STDERR: eval fn ReturnFacet[T:! type](U:! T) -> T;
  95. // CHECK:STDERR: ^
  96. // CHECK:STDERR:
  97. // CHECK:STDERR: fail_incomplete_self.carbon:[[@LINE+7]]:31: error: use of undefined generic function [MissingGenericFunctionDefinition]
  98. // CHECK:STDERR: require impls X where .XZ = ReturnFacet(Self);
  99. // CHECK:STDERR: ^~~~~~~~~~~
  100. // CHECK:STDERR: fail_incomplete_self.carbon:[[@LINE-22]]:1: note: generic function declared here [MissingGenericFunctionDefinitionHere]
  101. // CHECK:STDERR: eval fn ReturnFacet[T:! type](U:! T) -> T;
  102. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. // CHECK:STDERR:
  104. require impls X where .XZ = ReturnFacet(Self);
  105. }