adapt.carbon 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/adapter/adapt.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/adapter/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: member name `F` not found in `AdaptNotExtend` [MemberNameNotFoundInScope]
  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: %int_32: Core.IntLiteral = int_value 32 [template]
  42. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  43. // CHECK:STDOUT: %SomeClass.elem: type = unbound_element_type %SomeClass, %i32 [template]
  44. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template]
  45. // CHECK:STDOUT: %complete_type.e46: <witness> = complete_type_witness %struct_type.a.b [template]
  46. // CHECK:STDOUT: %SomeClassAdapter: type = class_type @SomeClassAdapter [template]
  47. // CHECK:STDOUT: %StructAdapter: type = class_type @StructAdapter [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int = %import_ref.187
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/...
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .SomeClass = %SomeClass.decl
  62. // CHECK:STDOUT: .SomeClassAdapter = %SomeClassAdapter.decl
  63. // CHECK:STDOUT: .StructAdapter = %StructAdapter.decl
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: %SomeClass.decl: type = class_decl @SomeClass [template = constants.%SomeClass] {} {}
  67. // CHECK:STDOUT: %SomeClassAdapter.decl: type = class_decl @SomeClassAdapter [template = constants.%SomeClassAdapter] {} {}
  68. // CHECK:STDOUT: %StructAdapter.decl: type = class_decl @StructAdapter [template = constants.%StructAdapter] {} {}
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: class @SomeClass {
  72. // CHECK:STDOUT: %.loc5: %SomeClass.elem = field_decl a, element0 [template]
  73. // CHECK:STDOUT: %.loc6: %SomeClass.elem = field_decl b, element1 [template]
  74. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.e46]
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: !members:
  77. // CHECK:STDOUT: .Self = constants.%SomeClass
  78. // CHECK:STDOUT: .a = %.loc5
  79. // CHECK:STDOUT: .b = %.loc6
  80. // CHECK:STDOUT: complete_type_witness = %complete_type
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: class @SomeClassAdapter {
  84. // CHECK:STDOUT: %SomeClass.ref: type = name_ref SomeClass, file.%SomeClass.decl [template = constants.%SomeClass]
  85. // CHECK:STDOUT: adapt_decl %SomeClass.ref [template]
  86. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.e46]
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = constants.%SomeClassAdapter
  90. // CHECK:STDOUT: complete_type_witness = %complete_type
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: class @StructAdapter {
  94. // CHECK:STDOUT: %int_32.loc14_14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  95. // CHECK:STDOUT: %i32.loc14_14: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  96. // CHECK:STDOUT: %int_32.loc14_23: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  97. // CHECK:STDOUT: %i32.loc14_23: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  98. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template = constants.%struct_type.a.b]
  99. // CHECK:STDOUT: adapt_decl %struct_type.a.b [template]
  100. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.e46]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !members:
  103. // CHECK:STDOUT: .Self = constants.%StructAdapter
  104. // CHECK:STDOUT: complete_type_witness = %complete_type
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: --- fail_not_extend.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %Adapted: type = class_type @Adapted [template]
  111. // CHECK:STDOUT: %F.type.f05: type = fn_type @F.1 [template]
  112. // CHECK:STDOUT: %F.8ad: %F.type.f05 = struct_value () [template]
  113. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  114. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  115. // CHECK:STDOUT: %AdaptNotExtend: type = class_type @AdaptNotExtend [template]
  116. // CHECK:STDOUT: %F.type.b25: type = fn_type @F.2 [template]
  117. // CHECK:STDOUT: %F.c41: %F.type.b25 = struct_value () [template]
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: imports {
  121. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  122. // CHECK:STDOUT: import Core//prelude
  123. // CHECK:STDOUT: import Core//prelude/...
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: file {
  128. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  129. // CHECK:STDOUT: .Core = imports.%Core
  130. // CHECK:STDOUT: .Adapted = %Adapted.decl
  131. // CHECK:STDOUT: .AdaptNotExtend = %AdaptNotExtend.decl
  132. // CHECK:STDOUT: .F = %F.decl
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %Core.import = import Core
  135. // CHECK:STDOUT: %Adapted.decl: type = class_decl @Adapted [template = constants.%Adapted] {} {}
  136. // CHECK:STDOUT: %AdaptNotExtend.decl: type = class_decl @AdaptNotExtend [template = constants.%AdaptNotExtend] {} {}
  137. // CHECK:STDOUT: %F.decl: %F.type.b25 = fn_decl @F.2 [template = constants.%F.c41] {
  138. // CHECK:STDOUT: %a.patt: %AdaptNotExtend = binding_pattern a
  139. // CHECK:STDOUT: %a.param_patt: %AdaptNotExtend = value_param_pattern %a.patt, runtime_param0
  140. // CHECK:STDOUT: } {
  141. // CHECK:STDOUT: %a.param: %AdaptNotExtend = value_param runtime_param0
  142. // CHECK:STDOUT: %AdaptNotExtend.ref: type = name_ref AdaptNotExtend, file.%AdaptNotExtend.decl [template = constants.%AdaptNotExtend]
  143. // CHECK:STDOUT: %a: %AdaptNotExtend = bind_name a, %a.param
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: class @Adapted {
  148. // CHECK:STDOUT: %F.decl: %F.type.f05 = fn_decl @F.1 [template = constants.%F.8ad] {} {}
  149. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !members:
  152. // CHECK:STDOUT: .Self = constants.%Adapted
  153. // CHECK:STDOUT: .F = %F.decl
  154. // CHECK:STDOUT: complete_type_witness = %complete_type
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: class @AdaptNotExtend {
  158. // CHECK:STDOUT: %Adapted.ref: type = name_ref Adapted, file.%Adapted.decl [template = constants.%Adapted]
  159. // CHECK:STDOUT: adapt_decl %Adapted.ref [template]
  160. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !members:
  163. // CHECK:STDOUT: .Self = constants.%AdaptNotExtend
  164. // CHECK:STDOUT: complete_type_witness = %complete_type
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: fn @F.1();
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn @F.2(%a.param_patt: %AdaptNotExtend) {
  170. // CHECK:STDOUT: !entry:
  171. // CHECK:STDOUT: %a.ref: %AdaptNotExtend = name_ref a, %a
  172. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [template = <error>]
  173. // CHECK:STDOUT: return
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: