fail_ref_self.carbon 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/convert.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/fail_ref_self.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_ref_self.carbon
  14. class Class {
  15. fn F[ref self: Self]();
  16. }
  17. fn Make() -> Class;
  18. fn F(c: Class, p: Class*) {
  19. // CHECK:STDERR: fail_ref_self.carbon:[[@LINE+7]]:3: error: value expression passed to reference parameter [ValueForRefParam]
  20. // CHECK:STDERR: c.F();
  21. // CHECK:STDERR: ^
  22. // CHECK:STDERR: fail_ref_self.carbon:[[@LINE-9]]:8: note: initializing function parameter [InCallToFunctionParam]
  23. // CHECK:STDERR: fn F[ref self: Self]();
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. c.F();
  27. // This call is OK.
  28. (*p).F();
  29. // So is this one.
  30. Make().F();
  31. }
  32. // CHECK:STDOUT: --- fail_ref_self.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  36. // CHECK:STDOUT: %pattern_type.904: type = pattern_type %Class [concrete]
  37. // CHECK:STDOUT: %Class.F.type: type = fn_type @Class.F [concrete]
  38. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  39. // CHECK:STDOUT: %Class.F: %Class.F.type = struct_value () [concrete]
  40. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  41. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  42. // CHECK:STDOUT: %.7f1: form = init_form %Class, call_param0 [concrete]
  43. // CHECK:STDOUT: %Make.type: type = fn_type @Make [concrete]
  44. // CHECK:STDOUT: %Make: %Make.type = struct_value () [concrete]
  45. // CHECK:STDOUT: %ptr.8e5: type = ptr_type %Class [concrete]
  46. // CHECK:STDOUT: %pattern_type.018: type = pattern_type %ptr.8e5 [concrete]
  47. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  48. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  49. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  50. // CHECK:STDOUT: %DestroyOp.type: type = fn_type @DestroyOp [concrete]
  51. // CHECK:STDOUT: %DestroyOp: %DestroyOp.type = struct_value () [concrete]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: imports {
  55. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  56. // CHECK:STDOUT: .Destroy = %Core.Destroy
  57. // CHECK:STDOUT: import Core//prelude
  58. // CHECK:STDOUT: import Core//prelude/...
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .Class = %Class.decl
  67. // CHECK:STDOUT: .Make = %Make.decl
  68. // CHECK:STDOUT: .F = %F.decl
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %Core.import = import Core
  71. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  72. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [concrete = constants.%Make] {
  73. // CHECK:STDOUT: %return.patt: %pattern_type.904 = return_slot_pattern [concrete]
  74. // CHECK:STDOUT: %return.param_patt: %pattern_type.904 = out_param_pattern %return.patt, call_param0 [concrete]
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  77. // CHECK:STDOUT: %.loc19: form = init_form %Class.ref, call_param0 [concrete = constants.%.7f1]
  78. // CHECK:STDOUT: %return.param: ref %Class = out_param call_param0
  79. // CHECK:STDOUT: %return: ref %Class = return_slot %return.param
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  82. // CHECK:STDOUT: %c.patt: %pattern_type.904 = value_binding_pattern c [concrete]
  83. // CHECK:STDOUT: %c.param_patt: %pattern_type.904 = value_param_pattern %c.patt, call_param0 [concrete]
  84. // CHECK:STDOUT: %p.patt: %pattern_type.018 = value_binding_pattern p [concrete]
  85. // CHECK:STDOUT: %p.param_patt: %pattern_type.018 = value_param_pattern %p.patt, call_param1 [concrete]
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: %c.param: %Class = value_param call_param0
  88. // CHECK:STDOUT: %Class.ref.loc21_9: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  89. // CHECK:STDOUT: %c: %Class = value_binding c, %c.param
  90. // CHECK:STDOUT: %p.param: %ptr.8e5 = value_param call_param1
  91. // CHECK:STDOUT: %.loc21: type = splice_block %ptr [concrete = constants.%ptr.8e5] {
  92. // CHECK:STDOUT: %Class.ref.loc21_19: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  93. // CHECK:STDOUT: %ptr: type = ptr_type %Class.ref.loc21_19 [concrete = constants.%ptr.8e5]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %p: %ptr.8e5 = value_binding p, %p.param
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: class @Class {
  100. // CHECK:STDOUT: %Class.F.decl: %Class.F.type = fn_decl @Class.F [concrete = constants.%Class.F] {
  101. // CHECK:STDOUT: %self.patt: %pattern_type.904 = ref_binding_pattern self [concrete]
  102. // CHECK:STDOUT: %self.param_patt: %pattern_type.904 = ref_param_pattern %self.patt, call_param0 [concrete]
  103. // CHECK:STDOUT: } {
  104. // CHECK:STDOUT: %self.param: ref %Class = ref_param call_param0
  105. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  106. // CHECK:STDOUT: %self: ref %Class = ref_binding self, %self.param
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  109. // CHECK:STDOUT: complete_type_witness = %complete_type
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Self = constants.%Class
  113. // CHECK:STDOUT: .F = %Class.F.decl
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Class.F(%self.param: %Class);
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @Make() -> %return.param: %Class;
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: fn @F(%c.param: %Class, %p.param: %ptr.8e5) {
  121. // CHECK:STDOUT: !entry:
  122. // CHECK:STDOUT: %c.ref: %Class = name_ref c, %c
  123. // CHECK:STDOUT: %F.ref.loc29: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  124. // CHECK:STDOUT: %Class.F.bound.loc29: <bound method> = bound_method %c.ref, %F.ref.loc29
  125. // CHECK:STDOUT: %Class.F.call.loc29: init %empty_tuple.type = call %Class.F.bound.loc29(<error>)
  126. // CHECK:STDOUT: %p.ref: %ptr.8e5 = name_ref p, %p
  127. // CHECK:STDOUT: %.loc32: ref %Class = deref %p.ref
  128. // CHECK:STDOUT: %F.ref.loc32: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  129. // CHECK:STDOUT: %Class.F.bound.loc32: <bound method> = bound_method %.loc32, %F.ref.loc32
  130. // CHECK:STDOUT: %Class.F.call.loc32: init %empty_tuple.type = call %Class.F.bound.loc32(%.loc32)
  131. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, file.%Make.decl [concrete = constants.%Make]
  132. // CHECK:STDOUT: %.loc35_8.1: ref %Class = temporary_storage
  133. // CHECK:STDOUT: %Make.call: init %Class = call %Make.ref() to %.loc35_8.1
  134. // CHECK:STDOUT: %.loc35_8.2: ref %Class = temporary %.loc35_8.1, %Make.call
  135. // CHECK:STDOUT: %F.ref.loc35: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  136. // CHECK:STDOUT: %Class.F.bound.loc35: <bound method> = bound_method %.loc35_8.2, %F.ref.loc35
  137. // CHECK:STDOUT: %Class.F.call.loc35: init %empty_tuple.type = call %Class.F.bound.loc35(%.loc35_8.2)
  138. // CHECK:STDOUT: %DestroyOp.bound: <bound method> = bound_method %.loc35_8.2, constants.%DestroyOp
  139. // CHECK:STDOUT: %DestroyOp.call: init %empty_tuple.type = call %DestroyOp.bound(%.loc35_8.2)
  140. // CHECK:STDOUT: return
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn @DestroyOp(%self.param: %Class) = "no_op";
  144. // CHECK:STDOUT: