fail_addr_self.carbon 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
  10. class Class {
  11. fn F[addr self: Class*]();
  12. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+4]]:8: error: `addr` can only be applied to a binding with a pointer type [AddrOnNonPointerType]
  13. // CHECK:STDERR: fn G[addr self: Class]();
  14. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  15. // CHECK:STDERR:
  16. fn G[addr self: Class]();
  17. }
  18. fn F(c: Class, p: Class*) {
  19. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+7]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  20. // CHECK:STDERR: c.F();
  21. // CHECK:STDERR: ^
  22. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-12]]:8: note: initializing function parameter [InCallToFunctionParam]
  23. // CHECK:STDERR: fn F[addr self: Class*]();
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. c.F();
  27. c.G();
  28. // This call is OK.
  29. (*p).F();
  30. (*p).G();
  31. }
  32. // CHECK:STDOUT: --- fail_addr_self.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  36. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [template]
  37. // CHECK:STDOUT: %F.type.f1b: type = fn_type @F.1 [template]
  38. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  39. // CHECK:STDOUT: %F.1f2: %F.type.f1b = struct_value () [template]
  40. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  41. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  42. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  43. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  44. // CHECK:STDOUT: %F.type.b25: type = fn_type @F.2 [template]
  45. // CHECK:STDOUT: %F.c41: %F.type.b25 = struct_value () [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/...
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .Class = %Class.decl
  59. // CHECK:STDOUT: .F = %F.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  63. // CHECK:STDOUT: %F.decl: %F.type.b25 = fn_decl @F.2 [template = constants.%F.c41] {
  64. // CHECK:STDOUT: %c.patt: %Class = binding_pattern c
  65. // CHECK:STDOUT: %c.param_patt: %Class = value_param_pattern %c.patt, runtime_param0
  66. // CHECK:STDOUT: %p.patt: %ptr.e71 = binding_pattern p
  67. // CHECK:STDOUT: %p.param_patt: %ptr.e71 = value_param_pattern %p.patt, runtime_param1
  68. // CHECK:STDOUT: } {
  69. // CHECK:STDOUT: %c.param: %Class = value_param runtime_param0
  70. // CHECK:STDOUT: %Class.ref.loc20_9: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  71. // CHECK:STDOUT: %c: %Class = bind_name c, %c.param
  72. // CHECK:STDOUT: %p.param: %ptr.e71 = value_param runtime_param1
  73. // CHECK:STDOUT: %.loc20: type = splice_block %ptr [template = constants.%ptr.e71] {
  74. // CHECK:STDOUT: %Class.ref.loc20_19: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  75. // CHECK:STDOUT: %ptr: type = ptr_type %Class [template = constants.%ptr.e71]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %p: %ptr.e71 = bind_name p, %p.param
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: class @Class {
  82. // CHECK:STDOUT: %F.decl: %F.type.f1b = fn_decl @F.1 [template = constants.%F.1f2] {
  83. // CHECK:STDOUT: %self.patt: %ptr.e71 = binding_pattern self
  84. // CHECK:STDOUT: %self.param_patt: %ptr.e71 = value_param_pattern %self.patt, runtime_param0
  85. // CHECK:STDOUT: %.loc12_8: auto = addr_pattern %self.param_patt
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: %self.param: %ptr.e71 = value_param runtime_param0
  88. // CHECK:STDOUT: %.loc12_24: type = splice_block %ptr [template = constants.%ptr.e71] {
  89. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  90. // CHECK:STDOUT: %ptr: type = ptr_type %Class [template = constants.%ptr.e71]
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %self: %ptr.e71 = bind_name self, %self.param
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  95. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  96. // CHECK:STDOUT: %self.param_patt: %Class = value_param_pattern %self.patt, runtime_param0
  97. // CHECK:STDOUT: } {
  98. // CHECK:STDOUT: %self.param: %Class = value_param runtime_param0
  99. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  100. // CHECK:STDOUT: %self: %Class = bind_name self, %self.param
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  103. // CHECK:STDOUT: complete_type_witness = %complete_type
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: !members:
  106. // CHECK:STDOUT: .Self = constants.%Class
  107. // CHECK:STDOUT: .F = %F.decl
  108. // CHECK:STDOUT: .G = %G.decl
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @F.1[addr %self.param_patt: %ptr.e71]();
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @G[%self.param_patt: %Class]();
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @F.2(%c.param_patt: %Class, %p.param_patt: %ptr.e71) {
  116. // CHECK:STDOUT: !entry:
  117. // CHECK:STDOUT: %c.ref.loc28: %Class = name_ref c, %c
  118. // CHECK:STDOUT: %F.ref.loc28: %F.type.f1b = name_ref F, @Class.%F.decl [template = constants.%F.1f2]
  119. // CHECK:STDOUT: %F.bound.loc28: <bound method> = bound_method %c.ref.loc28, %F.ref.loc28
  120. // CHECK:STDOUT: %F.call.loc28: init %empty_tuple.type = call %F.bound.loc28(<error>)
  121. // CHECK:STDOUT: %c.ref.loc30: %Class = name_ref c, %c
  122. // CHECK:STDOUT: %G.ref.loc30: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  123. // CHECK:STDOUT: %G.bound.loc30: <bound method> = bound_method %c.ref.loc30, %G.ref.loc30
  124. // CHECK:STDOUT: %G.call.loc30: init %empty_tuple.type = call %G.bound.loc30(%c.ref.loc30)
  125. // CHECK:STDOUT: %p.ref.loc33: %ptr.e71 = name_ref p, %p
  126. // CHECK:STDOUT: %.loc33: ref %Class = deref %p.ref.loc33
  127. // CHECK:STDOUT: %F.ref.loc33: %F.type.f1b = name_ref F, @Class.%F.decl [template = constants.%F.1f2]
  128. // CHECK:STDOUT: %F.bound.loc33: <bound method> = bound_method %.loc33, %F.ref.loc33
  129. // CHECK:STDOUT: %addr: %ptr.e71 = addr_of %.loc33
  130. // CHECK:STDOUT: %F.call.loc33: init %empty_tuple.type = call %F.bound.loc33(%addr)
  131. // CHECK:STDOUT: %p.ref.loc35: %ptr.e71 = name_ref p, %p
  132. // CHECK:STDOUT: %.loc35_4.1: ref %Class = deref %p.ref.loc35
  133. // CHECK:STDOUT: %G.ref.loc35: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  134. // CHECK:STDOUT: %G.bound.loc35: <bound method> = bound_method %.loc35_4.1, %G.ref.loc35
  135. // CHECK:STDOUT: %.loc35_4.2: %Class = bind_value %.loc35_4.1
  136. // CHECK:STDOUT: %G.call.loc35: init %empty_tuple.type = call %G.bound.loc35(%.loc35_4.2)
  137. // CHECK:STDOUT: return
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: