member_lookup.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.035: %Copy.type = symbolic_binding T, 0 [symbolic]
  69. // CHECK:STDOUT: %T.as_type: type = facet_access_type %T.035 [symbolic]
  70. // CHECK:STDOUT: %Derived.b58: type = class_type @Derived, @Derived(%T.as_type) [symbolic]
  71. // CHECK:STDOUT: %pattern_type.be2: type = pattern_type %Derived.b58 [symbolic]
  72. // CHECK:STDOUT: %.435d17.2: Core.Form = init_form %T.as_type [symbolic]
  73. // CHECK:STDOUT: %pattern_type.3b9bb5.2: type = pattern_type %T.as_type [symbolic]
  74. // CHECK:STDOUT: %Base.7da: type = class_type @Base, @Base(%T.as_type) [symbolic]
  75. // CHECK:STDOUT: %require_complete.692: <witness> = require_complete_type %Base.7da [symbolic]
  76. // CHECK:STDOUT: %require_complete.89e: <witness> = require_complete_type %T.as_type [symbolic]
  77. // CHECK:STDOUT: %Derived.elem.f94: type = unbound_element_type %Derived.b58, %T.as_type [symbolic]
  78. // CHECK:STDOUT: %Base.elem.90f: type = unbound_element_type %Base.7da, %T.as_type [symbolic]
  79. // CHECK:STDOUT: %Copy.lookup_impl_witness.58d: <witness> = lookup_impl_witness %T.035, @Copy [symbolic]
  80. // CHECK:STDOUT: %Copy.WithSelf.Op.type.735e75.2: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.035) [symbolic]
  81. // CHECK:STDOUT: %.023: type = fn_type_with_self_type %Copy.WithSelf.Op.type.735e75.2, %T.035 [symbolic]
  82. // CHECK:STDOUT: %impl.elem0.594: %.023 = impl_witness_access %Copy.lookup_impl_witness.58d, element0 [symbolic]
  83. // CHECK:STDOUT: %specific_impl_fn.bdc: <specific function> = specific_impl_function %impl.elem0.594, @Copy.WithSelf.Op(%T.035) [symbolic]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: generic fn @AccessDerived(%T.loc13_19.2: %Copy.type) {
  87. // CHECK:STDOUT: <elided>
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: !definition:
  90. // CHECK:STDOUT: <elided>
  91. // CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived.loc13_45.1, %T.as_type.loc13_45.1 [symbolic = %Derived.elem (constants.%Derived.elem.f94)]
  92. // CHECK:STDOUT: %require_complete.loc15: <witness> = require_complete_type %T.as_type.loc13_45.1 [symbolic = %require_complete.loc15 (constants.%require_complete.89e)]
  93. // CHECK:STDOUT: %Copy.WithSelf.Op.type: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.loc13_19.1) [symbolic = %Copy.WithSelf.Op.type (constants.%Copy.WithSelf.Op.type.735e75.2)]
  94. // CHECK:STDOUT: %.loc15_11.3: type = fn_type_with_self_type %Copy.WithSelf.Op.type, %T.loc13_19.1 [symbolic = %.loc15_11.3 (constants.%.023)]
  95. // CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_19.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.58d)]
  96. // CHECK:STDOUT: %impl.elem0.loc15_11.2: @AccessDerived.%.loc15_11.3 (%.023) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.594)]
  97. // CHECK:STDOUT: %specific_impl_fn.loc15_11.2: <specific function> = specific_impl_function %impl.elem0.loc15_11.2, @Copy.WithSelf.Op(%T.loc13_19.1) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.bdc)]
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn(%x.param: @AccessDerived.%Derived.loc13_45.1 (%Derived.b58)) -> out %return.param: @AccessDerived.%T.as_type.loc13_45.1 (%T.as_type) {
  100. // CHECK:STDOUT: !entry:
  101. // CHECK:STDOUT: %x.ref: @AccessDerived.%Derived.loc13_45.1 (%Derived.b58) = name_ref x, %x
  102. // CHECK:STDOUT: %d.ref: @AccessDerived.%Derived.elem (%Derived.elem.f94) = name_ref d, @Derived.%.loc10 [concrete = @Derived.%.loc10]
  103. // CHECK:STDOUT: %.loc15_11.1: ref @AccessDerived.%T.as_type.loc13_45.1 (%T.as_type) = class_element_access %x.ref, element1
  104. // CHECK:STDOUT: %.loc15_11.2: @AccessDerived.%T.as_type.loc13_45.1 (%T.as_type) = acquire_value %.loc15_11.1
  105. // CHECK:STDOUT: %impl.elem0.loc15_11.1: @AccessDerived.%.loc15_11.3 (%.023) = impl_witness_access constants.%Copy.lookup_impl_witness.58d, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.594)]
  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.WithSelf.Op(constants.%T.035) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.bdc)]
  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: %Copy.WithSelf.Op.call: init @AccessDerived.%T.as_type.loc13_45.1 (%T.as_type) to %.loc13_51.1 = call %bound_method.loc15_11.2(%.loc15_11.2)
  111. // CHECK:STDOUT: return %Copy.WithSelf.Op.call to %return.param
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: generic fn @AccessBase(%T.loc19_16.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.as_type.loc19_42.1) [symbolic = %Base (constants.%Base.7da)]
  121. // CHECK:STDOUT: %require_complete.loc21_11: <witness> = require_complete_type %Base [symbolic = %require_complete.loc21_11 (constants.%require_complete.692)]
  122. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %T.as_type.loc19_42.1 [symbolic = %Base.elem (constants.%Base.elem.90f)]
  123. // CHECK:STDOUT: %require_complete.loc21_13: <witness> = require_complete_type %T.as_type.loc19_42.1 [symbolic = %require_complete.loc21_13 (constants.%require_complete.89e)]
  124. // CHECK:STDOUT: %Copy.WithSelf.Op.type: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.loc19_16.1) [symbolic = %Copy.WithSelf.Op.type (constants.%Copy.WithSelf.Op.type.735e75.2)]
  125. // CHECK:STDOUT: %.loc21_11.5: type = fn_type_with_self_type %Copy.WithSelf.Op.type, %T.loc19_16.1 [symbolic = %.loc21_11.5 (constants.%.023)]
  126. // CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc19_16.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.58d)]
  127. // CHECK:STDOUT: %impl.elem0.loc21_11.2: @AccessBase.%.loc21_11.5 (%.023) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.594)]
  128. // CHECK:STDOUT: %specific_impl_fn.loc21_11.2: <specific function> = specific_impl_function %impl.elem0.loc21_11.2, @Copy.WithSelf.Op(%T.loc19_16.1) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.bdc)]
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn(%x.param: @AccessBase.%Derived.loc19_42.1 (%Derived.b58)) -> out %return.param: @AccessBase.%T.as_type.loc19_42.1 (%T.as_type) {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %x.ref: @AccessBase.%Derived.loc19_42.1 (%Derived.b58) = name_ref x, %x
  133. // CHECK:STDOUT: %b.ref: @AccessBase.%Base.elem (%Base.elem.90f) = name_ref b, @Base.%.loc5 [concrete = @Base.%.loc5]
  134. // CHECK:STDOUT: %.loc21_11.1: ref @AccessBase.%Base (%Base.7da) = class_element_access %x.ref, element0
  135. // CHECK:STDOUT: %.loc21_11.2: ref @AccessBase.%Base (%Base.7da) = converted %x.ref, %.loc21_11.1
  136. // CHECK:STDOUT: %.loc21_11.3: ref @AccessBase.%T.as_type.loc19_42.1 (%T.as_type) = class_element_access %.loc21_11.2, element0
  137. // CHECK:STDOUT: %.loc21_11.4: @AccessBase.%T.as_type.loc19_42.1 (%T.as_type) = acquire_value %.loc21_11.3
  138. // CHECK:STDOUT: %impl.elem0.loc21_11.1: @AccessBase.%.loc21_11.5 (%.023) = impl_witness_access constants.%Copy.lookup_impl_witness.58d, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.594)]
  139. // CHECK:STDOUT: %bound_method.loc21_11.1: <bound method> = bound_method %.loc21_11.4, %impl.elem0.loc21_11.1
  140. // CHECK:STDOUT: %specific_impl_fn.loc21_11.1: <specific function> = specific_impl_function %impl.elem0.loc21_11.1, @Copy.WithSelf.Op(constants.%T.035) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.bdc)]
  141. // CHECK:STDOUT: %bound_method.loc21_11.2: <bound method> = bound_method %.loc21_11.4, %specific_impl_fn.loc21_11.1
  142. // CHECK:STDOUT: <elided>
  143. // CHECK:STDOUT: %Copy.WithSelf.Op.call: init @AccessBase.%T.as_type.loc19_42.1 (%T.as_type) to %.loc19_48.1 = call %bound_method.loc21_11.2(%.loc21_11.4)
  144. // CHECK:STDOUT: return %Copy.WithSelf.Op.call to %return.param
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: specific @AccessDerived(constants.%T.035) {
  149. // CHECK:STDOUT: %T.loc13_19.1 => constants.%T.035
  150. // CHECK:STDOUT: %T.as_type.loc13_45.1 => constants.%T.as_type
  151. // CHECK:STDOUT: %Derived.loc13_45.1 => constants.%Derived.b58
  152. // CHECK:STDOUT: %pattern_type.loc13_34 => constants.%pattern_type.be2
  153. // CHECK:STDOUT: %.loc13_51.2 => constants.%.435d17.2
  154. // CHECK:STDOUT: %pattern_type.loc13_51 => constants.%pattern_type.3b9bb5.2
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: specific @AccessBase(constants.%T.035) {
  158. // CHECK:STDOUT: %T.loc19_16.1 => constants.%T.035
  159. // CHECK:STDOUT: %T.as_type.loc19_42.1 => constants.%T.as_type
  160. // CHECK:STDOUT: %Derived.loc19_42.1 => constants.%Derived.b58
  161. // CHECK:STDOUT: %pattern_type.loc19_31 => constants.%pattern_type.be2
  162. // CHECK:STDOUT: %.loc19_48.2 => constants.%.435d17.2
  163. // CHECK:STDOUT: %pattern_type.loc19_48 => constants.%pattern_type.3b9bb5.2
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT: