ref.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // EXTRA-ARGS: --dump-sem-ir-ranges=only
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/function/call/ref.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/call/ref.carbon
  13. // --- basics.carbon
  14. library "[[@TEST_NAME]]";
  15. //@dump-sem-ir-begin
  16. fn F(ref x: i32);
  17. fn G() {
  18. var y: i32 = 0;
  19. F(ref y);
  20. }
  21. //@dump-sem-ir-end
  22. // --- class.carbon
  23. library "[[@TEST_NAME]]";
  24. //@dump-sem-ir-begin
  25. class C {
  26. fn F[ref self: Self]();
  27. }
  28. fn G() {
  29. var c: C;
  30. c.F();
  31. }
  32. //@dump-sem-ir-end
  33. // --- fail_ref_not_durable_ref.carbon
  34. library "[[@TEST_NAME]]";
  35. fn F(ref x: i32);
  36. fn G() {
  37. let y: i32 = 0;
  38. // CHECK:STDERR: fail_ref_not_durable_ref.carbon:[[@LINE+4]]:5: error: expression tagged with `ref` is not a durable reference [RefTagNotDurableRef]
  39. // CHECK:STDERR: F(ref y);
  40. // CHECK:STDERR: ^~~~~
  41. // CHECK:STDERR:
  42. F(ref y);
  43. }
  44. // --- fail_missing_ref.carbon
  45. library "[[@TEST_NAME]]";
  46. fn F(ref x: i32);
  47. fn G() {
  48. var y: i32 = 0;
  49. // CHECK:STDERR: fail_missing_ref.carbon:[[@LINE+7]]:5: error: argument to `ref` parameter not marked with `ref` [RefParamNoRefTag]
  50. // CHECK:STDERR: F(y);
  51. // CHECK:STDERR: ^
  52. // CHECK:STDERR: fail_missing_ref.carbon:[[@LINE-7]]:6: note: initializing function parameter [InCallToFunctionParam]
  53. // CHECK:STDERR: fn F(ref x: i32);
  54. // CHECK:STDERR: ^~~~~~~~~~
  55. // CHECK:STDERR:
  56. F(y);
  57. }
  58. // --- fail_unnecessary_ref.carbon
  59. library "[[@TEST_NAME]]";
  60. fn F(x: i32);
  61. fn G() {
  62. var y: i32 = 0;
  63. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE+4]]:24: error: `ref` tag is not an argument to a `ref` parameter [RefTagNoRefParam]
  64. // CHECK:STDERR: var z: (i32,) = (ref y,);
  65. // CHECK:STDERR: ^
  66. // CHECK:STDERR:
  67. var z: (i32,) = (ref y,);
  68. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE+7]]:9: error: `ref` tag is not an argument to a `ref` parameter [RefTagNoRefParam]
  69. // CHECK:STDERR: F(ref y);
  70. // CHECK:STDERR: ^
  71. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE-13]]:6: note: initializing function parameter [InCallToFunctionParam]
  72. // CHECK:STDERR: fn F(x: i32);
  73. // CHECK:STDERR: ^~~~~~
  74. // CHECK:STDERR:
  75. F(ref y);
  76. }
  77. // CHECK:STDOUT: --- basics.carbon
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: constants {
  80. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  81. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  82. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  83. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  84. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  85. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  86. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  87. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  88. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  89. // CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  90. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  91. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  92. // 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]
  93. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.2a1: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.f51 = struct_value () [symbolic]
  94. // CHECK:STDOUT: %ImplicitAs.impl_witness.bc9: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.132, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  95. // 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]
  96. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.51e = struct_value () [concrete]
  97. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.bc9) [concrete]
  98. // CHECK:STDOUT: %.322: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  99. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b [concrete]
  100. // 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]
  101. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  102. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  103. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  104. // CHECK:STDOUT: %facet_value: %type_where = facet_value %i32, () [concrete]
  105. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.d4e: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete]
  106. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.424: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.d4e = struct_value () [concrete]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: imports {
  110. // 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)]
  111. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.132 = impl_witness_table (%Core.import_ref.e24), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: file {
  115. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  116. // CHECK:STDOUT: %x.patt: %pattern_type.7ce = ref_binding_pattern x [concrete]
  117. // CHECK:STDOUT: %x.param_patt: %pattern_type.7ce = ref_param_pattern %x.patt, call_param0 [concrete]
  118. // CHECK:STDOUT: } {
  119. // CHECK:STDOUT: %x.param: ref %i32 = ref_param call_param0
  120. // CHECK:STDOUT: %.loc5: type = splice_block %i32 [concrete = constants.%i32] {
  121. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  122. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %x: ref %i32 = ref_binding x, %x.param
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {} {}
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn @F(%x.param: %i32);
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: fn @G() {
  132. // CHECK:STDOUT: !entry:
  133. // CHECK:STDOUT: name_binding_decl {
  134. // CHECK:STDOUT: %y.patt: %pattern_type.7ce = ref_binding_pattern y [concrete]
  135. // CHECK:STDOUT: %y.var_patt: %pattern_type.7ce = var_pattern %y.patt [concrete]
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %y.var: ref %i32 = var %y.var_patt
  138. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  139. // CHECK:STDOUT: %impl.elem0: %.322 = impl_witness_access constants.%ImplicitAs.impl_witness.bc9, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.e9b]
  140. // CHECK:STDOUT: %bound_method.loc8_3.1: <bound method> = bound_method %int_0, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  141. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  142. // CHECK:STDOUT: %bound_method.loc8_3.2: <bound method> = bound_method %int_0, %specific_fn [concrete = constants.%bound_method]
  143. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call: init %i32 = call %bound_method.loc8_3.2(%int_0) [concrete = constants.%int_0.6a9]
  144. // CHECK:STDOUT: %.loc8_3: init %i32 = converted %int_0, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_0.6a9]
  145. // CHECK:STDOUT: assign %y.var, %.loc8_3
  146. // CHECK:STDOUT: %.loc8_10: type = splice_block %i32 [concrete = constants.%i32] {
  147. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  148. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: %y: ref %i32 = ref_binding y, %y.var
  151. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  152. // CHECK:STDOUT: %y.ref: ref %i32 = name_ref y, %y
  153. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref(%y.ref)
  154. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound: <bound method> = bound_method %y.var, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.424
  155. // CHECK:STDOUT: <elided>
  156. // CHECK:STDOUT: %bound_method.loc8_3.3: <bound method> = bound_method %y.var, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn
  157. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc8_3.3(%y.var)
  158. // CHECK:STDOUT: return
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: --- class.carbon
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: constants {
  164. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  165. // CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete]
  166. // CHECK:STDOUT: %C.F.type: type = fn_type @C.F [concrete]
  167. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  168. // CHECK:STDOUT: %C.F: %C.F.type = struct_value () [concrete]
  169. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  170. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  171. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  172. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  173. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  174. // CHECK:STDOUT: %facet_value: %type_where = facet_value %C, () [concrete]
  175. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.eeb: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete]
  176. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.88a: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.eeb = struct_value () [concrete]
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: imports {
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: file {
  183. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  184. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {} {}
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: class @C {
  188. // CHECK:STDOUT: %C.F.decl: %C.F.type = fn_decl @C.F [concrete = constants.%C.F] {
  189. // CHECK:STDOUT: %self.patt: %pattern_type.c48 = ref_binding_pattern self [concrete]
  190. // CHECK:STDOUT: %self.param_patt: %pattern_type.c48 = ref_param_pattern %self.patt, call_param0 [concrete]
  191. // CHECK:STDOUT: } {
  192. // CHECK:STDOUT: %self.param: ref %C = ref_param call_param0
  193. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  194. // CHECK:STDOUT: %self: ref %C = ref_binding self, %self.param
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  197. // CHECK:STDOUT: complete_type_witness = %complete_type
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: !members:
  200. // CHECK:STDOUT: .Self = constants.%C
  201. // CHECK:STDOUT: .F = %C.F.decl
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: fn @C.F(%self.param: %C);
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: fn @G() {
  207. // CHECK:STDOUT: !entry:
  208. // CHECK:STDOUT: name_binding_decl {
  209. // CHECK:STDOUT: %c.patt: %pattern_type.c48 = ref_binding_pattern c [concrete]
  210. // CHECK:STDOUT: %c.var_patt: %pattern_type.c48 = var_pattern %c.patt [concrete]
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT: %c.var: ref %C = var %c.var_patt
  213. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  214. // CHECK:STDOUT: %c: ref %C = ref_binding c, %c.var
  215. // CHECK:STDOUT: %c.ref: ref %C = name_ref c, %c
  216. // CHECK:STDOUT: %F.ref: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
  217. // CHECK:STDOUT: %C.F.bound: <bound method> = bound_method %c.ref, %F.ref
  218. // CHECK:STDOUT: %C.F.call: init %empty_tuple.type = call %C.F.bound(%c.ref)
  219. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound: <bound method> = bound_method %c.var, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.88a
  220. // CHECK:STDOUT: <elided>
  221. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.var, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn
  222. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method(%c.var)
  223. // CHECK:STDOUT: return
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT: