init_as.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/init_as.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/init_as.carbon
  14. class Class {
  15. var a: i32;
  16. var b: i32;
  17. }
  18. fn F() -> i32 {
  19. return ({.a = 1, .b = 2} as Class).a;
  20. }
  21. // CHECK:STDOUT: --- init_as.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  25. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  26. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  27. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  28. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  29. // CHECK:STDOUT: %N: Core.IntLiteral = symbolic_binding N, 0 [symbolic]
  30. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  31. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [concrete]
  32. // CHECK:STDOUT: %struct_type.a.b.501: type = struct_type {.a: %i32, .b: %i32} [concrete]
  33. // CHECK:STDOUT: %complete_type.705: <witness> = complete_type_witness %struct_type.a.b.501 [concrete]
  34. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  35. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  36. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  37. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  38. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  39. // CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [concrete]
  40. // CHECK:STDOUT: %struct: %struct_type.a.b.cfd = struct_value (%int_1.5b8, %int_2.ecc) [concrete]
  41. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  42. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  43. // CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  44. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  45. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  46. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.f51: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  47. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.2a1: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.f51 = struct_value () [symbolic]
  48. // CHECK:STDOUT: %ImplicitAs.impl_witness.bc9: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.132, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  49. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.51e: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  50. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.51e = struct_value () [concrete]
  51. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.bc9) [concrete]
  52. // CHECK:STDOUT: %.322: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  53. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.265: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b [concrete]
  54. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  55. // CHECK:STDOUT: %bound_method.dfc: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  56. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  57. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.393: <bound method> = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b [concrete]
  58. // CHECK:STDOUT: %bound_method.9f7: <bound method> = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  59. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete]
  60. // CHECK:STDOUT: %Class.val: %Class = struct_value (%int_1.5d2, %int_2.ef8) [concrete]
  61. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  62. // CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
  63. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.24b: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
  64. // CHECK:STDOUT: %Int.as.Copy.impl.Op.c95: %Int.as.Copy.impl.Op.type.24b = struct_value () [symbolic]
  65. // CHECK:STDOUT: %Copy.impl_witness.fb7: <witness> = impl_witness imports.%Copy.impl_witness_table.b6a, @Int.as.Copy.impl(%int_32) [concrete]
  66. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.469: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
  67. // CHECK:STDOUT: %Int.as.Copy.impl.Op.dfd: %Int.as.Copy.impl.Op.type.469 = struct_value () [concrete]
  68. // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %i32, (%Copy.impl_witness.fb7) [concrete]
  69. // CHECK:STDOUT: %.65f: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet [concrete]
  70. // CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.dfd, @Int.as.Copy.impl.Op(%int_32) [concrete]
  71. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  72. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  73. // CHECK:STDOUT: %facet_value: %type_where = facet_value %Class, () [concrete]
  74. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.e99: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete]
  75. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.378: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.e99 = struct_value () [concrete]
  76. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn: <specific function> = specific_function %DestroyT.binding.as_type.as.Destroy.impl.Op.378, @DestroyT.binding.as_type.as.Destroy.impl.Op(%facet_value) [concrete]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: imports {
  80. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  81. // CHECK:STDOUT: .Int = %Core.Int
  82. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  83. // CHECK:STDOUT: .Copy = %Core.Copy
  84. // CHECK:STDOUT: .Destroy = %Core.Destroy
  85. // CHECK:STDOUT: import Core//prelude
  86. // CHECK:STDOUT: import Core//prelude/...
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  89. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  90. // CHECK:STDOUT: %Core.import_ref.e24: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.f51) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.2a1)]
  91. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.132 = impl_witness_table (%Core.import_ref.e24), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  92. // CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/parts/copy, Copy, loaded [concrete = constants.%Copy.type]
  93. // CHECK:STDOUT: %Core.import_ref.d12: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.24b) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Int.as.Copy.impl.%Int.as.Copy.impl.Op (constants.%Int.as.Copy.impl.Op.c95)]
  94. // CHECK:STDOUT: %Copy.impl_witness_table.b6a = impl_witness_table (%Core.import_ref.d12), @Int.as.Copy.impl [concrete]
  95. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: file {
  99. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  100. // CHECK:STDOUT: .Core = imports.%Core
  101. // CHECK:STDOUT: .Class = %Class.decl
  102. // CHECK:STDOUT: .F = %F.decl
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %Core.import = import Core
  105. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  106. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  107. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  108. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  109. // CHECK:STDOUT: } {
  110. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  111. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  112. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  113. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: class @Class {
  118. // CHECK:STDOUT: %int_32.loc16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  119. // CHECK:STDOUT: %i32.loc16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  120. // CHECK:STDOUT: %.loc16: %Class.elem = field_decl a, element0 [concrete]
  121. // CHECK:STDOUT: %int_32.loc17: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  122. // CHECK:STDOUT: %i32.loc17: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  123. // CHECK:STDOUT: %.loc17: %Class.elem = field_decl b, element1 [concrete]
  124. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.a.b.501 [concrete = constants.%complete_type.705]
  125. // CHECK:STDOUT: complete_type_witness = %complete_type
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Self = constants.%Class
  129. // CHECK:STDOUT: .a = %.loc16
  130. // CHECK:STDOUT: .b = %.loc17
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: fn @F() -> %i32 {
  134. // CHECK:STDOUT: !entry:
  135. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  136. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  137. // CHECK:STDOUT: %.loc21_26.1: %struct_type.a.b.cfd = struct_literal (%int_1, %int_2) [concrete = constants.%struct]
  138. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  139. // CHECK:STDOUT: %impl.elem0.loc21_26.1: %.322 = impl_witness_access constants.%ImplicitAs.impl_witness.bc9, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b]
  140. // CHECK:STDOUT: %bound_method.loc21_26.1: <bound method> = bound_method %int_1, %impl.elem0.loc21_26.1 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.265]
  141. // CHECK:STDOUT: %specific_fn.loc21_26.1: <specific function> = specific_function %impl.elem0.loc21_26.1, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  142. // CHECK:STDOUT: %bound_method.loc21_26.2: <bound method> = bound_method %int_1, %specific_fn.loc21_26.1 [concrete = constants.%bound_method.dfc]
  143. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc21_26.1: init %i32 = call %bound_method.loc21_26.2(%int_1) [concrete = constants.%int_1.5d2]
  144. // CHECK:STDOUT: %.loc21_26.2: init %i32 = converted %int_1, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc21_26.1 [concrete = constants.%int_1.5d2]
  145. // CHECK:STDOUT: %.loc21_26.3: ref %Class = temporary_storage
  146. // CHECK:STDOUT: %.loc21_26.4: ref %i32 = class_element_access %.loc21_26.3, element0
  147. // CHECK:STDOUT: %.loc21_26.5: init %i32 = initialize_from %.loc21_26.2 to %.loc21_26.4 [concrete = constants.%int_1.5d2]
  148. // CHECK:STDOUT: %impl.elem0.loc21_26.2: %.322 = impl_witness_access constants.%ImplicitAs.impl_witness.bc9, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b]
  149. // CHECK:STDOUT: %bound_method.loc21_26.3: <bound method> = bound_method %int_2, %impl.elem0.loc21_26.2 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.393]
  150. // CHECK:STDOUT: %specific_fn.loc21_26.2: <specific function> = specific_function %impl.elem0.loc21_26.2, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  151. // CHECK:STDOUT: %bound_method.loc21_26.4: <bound method> = bound_method %int_2, %specific_fn.loc21_26.2 [concrete = constants.%bound_method.9f7]
  152. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc21_26.2: init %i32 = call %bound_method.loc21_26.4(%int_2) [concrete = constants.%int_2.ef8]
  153. // CHECK:STDOUT: %.loc21_26.6: init %i32 = converted %int_2, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc21_26.2 [concrete = constants.%int_2.ef8]
  154. // CHECK:STDOUT: %.loc21_26.7: ref %i32 = class_element_access %.loc21_26.3, element1
  155. // CHECK:STDOUT: %.loc21_26.8: init %i32 = initialize_from %.loc21_26.6 to %.loc21_26.7 [concrete = constants.%int_2.ef8]
  156. // CHECK:STDOUT: %.loc21_26.9: init %Class = class_init (%.loc21_26.5, %.loc21_26.8), %.loc21_26.3 [concrete = constants.%Class.val]
  157. // CHECK:STDOUT: %.loc21_26.10: ref %Class = temporary %.loc21_26.3, %.loc21_26.9
  158. // CHECK:STDOUT: %.loc21_28: ref %Class = converted %.loc21_26.1, %.loc21_26.10
  159. // CHECK:STDOUT: %a.ref: %Class.elem = name_ref a, @Class.%.loc16 [concrete = @Class.%.loc16]
  160. // CHECK:STDOUT: %.loc21_37.1: ref %i32 = class_element_access %.loc21_28, element0
  161. // CHECK:STDOUT: %.loc21_37.2: %i32 = acquire_value %.loc21_37.1
  162. // CHECK:STDOUT: %impl.elem0.loc21_37: %.65f = impl_witness_access constants.%Copy.impl_witness.fb7, element0 [concrete = constants.%Int.as.Copy.impl.Op.dfd]
  163. // CHECK:STDOUT: %bound_method.loc21_37.1: <bound method> = bound_method %.loc21_37.2, %impl.elem0.loc21_37
  164. // CHECK:STDOUT: %specific_fn.loc21_37: <specific function> = specific_function %impl.elem0.loc21_37, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn]
  165. // CHECK:STDOUT: %bound_method.loc21_37.2: <bound method> = bound_method %.loc21_37.2, %specific_fn.loc21_37
  166. // CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc21_37.2(%.loc21_37.2)
  167. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc21_26.10, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.378
  168. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn: <specific function> = specific_function constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.378, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  169. // CHECK:STDOUT: %bound_method.loc21_26.5: <bound method> = bound_method %.loc21_26.10, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn
  170. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc21_26.5(%.loc21_26.10)
  171. // CHECK:STDOUT: return %Int.as.Copy.impl.Op.call to %return
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT: