member_lookup.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/int.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/class/generic/member_lookup.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/member_lookup.carbon
  12. // --- member_access.carbon
  13. library "[[@TEST_NAME]]";
  14. base class Base(T:! type) {
  15. var b: T;
  16. }
  17. class Derived(T:! type) {
  18. extend base: Base(T);
  19. var d: T;
  20. }
  21. fn AccessDerived[T:! Core.Copy](x: Derived(T)) -> T {
  22. //@dump-sem-ir-begin
  23. return x.d;
  24. //@dump-sem-ir-end
  25. }
  26. fn AccessBase[T:! Core.Copy](x: Derived(T)) -> T {
  27. //@dump-sem-ir-begin
  28. return x.b;
  29. //@dump-sem-ir-end
  30. }
  31. fn AccessConcrete(x: Derived(i32)) -> i32 {
  32. return x.b;
  33. }
  34. // --- fail_no_member.carbon
  35. library "[[@TEST_NAME]]";
  36. base class Base(T:! type) {
  37. var b: T;
  38. }
  39. class Derived(T:! type) {
  40. extend base: Base(T);
  41. var d: T;
  42. }
  43. fn AccessMissingBase[T:! type](x: Base(T)) -> T {
  44. // CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Base(T)` [MemberNameNotFoundInSpecificScope]
  45. // CHECK:STDERR: return x.nonesuch;
  46. // CHECK:STDERR: ^~~~~~~~~~
  47. // CHECK:STDERR:
  48. return x.nonesuch;
  49. }
  50. fn AccessMissingDerived[T:! type](x: Derived(T)) -> T {
  51. // CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Derived(T)` [MemberNameNotFoundInSpecificScope]
  52. // CHECK:STDERR: return x.nonesuch;
  53. // CHECK:STDERR: ^~~~~~~~~~
  54. // CHECK:STDERR:
  55. return x.nonesuch;
  56. }
  57. fn AccessMissingConcrete(x: Derived(i32)) -> i32 {
  58. // CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Derived(i32)` [MemberNameNotFoundInSpecificScope]
  59. // CHECK:STDERR: return x.nonesuch;
  60. // CHECK:STDERR: ^~~~~~~~~~
  61. // CHECK:STDERR:
  62. return x.nonesuch;
  63. }
  64. // CHECK:STDOUT: --- member_access.carbon
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: constants {
  67. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  68. // CHECK:STDOUT: %T.be8: %Copy.type = bind_symbolic_name T, 0 [symbolic]
  69. // CHECK:STDOUT: %T.binding.as_type: type = symbolic_binding_type T, 0, %T.be8 [symbolic]
  70. // CHECK:STDOUT: %Derived.b36: type = class_type @Derived, @Derived(%T.binding.as_type) [symbolic]
  71. // CHECK:STDOUT: %pattern_type.50f: type = pattern_type %Derived.b36 [symbolic]
  72. // CHECK:STDOUT: %pattern_type.17e4b7.1: type = pattern_type %T.binding.as_type [symbolic]
  73. // CHECK:STDOUT: %require_complete.1cd: <witness> = require_complete_type %T.binding.as_type [symbolic]
  74. // CHECK:STDOUT: %Base.342: type = class_type @Base, @Base(%T.binding.as_type) [symbolic]
  75. // CHECK:STDOUT: %require_complete.254: <witness> = require_complete_type %Base.342 [symbolic]
  76. // CHECK:STDOUT: %Derived.elem.0da: type = unbound_element_type %Derived.b36, %T.binding.as_type [symbolic]
  77. // CHECK:STDOUT: %Base.elem.b9e: type = unbound_element_type %Base.342, %T.binding.as_type [symbolic]
  78. // CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
  79. // CHECK:STDOUT: %Copy.lookup_impl_witness.e15: <witness> = lookup_impl_witness %T.be8, @Copy [symbolic]
  80. // CHECK:STDOUT: %.427: type = fn_type_with_self_type %Copy.Op.type, %T.be8 [symbolic]
  81. // CHECK:STDOUT: %impl.elem0.168: %.427 = impl_witness_access %Copy.lookup_impl_witness.e15, element0 [symbolic]
  82. // CHECK:STDOUT: %specific_impl_fn.2ce: <specific function> = specific_impl_function %impl.elem0.168, @Copy.Op(%T.be8) [symbolic]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: imports {
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: generic fn @AccessDerived(%T.loc13_18.2: %Copy.type) {
  89. // CHECK:STDOUT: <elided>
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !definition:
  92. // CHECK:STDOUT: <elided>
  93. // CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived.loc13_45.1, %T.binding.as_type [symbolic = %Derived.elem (constants.%Derived.elem.0da)]
  94. // CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_18.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.e15)]
  95. // CHECK:STDOUT: %.loc15_11.4: type = fn_type_with_self_type constants.%Copy.Op.type, %T.loc13_18.1 [symbolic = %.loc15_11.4 (constants.%.427)]
  96. // CHECK:STDOUT: %impl.elem0.loc15_11.2: @AccessDerived.%.loc15_11.4 (%.427) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.168)]
  97. // CHECK:STDOUT: %specific_impl_fn.loc15_11.2: <specific function> = specific_impl_function %impl.elem0.loc15_11.2, @Copy.Op(%T.loc13_18.1) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.2ce)]
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn(%x.param: @AccessDerived.%Derived.loc13_45.1 (%Derived.b36)) -> %return.param: @AccessDerived.%T.binding.as_type (%T.binding.as_type) {
  100. // CHECK:STDOUT: !entry:
  101. // CHECK:STDOUT: %x.ref: @AccessDerived.%Derived.loc13_45.1 (%Derived.b36) = name_ref x, %x
  102. // CHECK:STDOUT: %d.ref: @AccessDerived.%Derived.elem (%Derived.elem.0da) = name_ref d, @Derived.%.loc10 [concrete = @Derived.%.loc10]
  103. // CHECK:STDOUT: %.loc15_11.1: ref @AccessDerived.%T.binding.as_type (%T.binding.as_type) = class_element_access %x.ref, element1
  104. // CHECK:STDOUT: %.loc15_11.2: @AccessDerived.%T.binding.as_type (%T.binding.as_type) = bind_value %.loc15_11.1
  105. // CHECK:STDOUT: %impl.elem0.loc15_11.1: @AccessDerived.%.loc15_11.4 (%.427) = impl_witness_access constants.%Copy.lookup_impl_witness.e15, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.168)]
  106. // CHECK:STDOUT: %bound_method.loc15_11.1: <bound method> = bound_method %.loc15_11.2, %impl.elem0.loc15_11.1
  107. // CHECK:STDOUT: %specific_impl_fn.loc15_11.1: <specific function> = specific_impl_function %impl.elem0.loc15_11.1, @Copy.Op(constants.%T.be8) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.2ce)]
  108. // CHECK:STDOUT: %bound_method.loc15_11.2: <bound method> = bound_method %.loc15_11.2, %specific_impl_fn.loc15_11.1
  109. // CHECK:STDOUT: <elided>
  110. // CHECK:STDOUT: %.loc15_11.3: init @AccessDerived.%T.binding.as_type (%T.binding.as_type) = call %bound_method.loc15_11.2(%.loc15_11.2) to %.loc13_48
  111. // CHECK:STDOUT: return %.loc15_11.3 to %return
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: generic fn @AccessBase(%T.loc19_15.2: %Copy.type) {
  116. // CHECK:STDOUT: <elided>
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !definition:
  119. // CHECK:STDOUT: <elided>
  120. // CHECK:STDOUT: %Base: type = class_type @Base, @Base(%T.binding.as_type) [symbolic = %Base (constants.%Base.342)]
  121. // CHECK:STDOUT: %require_complete.loc21_11: <witness> = require_complete_type %Base [symbolic = %require_complete.loc21_11 (constants.%require_complete.254)]
  122. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %T.binding.as_type [symbolic = %Base.elem (constants.%Base.elem.b9e)]
  123. // CHECK:STDOUT: %require_complete.loc21_13: <witness> = require_complete_type %T.binding.as_type [symbolic = %require_complete.loc21_13 (constants.%require_complete.1cd)]
  124. // CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc19_15.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.e15)]
  125. // CHECK:STDOUT: %.loc21_11.6: type = fn_type_with_self_type constants.%Copy.Op.type, %T.loc19_15.1 [symbolic = %.loc21_11.6 (constants.%.427)]
  126. // CHECK:STDOUT: %impl.elem0.loc21_11.2: @AccessBase.%.loc21_11.6 (%.427) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.168)]
  127. // CHECK:STDOUT: %specific_impl_fn.loc21_11.2: <specific function> = specific_impl_function %impl.elem0.loc21_11.2, @Copy.Op(%T.loc19_15.1) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.2ce)]
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn(%x.param: @AccessBase.%Derived.loc19_42.1 (%Derived.b36)) -> %return.param: @AccessBase.%T.binding.as_type (%T.binding.as_type) {
  130. // CHECK:STDOUT: !entry:
  131. // CHECK:STDOUT: %x.ref: @AccessBase.%Derived.loc19_42.1 (%Derived.b36) = name_ref x, %x
  132. // CHECK:STDOUT: %b.ref: @AccessBase.%Base.elem (%Base.elem.b9e) = name_ref b, @Base.%.loc5 [concrete = @Base.%.loc5]
  133. // CHECK:STDOUT: %.loc21_11.1: ref @AccessBase.%Base (%Base.342) = class_element_access %x.ref, element0
  134. // CHECK:STDOUT: %.loc21_11.2: ref @AccessBase.%Base (%Base.342) = converted %x.ref, %.loc21_11.1
  135. // CHECK:STDOUT: %.loc21_11.3: ref @AccessBase.%T.binding.as_type (%T.binding.as_type) = class_element_access %.loc21_11.2, element0
  136. // CHECK:STDOUT: %.loc21_11.4: @AccessBase.%T.binding.as_type (%T.binding.as_type) = bind_value %.loc21_11.3
  137. // CHECK:STDOUT: %impl.elem0.loc21_11.1: @AccessBase.%.loc21_11.6 (%.427) = impl_witness_access constants.%Copy.lookup_impl_witness.e15, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.168)]
  138. // CHECK:STDOUT: %bound_method.loc21_11.1: <bound method> = bound_method %.loc21_11.4, %impl.elem0.loc21_11.1
  139. // CHECK:STDOUT: %specific_impl_fn.loc21_11.1: <specific function> = specific_impl_function %impl.elem0.loc21_11.1, @Copy.Op(constants.%T.be8) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.2ce)]
  140. // CHECK:STDOUT: %bound_method.loc21_11.2: <bound method> = bound_method %.loc21_11.4, %specific_impl_fn.loc21_11.1
  141. // CHECK:STDOUT: <elided>
  142. // CHECK:STDOUT: %.loc21_11.5: init @AccessBase.%T.binding.as_type (%T.binding.as_type) = call %bound_method.loc21_11.2(%.loc21_11.4) to %.loc19_45
  143. // CHECK:STDOUT: return %.loc21_11.5 to %return
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: specific @AccessDerived(constants.%T.be8) {
  148. // CHECK:STDOUT: %T.loc13_18.1 => constants.%T.be8
  149. // CHECK:STDOUT: %T.binding.as_type => constants.%T.binding.as_type
  150. // CHECK:STDOUT: %Derived.loc13_45.1 => constants.%Derived.b36
  151. // CHECK:STDOUT: %pattern_type.loc13_33 => constants.%pattern_type.50f
  152. // CHECK:STDOUT: %pattern_type.loc13_48 => constants.%pattern_type.17e4b7.1
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: specific @AccessBase(constants.%T.be8) {
  156. // CHECK:STDOUT: %T.loc19_15.1 => constants.%T.be8
  157. // CHECK:STDOUT: %T.binding.as_type => constants.%T.binding.as_type
  158. // CHECK:STDOUT: %Derived.loc19_42.1 => constants.%Derived.b36
  159. // CHECK:STDOUT: %pattern_type.loc19_30 => constants.%pattern_type.50f
  160. // CHECK:STDOUT: %pattern_type.loc19_45 => constants.%pattern_type.17e4b7.1
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT: