validate_impl_constraints.carbon 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/facet/validate_impl_constraints.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/validate_impl_constraints.carbon
  12. // --- self_impls_modifies_assoc_constant.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I { let X:! type; }
  15. fn F(T:! I where .X = ()) {}
  16. fn G(T:! I where .Self impls (I where .X = ())) {
  17. F(T);
  18. }
  19. // --- fail_self_impls_modifies_assoc_constant_type_differs.carbon
  20. library "[[@TEST_NAME]]";
  21. interface I { let X:! type; }
  22. fn F(T:! I where .X = ()) {}
  23. fn G(T:! I where .Self impls (I where .X = {})) {
  24. // CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = {}` into type implementing `I where .(I.X) = ()` [ConversionFailureFacetToFacet]
  25. // CHECK:STDERR: F(T);
  26. // CHECK:STDERR: ^~~~
  27. // CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  28. // CHECK:STDERR: fn F(T:! I where .X = ()) {}
  29. // CHECK:STDERR: ^
  30. // CHECK:STDERR:
  31. F(T);
  32. }
  33. // --- fail_todo_where_impls_tests_associated_constant_of_generic_type.carbon
  34. library "[[@TEST_NAME]]";
  35. class C(U:! type) {}
  36. // C(U) impls M if U impls L.
  37. interface L {}
  38. interface M { let M0:! type; }
  39. impl forall [U:! L] C(U) as M where .M0 = {} {}
  40. // U requires that C(.Self) impls M.
  41. // - C(.Self) impls M can be rewritten as C(U) impls M.
  42. // - C(U) impls M if U impls L => Requires U impls L.
  43. fn F(U:! type where C(.Self) impls (M where .M0 = {})) {}
  44. fn G(T:! L) {
  45. // CHECK:STDERR: fail_todo_where_impls_tests_associated_constant_of_generic_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where...` [ConversionFailureFacetToFacet]
  46. // CHECK:STDERR: F(T);
  47. // CHECK:STDERR: ^~~~
  48. // CHECK:STDERR: fail_todo_where_impls_tests_associated_constant_of_generic_type.carbon:[[@LINE-6]]:6: note: initializing generic parameter `U` declared here [InitializingGenericParam]
  49. // CHECK:STDERR: fn F(U:! type where C(.Self) impls (M where .M0 = {})) {}
  50. // CHECK:STDERR: ^
  51. // CHECK:STDERR:
  52. F(T);
  53. }
  54. // --- fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon
  55. library "[[@TEST_NAME]]";
  56. class C(U:! type) {}
  57. // C(U) impls M if U impls L.
  58. interface L {}
  59. interface M { let M0:! type; }
  60. impl forall [U:! L] C(U) as M where .M0 = () {}
  61. // U requires that C(.Self) impls M.
  62. // - C(.Self) impls M can be rewritten as C(U) impls M.
  63. // - C(U) impls M if U impls L => Requires U impls L.
  64. fn F(U:! type where C(.Self) impls (M where .M0 = {})) {}
  65. fn G(T:! L) {
  66. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where...` [ConversionFailureFacetToFacet]
  67. // CHECK:STDERR: F(T);
  68. // CHECK:STDERR: ^~~~
  69. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE-6]]:6: note: initializing generic parameter `U` declared here [InitializingGenericParam]
  70. // CHECK:STDERR: fn F(U:! type where C(.Self) impls (M where .M0 = {})) {}
  71. // CHECK:STDERR: ^
  72. // CHECK:STDERR:
  73. F(T);
  74. }
  75. // --- self_in_interface_generic_param_unconstrained.carbon
  76. library "[[@TEST_NAME]]";
  77. interface Z {}
  78. interface I(T:! type) {}
  79. fn F(T:! I(.Self) where .Self impls Z) {}
  80. fn G(T:! Z & I(.Self)) {
  81. F(T);
  82. }
  83. // --- fail_todo_self_in_interface_generic_param_constrained.carbon
  84. library "[[@TEST_NAME]]";
  85. interface Z {}
  86. interface I(T:! Z) {}
  87. // Implied constraint: .Self impls Z, which is satisfied and checked at the end
  88. // of the fn signature.
  89. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:10: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
  90. // CHECK:STDERR: fn F(T:! I(.Self) where .Self impls Z) {}
  91. // CHECK:STDERR: ^~~~~~~~
  92. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-7]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  93. // CHECK:STDERR: interface I(T:! Z) {}
  94. // CHECK:STDERR: ^
  95. // CHECK:STDERR:
  96. fn F(T:! I(.Self) where .Self impls Z) {}
  97. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:14: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
  98. // CHECK:STDERR: fn G(T:! Z & I(.Self)) {
  99. // CHECK:STDERR: ^~~~~~~~
  100. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-16]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  101. // CHECK:STDERR: interface I(T:! Z) {}
  102. // CHECK:STDERR: ^
  103. // CHECK:STDERR:
  104. fn G(T:! Z & I(.Self)) {
  105. F(T);
  106. }