designator.carbon 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/where_expr/designator.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/where_expr/designator.carbon
  10. // --- success.carbon
  11. library "[[@TEST_NAME]]";
  12. interface I {
  13. let Member:! type;
  14. }
  15. //@dump-sem-ir-begin
  16. fn PeriodSelf(T:! I where .Self == ());
  17. fn PeriodMember(U:! I where .Member == ());
  18. fn TypeSelfImpls(V:! type where .Self impls I);
  19. //@dump-sem-ir-end
  20. // --- fail_wrong_member.carbon
  21. library "[[@TEST_NAME]]";
  22. interface J {
  23. let Member:! type;
  24. }
  25. // CHECK:STDERR: fail_wrong_member.carbon:[[@LINE+4]]:31: error: member name `Mismatch` not found in `J` [MemberNameNotFoundInInstScope]
  26. // CHECK:STDERR: fn PeriodMismatch(W:! J where .Mismatch = {});
  27. // CHECK:STDERR: ^~~~~~~~~
  28. // CHECK:STDERR:
  29. fn PeriodMismatch(W:! J where .Mismatch = {});
  30. // --- fail_designator_matches_var.carbon
  31. library "[[@TEST_NAME]]";
  32. fn Foo() -> () {
  33. var x: ();
  34. // CHECK:STDERR: fail_designator_matches_var.carbon:[[@LINE+5]]:10: error: name `.Self` not found [NameNotFound]
  35. // CHECK:STDERR: return .x;
  36. // CHECK:STDERR: ^~
  37. // CHECK:STDERR: fail_designator_matches_var.carbon: note: designator may only be used when `.Self` is in scope [NoPeriodSelfForDesignator]
  38. // CHECK:STDERR:
  39. return .x;
  40. }
  41. // --- fail_unknown_designator.carbon
  42. library "[[@TEST_NAME]]";
  43. fn Bar() -> () {
  44. // CHECK:STDERR: fail_unknown_designator.carbon:[[@LINE+5]]:10: error: name `.Self` not found [NameNotFound]
  45. // CHECK:STDERR: return .undef;
  46. // CHECK:STDERR: ^~~~~~
  47. // CHECK:STDERR: fail_unknown_designator.carbon: note: designator may only be used when `.Self` is in scope [NoPeriodSelfForDesignator]
  48. // CHECK:STDERR:
  49. return .undef;
  50. }
  51. // --- fail_dot_self_method_return_value.carbon
  52. library "[[@TEST_NAME]]";
  53. class C {
  54. // CHECK:STDERR: fail_dot_self_method_return_value.carbon:[[@LINE+4]]:27: error: name `.Self` not found [NameNotFound]
  55. // CHECK:STDERR: fn F() -> Self { return .Self; }
  56. // CHECK:STDERR: ^~~~~
  57. // CHECK:STDERR:
  58. fn F() -> Self { return .Self; }
  59. }
  60. // --- fail_dot_self_method_return_type.carbon
  61. library "[[@TEST_NAME]]";
  62. class D {
  63. // CHECK:STDERR: fail_dot_self_method_return_type.carbon:[[@LINE+4]]:13: error: name `.Self` not found [NameNotFound]
  64. // CHECK:STDERR: fn G() -> .Self { return Self; }
  65. // CHECK:STDERR: ^~~~~
  66. // CHECK:STDERR:
  67. fn G() -> .Self { return Self; }
  68. }
  69. // CHECK:STDOUT: --- success.carbon
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: constants {
  72. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  73. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  74. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%Member [concrete]
  75. // CHECK:STDOUT: %.Self.258: %I.type = bind_symbolic_name .Self [symbolic_self]
  76. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  77. // CHECK:STDOUT: %I_where.type: type = facet_type <@I where TODO> [concrete]
  78. // CHECK:STDOUT: %T: %I_where.type = bind_symbolic_name T, 0 [symbolic]
  79. // CHECK:STDOUT: %pattern_type.f03: type = pattern_type %I_where.type [concrete]
  80. // CHECK:STDOUT: %PeriodSelf.type: type = fn_type @PeriodSelf [concrete]
  81. // CHECK:STDOUT: %PeriodSelf: %PeriodSelf.type = struct_value () [concrete]
  82. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.258 [symbolic_self]
  83. // CHECK:STDOUT: %I.lookup_impl_witness: <witness> = lookup_impl_witness %.Self.258, @I [symbolic_self]
  84. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access %I.lookup_impl_witness, element0 [symbolic_self]
  85. // CHECK:STDOUT: %U: %I_where.type = bind_symbolic_name U, 0 [symbolic]
  86. // CHECK:STDOUT: %PeriodMember.type: type = fn_type @PeriodMember [concrete]
  87. // CHECK:STDOUT: %PeriodMember: %PeriodMember.type = struct_value () [concrete]
  88. // CHECK:STDOUT: %.Self.644: type = bind_symbolic_name .Self [symbolic_self]
  89. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls @I> [concrete]
  90. // CHECK:STDOUT: %V: %type_where = bind_symbolic_name V, 0 [symbolic]
  91. // CHECK:STDOUT: %pattern_type.2b5: type = pattern_type %type_where [concrete]
  92. // CHECK:STDOUT: %TypeSelfImpls.type: type = fn_type @TypeSelfImpls [concrete]
  93. // CHECK:STDOUT: %TypeSelfImpls: %TypeSelfImpls.type = struct_value () [concrete]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: imports {
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: file {
  100. // CHECK:STDOUT: %PeriodSelf.decl: %PeriodSelf.type = fn_decl @PeriodSelf [concrete = constants.%PeriodSelf] {
  101. // CHECK:STDOUT: %T.patt: %pattern_type.f03 = symbolic_binding_pattern T, 0 [concrete]
  102. // CHECK:STDOUT: } {
  103. // CHECK:STDOUT: %.loc9_21.1: type = splice_block %.loc9_21.2 [concrete = constants.%I_where.type] {
  104. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  105. // CHECK:STDOUT: <elided>
  106. // CHECK:STDOUT: %.Self.ref: %I.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.258]
  107. // CHECK:STDOUT: %.loc9_37: %empty_tuple.type = tuple_literal ()
  108. // CHECK:STDOUT: %.loc9_21.2: type = where_expr %.Self [concrete = constants.%I_where.type] {
  109. // CHECK:STDOUT: requirement_equivalent %.Self.ref, %.loc9_37
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %T.loc9_15.1: %I_where.type = bind_symbolic_name T, 0 [symbolic = %T.loc9_15.2 (constants.%T)]
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %PeriodMember.decl: %PeriodMember.type = fn_decl @PeriodMember [concrete = constants.%PeriodMember] {
  115. // CHECK:STDOUT: %U.patt: %pattern_type.f03 = symbolic_binding_pattern U, 0 [concrete]
  116. // CHECK:STDOUT: } {
  117. // CHECK:STDOUT: %.loc11_23.1: type = splice_block %.loc11_23.2 [concrete = constants.%I_where.type] {
  118. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  119. // CHECK:STDOUT: <elided>
  120. // CHECK:STDOUT: %.Self.ref: %I.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.258]
  121. // CHECK:STDOUT: %Member.ref: %I.assoc_type = name_ref Member, @Member.%assoc0 [concrete = constants.%assoc0]
  122. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.ref [symbolic_self = constants.%.Self.as_type]
  123. // CHECK:STDOUT: %.loc11_29: type = converted %.Self.ref, %.Self.as_type [symbolic_self = constants.%.Self.as_type]
  124. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access constants.%I.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  125. // CHECK:STDOUT: %.loc11_41: %empty_tuple.type = tuple_literal ()
  126. // CHECK:STDOUT: %.loc11_23.2: type = where_expr %.Self [concrete = constants.%I_where.type] {
  127. // CHECK:STDOUT: requirement_equivalent %impl.elem0, %.loc11_41
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %U.loc11_17.1: %I_where.type = bind_symbolic_name U, 0 [symbolic = %U.loc11_17.2 (constants.%U)]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %TypeSelfImpls.decl: %TypeSelfImpls.type = fn_decl @TypeSelfImpls [concrete = constants.%TypeSelfImpls] {
  133. // CHECK:STDOUT: %V.patt: %pattern_type.2b5 = symbolic_binding_pattern V, 0 [concrete]
  134. // CHECK:STDOUT: } {
  135. // CHECK:STDOUT: %.loc13_27.1: type = splice_block %.loc13_27.2 [concrete = constants.%type_where] {
  136. // CHECK:STDOUT: <elided>
  137. // CHECK:STDOUT: %.Self.ref: type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.644]
  138. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  139. // CHECK:STDOUT: %.loc13_27.2: type = where_expr %.Self [concrete = constants.%type_where] {
  140. // CHECK:STDOUT: requirement_impls %.Self.ref, %I.ref
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %V.loc13_18.1: %type_where = bind_symbolic_name V, 0 [symbolic = %V.loc13_18.2 (constants.%V)]
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: generic fn @PeriodSelf(%T.loc9_15.1: %I_where.type) {
  148. // CHECK:STDOUT: %T.loc9_15.2: %I_where.type = bind_symbolic_name T, 0 [symbolic = %T.loc9_15.2 (constants.%T)]
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: fn();
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: generic fn @PeriodMember(%U.loc11_17.1: %I_where.type) {
  154. // CHECK:STDOUT: %U.loc11_17.2: %I_where.type = bind_symbolic_name U, 0 [symbolic = %U.loc11_17.2 (constants.%U)]
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn();
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: generic fn @TypeSelfImpls(%V.loc13_18.1: %type_where) {
  160. // CHECK:STDOUT: %V.loc13_18.2: %type_where = bind_symbolic_name V, 0 [symbolic = %V.loc13_18.2 (constants.%V)]
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn();
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: specific @PeriodSelf(constants.%T) {
  166. // CHECK:STDOUT: %T.loc9_15.2 => constants.%T
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: specific @PeriodMember(constants.%U) {
  170. // CHECK:STDOUT: %U.loc11_17.2 => constants.%U
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: specific @TypeSelfImpls(constants.%V) {
  174. // CHECK:STDOUT: %V.loc13_18.2 => constants.%V
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: