adapt.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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+4]]:3: error: member name `F` not found in `AdaptNotExtend` [MemberNameNotFoundInScope]
  33. // CHECK:STDERR: a.F();
  34. // CHECK:STDERR: ^~~
  35. // CHECK:STDERR:
  36. a.F();
  37. }
  38. // --- fail_misplaced.carbon
  39. fn F() {
  40. // CHECK:STDERR: fail_misplaced.carbon:[[@LINE+4]]:3: error: `adapt` declaration outside class [ClassSpecificDeclOutsideClass]
  41. // CHECK:STDERR: adapt i32;
  42. // CHECK:STDERR: ^~~~~~~~~~
  43. // CHECK:STDERR:
  44. adapt i32;
  45. }
  46. interface I {
  47. // CHECK:STDERR: fail_misplaced.carbon:[[@LINE+4]]:3: error: `adapt` declaration outside class [ClassSpecificDeclOutsideClass]
  48. // CHECK:STDERR: adapt i32;
  49. // CHECK:STDERR: ^~~~~~~~~~
  50. // CHECK:STDERR:
  51. adapt i32;
  52. }
  53. // CHECK:STDOUT: --- basic.carbon
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: constants {
  56. // CHECK:STDOUT: %SomeClass: type = class_type @SomeClass [template]
  57. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  58. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  59. // CHECK:STDOUT: %SomeClass.elem: type = unbound_element_type %SomeClass, %i32 [template]
  60. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template]
  61. // CHECK:STDOUT: %complete_type.705: <witness> = complete_type_witness %struct_type.a.b [template]
  62. // CHECK:STDOUT: %SomeClassAdapter: type = class_type @SomeClassAdapter [template]
  63. // CHECK:STDOUT: %StructAdapter: type = class_type @StructAdapter [template]
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: imports {
  67. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  68. // CHECK:STDOUT: .Int = %Core.Int
  69. // CHECK:STDOUT: import Core//prelude
  70. // CHECK:STDOUT: import Core//prelude/...
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: file {
  75. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  76. // CHECK:STDOUT: .Core = imports.%Core
  77. // CHECK:STDOUT: .SomeClass = %SomeClass.decl
  78. // CHECK:STDOUT: .SomeClassAdapter = %SomeClassAdapter.decl
  79. // CHECK:STDOUT: .StructAdapter = %StructAdapter.decl
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %Core.import = import Core
  82. // CHECK:STDOUT: %SomeClass.decl: type = class_decl @SomeClass [template = constants.%SomeClass] {} {}
  83. // CHECK:STDOUT: %SomeClassAdapter.decl: type = class_decl @SomeClassAdapter [template = constants.%SomeClassAdapter] {} {}
  84. // CHECK:STDOUT: %StructAdapter.decl: type = class_decl @StructAdapter [template = constants.%StructAdapter] {} {}
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: class @SomeClass {
  88. // CHECK:STDOUT: %.loc5_8: %SomeClass.elem = field_decl a, element0 [template]
  89. // CHECK:STDOUT: name_binding_decl {
  90. // CHECK:STDOUT: %.loc5_3: %SomeClass.elem = var_pattern %.loc5_8
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %.var.loc5: ref %SomeClass.elem = var <none>
  93. // CHECK:STDOUT: %.loc6_8: %SomeClass.elem = field_decl b, element1 [template]
  94. // CHECK:STDOUT: name_binding_decl {
  95. // CHECK:STDOUT: %.loc6_3: %SomeClass.elem = var_pattern %.loc6_8
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %.var.loc6: ref %SomeClass.elem = var <none>
  98. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.705]
  99. // CHECK:STDOUT: complete_type_witness = %complete_type
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !members:
  102. // CHECK:STDOUT: .Self = constants.%SomeClass
  103. // CHECK:STDOUT: .a = %.loc5_8
  104. // CHECK:STDOUT: .b = %.loc6_8
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: class @SomeClassAdapter {
  108. // CHECK:STDOUT: %SomeClass.ref: type = name_ref SomeClass, file.%SomeClass.decl [template = constants.%SomeClass]
  109. // CHECK:STDOUT: adapt_decl %SomeClass.ref [template]
  110. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.705]
  111. // CHECK:STDOUT: complete_type_witness = %complete_type
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: !members:
  114. // CHECK:STDOUT: .Self = constants.%SomeClassAdapter
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: class @StructAdapter {
  118. // CHECK:STDOUT: %int_32.loc14_14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  119. // CHECK:STDOUT: %i32.loc14_14: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  120. // CHECK:STDOUT: %int_32.loc14_23: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  121. // CHECK:STDOUT: %i32.loc14_23: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  122. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template = constants.%struct_type.a.b]
  123. // CHECK:STDOUT: adapt_decl %struct_type.a.b [template]
  124. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.705]
  125. // CHECK:STDOUT: complete_type_witness = %complete_type
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Self = constants.%StructAdapter
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: --- fail_not_extend.carbon
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: constants {
  134. // CHECK:STDOUT: %Adapted: type = class_type @Adapted [template]
  135. // CHECK:STDOUT: %F.type.967: type = fn_type @F.1 [template]
  136. // CHECK:STDOUT: %F.9eb: %F.type.967 = struct_value () [template]
  137. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  138. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  139. // CHECK:STDOUT: %AdaptNotExtend: type = class_type @AdaptNotExtend [template]
  140. // CHECK:STDOUT: %F.type.b25: type = fn_type @F.2 [template]
  141. // CHECK:STDOUT: %F.c41: %F.type.b25 = struct_value () [template]
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: imports {
  145. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  146. // CHECK:STDOUT: import Core//prelude
  147. // CHECK:STDOUT: import Core//prelude/...
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  153. // CHECK:STDOUT: .Core = imports.%Core
  154. // CHECK:STDOUT: .Adapted = %Adapted.decl
  155. // CHECK:STDOUT: .AdaptNotExtend = %AdaptNotExtend.decl
  156. // CHECK:STDOUT: .F = %F.decl
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %Core.import = import Core
  159. // CHECK:STDOUT: %Adapted.decl: type = class_decl @Adapted [template = constants.%Adapted] {} {}
  160. // CHECK:STDOUT: %AdaptNotExtend.decl: type = class_decl @AdaptNotExtend [template = constants.%AdaptNotExtend] {} {}
  161. // CHECK:STDOUT: %F.decl: %F.type.b25 = fn_decl @F.2 [template = constants.%F.c41] {
  162. // CHECK:STDOUT: %a.patt: %AdaptNotExtend = binding_pattern a
  163. // CHECK:STDOUT: %a.param_patt: %AdaptNotExtend = value_param_pattern %a.patt, runtime_param0
  164. // CHECK:STDOUT: } {
  165. // CHECK:STDOUT: %a.param: %AdaptNotExtend = value_param runtime_param0
  166. // CHECK:STDOUT: %AdaptNotExtend.ref: type = name_ref AdaptNotExtend, file.%AdaptNotExtend.decl [template = constants.%AdaptNotExtend]
  167. // CHECK:STDOUT: %a: %AdaptNotExtend = bind_name a, %a.param
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: class @Adapted {
  172. // CHECK:STDOUT: %F.decl: %F.type.967 = fn_decl @F.1 [template = constants.%F.9eb] {} {}
  173. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  174. // CHECK:STDOUT: complete_type_witness = %complete_type
  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.ref [template]
  184. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  185. // CHECK:STDOUT: complete_type_witness = %complete_type
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: !members:
  188. // CHECK:STDOUT: .Self = constants.%AdaptNotExtend
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn @F.1();
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @F.2(%a.param_patt: %AdaptNotExtend) {
  194. // CHECK:STDOUT: !entry:
  195. // CHECK:STDOUT: %a.ref: %AdaptNotExtend = name_ref a, %a
  196. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [template = <error>]
  197. // CHECK:STDOUT: return
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: --- fail_misplaced.carbon
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: constants {
  203. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  204. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  205. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  206. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  207. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  208. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: imports {
  212. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  213. // CHECK:STDOUT: .Int = %Core.Int
  214. // CHECK:STDOUT: import Core//prelude
  215. // CHECK:STDOUT: import Core//prelude/...
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: file {
  220. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  221. // CHECK:STDOUT: .Core = imports.%Core
  222. // CHECK:STDOUT: .F = %F.decl
  223. // CHECK:STDOUT: .I = %I.decl
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT: %Core.import = import Core
  226. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  227. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: interface @I {
  231. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  232. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  233. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: !members:
  236. // CHECK:STDOUT: .Self = %Self
  237. // CHECK:STDOUT: witness = ()
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: fn @F() {
  241. // CHECK:STDOUT: !entry:
  242. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  243. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  244. // CHECK:STDOUT: return
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT: