adapt.carbon 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/class/adapt.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/adapt.carbon
  10. // --- basic.carbon
  11. library "[[@TEST_NAME]]";
  12. class SomeClass {
  13. var a: i32;
  14. var b: i32;
  15. }
  16. class SomeClassAdapter {
  17. adapt SomeClass;
  18. }
  19. class StructAdapter {
  20. adapt {.a: i32, .b: i32};
  21. }
  22. // --- fail_not_extend.carbon
  23. library "[[@TEST_NAME]]";
  24. class Adapted {
  25. fn F();
  26. }
  27. class AdaptNotExtend {
  28. adapt Adapted;
  29. }
  30. fn F(a: AdaptNotExtend) {
  31. // `Adapted` is not extended, so lookup for `F` finds nothing.
  32. // CHECK:STDERR: fail_not_extend.carbon:[[@LINE+3]]:3: error: name `F` not found
  33. // CHECK:STDERR: a.F();
  34. // CHECK:STDERR: ^~~
  35. a.F();
  36. }
  37. // CHECK:STDOUT: --- basic.carbon
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: constants {
  40. // CHECK:STDOUT: %SomeClass: type = class_type @SomeClass [template]
  41. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  42. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  43. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  44. // CHECK:STDOUT: %.2: type = unbound_element_type %SomeClass, i32 [template]
  45. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32} [template]
  46. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  47. // CHECK:STDOUT: %SomeClassAdapter: type = class_type @SomeClassAdapter [template]
  48. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  49. // CHECK:STDOUT: %.6: <witness> = complete_type_witness %SomeClass [template]
  50. // CHECK:STDOUT: %StructAdapter: type = class_type @StructAdapter [template]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: imports {
  54. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  55. // CHECK:STDOUT: .Int32 = %import_ref
  56. // CHECK:STDOUT: import Core//prelude
  57. // CHECK:STDOUT: import Core//prelude/operators
  58. // CHECK:STDOUT: import Core//prelude/types
  59. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  60. // CHECK:STDOUT: import Core//prelude/operators/as
  61. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  62. // CHECK:STDOUT: import Core//prelude/operators/comparison
  63. // CHECK:STDOUT: import Core//prelude/types/bool
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  70. // CHECK:STDOUT: .Core = imports.%Core
  71. // CHECK:STDOUT: .SomeClass = %SomeClass.decl
  72. // CHECK:STDOUT: .SomeClassAdapter = %SomeClassAdapter.decl
  73. // CHECK:STDOUT: .StructAdapter = %StructAdapter.decl
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %Core.import = import Core
  76. // CHECK:STDOUT: %SomeClass.decl: type = class_decl @SomeClass [template = constants.%SomeClass] {} {}
  77. // CHECK:STDOUT: %SomeClassAdapter.decl: type = class_decl @SomeClassAdapter [template = constants.%SomeClassAdapter] {} {}
  78. // CHECK:STDOUT: %StructAdapter.decl: type = class_decl @StructAdapter [template = constants.%StructAdapter] {} {}
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: class @SomeClass {
  82. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  83. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  84. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32.loc5, %.loc5_10.1 [template = i32]
  85. // CHECK:STDOUT: %.loc5_8: %.2 = field_decl a, element0 [template]
  86. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc6_10.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  88. // CHECK:STDOUT: %.loc6_10.2: type = converted %int.make_type_32.loc6, %.loc6_10.1 [template = i32]
  89. // CHECK:STDOUT: %.loc6_8: %.2 = field_decl b, element1 [template]
  90. // CHECK:STDOUT: %.loc7: <witness> = complete_type_witness %.3 [template = constants.%.4]
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !members:
  93. // CHECK:STDOUT: .Self = constants.%SomeClass
  94. // CHECK:STDOUT: .a = %.loc5_8
  95. // CHECK:STDOUT: .b = %.loc6_8
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: class @SomeClassAdapter {
  99. // CHECK:STDOUT: %SomeClass.ref: type = name_ref SomeClass, file.%SomeClass.decl [template = constants.%SomeClass]
  100. // CHECK:STDOUT: adapt_decl %SomeClass
  101. // CHECK:STDOUT: %.loc11: <witness> = complete_type_witness %SomeClass [template = constants.%.6]
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = constants.%SomeClassAdapter
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: class @StructAdapter {
  108. // CHECK:STDOUT: %int.make_type_32.loc14_14: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc14_14.1: type = value_of_initializer %int.make_type_32.loc14_14 [template = i32]
  110. // CHECK:STDOUT: %.loc14_14.2: type = converted %int.make_type_32.loc14_14, %.loc14_14.1 [template = i32]
  111. // CHECK:STDOUT: %int.make_type_32.loc14_23: init type = call constants.%Int32() [template = i32]
  112. // CHECK:STDOUT: %.loc14_23.1: type = value_of_initializer %int.make_type_32.loc14_23 [template = i32]
  113. // CHECK:STDOUT: %.loc14_23.2: type = converted %int.make_type_32.loc14_23, %.loc14_23.1 [template = i32]
  114. // CHECK:STDOUT: %.loc14_26: type = struct_type {.a: i32, .b: i32} [template = constants.%.3]
  115. // CHECK:STDOUT: adapt_decl %.3
  116. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.3 [template = constants.%.4]
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !members:
  119. // CHECK:STDOUT: .Self = constants.%StructAdapter
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: --- fail_not_extend.carbon
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: constants {
  127. // CHECK:STDOUT: %Adapted: type = class_type @Adapted [template]
  128. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  129. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  130. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  131. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  132. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  133. // CHECK:STDOUT: %AdaptNotExtend: type = class_type @AdaptNotExtend [template]
  134. // CHECK:STDOUT: %.4: type = ptr_type %.2 [template]
  135. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %Adapted [template]
  136. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  137. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: imports {
  141. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  142. // CHECK:STDOUT: import Core//prelude
  143. // CHECK:STDOUT: import Core//prelude/operators
  144. // CHECK:STDOUT: import Core//prelude/types
  145. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  146. // CHECK:STDOUT: import Core//prelude/operators/as
  147. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  148. // CHECK:STDOUT: import Core//prelude/operators/comparison
  149. // CHECK:STDOUT: import Core//prelude/types/bool
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: file {
  154. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  155. // CHECK:STDOUT: .Core = imports.%Core
  156. // CHECK:STDOUT: .Adapted = %Adapted.decl
  157. // CHECK:STDOUT: .AdaptNotExtend = %AdaptNotExtend.decl
  158. // CHECK:STDOUT: .F = %F.decl
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %Core.import = import Core
  161. // CHECK:STDOUT: %Adapted.decl: type = class_decl @Adapted [template = constants.%Adapted] {} {}
  162. // CHECK:STDOUT: %AdaptNotExtend.decl: type = class_decl @AdaptNotExtend [template = constants.%AdaptNotExtend] {} {}
  163. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  164. // CHECK:STDOUT: %a.patt: %AdaptNotExtend = binding_pattern a
  165. // CHECK:STDOUT: } {
  166. // CHECK:STDOUT: %AdaptNotExtend.ref: type = name_ref AdaptNotExtend, file.%AdaptNotExtend.decl [template = constants.%AdaptNotExtend]
  167. // CHECK:STDOUT: %a.param: %AdaptNotExtend = param a, runtime_param0
  168. // CHECK:STDOUT: %a: %AdaptNotExtend = bind_name a, %a.param
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: class @Adapted {
  173. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {} {}
  174. // CHECK:STDOUT: %.loc6: <witness> = complete_type_witness %.2 [template = constants.%.3]
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: !members:
  177. // CHECK:STDOUT: .Self = constants.%Adapted
  178. // CHECK:STDOUT: .F = %F.decl
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: class @AdaptNotExtend {
  182. // CHECK:STDOUT: %Adapted.ref: type = name_ref Adapted, file.%Adapted.decl [template = constants.%Adapted]
  183. // CHECK:STDOUT: adapt_decl %Adapted
  184. // CHECK:STDOUT: %.loc10: <witness> = complete_type_witness %Adapted [template = constants.%.5]
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !members:
  187. // CHECK:STDOUT: .Self = constants.%AdaptNotExtend
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn @F.1();
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: fn @F.2(%a: %AdaptNotExtend) {
  193. // CHECK:STDOUT: !entry:
  194. // CHECK:STDOUT: %a.ref: %AdaptNotExtend = name_ref a, %a
  195. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [template = <error>]
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: