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+7]]:9: error: expression tagged with `ref` is not a durable reference [RefTagNotDurableRef]
  39. // CHECK:STDERR: F(ref y);
  40. // CHECK:STDERR: ^
  41. // CHECK:STDERR: fail_ref_not_durable_ref.carbon:[[@LINE-7]]:6: note: initializing function parameter [InCallToFunctionParam]
  42. // CHECK:STDERR: fn F(ref x: i32);
  43. // CHECK:STDERR: ^~~~~~~~~~
  44. // CHECK:STDERR:
  45. F(ref y);
  46. }
  47. // --- fail_missing_ref.carbon
  48. library "[[@TEST_NAME]]";
  49. fn F(ref x: i32);
  50. fn G() {
  51. var y: i32 = 0;
  52. // CHECK:STDERR: fail_missing_ref.carbon:[[@LINE+7]]:5: error: argument to `ref` parameter not marked with `ref` [RefParamNoRefTag]
  53. // CHECK:STDERR: F(y);
  54. // CHECK:STDERR: ^
  55. // CHECK:STDERR: fail_missing_ref.carbon:[[@LINE-7]]:6: note: initializing function parameter [InCallToFunctionParam]
  56. // CHECK:STDERR: fn F(ref x: i32);
  57. // CHECK:STDERR: ^~~~~~~~~~
  58. // CHECK:STDERR:
  59. F(y);
  60. }
  61. // --- fail_unnecessary_ref.carbon
  62. library "[[@TEST_NAME]]";
  63. fn F(x: i32);
  64. fn G() {
  65. var y: i32 = 0;
  66. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE+4]]:20: error: `ref` tag is not an argument to a `ref` parameter [RefTagNoRefParam]
  67. // CHECK:STDERR: var z: (i32,) = (ref y,);
  68. // CHECK:STDERR: ^~~~~
  69. // CHECK:STDERR:
  70. var z: (i32,) = (ref y,);
  71. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE+7]]:5: error: `ref` tag is not an argument to a `ref` parameter [RefTagNoRefParam]
  72. // CHECK:STDERR: F(ref y);
  73. // CHECK:STDERR: ^~~~~
  74. // CHECK:STDERR: fail_unnecessary_ref.carbon:[[@LINE-13]]:6: note: initializing function parameter [InCallToFunctionParam]
  75. // CHECK:STDERR: fn F(x: i32);
  76. // CHECK:STDERR: ^~~~~~
  77. // CHECK:STDERR:
  78. F(ref y);
  79. }
  80. // CHECK:STDOUT: --- basics.carbon
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: constants {
  83. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  84. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  85. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  86. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  87. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  88. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  89. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  90. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  91. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  92. // CHECK:STDOUT: %ImplicitAs.type.e8c: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  93. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  94. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  95. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  96. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6 = struct_value () [symbolic]
  97. // CHECK:STDOUT: %ImplicitAs.impl_witness.6bc: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.74f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  98. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  99. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d = struct_value () [concrete]
  100. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.6bc) [concrete]
  101. // CHECK:STDOUT: %.863: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  102. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete]
  103. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  104. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  105. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  106. // CHECK:STDOUT: %DestroyOp.type: type = fn_type @DestroyOp [concrete]
  107. // CHECK:STDOUT: %DestroyOp: %DestroyOp.type = struct_value () [concrete]
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: imports {
  111. // CHECK:STDOUT: %Core.import_ref.42d: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6) = 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.3c2)]
  112. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.74f = impl_witness_table (%Core.import_ref.42d), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: file {
  116. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  117. // CHECK:STDOUT: %x.patt: %pattern_type.7ce = ref_binding_pattern x [concrete]
  118. // CHECK:STDOUT: %x.param_patt: %pattern_type.7ce = ref_param_pattern %x.patt, call_param0 [concrete]
  119. // CHECK:STDOUT: } {
  120. // CHECK:STDOUT: %x.param: ref %i32 = ref_param call_param0
  121. // CHECK:STDOUT: %.loc5: type = splice_block %i32 [concrete = constants.%i32] {
  122. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  123. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: %x: ref %i32 = ref_binding x, %x.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {} {}
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @F(%x.param: %i32);
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @G() {
  133. // CHECK:STDOUT: !entry:
  134. // CHECK:STDOUT: name_binding_decl {
  135. // CHECK:STDOUT: %y.patt: %pattern_type.7ce = ref_binding_pattern y [concrete]
  136. // CHECK:STDOUT: %y.var_patt: %pattern_type.7ce = var_pattern %y.patt [concrete]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %y.var: ref %i32 = var %y.var_patt
  139. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  140. // CHECK:STDOUT: %impl.elem0: %.863 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  141. // CHECK:STDOUT: %bound_method.loc8_3.1: <bound method> = bound_method %int_0, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  142. // 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]
  143. // CHECK:STDOUT: %bound_method.loc8_3.2: <bound method> = bound_method %int_0, %specific_fn [concrete = constants.%bound_method]
  144. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call: init %i32 = call %bound_method.loc8_3.2(%int_0) [concrete = constants.%int_0.6a9]
  145. // CHECK:STDOUT: %.loc8_3: init %i32 = converted %int_0, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_0.6a9]
  146. // CHECK:STDOUT: assign %y.var, %.loc8_3
  147. // CHECK:STDOUT: %.loc8_10: type = splice_block %i32 [concrete = constants.%i32] {
  148. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  149. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: %y: ref %i32 = ref_binding y, %y.var
  152. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  153. // CHECK:STDOUT: %y.ref: ref %i32 = name_ref y, %y
  154. // CHECK:STDOUT: %.loc10: %i32 = ref_tag %y.ref
  155. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref(%.loc10)
  156. // CHECK:STDOUT: %DestroyOp.bound: <bound method> = bound_method %y.var, constants.%DestroyOp
  157. // CHECK:STDOUT: %DestroyOp.call: init %empty_tuple.type = call %DestroyOp.bound(%y.var)
  158. // CHECK:STDOUT: return
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: fn @DestroyOp(%self.param: %i32) = "no_op";
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: --- class.carbon
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: constants {
  166. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  167. // CHECK:STDOUT: %pattern_type.7c7: type = pattern_type %C [concrete]
  168. // CHECK:STDOUT: %C.F.type: type = fn_type @C.F [concrete]
  169. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  170. // CHECK:STDOUT: %C.F: %C.F.type = struct_value () [concrete]
  171. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  172. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  173. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  174. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  175. // CHECK:STDOUT: %DestroyOp.type: type = fn_type @DestroyOp [concrete]
  176. // CHECK:STDOUT: %DestroyOp: %DestroyOp.type = 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.7c7 = ref_binding_pattern self [concrete]
  190. // CHECK:STDOUT: %self.param_patt: %pattern_type.7c7 = 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.7c7 = ref_binding_pattern c [concrete]
  210. // CHECK:STDOUT: %c.var_patt: %pattern_type.7c7 = 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: %DestroyOp.bound: <bound method> = bound_method %c.var, constants.%DestroyOp
  220. // CHECK:STDOUT: %DestroyOp.call: init %empty_tuple.type = call %DestroyOp.bound(%c.var)
  221. // CHECK:STDOUT: return
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: fn @DestroyOp(%self.param: %C) = "no_op";
  225. // CHECK:STDOUT: