fail_ref_self.carbon 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.761: 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: %Make.type: type = fn_type @Make [concrete]
  43. // CHECK:STDOUT: %Make: %Make.type = struct_value () [concrete]
  44. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [concrete]
  45. // CHECK:STDOUT: %pattern_type.796: type = pattern_type %ptr.e71 [concrete]
  46. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  47. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  49. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  50. // CHECK:STDOUT: %facet_value: %type_where = facet_value %Class, () [concrete]
  51. // 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]
  52. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.378: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.e99 = struct_value () [concrete]
  53. // 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]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  58. // CHECK:STDOUT: .Destroy = %Core.Destroy
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/...
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .Class = %Class.decl
  69. // CHECK:STDOUT: .Make = %Make.decl
  70. // CHECK:STDOUT: .F = %F.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  74. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [concrete = constants.%Make] {
  75. // CHECK:STDOUT: %return.patt: %pattern_type.761 = return_slot_pattern [concrete]
  76. // CHECK:STDOUT: %return.param_patt: %pattern_type.761 = out_param_pattern %return.patt, call_param0 [concrete]
  77. // CHECK:STDOUT: } {
  78. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  79. // CHECK:STDOUT: %return.param: ref %Class = out_param call_param0
  80. // CHECK:STDOUT: %return: ref %Class = return_slot %return.param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  83. // CHECK:STDOUT: %c.patt: %pattern_type.761 = value_binding_pattern c [concrete]
  84. // CHECK:STDOUT: %c.param_patt: %pattern_type.761 = value_param_pattern %c.patt, call_param0 [concrete]
  85. // CHECK:STDOUT: %p.patt: %pattern_type.796 = value_binding_pattern p [concrete]
  86. // CHECK:STDOUT: %p.param_patt: %pattern_type.796 = value_param_pattern %p.patt, call_param1 [concrete]
  87. // CHECK:STDOUT: } {
  88. // CHECK:STDOUT: %c.param: %Class = value_param call_param0
  89. // CHECK:STDOUT: %Class.ref.loc21_9: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  90. // CHECK:STDOUT: %c: %Class = value_binding c, %c.param
  91. // CHECK:STDOUT: %p.param: %ptr.e71 = value_param call_param1
  92. // CHECK:STDOUT: %.loc21: type = splice_block %ptr [concrete = constants.%ptr.e71] {
  93. // CHECK:STDOUT: %Class.ref.loc21_19: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  94. // CHECK:STDOUT: %ptr: type = ptr_type %Class.ref.loc21_19 [concrete = constants.%ptr.e71]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %p: %ptr.e71 = value_binding p, %p.param
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: class @Class {
  101. // CHECK:STDOUT: %Class.F.decl: %Class.F.type = fn_decl @Class.F [concrete = constants.%Class.F] {
  102. // CHECK:STDOUT: %self.patt: %pattern_type.761 = ref_binding_pattern self [concrete]
  103. // CHECK:STDOUT: %self.param_patt: %pattern_type.761 = ref_param_pattern %self.patt, call_param0 [concrete]
  104. // CHECK:STDOUT: } {
  105. // CHECK:STDOUT: %self.param: ref %Class = ref_param call_param0
  106. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  107. // CHECK:STDOUT: %self: ref %Class = ref_binding self, %self.param
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  110. // CHECK:STDOUT: complete_type_witness = %complete_type
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !members:
  113. // CHECK:STDOUT: .Self = constants.%Class
  114. // CHECK:STDOUT: .F = %Class.F.decl
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Class.F(%self.param: %Class);
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @Make() -> %return.param: %Class;
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: fn @F(%c.param: %Class, %p.param: %ptr.e71) {
  122. // CHECK:STDOUT: !entry:
  123. // CHECK:STDOUT: %c.ref: %Class = name_ref c, %c
  124. // CHECK:STDOUT: %F.ref.loc29: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  125. // CHECK:STDOUT: %Class.F.bound.loc29: <bound method> = bound_method %c.ref, %F.ref.loc29
  126. // CHECK:STDOUT: %Class.F.call.loc29: init %empty_tuple.type = call %Class.F.bound.loc29(<error>)
  127. // CHECK:STDOUT: %p.ref: %ptr.e71 = name_ref p, %p
  128. // CHECK:STDOUT: %.loc32: ref %Class = deref %p.ref
  129. // CHECK:STDOUT: %F.ref.loc32: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  130. // CHECK:STDOUT: %Class.F.bound.loc32: <bound method> = bound_method %.loc32, %F.ref.loc32
  131. // CHECK:STDOUT: %Class.F.call.loc32: init %empty_tuple.type = call %Class.F.bound.loc32(%.loc32)
  132. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, file.%Make.decl [concrete = constants.%Make]
  133. // CHECK:STDOUT: %.loc35_8.1: ref %Class = temporary_storage
  134. // CHECK:STDOUT: %Make.call: init %Class = call %Make.ref() to %.loc35_8.1
  135. // CHECK:STDOUT: %.loc35_8.2: ref %Class = temporary %.loc35_8.1, %Make.call
  136. // CHECK:STDOUT: %F.ref.loc35: %Class.F.type = name_ref F, @Class.%Class.F.decl [concrete = constants.%Class.F]
  137. // CHECK:STDOUT: %Class.F.bound.loc35: <bound method> = bound_method %.loc35_8.2, %F.ref.loc35
  138. // CHECK:STDOUT: %Class.F.call.loc35: init %empty_tuple.type = call %Class.F.bound.loc35(%.loc35_8.2)
  139. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc35_8.2, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.378
  140. // 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]
  141. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc35_8.2, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn
  142. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method(%.loc35_8.2)
  143. // CHECK:STDOUT: return
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: